parser

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: BSD-3-Clause Imports: 13 Imported by: 2

Documentation

Overview

Package parser implements the tokenization of a CSS input and the construction of the corresponding Abstract Syntax Tree.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ColorKeywords maps color names to RGBA values
	ColorKeywords = map[string]Color{}
)

Functions

func ParseNth

func ParseNth(input []Token) *[2]int

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

func Serialize

func Serialize(nodes []Token) string

Serialize nodes to CSS syntax. This should be used for `ComponentValue` as it takes care of corner cases such as “;“ between declarations, and consecutive identifiers that would otherwise parse back as the same token.

func SerializeOne

func SerializeOne(node Token) string

Serialize this node to CSS syntax

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
}

func (AtRule) Type

func (t AtRule) Type() TokenType

type Color

type Color struct {
	Type ColorType
	RGBA RGBA
}

func ParseColor

func ParseColor(_token Token) Color

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

func ParseColorString(color string) Color

ParseColorString tokenize the input before calling `ParseColor`.

func (Color) IsNone

func (c Color) IsNone() bool

type ColorType

type ColorType uint8
const (
	// ColorInvalid is an empty or invalid color specification.
	ColorInvalid ColorType = iota
	// ColorCurrentColor represents the special value "currentColor"
	// which need document context to be resolved.
	ColorCurrentColor
	// ColorRGBA is a standard rgba color.
	ColorRGBA
)

type Comment

type Comment stringToken

func (Comment) Type

func (t Comment) Type() TokenType

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 HashToken

type HashToken struct {
	Value string
	Pos
	IsIdentifier bool
}

func (HashToken) Type

func (t HashToken) 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

type NumericToken struct {
	Representation string
	Pos
	Value     utils.Fl
	IsInteger bool
}

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

type ParseError struct {
	Kind    string
	Message string
	Pos
}

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 Pos added in v0.0.2

type Pos struct {
	Line, Column int
}

func (Pos) Position added in v0.0.2

func (n Pos) Position() Pos

type QualifiedRule

type QualifiedRule struct {
	Prelude, Content *[]Token
	Pos
}

func (QualifiedRule) Type

func (t QualifiedRule) Type() TokenType

type RGBA

type RGBA struct {
	R, G, B, A utils.Fl
}

values in [-1, 1]

func (RGBA) IsNone

func (color RGBA) IsNone() bool

func (RGBA) RGBA

func (c RGBA) RGBA() (r, g, b, a uint32)

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.

func (RGBA) Unpack

func (color RGBA) Unpack() (r, g, b, a utils.Fl)

type SquareBracketsBlock

type SquareBracketsBlock bracketsBlock

func (SquareBracketsBlock) Type

func (t SquareBracketsBlock) Type() TokenType

type StringToken

type StringToken struct {
	Value string
	Pos
	// contains filtered or unexported fields
}

func (StringToken) Type

func (t StringToken) Type() TokenType

type Token

type Token interface {
	Position() Pos
	Type() TokenType
	// contains filtered or unexported methods
}

func ParseDeclarationList

func ParseDeclarationList(input []Token, skipComments, skipWhitespace bool) []Token

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

func ParseDeclarationListString(css string, skipComments, skipWhitespace bool) []Token

ParseDeclarationListString tokenizes `css` and calls `ParseDeclarationList`.

func ParseOneComponentValue

func ParseOneComponentValue(input []Token) Token

Parse a single `component value`. This is used e.g. for an attribute value referred to by “attr(foo length)“.

func ParseOneDeclaration

func ParseOneDeclaration(input []Token) Token

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

func ParseRuleList(input []Token, skipComments, skipWhitespace bool) []Token

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

func ParseRuleListString(css string, skipComments, skipWhitespace bool) []Token

ParseRuleListString tokenizes `css` and calls `ParseRuleListString`.

func ParseStylesheet

func ParseStylesheet(input []Token, skipComments, skipWhitespace bool) []Token

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

func ParseStylesheetBytes(input []byte, skipComments, skipWhitespace bool) []Token

ParseStylesheetBytes tokenizes `input` and calls `ParseStylesheet`.

func Tokenize

func Tokenize(css []byte, skipComments bool) []Token

Tokenize parses a list of component values. If `skipComments` is true, ignore CSS comments : the return values (and recursively its blocks and functions) will not contain any `Comment` object.

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 URLToken

type URLToken struct {
	Value string
	Pos
	// contains filtered or unexported fields
}

func (URLToken) Type

func (t URLToken) Type() TokenType

type UnicodeRangeToken

type UnicodeRangeToken struct {
	Pos
	Start, End uint32
}

func (UnicodeRangeToken) Type

func (t UnicodeRangeToken) Type() TokenType

type WhitespaceToken

type WhitespaceToken stringToken

func (WhitespaceToken) Type

func (t WhitespaceToken) Type() TokenType

Jump to

Keyboard shortcuts

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