css

package
v0.0.0-...-57974ef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package css contains tools for parsing a CSS3 stylesheet.

Index

Constants

View Source
const (
	// ErrorToken means an error occurred during tokenization.
	ErrorToken = iota
	// EOFToken marks the end of the input.
	EOFToken

	// IdentToken is an identity token
	IdentToken
	// FunctionToken is a function token
	FunctionToken
	// AtKeywordToken is an @ keyword
	AtKeywordToken
	// HashToken is a hash
	HashToken
	// StringToken is a quoted string
	StringToken
	// BadStringToken is a badly parsed quoted string
	BadStringToken
	// URLToken is a URL
	URLToken
	// BadURLToken denotes a badly formed URL
	BadURLToken
	// DelimToken is a single code point
	DelimToken
	// NumberToken is a number
	NumberToken
	// PercentageToken is a percentage
	PercentageToken
	// DimensionToken stores a number with a dimension
	DimensionToken
	// UnicodeRangeToken sets a range of hex numbers
	UnicodeRangeToken
	// IncludeMatchToken is ~=
	IncludeMatchToken
	// DashMatchToken is |=
	DashMatchToken
	// PrefixMatchToken is ^=
	PrefixMatchToken
	// SuffixMatchToken is $=
	SuffixMatchToken
	// SubstringMatchToken is *=
	SubstringMatchToken
	// ColumnToken : U+007C VERTICAL LINE (|)
	ColumnToken
	// WhitespaceToken : a U+000A LINE FEED (\n), U+0009 CHARACTER TABULATION (\t), or U+0020 SPACE ( ).
	WhitespaceToken
	// CDOToken : <!--
	CDOToken
	// CDCToken : -->
	CDCToken
	// ColonToken : U+003A COLON (:)
	ColonToken
	// SemicolonToken : U+003B SEMICOLON (;)
	SemicolonToken
	// CommaToken : U+002C COMMA (,)
	CommaToken
	// OpenSquareToken : U+005B LEFT SQUARE BRACKET ([)
	OpenSquareToken
	// CloseSquareToken : U+005D RIGHT SQUARE BRACKET (])
	CloseSquareToken
	// OpenParenToken : U+0028 LEFT PARENTHESIS (()
	OpenParenToken
	// CloseParenToken : U+0029 RIGHT PARENTHESIS ())
	CloseParenToken
	// OpenCurlyToken : U+007B LEFT CURLY BRACKET ({)
	OpenCurlyToken
	// CloseCurlyToken : U+007D RIGHT CURLY BRACKET (})
	CloseCurlyToken
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Segment

type Segment struct {
	Type SegmentType
	Data string
}

Segment is a portion of CSS.

type SegmentType

type SegmentType uint32

A SegmentType is the type of a Segment.

const (
	// ByteType is everything not of the other types below.
	ByteType SegmentType = iota
	// ImageURLType is a URL for an image. Note that the value has been
	// stripped of the surrounding 'url' identity token. To reconstruct a stylesheet,
	// this value must be enclosed into a url token or function token with name 'url'.
	ImageURLType
	// FontURLType is similar to an image URL, but for fonts.
	FontURLType
)

type Segments

type Segments []Segment

Segments is a slice of individual Segment structs.

func ParseURLs

func ParseURLs(css string) (Segments, error)

ParseURLs chops a style sheet into Segments. Each segment is either a UTF8 encoded byte string, or an image or font URL. This is used to modify the URLs to point at a CDN. Note that when combining the segments back to a stylesheet, the client code must emit url() around URLs. This is done so that client code can choose the quote character as in url("http://foo.com") or url('http://foo.com/') or even leave out the quote character as in url(http://foo.com/). Note that CSS supports escaping quote characters within a string by prefixing with a backslash, so " inside a URL may be written as \".

type Token

type Token struct {
	Type  TokenType
	Value string
	Extra string
	// contains filtered or unexported fields
}

A Token consists of a TokenType, Value, and optional contextual data. startPos, endPos are the indices in the input CSS being parsed, that this token corresponds to, e.g. Tokenizer.input[startPos:endPos].

func (*Token) String

func (t *Token) String() string

String returns the raw string representation for the token. The original input may not be preserved, due to preprocessing.

This differs from the token's value. For example, the input of "@font-face" is tokenized as an AtKeywordToken, with a value of "font-face" (without the @ sign). However, its String() will be "@font-face".

type TokenType

type TokenType int

A TokenType is a type of Token

type Tokenizer

type Tokenizer struct {
	// contains filtered or unexported fields
}

Tokenizer returns a stream of CSS Tokens.

func NewTokenizer

func NewTokenizer(input string) *Tokenizer

NewTokenizer returns a new CSS Tokenizer for the string, which is assumed to be UTF-8 encoded.

func (*Tokenizer) All

func (z *Tokenizer) All() []Token

All returns all the tokens

func (*Tokenizer) Next

func (z *Tokenizer) Next() Token

Next scans the next token and returns it

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL