css

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package css parses CSS syntax into Growse-owned rules.

Index

Constants

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
}

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 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 ImportRule

type ImportRule struct {
	URL   string
	Media []MediaQuery
}

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
}

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.

type PseudoClass

type PseudoClass struct {
	Kind     PseudoClassKind
	A        int
	B        int
	Negation *CompoundSelector
}

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
	PseudoEmpty
	PseudoFirstChild
	PseudoLastChild
	PseudoOnlyChild
	PseudoFirstOfType
	PseudoLastOfType
	PseudoOnlyOfType
	PseudoNthChild
	PseudoNthLastChild
	PseudoNthOfType
	PseudoNthLastOfType
	PseudoNot
	PseudoLink
	PseudoFocus
	PseudoEnabled
	PseudoDisabled
	PseudoChecked
	PseudoValid
	PseudoInvalid
)

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
}

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 (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
}

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.

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