tinycss2

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: BSD-3-Clause, BSD-3-Clause Imports: 7 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tinycss2 is a port of the Python package tinycss2 1.5.1, a low-level CSS parser and generator. It parses strings into tokens, blocks and rules, and serializes them back to CSS (D50). It is public, so that ivy can port the layout of WeasyPrint, which reads its CSS with tinycss2.

How the Python types map to Go

Each class of tinycss2/ast.py is a struct of the same name, and a pointer to it implements Node. Node.Type returns the class attribute type of upstream, such as "ident" or "{} block", so that a port can switch on it as upstream code does. Each attribute is a field in MixedCaps: value is Value, lower_value is LowerValue, int_value is IntValue, is_integer is IsInteger, lower_unit is LowerUnit, arguments is Arguments, content is Content and prelude is Prelude. source_line and source_column are the fields SourceLine and SourceColumn of the embedded Location.

  • A Python list of component values is a []Node.
  • None, where upstream returns it for a node, is a nil Node.
  • The None of AtRule.content is a nil Content. An empty block gives an empty Content that is not nil.
  • The None of int_value is IsInteger false.
  • The comparison `token == ','` of a LiteralToken is IsLiteral(token, ",").
  • The functions that take a string or an iterable of component values take an Input, which is a string or a []Node.
  • The keyword arguments skip_comments and skip_whitespace are the options WithSkipComments and WithSkipWhitespace.
  • parse_color of color3.py, color4.py and color5.py are ParseColor3, ParseColor4 and ParseColor5. RGBA is RGBA, and the Color of color4.py and color5.py is Color. The string 'currentColor' is a second result of true.
  • parse_nth returns a, b and true, or false for None.
  • webencodings.Encoding is *Encoding, and lookup is LookupEncoding.

Differences from upstream

The port keeps the behavior of upstream, with its faults, except where Python raises an exception on bad input, or where Go cannot hold the same value (D50):

  • The tokenizer nests at most DefaultMaxDepth blocks and functions, or the value of WithMaxDepth. A deeper block becomes a ParseError of the kind "too-deep", and its content is dropped.
  • An escape of a surrogate code point gives U+FFFD, as a Go string cannot hold a lone surrogate.
  • An integer out of the range of int64 saturates in IntValue.
  • Serialization writes nothing for a ParseError of a kind that upstream cannot serialize, where upstream raises TypeError.
  • The color parsers return no color where upstream raises an exception.
  • The bytes of a stylesheet decode from UTF-8, UTF-16, windows-1252 and x-user-defined only. Another encoding label counts as unknown.

Index

Constants

View Source
const (
	TypeError               = "error"
	TypeComment             = "comment"
	TypeWhitespace          = "whitespace"
	TypeLiteral             = "literal"
	TypeIdent               = "ident"
	TypeAtKeyword           = "at-keyword"
	TypeHash                = "hash"
	TypeString              = "string"
	TypeURL                 = "url"
	TypeUnicodeRange        = "unicode-range"
	TypeNumber              = "number"
	TypePercentage          = "percentage"
	TypeDimension           = "dimension"
	TypeParenthesesBlock    = "() block"
	TypeSquareBracketsBlock = "[] block"
	TypeCurlyBracketsBlock  = "{} block"
	TypeFunction            = "function"
	TypeDeclaration         = "declaration"
	TypeQualifiedRule       = "qualified-rule"
	TypeAtRule              = "at-rule"
)

The values of Node.Type, one for each type of node. They are the values of the class attribute type of upstream, so that a port of code that reads the tree of tinycss2 compares the same strings.

View Source
const DefaultMaxDepth = 256

DefaultMaxDepth is the default of WithMaxDepth: the most blocks and functions that the tokenizer nests in each other.

View Source
const Version = "1.5.1"

Version is VERSION of tinycss2, the version that the package ports.

Variables

View Source
var ColorSchemes = map[string]bool{"light": true, "dark": true}

ColorSchemes is COLOR_SCHEMES in tinycss2 tinycss2/color5.py: the color schemes that light-dark() resolves.

View Source
var ColorSpaces = map[string]bool{
	"srgb": true, "srgb-linear": true,
	"display-p3": true, "a98-rgb": true, "prophoto-rgb": true, "rec2020": true,
	"xyz": true, "xyz-d50": true, "xyz-d65": true,
	"hsl": true, "lab": true,
	// contains filtered or unexported fields
}

ColorSpaces is COLOR_SPACES in tinycss2 tinycss2/color4.py: the color spaces of CSS Color Level 4.

View Source
var D50 = [3]float64{0.3457 / 0.3585, 1, (1 - 0.3457 - 0.3585) / 0.3585}

D50 is D50 in tinycss2 tinycss2/color4.py: the XYZ values of the D50 white point, normalized to Y=1.

View Source
var D65 = [3]float64{0.3127 / 0.3290, 1, (1 - 0.3127 - 0.3290) / 0.3290}

D65 is D65 in tinycss2 tinycss2/color4.py: the XYZ values of the D65 white point, normalized to Y=1.

View Source
var (
	// UTF8 is UTF8 of webencodings.
	UTF8 = &Encoding{"utf-8", decodeUTF8}
)

The encodings that the port decodes.

Functions

func ASCIILower

func ASCIILower(s string) string

ASCIILower is ascii_lower of webencodings, which tinycss2 and cssselect2 use. It maps only the ASCII letters A to Z to lower case.

func IsLiteral

func IsLiteral(n Node, v string) bool

IsLiteral is LiteralToken.__eq__ in tinycss2 tinycss2/ast.py. It reports whether n is a LiteralToken with the value v, as `n == v` does in upstream. It is false for a nil node and for every other node type.

func ParseNth

func ParseNth[T Input](input T) (a, b int64, ok bool)

ParseNth is parse_nth in tinycss2 tinycss2/nth.py. It parses <An+B>, as found in :nth-child() and the related pseudo-classes of selectors.

tinycss2 holds no full selector parser, but it holds this bit of syntax, as it is tricky to define on top of a CSS tokenizer.

It returns a and b, and false if the input is invalid. The tokenizer drops the comments of a string input.

func ParseStylesheetBytes

func ParseStylesheetBytes(cssBytes []byte, protocolEncoding string, environmentEncoding *Encoding, opts ...Option) ([]Node, *Encoding)

ParseStylesheetBytes is parse_stylesheet_bytes in tinycss2 tinycss2/bytes.py. It parses a stylesheet from bytes, and finds its character encoding as web browsers do. Use it to read a file or a URL.

It finds the encoding as DecodeStylesheetBytes does. The last fallback is UTF-8. The options work as in ParseStylesheet.

It returns the rules, as ParseStylesheet does, and the encoding that it used. If the rules hold an @import rule, the encoding is the environment encoding of the imported stylesheet.

func Serialize

func Serialize(nodes []Node) string

Serialize is serialize in tinycss2 tinycss2/serializer.py. It serializes nodes to CSS syntax.

Use it for component values, instead of Node.Serialize on each node, as it takes care of corner cases such as ";" between declarations, and consecutive identifiers that would otherwise parse back as the same token.

func SerializeIdentifier

func SerializeIdentifier(value string) string

SerializeIdentifier is serialize_identifier in tinycss2 tinycss2/serializer.py. It serializes any string as a CSS identifier: the result parses as an IdentToken whose Value is value.

Upstream raises IndexError for the empty string. The port returns the empty string for it (D50).

func SerializeNth

func SerializeNth(a, b int64) string

SerializeNth is serialize_nth in tinycss2 tinycss2/nth.py. It serializes <An+B> as an "an+b" string.

Types

type AtKeywordToken

type AtKeywordToken struct {
	Location

	// Value is the unescaped value, without the "@".
	Value string
	// LowerValue is Value in ASCII lower case. Compare it to a CSS
	// at-keyword.
	LowerValue string
}

AtKeywordToken is AtKeywordToken in tinycss2 tinycss2/ast.py, an at-keyword-token: "@" and its value.

func NewAtKeywordToken

func NewAtKeywordToken(line, column int, value string) *AtKeywordToken

NewAtKeywordToken makes an AtKeywordToken, and sets its LowerValue.

func (*AtKeywordToken) Serialize

func (t *AtKeywordToken) Serialize() string

Serialize satisfies Node.

func (*AtKeywordToken) String

func (t *AtKeywordToken) String() string

String satisfies Node.

func (*AtKeywordToken) Type

func (*AtKeywordToken) Type() string

Type satisfies Node.

type AtRule

type AtRule struct {
	Location

	// AtKeyword is the unescaped value of the at-keyword of the rule,
	// without the "@".
	AtKeyword string
	// LowerAtKeyword is AtKeyword in ASCII lower case. Compare it to a CSS
	// at-keyword.
	LowerAtKeyword string
	// Prelude is the part of the rule before the {} block or the
	// semicolon, as a list of component values.
	Prelude []Node
	// Content is the content of the {} block of the rule, as a list of
	// component values. It is nil for an at-rule that ends with a
	// semicolon, where upstream holds None. It is not nil for an empty
	// block.
	Content []Node
}

AtRule is AtRule in tinycss2 tinycss2/ast.py, an at-rule: "@", the at-keyword, the prelude, and a {} block or ";".

The meaning of an at-rule depends on its at-keyword and on its context. Most types of at-rules are only allowed in some contexts, and must end with a {} block or with a semicolon.

func NewAtRule

func NewAtRule(line, column int, atKeyword, lowerAtKeyword string, prelude, content []Node) *AtRule

NewAtRule makes an AtRule. A nil content is the None of upstream.

func (*AtRule) Serialize

func (r *AtRule) Serialize() string

Serialize satisfies Node.

func (*AtRule) String

func (r *AtRule) String() string

String satisfies Node.

func (*AtRule) Type

func (*AtRule) Type() string

Type satisfies Node.

type Color

type Color struct {
	// Space is the color space.
	Space string
	// Coordinates holds the coordinates in the color space.
	Coordinates []float64
	// Missing tells, for each coordinate, whether it is undefined.
	Missing []bool
	// Alpha is the alpha channel, in [0, 1].
	Alpha float64
}

Color is Color in tinycss2 tinycss2/color4.py, and the Color of tinycss2/color5.py. It is a specified color in a defined color space.

The color space is one of ColorSpaces, "device-cmyk", or a custom space of CSS Color Level 5 that starts with "--". The coordinates are floats with undefined ranges, and the alpha channel is clipped to [0, 1].

Upstream holds None for a coordinate that is undefined, from the keyword none. In Go, Missing[i] is true for it and Coordinates[i] is 0. A nil Missing means that each coordinate is defined.

func ParseColor4

func ParseColor4(token Node) (color *Color, currentColor bool)

ParseColor4 is parse_color in tinycss2 tinycss2/color4.py. It parses a color value as CSS Color Level 4 defines it, from a component value. Parse a string first, as for ParseColor3.

It returns nil and false for a value that is not a valid color, and nil and true for the keyword currentcolor, where upstream returns the string 'currentcolor'. It returns a Color for every other value, with the keywords.

Upstream raises ValueError for "color()" with no argument. The port returns nil and false for it (D50).

func ParseColor5

func ParseColor5(token Node, colorSchemes ...string) (color *Color, currentColor bool)

ParseColor5 is parse_color in tinycss2 tinycss2/color5.py. It parses a color value as CSS Color Level 5 defines it, from a component value. Parse a string first, as for ParseColor3.

colorSchemes resolves the function light-dark(). The first item that is "light" or "dark" wins, and the default is "light". Upstream takes None, the string 'normal' or an iterable of color schemes, and each of them that holds no "light" and no "dark" gives "light" too.

It returns nil and false for a value that is not a valid color, nil and true for the keyword currentcolor, and a Color for every other value.

Upstream raises an exception for "color()" with no argument, for the keyword none in device-cmyk(), and for an invalid alpha value in device-cmyk() or in color() of a custom color space. The port returns nil and false for each of them (D50).

func (*Color) Equal

func (c *Color) Equal(other *Color) bool

Equal is Color.__eq__ in tinycss2 tinycss2/color4.py for two colors. It compares the spaces and the coordinates, and not the alpha channels, as upstream does.

func (*Color) IsMissing

func (c *Color) IsMissing(i int) bool

IsMissing reports whether coordinate i is undefined, the None of upstream.

func (*Color) String

func (c *Color) String() string

String is Color.__repr__ in tinycss2 tinycss2/color4.py, such as "color(srgb 1.0 0.0 0.0 / 1.0)".

func (*Color) To

func (c *Color) To(space string) (*Color, bool)

To is Color.to in tinycss2 tinycss2/color4.py. It returns a new color with the coordinates transformed to the given space. Each undefined coordinate becomes 0.

It supports these combinations: from hsl and hwb to srgb, from lab and lch to xyz-d50, from oklab and oklch to xyz-d65, and from xyz-d50, xyz-d65, lch, oklab and oklch to lab. It returns false for another combination, where upstream raises NotImplementedError.

type Comment

type Comment struct {
	Location

	// Value is the content of the comment, between "/*" and "*/".
	Value string
}

Comment is Comment in tinycss2 tinycss2/ast.py, a CSS comment.

The option WithSkipComments of the functions that parse ignores comments.

func (*Comment) Serialize

func (c *Comment) Serialize() string

Serialize satisfies Node.

func (*Comment) String

func (c *Comment) String() string

String satisfies Node.

func (*Comment) Type

func (*Comment) Type() string

Type satisfies Node.

type CurlyBracketsBlock

type CurlyBracketsBlock struct {
	Location

	// Content is the content of the block, as a list of component values.
	// The "{" and "}" markers are not in the list. The tokenizer never
	// leaves it nil, so that the Content of an AtRule tells a block from a
	// semicolon.
	Content []Node
}

CurlyBracketsBlock is CurlyBracketsBlock in tinycss2 tinycss2/ast.py, a {}-block.

func NewCurlyBracketsBlock

func NewCurlyBracketsBlock(line, column int, content []Node) *CurlyBracketsBlock

NewCurlyBracketsBlock makes a CurlyBracketsBlock.

func (*CurlyBracketsBlock) Serialize

func (t *CurlyBracketsBlock) Serialize() string

Serialize satisfies Node.

func (*CurlyBracketsBlock) String

func (t *CurlyBracketsBlock) String() string

String satisfies Node.

func (*CurlyBracketsBlock) Type

func (*CurlyBracketsBlock) Type() string

Type satisfies Node.

type Declaration

type Declaration struct {
	Location

	// Name is the unescaped name.
	Name string
	// LowerName is Name in ASCII lower case. Compare it to a CSS property or
	// descriptor name.
	LowerName string
	// Value is the value of the declaration, as a list of component values:
	// anything between ":" and the end of the declaration, or "!important".
	Value []Node
	// Important is true if the declaration had an "!important" marker. The
	// consumer must reject it for a declaration that does not accept it,
	// such as a descriptor that is not a property.
	Important bool
}

Declaration is Declaration in tinycss2 tinycss2/ast.py, a property or descriptor declaration: the name, ":", the value and an optional "!important".

func NewDeclaration

func NewDeclaration(line, column int, name, lowerName string, value []Node, important bool) *Declaration

NewDeclaration makes a Declaration.

func (*Declaration) Serialize

func (d *Declaration) Serialize() string

Serialize satisfies Node.

func (*Declaration) String

func (d *Declaration) String() string

String satisfies Node.

func (*Declaration) Type

func (*Declaration) Type() string

Type satisfies Node.

type DimensionToken

type DimensionToken struct {
	Location

	// Value is the numeric value.
	Value float64
	// IntValue is the numeric value as an integer when IsInteger is true,
	// as in NumberToken.
	IntValue int64
	// IsInteger reports whether the value was syntactically an integer.
	IsInteger bool
	// Representation is the CSS representation of the value, without the
	// unit.
	Representation string
	// Unit is the unescaped unit.
	Unit string
	// LowerUnit is Unit in ASCII lower case. Compare it to a CSS unit.
	LowerUnit string
}

DimensionToken is DimensionToken in tinycss2 tinycss2/ast.py, a dimension-token: the representation and the unit.

func NewDimensionToken

func NewDimensionToken(line, column int, value float64, intValue int64, isInteger bool, representation, unit string) *DimensionToken

NewDimensionToken makes a DimensionToken, as NewNumberToken does, and sets its LowerUnit.

func (*DimensionToken) Serialize

func (t *DimensionToken) Serialize() string

Serialize satisfies Node.

func (*DimensionToken) String

func (t *DimensionToken) String() string

String satisfies Node.

func (*DimensionToken) Type

func (*DimensionToken) Type() string

Type satisfies Node.

type Encoding

type Encoding struct {
	// Name is the name of the encoding in the Encoding Standard, such as
	// "utf-8" or "windows-1252".
	Name string
	// contains filtered or unexported fields
}

Encoding is Encoding of webencodings, the character encoding of a stylesheet.

The port decodes UTF-8, UTF-16LE, UTF-16BE, windows-1252 and x-user-defined, with all the labels that webencodings gives them. Python decodes every encoding of the Encoding Standard. For a label of another encoding, LookupEncoding returns nil, as for an unknown label (D50).

func DecodeStylesheetBytes

func DecodeStylesheetBytes(cssBytes []byte, protocolEncoding string, environmentEncoding *Encoding) (string, *Encoding)

DecodeStylesheetBytes is decode_stylesheet_bytes in tinycss2 tinycss2/bytes.py. It finds the character encoding of a CSS stylesheet and decodes it, from a BOM, an @charset rule and the encoding meta-information.

protocolEncoding is the encoding label of HTTP or of an equivalent protocol, such as the charset parameter of the Content-Type header, or the empty string. environmentEncoding is the environment encoding, or nil.

It returns the decoded string and the encoding that it used.

func LookupEncoding

func LookupEncoding(label string) *Encoding

LookupEncoding is lookup of webencodings. It returns the encoding of a label, or nil. It strips the ASCII whitespace of the label, and matches it in ASCII lower case.

type FunctionBlock

type FunctionBlock struct {
	Location

	// Name is the unescaped name of the function.
	Name string
	// LowerName is Name in ASCII lower case. Compare it to a CSS function
	// name.
	LowerName string
	// Arguments is the arguments of the function, as a list of component
	// values. The "(" and ")" markers are not in the list. Commas are not
	// special. They are LiteralToken values in the list.
	Arguments []Node
}

FunctionBlock is FunctionBlock in tinycss2 tinycss2/ast.py, a function-block: the name, "(", the arguments and ")".

func NewFunctionBlock

func NewFunctionBlock(line, column int, name string, arguments []Node) *FunctionBlock

NewFunctionBlock makes a FunctionBlock, and sets its LowerName.

func (*FunctionBlock) Serialize

func (t *FunctionBlock) Serialize() string

Serialize satisfies Node.

func (*FunctionBlock) String

func (t *FunctionBlock) String() string

String satisfies Node.

func (*FunctionBlock) Type

func (*FunctionBlock) Type() string

Type satisfies Node.

type HashToken

type HashToken struct {
	Location

	// Value is the unescaped value, without the "#".
	Value string
	// IsIdentifier is true if the CSS source of the token was "#" and a
	// valid identifier. Only such hash tokens are valid ID selectors.
	IsIdentifier bool
}

HashToken is HashToken in tinycss2 tinycss2/ast.py, a hash-token: "#" and its value.

func NewHashToken

func NewHashToken(line, column int, value string, isIdentifier bool) *HashToken

NewHashToken makes a HashToken.

func (*HashToken) Serialize

func (t *HashToken) Serialize() string

Serialize satisfies Node.

func (*HashToken) String

func (t *HashToken) String() string

String satisfies Node.

func (*HashToken) Type

func (*HashToken) Type() string

Type satisfies Node.

type IdentToken

type IdentToken struct {
	Location

	// Value is the unescaped value.
	Value string
	// LowerValue is Value in ASCII lower case. Compare it to a CSS keyword.
	LowerValue string
}

IdentToken is IdentToken in tinycss2 tinycss2/ast.py, an ident-token.

func NewIdentToken

func NewIdentToken(line, column int, value string) *IdentToken

NewIdentToken makes an IdentToken, and sets its LowerValue.

func (*IdentToken) Serialize

func (t *IdentToken) Serialize() string

Serialize satisfies Node.

func (*IdentToken) String

func (t *IdentToken) String() string

String satisfies Node.

func (*IdentToken) Type

func (*IdentToken) Type() string

Type satisfies Node.

type Input

type Input interface {
	string | []Node
}

Input is the type of the input of the functions that parse. Upstream takes a string or an iterable of component values.

type LiteralToken

type LiteralToken struct {
	Location

	// Value is a string of one to four characters.
	Value string
}

LiteralToken is LiteralToken in tinycss2 tinycss2/ast.py. It represents one or more characters as in the CSS source.

Upstream compares a LiteralToken equal to its value, as in `node == ';'`. In Go, IsLiteral does that comparison.

This regroups what the specification defines as separate token types: the colon ":", the semicolon ";", the comma ",", the cdc "-->", the cdo "<!--", the include-match "~=", the dash-match "|=", the prefix-match "^=", the suffix-match "$=", the substring-match "*=", the column "||", and the delim-token, a single ASCII character that is not part of any other token.

func NewLiteralToken

func NewLiteralToken(line, column int, value string) *LiteralToken

NewLiteralToken makes a LiteralToken.

func (*LiteralToken) Serialize

func (t *LiteralToken) Serialize() string

Serialize satisfies Node.

func (*LiteralToken) String

func (t *LiteralToken) String() string

String satisfies Node.

func (*LiteralToken) Type

func (*LiteralToken) Type() string

Type satisfies Node.

type Location

type Location struct {
	// SourceLine is the line number of the start of the node in the CSS
	// source. It starts at 1.
	SourceLine int
	// SourceColumn is the column number within SourceLine of the start of
	// the node in the CSS source. It starts at 1, and counts code points.
	SourceColumn int
}

Location holds the attributes source_line and source_column of Node in tinycss2 tinycss2/ast.py. Each node type embeds it.

func (Location) Source

func (l Location) Source() (line, column int)

Source satisfies Node.

type Node

type Node interface {
	// Type is the class attribute type of upstream. It is one of the Type
	// constants of this package.
	Type() string
	// Source returns the attributes source_line and source_column of
	// upstream: the line and the column of the start of the node in the CSS
	// source. Both start at 1.
	Source() (line, column int)
	// Serialize is Node.serialize. It serializes the node to CSS syntax.
	Serialize() string
	// String is the __repr__ of the node, such as "<IdentToken foo>".
	String() string
	// contains filtered or unexported methods
}

Node is Node in tinycss2 tinycss2/ast.py. Every node type implements it. Each node type is a pointer to a struct of this file, and Type gives its type as a string, as the attribute type of upstream does:

