Documentation
¶
Overview ¶
Package parser implements the tokenization of a CSS input and the construction of the corresponding Abstract Syntax Tree.
Index ¶
- Variables
- func ParseNth(input []Token) *[2]int
- func Serialize(nodes []Token) string
- func SerializeOne(node Token) string
- type AtKeywordToken
- type AtRule
- type Color
- type ColorType
- type Comment
- type CurlyBracketsBlock
- type Declaration
- type DimensionToken
- type FunctionBlock
- type HashToken
- type IdentToken
- type LiteralToken
- type LowerableString
- type NumberToken
- type NumericToken
- type ParenthesesBlock
- type ParseError
- type PercentageToken
- type Pos
- type QualifiedRule
- type RGBA
- type SquareBracketsBlock
- type StringToken
- type Token
- func ParseDeclarationList(input []Token, skipComments, skipWhitespace bool) []Token
- func ParseDeclarationListString(css string, skipComments, skipWhitespace bool) []Token
- func ParseOneComponentValue(input []Token) Token
- func ParseOneDeclaration(input []Token) Token
- func ParseRuleList(input []Token, skipComments, skipWhitespace bool) []Token
- func ParseRuleListString(css string, skipComments, skipWhitespace bool) []Token
- func ParseStylesheet(input []Token, skipComments, skipWhitespace bool) []Token
- func ParseStylesheetBytes(input []byte, skipComments, skipWhitespace bool) []Token
- func Tokenize(css []byte, skipComments bool) []Token
- type TokenIterator
- type TokenType
- type URLToken
- type UnicodeRangeToken
- type WhitespaceToken
Constants ¶
This section is empty.
Variables ¶
var ( // ColorKeywords maps color names to RGBA values ColorKeywords = map[string]Color{} )
Functions ¶
func ParseNth ¶
Parse `<An+B> <http://drafts.csswg.org/csswg/css-syntax-3/#anb>`_, as found in `:nth-child() <http://drafts.csswg.org/csswg/selectors/#nth-child-pseudo>` and related Selector pseudo-classes. Although tinycss2 does not include a full Selector parser, this bit of syntax is included as it is particularly tricky to define on top of a CSS tokenizer. Returns [a, b] or nil
Types ¶
type AtKeywordToken ¶
type AtKeywordToken struct { Value LowerableString Pos }
func (AtKeywordToken) Type ¶
func (t AtKeywordToken) Type() TokenType
type AtRule ¶
type AtRule struct { AtKeyword LowerableString QualifiedRule }
type Color ¶
func ParseColor ¶
Parse a color value as defined in `CSS Color Level 3 <http://www.w3.org/TR/css3-color/>`. Returns :
- zero Color if the input is not a valid color value. (No error is returned.)
- CurrentColor for the *currentColor* keyword
- RGBA color for every other values (including keywords, HSL && HSLA.) The alpha channel is clipped to [0, 1] but red, green, or blue can be out of range (eg. “rgb(-10%, 120%, 0%)“ is represented as “(-0.1, 1.2, 0, 1)“.
func ParseColorString ¶
ParseColorString tokenize the input before calling `ParseColor`.
type CurlyBracketsBlock ¶
type CurlyBracketsBlock bracketsBlock
func (CurlyBracketsBlock) Type ¶
func (t CurlyBracketsBlock) Type() TokenType
type Declaration ¶
type Declaration struct { Name LowerableString Value []Token Pos Important bool }
func (Declaration) Type ¶
func (t Declaration) Type() TokenType
type DimensionToken ¶
type DimensionToken struct { Unit LowerableString NumericToken }
func (DimensionToken) Type ¶
func (t DimensionToken) Type() TokenType
type FunctionBlock ¶
type FunctionBlock struct { Arguments *[]Token Name LowerableString Pos }
func (FunctionBlock) Type ¶
func (t FunctionBlock) Type() TokenType
type IdentToken ¶
type IdentToken struct { Value LowerableString Pos }
func (IdentToken) Type ¶
func (t IdentToken) Type() TokenType
type LiteralToken ¶
type LiteralToken stringToken
func (LiteralToken) Type ¶
func (t LiteralToken) Type() TokenType
type LowerableString ¶
type LowerableString string
LowerableString is a string which can be normalized to ASCII lower case
func (LowerableString) Lower ¶
func (s LowerableString) Lower() string
type NumberToken ¶
type NumberToken NumericToken
func NewNumberToken ¶ added in v0.0.2
func NewNumberToken(v utils.Fl, pos Pos) NumberToken
func (NumberToken) IntValue ¶
func (t NumberToken) IntValue() int
func (NumberToken) Type ¶
func (t NumberToken) Type() TokenType
type NumericToken ¶ added in v0.0.2
func (NumericToken) IntValue ¶ added in v0.0.2
func (t NumericToken) IntValue() int
IntValue returns the rounded value Should be used only if `IsInteger` is true
type ParenthesesBlock ¶
type ParenthesesBlock bracketsBlock
func (ParenthesesBlock) Type ¶
func (t ParenthesesBlock) Type() TokenType
type ParseError ¶
func (ParseError) Type ¶
func (t ParseError) Type() TokenType
type PercentageToken ¶
type PercentageToken NumericToken
func (PercentageToken) IntValue ¶
func (t PercentageToken) IntValue() int
func (PercentageToken) Type ¶
func (t PercentageToken) Type() TokenType
type QualifiedRule ¶
func (QualifiedRule) Type ¶
func (t QualifiedRule) Type() TokenType
type RGBA ¶
values in [-1, 1]
func (RGBA) RGBA ¶
RGBA returns the alpha-premultiplied red, green, blue and alpha values for the color. Each value ranges within [0, 0xffff], but is represented by a uint32 so that multiplying by a blend factor up to 0xffff will not overflow.
An alpha-premultiplied color component c has been scaled by alpha (a), so has valid values 0 <= c <= a.
type SquareBracketsBlock ¶
type SquareBracketsBlock bracketsBlock
func (SquareBracketsBlock) Type ¶
func (t SquareBracketsBlock) Type() TokenType
type StringToken ¶
func (StringToken) Type ¶
func (t StringToken) Type() TokenType
type Token ¶
func ParseDeclarationList ¶
Parse a `declaration list` (which may also contain at-rules). This is used e.g. for the `QualifiedRule.content` of a style rule or “@page“ rule, or for the “style“ attribute of an HTML element. In contexts that don’t expect any at-rule, all :class:`AtRule` objects should simply be rejected as invalid. If `skipComments`, ignore CSS comments at the top-level of the list. If the input is a string, ignore all comments. If `skipWhitespace`, ignore whitespace at the top-level of the list. Whitespace is still preserved in the `Declaration.value` of declarations and the `AtRule.prelude` and `AtRule.content` of at-rules.
func ParseDeclarationListString ¶
ParseDeclarationListString tokenizes `css` and calls `ParseDeclarationList`.
func ParseOneComponentValue ¶
Parse a single `component value`. This is used e.g. for an attribute value referred to by “attr(foo length)“.
func ParseOneDeclaration ¶
Parse a single `declaration`. This is used e.g. for a declaration in an `@supports <http://drafts.csswg.org/csswg/css-conditional/#at-supports>`_ test. Any whitespace or comment before the “:“ colon is dropped.
func ParseRuleList ¶
Parse a non-top-level `rule list`. This is used for parsing the `AtRule.content` of nested rules like “@media“. This differs from :func:`ParseStylesheet` in that top-level “<!--“ and “-->“ tokens are not ignored. skipComments:
Ignore CSS comments at the top-level of the list. If the input is a string, ignore all comments.
skipWhitespace:
Ignore whitespace at the top-level of the list. Whitespace is still preserved in the `QualifiedRule.prelude` and the `QualifiedRule.content` of rules.
func ParseRuleListString ¶
ParseRuleListString tokenizes `css` and calls `ParseRuleListString`.
func ParseStylesheet ¶
Parse a stylesheet from tokens. This is used e.g. for a “<style>“ HTML element. This differs from `parseRuleList` in that top-level “<!--“ && “-->“ tokens are ignored. This is a legacy quirk for the “<style>“ HTML element. If `skipComments` is true, ignore CSS comments at the top-level of the stylesheet. If the input is a string, ignore all comments. If `skipWhitespace` is true, ignore whitespace at the top-level of the stylesheet. Whitespace is still preserved in the `QualifiedRule.Prelude` and the `QualifiedRule.Content` of rules.
func ParseStylesheetBytes ¶
ParseStylesheetBytes tokenizes `input` and calls `ParseStylesheet`.
type TokenIterator ¶
type TokenIterator struct {
// contains filtered or unexported fields
}
func NewTokenIterator ¶
func NewTokenIterator(tokens []Token) *TokenIterator
func (TokenIterator) HasNext ¶
func (it TokenIterator) HasNext() bool
func (*TokenIterator) Next ¶
func (it *TokenIterator) Next() (t Token)
Next returns the next token or nil at the end
type TokenType ¶
type TokenType string
const ( QualifiedRuleT TokenType = "qualified-rule" AtRuleT TokenType = "at-rule" DeclarationT TokenType = "declaration" ParseErrorT TokenType = "error" CommentT TokenType = "comment" WhitespaceTokenT TokenType = "whitespace" LiteralTokenT TokenType = "literal" IdentTokenT TokenType = "ident" AtKeywordTokenT TokenType = "at-keyword" HashTokenT TokenType = "hash" StringTokenT TokenType = "string" URLTokenT TokenType = "url" UnicodeRangeTokenT TokenType = "unicode-range" NumberTokenT TokenType = "number" PercentageTokenT TokenType = "percentage" DimensionTokenT TokenType = "dimension" ParenthesesBlockT TokenType = "() block" SquareBracketsBlockT TokenType = "[] block" CurlyBracketsBlockT TokenType = "{} block" FunctionBlockT TokenType = "function" )
type UnicodeRangeToken ¶
func (UnicodeRangeToken) Type ¶
func (t UnicodeRangeToken) Type() TokenType
type WhitespaceToken ¶
type WhitespaceToken stringToken
func (WhitespaceToken) Type ¶
func (t WhitespaceToken) Type() TokenType