css

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package css parses CSS syntax into Growse-owned rules.

Index

Constants

View Source
const (
	MaxStylesheetRules          = 100_000
	MaxSelectorsPerRule         = 256
	MaxSelectorCombinators      = 64
	MaxFunctionalSelectorDepth  = 32
	MaxCascadeLayers            = 256
	MaxCustomProperties         = 4_096
	MaxCustomPropertyValueBytes = 64 << 10
	MaxCSSFunctionDepth         = 64
)
View Source
const (
	// MaxKeyframesRules bounds named keyframe storage per stylesheet.
	MaxKeyframesRules = 256
	// MaxFramesPerKeyframesRule bounds selector blocks in one @keyframes rule.
	MaxFramesPerKeyframesRule = 256
	// MaxDeclarationsPerKeyframe bounds work performed for one sampled frame.
	MaxDeclarationsPerKeyframe = 64
	// MaxOffsetsPerKeyframe bounds comma-separated selectors in one block.
	MaxOffsetsPerKeyframe = 64
)

Variables

This section is empty.

Functions

func DecodeString

func DecodeString(value string) (string, bool)

DecodeString decodes one quoted CSS string, including hexadecimal escapes.

func ResolveResourceURLs

func ResolveResourceURLs(stylesheet *Stylesheet, baseURL *url.URL)

ResolveResourceURLs makes supported CSS resource URLs absolute while their stylesheet base URL is still known.

Types

type AttributeMatcher

type AttributeMatcher uint8

AttributeMatcher identifies how an attribute value is compared.

const (
	AttributePresent AttributeMatcher = iota
	AttributeExact
	AttributeIncludes
	AttributeDashMatch
	AttributePrefix
	AttributeSuffix
	AttributeSubstring
)

type AttributeSelector

type AttributeSelector struct {
	Name            string
	Matcher         AttributeMatcher
	Value           string
	CaseInsensitive bool
}

AttributeSelector is one attribute condition in a compound selector.

type Combinator

type Combinator uint8

Combinator describes the relationship between two adjacent compounds.

const (
	CombinatorDescendant Combinator = iota
	CombinatorChild
	CombinatorAdjacentSibling
	CombinatorGeneralSibling
)

type ComponentKind

type ComponentKind uint8

ComponentKind identifies a component value token.

const (
	ComponentIdentifier ComponentKind = iota
	ComponentFunction
	ComponentAtKeyword
	ComponentHash
	ComponentString
	ComponentURL
	ComponentNumber
	ComponentPercentage
	ComponentDimension
	ComponentWhitespace
	ComponentDelimiter
	ComponentBlockStart
	ComponentBlockEnd
	ComponentComma
	ComponentBad
)

type ComponentValue

type ComponentValue struct {
	Kind ComponentKind
	Raw  string
}

ComponentValue is one typed token in a declaration value.

type CompoundSelector

type CompoundSelector struct {
	Universal     bool
	Type          string
	IDs           []string
	Classes       []string
	Attributes    []AttributeSelector
	Pseudos       []PseudoClass
	Hover         bool
	PseudoElement PseudoElementKind
}

CompoundSelector is a sequence of simple selectors without a combinator.

type ContainerQuery added in v0.15.0

type ContainerQuery struct {
	Name     string
	Features []MediaFeature
	Valid    bool
}

ContainerQuery is the supported named inline-size query subset.

type Declaration

type Declaration struct {
	Property  string
	Value     Value
	Important bool
}

Declaration is one CSS property/value pair.

func ParseDeclarations

func ParseDeclarations(value string) ([]Declaration, error)

ParseDeclarations parses the contents of an element's style attribute.

type FontFaceRule added in v0.15.0

type FontFaceRule struct {
	Family       string
	Source       string
	Style        string
	Weight       string
	Stretch      string
	UnicodeRange string
	Display      string
}

FontFaceRule retains descriptors needed for bounded font selection/loading.

type ImportRule

type ImportRule struct {
	URL     string
	Media   []MediaQuery
	Layer   string
	Layered bool
}

ImportRule references a stylesheet and an optional media query list.

type Keyframe

type Keyframe struct {
	Offsets      []float64
	Declarations []Declaration
}

Keyframe contains normalized offsets and declarations for one keyframe block.

type KeyframesRule

type KeyframesRule struct {
	Name   string
	Frames []Keyframe
}

KeyframesRule is one named CSS animation keyframe list.

type MediaFeature

type MediaFeature struct {
	Name       string
	Value      string
	Comparator string
}

MediaFeature is a media feature name and its optional value.

type MediaModifier

type MediaModifier uint8

MediaModifier changes how one media query is interpreted.

const (
	MediaModifierNone MediaModifier = iota
	MediaModifierNot
	MediaModifierOnly
)

type MediaQuery

type MediaQuery struct {
	Modifier MediaModifier
	Type     string
	Features []MediaFeature
}

MediaQuery is one comma-separated media query. Features are combined by and.

func ParseMediaQueryList added in v0.15.0

func ParseMediaQueryList(value string) []MediaQuery

ParseMediaQueryList parses the subset shared by author CSS and matchMedia.

type PropertyRule added in v0.15.0

type PropertyRule struct {
	Name         string
	Syntax       string
	InitialValue string
	Inherits     bool
	InheritsSet  bool
	Valid        bool
}

PropertyRule is a registered custom property definition.

type PseudoClass

type PseudoClass struct {
	Kind      PseudoClassKind
	A         int
	B         int
	Negation  *CompoundSelector
	Selectors []Selector
}

PseudoClass stores a pseudo-class and its parsed arguments. Nth expressions use A*n+B, while :not() stores its Level 3 simple-selector argument.

type PseudoClassKind

type PseudoClassKind uint8

PseudoClassKind identifies a supported pseudo-class condition.

const (
	PseudoRoot PseudoClassKind = iota
	PseudoHost
	PseudoOpen
	PseudoEmpty
	PseudoFirstChild
	PseudoLastChild
	PseudoOnlyChild
	PseudoFirstOfType
	PseudoLastOfType
	PseudoOnlyOfType
	PseudoNthChild
	PseudoNthLastChild
	PseudoNthOfType
	PseudoNthLastOfType
	PseudoNot
	PseudoIs
	PseudoWhere
	PseudoHas
	PseudoScope
	PseudoRelativeScope
	PseudoLink
	PseudoFocus
	PseudoEnabled
	PseudoDisabled
	PseudoChecked
	PseudoValid
	PseudoInvalid
	PseudoDefined
	PseudoPlaceholderShown
	PseudoReadOnly
	PseudoReadWrite
	PseudoRequired
	PseudoOptional
	PseudoFocusVisible
	PseudoFocusWithin
)

type PseudoElementKind

type PseudoElementKind uint8

PseudoElementKind identifies generated content attached to an element.

const (
	PseudoElementNone PseudoElementKind = iota
	PseudoElementBefore
	PseudoElementAfter
)

type Rule

type Rule struct {
	Kind         RuleKind
	Selectors    []Selector
	Declarations []Declaration
	Order        int
	Media        [][]MediaQuery
	Supports     []SupportsCondition
	Containers   []ContainerQuery
	// Layer is empty for unlayered author rules.
	Layer string
}

Rule associates selectors with declarations.

type RuleKind

type RuleKind uint8

RuleKind identifies the grammar represented by a rule.

const (
	// RuleStyle associates selectors with declarations.
	RuleStyle RuleKind = iota
	// RuleAt represents an at-rule. At-rule evaluation is added incrementally.
	RuleAt
)

type Selector

type Selector struct {
	Kind        SelectorKind
	Tag         string
	Class       string
	ID          string
	Hover       bool
	Compounds   []CompoundSelector
	Combinators []Combinator
}

Selector is a parsed selector. The legacy fields remain populated for the original four selector forms while Compounds is the canonical AST.

func ParseSelectorList added in v0.15.0

func ParseSelectorList(value string) []Selector

ParseSelectorList parses a bounded DOM/CSS selector list using the same grammar accepted by the style engine.

func (Selector) Specificity

func (s Selector) Specificity() [3]int

Specificity returns the selector's (ID, class, tag) specificity tuple.

type SelectorKind

type SelectorKind uint8

SelectorKind identifies the limited selector forms supported by the MVP.

const (
	SelectorTag SelectorKind = iota
	SelectorClass
	SelectorID
	SelectorTagClass
	SelectorUniversal
	SelectorCompound
)

type Stylesheet

type Stylesheet struct {
	Rules     []Rule
	Imports   []ImportRule
	Keyframes []KeyframesRule
	// LayerOrder contains author cascade layers in first-declaration order.
	LayerOrder []string
	FontFaces  []FontFaceRule
	Properties []PropertyRule
}

Stylesheet is an ordered collection of CSS rules.

func Parse

func Parse(reader io.Reader) (*Stylesheet, error)

Parse reads a stylesheet. Unsupported selectors and at-rules are ignored.

func (*Stylesheet) Append

func (s *Stylesheet) Append(other *Stylesheet)

Append adds another stylesheet while preserving global source order.

func (*Stylesheet) NestUnderLayer added in v0.15.0

func (s *Stylesheet) NestUnderLayer(parent string)

NestUnderLayer makes every rule in a loaded stylesheet participate in the layer named by an @import layer() clause.

type SupportsCondition added in v0.15.0

type SupportsCondition struct {
	Kind      SupportsKind
	Property  string
	Value     string
	Selectors []Selector
	Children  []SupportsCondition
}

SupportsCondition retains a parsed feature query for evaluation against the style engine's actual declaration and selector capabilities.

type SupportsKind added in v0.15.0

type SupportsKind uint8

SupportsKind identifies the boolean grammar of an @supports condition.

const (
	SupportsUnknown SupportsKind = iota
	SupportsDeclaration
	SupportsSelector
	SupportsNot
	SupportsAnd
	SupportsOr
)

type Value

type Value struct {
	Raw        string
	Components []ComponentValue
}

Value is a declaration value before property-specific computation. Raw preserves the serialized input while Components exposes typed CSS tokens.

Jump to

Keyboard shortcuts

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