if node.Type() == tinycss2.TypeWhitespace {

A type switch on the struct type works too:

if t, ok := node.(*tinycss2.WhitespaceToken); ok {

func ParseBlocksContents

func ParseBlocksContents[T Input](input T, opts ...Option) []Node

ParseBlocksContents is parse_blocks_contents in tinycss2 tinycss2/parser.py. It parses the contents of a block, which is used for example for the Content of a style rule or of an @page rule, or for the style attribute of an HTML element.

In a context that expects no at-rule or no qualified rule, reject each *AtRule or *QualifiedRule as invalid.

WithSkipComments drops the comments at the top level of the list, and, if the input is a string, every comment. WithSkipWhitespace drops the whitespace at the top level of the list.

It returns a list of *Declaration, *AtRule, *QualifiedRule, *Comment (without WithSkipComments), *WhitespaceToken (without WithSkipWhitespace) and *ParseError values.

func ParseComponentValueList

func ParseComponentValueList(input string, opts ...Option) []Node

ParseComponentValueList is parse_component_value_list in tinycss2 tinycss2/tokenizer.py. It parses a list of component values.

With WithSkipComments(true), the result, and recursively its blocks and functions, holds no Comment. WithMaxDepth bounds the nesting of the blocks and functions (D50).

The tokenizer works on the code points of css, as Python does, so that each column counts code points. A byte of css that is not valid UTF-8 reads as U+FFFD.

func ParseDeclarationList

func ParseDeclarationList[T Input](input T, opts ...Option) []Node

ParseDeclarationList is parse_declaration_list in tinycss2 tinycss2/parser.py. It parses a declaration list, which can also hold at-rules. Upstream deprecates it for ParseBlocksContents, as CSS Syntax Level 3 removed it.

The options work as in ParseBlocksContents. It returns a list of *Declaration, *AtRule, *Comment, *WhitespaceToken and *ParseError values.

func ParseOneComponentValue

func ParseOneComponentValue[T Input](input T, opts ...Option) Node

ParseOneComponentValue is parse_one_component_value in tinycss2 tinycss2/parser.py. It parses a single component value, which is used for example for an attribute value referred to by `attr(foo length)`.

It returns a component value that is neither whitespace nor a comment, or a *ParseError.

func ParseOneDeclaration

func ParseOneDeclaration[T Input](input T, opts ...Option) Node

ParseOneDeclaration is parse_one_declaration in tinycss2 tinycss2/parser.py. It parses a single declaration, which is used for example for a declaration in an @supports test.

It returns a *Declaration or a *ParseError. It drops any whitespace or comment before the ":" colon.

func ParseOneRule

func ParseOneRule[T Input](input T, opts ...Option) Node

ParseOneRule is parse_one_rule in tinycss2 tinycss2/parser.py. It parses a single qualified rule or at-rule, which is used for example by insertRule() of CSSOM.

It returns a *QualifiedRule, an *AtRule or a *ParseError. It drops any whitespace or comment before or after the rule.

func ParseRuleList

func ParseRuleList[T Input](input T, opts ...Option) []Node

ParseRuleList is parse_rule_list in tinycss2 tinycss2/parser.py. It parses a rule list that is not at the top level, such as the Content of an @media rule. Upstream deprecates it for ParseBlocksContents.

It differs from ParseStylesheet: it keeps the "<!--" and "-->" tokens at the top level. The options work as in ParseBlocksContents. It returns a list of *QualifiedRule, *AtRule, *Comment, *WhitespaceToken and *ParseError values.

func ParseStylesheet

func ParseStylesheet[T Input](input T, opts ...Option) []Node

ParseStylesheet is parse_stylesheet in tinycss2 tinycss2/parser.py. It parses a stylesheet from text, such as the text of a style element of HTML.

It differs from ParseRuleList: it drops the "<!--" and "-->" tokens at the top level, a legacy quirk of the style element of HTML. The options work as in ParseBlocksContents. It returns a list of *QualifiedRule, *AtRule, *Comment, *WhitespaceToken and *ParseError values.

type NumberToken

type NumberToken struct {
	Location

	// Value is the numeric value.
	Value float64
	// IntValue is the numeric value as an integer when IsInteger is true,
	// and 0 when it is false. Upstream holds None for false. An integer
	// out of the range of int64 saturates, where Python keeps every digit.
	IntValue int64
	// IsInteger reports whether the token was syntactically an integer.
	IsInteger bool
	// Representation is the CSS representation of the value.
	Representation string
}

NumberToken is NumberToken in tinycss2 tinycss2/ast.py, a number-token.

func NewNumberToken

func NewNumberToken(line, column int, value float64, intValue int64, isInteger bool, representation string) *NumberToken

NewNumberToken makes a NumberToken. Upstream takes an int_value of None for a number that is not an integer. In Go, isInteger is false for it.

func (*NumberToken) Serialize

func (t *NumberToken) Serialize() string

Serialize satisfies Node.

func (*NumberToken) String

func (t *NumberToken) String() string

String satisfies Node.

func (*NumberToken) Type

func (*NumberToken) Type() string

Type satisfies Node.

type Option

type Option func(*options)

Option is an option of the functions that parse. It changes the configuration of one call.

func WithMaxDepth

func WithMaxDepth(n int) Option

WithMaxDepth sets the most blocks and functions that the tokenizer nests in each other. Upstream has no such limit. At the limit, the tokenizer adds a ParseError of the kind "too-deep" in place of the block or the function that is too deep, and drops its content. A value of 0 or less sets DefaultMaxDepth.

func WithSkipComments

func WithSkipComments(v bool) Option

WithSkipComments is the argument skip_comments of upstream. If v is true and the input is a string, the tokenizer drops every comment. If v is true, the functions that parse a list also drop the comments at the top level of the list.

func WithSkipWhitespace

func WithSkipWhitespace(v bool) Option

WithSkipWhitespace is the argument skip_whitespace of upstream. If v is true, the functions that parse a list drop the whitespace at the top level of the list. The whitespace in the value of a declaration, and in the prelude and the content of a rule, stays.

type ParenthesesBlock

type ParenthesesBlock struct {
	Location

	// Content is the content of the block, as a list of component values.
	// The "(" and ")" markers are not in the list.
	Content []Node
}

ParenthesesBlock is ParenthesesBlock in tinycss2 tinycss2/ast.py, a ()-block.

func NewParenthesesBlock

func NewParenthesesBlock(line, column int, content []Node) *ParenthesesBlock

NewParenthesesBlock makes a ParenthesesBlock.

func (*ParenthesesBlock) Serialize

func (t *ParenthesesBlock) Serialize() string

Serialize satisfies Node.

func (*ParenthesesBlock) String

func (t *ParenthesesBlock) String() string

String satisfies Node.

func (*ParenthesesBlock) Type

func (*ParenthesesBlock) Type() string

Type satisfies Node.

type ParseError

type ParseError struct {
	Location

	// Kind is a machine-readable string that tells the type of error, such
	// as "bad-url".
	Kind string
	// Message is a human-readable explanation of the error.
	Message string
}

ParseError is ParseError in tinycss2 tinycss2/ast.py. It is a syntax error of some sort, and it can occur anywhere in the tree.

Syntax errors are not fatal in the parser, to allow for different error handling behaviors. For example, an error in a Selector list makes the whole rule invalid, but an error in a Media Query list only replaces one comma-separated query with "not all".

func NewParseError

func NewParseError(line, column int, kind, message string) *ParseError

NewParseError makes a ParseError, as the constructor of upstream does.

func (*ParseError) Serialize

func (e *ParseError) Serialize() string

Serialize satisfies Node.

func (*ParseError) String

func (e *ParseError) String() string

String satisfies Node.

func (*ParseError) Type

func (*ParseError) Type() string

Type satisfies Node.

type PercentageToken

type PercentageToken struct {
	Location

	// Value is the numeric value.
	Value float64
	// IntValue is the numeric value as an integer when IsInteger is true,
	// as in NumberToken.
	IntValue int64
	// IsInteger reports whether the value was syntactically an integer.
	IsInteger bool
	// Representation is the CSS representation of the value, without the
	// unit.
	Representation string
}

PercentageToken is PercentageToken in tinycss2 tinycss2/ast.py, a percentage-token: the representation and "%".

func NewPercentageToken

func NewPercentageToken(line, column int, value float64, intValue int64, isInteger bool, representation string) *PercentageToken

NewPercentageToken makes a PercentageToken, as NewNumberToken does.

func (*PercentageToken) Serialize

func (t *PercentageToken) Serialize() string

Serialize satisfies Node.

func (*PercentageToken) String

func (t *PercentageToken) String() string

String satisfies Node.

func (*PercentageToken) Type

func (*PercentageToken) Type() string

Type satisfies Node.

type QualifiedRule

type QualifiedRule struct {
	Location

	// Prelude is the part of the rule before the {} block, as a list of
	// component values.
	Prelude []Node
	// Content is the part of the rule inside the {} block, as a list of
	// component values.
	Content []Node
}

QualifiedRule is QualifiedRule in tinycss2 tinycss2/ast.py, a qualified rule: the prelude, "{", the content and "}".

The meaning of a qualified rule depends on its context. At the top level of a stylesheet, or in a conditional rule such as "@media", it is a style rule. Its Prelude is a list of selectors, and its Content is a list of property declarations.

func NewQualifiedRule

func NewQualifiedRule(line, column int, prelude, content []Node) *QualifiedRule

NewQualifiedRule makes a QualifiedRule.

func (*QualifiedRule) Serialize

func (r *QualifiedRule) Serialize() string

Serialize satisfies Node.

func (*QualifiedRule) String

func (r *QualifiedRule) String() string

String satisfies Node.

func (*QualifiedRule) Type

func (*QualifiedRule) Type() string

Type satisfies Node.

type RGBA

type RGBA struct {
	Red, Green, Blue, Alpha float64
}

RGBA is RGBA in tinycss2 tinycss2/color3.py, an RGBA color: four floats in the range 0 to 1.

func ParseColor3

func ParseColor3(token Node) (color *RGBA, currentColor bool)

ParseColor3 is parse_color in tinycss2 tinycss2/color3.py. It parses a color value as CSS Color Level 3 defines it, from a component value. Upstream also takes a string. In Go, parse the string first:

tinycss2.ParseColor3(tinycss2.ParseOneComponentValue(s, tinycss2.WithSkipComments(true)))

It returns nil and false for a value that is not a valid color. It returns nil and true for the keyword currentColor, where upstream returns the string 'currentColor'. It returns an RGBA for every other value, with the keywords, HSL and HSLA. The alpha channel is clipped to [0, 1], but red, green and blue can be out of range: rgb(-10%, 120%, 0%) gives (-0.1, 1.2, 0, 1).

type SquareBracketsBlock

type SquareBracketsBlock struct {
	Location

	// Content is the content of the block, as a list of component values.
	// The "[" and "]" markers are not in the list.
	Content []Node
}

SquareBracketsBlock is SquareBracketsBlock in tinycss2 tinycss2/ast.py, a []-block.

func NewSquareBracketsBlock

func NewSquareBracketsBlock(line, column int, content []Node) *SquareBracketsBlock

NewSquareBracketsBlock makes a SquareBracketsBlock.

func (*SquareBracketsBlock) Serialize

func (t *SquareBracketsBlock) Serialize() string

Serialize satisfies Node.

func (*SquareBracketsBlock) String

func (t *SquareBracketsBlock) String() string

String satisfies Node.

func (*SquareBracketsBlock) Type

func (*SquareBracketsBlock) Type() string

Type satisfies Node.

type StringToken

type StringToken struct {
	Location

	// Value is the unescaped value, without the quotes.
	Value string
	// Representation is the serialized form of the token.
	Representation string
}

StringToken is StringToken in tinycss2 tinycss2/ast.py, a string-token.

func NewStringToken

func NewStringToken(line, column int, value, representation string) *StringToken

NewStringToken makes a StringToken.

func (*StringToken) Serialize

func (t *StringToken) Serialize() string

Serialize satisfies Node.

func (*StringToken) String

func (t *StringToken) String() string

String satisfies Node.

func (*StringToken) Type

func (*StringToken) Type() string

Type satisfies Node.

type URLToken

type URLToken struct {
	Location

	// Value is the unescaped URL, without "url(" and ")".
	Value string
	// Representation is the serialized form of the token.
	Representation string
}

URLToken is URLToken in tinycss2 tinycss2/ast.py, a url-token.

func NewURLToken

func NewURLToken(line, column int, value, representation string) *URLToken

NewURLToken makes a URLToken.

func (*URLToken) Serialize

func (t *URLToken) Serialize() string

Serialize satisfies Node.

func (*URLToken) String

func (t *URLToken) String() string

String satisfies Node.

func (*URLToken) Type

func (*URLToken) Type() string

Type satisfies Node.

type UnicodeRangeToken

type UnicodeRangeToken struct {
	Location

	// Start is the start of the range, from 0 to 0xFFFFFF.
	Start int
	// End is the end of the range. It is Start when the source gave one
	// value only.
	End int
}

UnicodeRangeToken is UnicodeRangeToken in tinycss2 tinycss2/ast.py, a unicode-range-token.

func NewUnicodeRangeToken

func NewUnicodeRangeToken(line, column, start, end int) *UnicodeRangeToken

NewUnicodeRangeToken makes a UnicodeRangeToken.

func (*UnicodeRangeToken) Serialize

func (t *UnicodeRangeToken) Serialize() string

Serialize satisfies Node.

func (*UnicodeRangeToken) String

func (t *UnicodeRangeToken) String() string

String satisfies Node.

func (*UnicodeRangeToken) Type

func (*UnicodeRangeToken) Type() string

Type satisfies Node.

type WhitespaceToken

type WhitespaceToken struct {
	Location

	// Value is the whitespace sequence, as in the original CSS source.
	Value string
}

WhitespaceToken is WhitespaceToken in tinycss2 tinycss2/ast.py, a whitespace-token.

func NewWhitespaceToken

func NewWhitespaceToken(line, column int, value string) *WhitespaceToken

NewWhitespaceToken makes a WhitespaceToken.

func (*WhitespaceToken) Serialize

func (t *WhitespaceToken) Serialize() string

Serialize satisfies Node.

func (*WhitespaceToken) String

func (t *WhitespaceToken) String() string

String satisfies Node.

func (*WhitespaceToken) Type

func (*WhitespaceToken) Type() string

Type satisfies Node.

Jump to

Keyboard shortcuts

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