Documentation
¶
Overview ¶
Package css provides CSS parsing, cascading, specificity, and selector matching for SVG optimization, ported from SVGO's lib/style.js.
Index ¶
- func CompactStylesheet(cssText string, shouldSkip func(selector, mediaQuery string) bool) string
- func CompareSpecificity(a, b Specificity) int
- func ContainsAnyPseudoClass(s string) bool
- func IncludesAttrSelector(rules []StylesheetRule, name string, value *string) bool
- func Matches(node *svgast.Element, selector string, parents map[svgast.Node]svgast.Parent) bool
- func MatchesErr(node *svgast.Element, selector string, parents map[svgast.Node]svgast.Parent) (matched, ok bool)
- func QuerySelectorAll(node svgast.Node, selector string, parents map[svgast.Node]svgast.Parent) []*svgast.Element
- func QuerySelectorAllErr(node svgast.Node, selector string, parents map[svgast.Node]svgast.Parent) (results []*svgast.Element, ok bool)
- func SelectorClassNames(selector string) []string
- func SelectorIncludesAttr(selector string, name string, value *string, traversed bool) bool
- func SelectorLeadingID(selector string) (string, bool)
- func StripAllPseudoClasses(s string) string
- func StripPseudoClasses(s string) string
- type ComputedStyle
- type ComputedStyleType
- type ComputedStyles
- type Specificity
- type Stylesheet
- type StylesheetDeclaration
- type StylesheetRule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompactStylesheet ¶
CompactStylesheet generates compact CSS from raw CSS text while optionally removing specified selectors. At-rules are preserved in compact form. shouldSkip is called for each rule with its selector and media query context to determine if the rule should be removed from the output. If shouldSkip is nil, all rules are preserved.
func CompareSpecificity ¶
func CompareSpecificity(a, b Specificity) int
CompareSpecificity compares two specificity tuples. Returns -1 if a < b, 0 if a == b, 1 if a > b.
func ContainsAnyPseudoClass ¶ added in v0.2.0
ContainsAnyPseudoClass reports whether a selector contains any pseudo-class, mirroring `hasPseudoClasses` in SVGO's lib/style.js parseRule. Pseudo-elements (::) do not count.
func IncludesAttrSelector ¶
func IncludesAttrSelector(rules []StylesheetRule, name string, value *string) bool
IncludesAttrSelector determines if any CSS rule's selector includes or traverses the given attribute name. Optionally checks for a specific value.
Classes and IDs are generated as attribute selectors, so you can check for if a .class or #id is included by passing name="class" or name="id" respectively.
This is ported from SVGO's includesAttrSelector in lib/style.js. In the SVGO codebase, this is called as:
includesAttrSelector(rule.selector, name)
with traversed=false and value=null by default.
func Matches ¶
Matches checks if an element matches a CSS selector string. parents provides the parent map for traversal.
func MatchesErr ¶ added in v0.2.0
func MatchesErr(node *svgast.Element, selector string, parents map[svgast.Node]svgast.Parent) (matched, ok bool)
MatchesErr is Matches, additionally reporting whether the selector could be fully evaluated. When ok is false the selector contains something css-select throws on, and SVGO skips the rule entirely.
func QuerySelectorAll ¶
func QuerySelectorAll(node svgast.Node, selector string, parents map[svgast.Node]svgast.Parent) []*svgast.Element
QuerySelectorAll finds all descendant elements matching a selector. Selectors css-select would throw on yield no matches.
func QuerySelectorAllErr ¶ added in v0.2.0
func QuerySelectorAllErr(node svgast.Node, selector string, parents map[svgast.Node]svgast.Parent) (results []*svgast.Element, ok bool)
QuerySelectorAllErr is QuerySelectorAll, additionally reporting whether the selector could be fully evaluated. SVGO wraps its querySelectorAll call in a try/catch and skips the rule when css-select throws; ok mirrors that signal.
func SelectorClassNames ¶ added in v0.2.0
SelectorClassNames returns the names of the class selectors that appear directly in a selector, in source order. It mirrors the ClassSelector children csstree reports for a Selector node, so names nested inside functional pseudo-classes such as :not() are excluded.
func SelectorIncludesAttr ¶ added in v0.2.0
SelectorIncludesAttr reports whether a single selector string references the given attribute name, optionally with a specific value.
When traversed is true only references that are followed by a combinator count, mirroring the csswhat.isTraversal check SVGO performs on the segment after the matched one.
func SelectorLeadingID ¶ added in v0.2.0
SelectorLeadingID returns the name of a selector's first sub-selector when that sub-selector is an ID selector.
func StripAllPseudoClasses ¶ added in v0.2.0
StripAllPseudoClasses removes every pseudo-class, including its functional argument, from a selector. This mirrors SVGO's lib/style.js parseRule, which walks the selector and removes every PseudoClassSelector node before using the result for matching. Pseudo-elements (::) are preserved, as SVGO preserves them too.
func StripPseudoClasses ¶
StripPseudoClasses removes non-evaluatable pseudo-classes from a selector string, preserving pseudo-elements (::) and evaluatable pseudo-classes like :not(), :is(), etc.
Types ¶
type ComputedStyle ¶
type ComputedStyle struct {
Type ComputedStyleType
Inherited bool
Value string // only meaningful when Type == StyleStatic
}
ComputedStyle represents a computed style property for an element.
type ComputedStyleType ¶
type ComputedStyleType int
ComputedStyleType indicates whether a computed style is static or dynamic.
const ( // StyleStatic is a resolved static CSS value. StyleStatic ComputedStyleType = iota // StyleDynamic is a value that depends on pseudo-classes or media queries. StyleDynamic )
type ComputedStyles ¶
type ComputedStyles map[string]*ComputedStyle
ComputedStyles maps property names to their computed styles.
func ComputeOwnStyle ¶
func ComputeOwnStyle(stylesheet *Stylesheet, node *svgast.Element) ComputedStyles
ComputeOwnStyle computes the styles for a specific element (not inherited).
func ComputeStyle ¶
func ComputeStyle(stylesheet *Stylesheet, node *svgast.Element) ComputedStyles
ComputeStyle computes styles for an element with inheritance from parents.
type Specificity ¶
type Specificity [4]int
Specificity represents CSS selector specificity as [a, b, c, d]. a = inline style, b = IDs, c = classes/attributes, d = elements.
func CalculateSpecificity ¶
func CalculateSpecificity(selector string) Specificity
CalculateSpecificity calculates the CSS specificity of a selector string. Returns [inline, ids, classes, elements]; index 0 is always 0 for stylesheet rules. Mirrors csso's syntax.specificity().
type Stylesheet ¶
type Stylesheet struct {
Rules []StylesheetRule
Parents map[svgast.Node]svgast.Parent
}
Stylesheet represents collected CSS rules and parent map.
func CollectStylesheet ¶
func CollectStylesheet(root *svgast.Root) *Stylesheet
CollectStylesheet collects CSS rules from all <style> elements and builds the parent map for the entire tree.
type StylesheetDeclaration ¶
StylesheetDeclaration represents a single CSS property declaration.
func ParseStyleDeclarations ¶
func ParseStyleDeclarations(cssText string) []StylesheetDeclaration
ParseStyleDeclarations parses an inline style attribute value into a slice of StylesheetDeclarations. e.g. "fill:red; stroke:blue !important" → [{fill, red, false}, {stroke, blue, true}]
type StylesheetRule ¶
type StylesheetRule struct {
// Dynamic and Selector implement SVGO's inlineStyles policy: the
// pseudo-classes css-select can evaluate stay in the selector.
// StyleDynamic and StyleSelector implement lib/style.js's policy: every
// pseudo-class is stripped and the rule becomes dynamic instead.
Dynamic bool
StyleDynamic bool
Selector string // cleaned selector (pseudo-classes stripped for matching)
StyleSelector string // selector with every pseudo-class stripped
OriginalSelector string // original selector (with pseudo-classes, for CSS output)
Specificity Specificity
Declarations []StylesheetDeclaration
MediaQuery string // at-rule media query context (e.g., "media screen"), empty for top-level
}
StylesheetRule represents a CSS rule with its selector, specificity, and declarations.
func ParseStylesheet ¶
func ParseStylesheet(cssText string, dynamic bool) []StylesheetRule
ParseStylesheet parses a CSS stylesheet string into rules. dynamic indicates whether the rules come from a media-dependent context.