css

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package css provides CSS parsing, cascading, specificity, and selector matching for SVG optimization, ported from SVGO's lib/style.js.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompactStylesheet

func CompactStylesheet(cssText string, shouldSkip func(selector, mediaQuery string) bool) string

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

func Matches(node *svgast.Element, selector string, parents map[svgast.Node]svgast.Parent) bool

Matches checks if an element matches a CSS selector string. parents provides the parent map for traversal.

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.

func StripPseudoClasses

func StripPseudoClasses(s string) string

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]. This is a simplified implementation for SVG selectors.

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

type StylesheetDeclaration struct {
	Name      string
	Value     string
	Important bool
}

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          bool
	Selector         string // cleaned selector (pseudo-classes stripped for matching)
	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.

Jump to

Keyboard shortcuts

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