This document outlines the boundaries between standard CSSOM specifications and custom extensions in this library.
These APIs are defined in the CSSOM-1 specification. They are designed to mimic the standard browser APIs.
CSSStyleSheetCSSStyleRuleCSSMediaRuleCSSSupportsRuleCSSFontFaceRuleCSSPageRuleCSSKeyframesRuleCSSKeyframeRuleCSSNamespaceRuleCSSImportRuleCSSStyleDeclarationMediaListStyleSheetListLinkStyle
- Constructors: Standard CSSOM usually instantiates these via the DOM. We allow direct instantiation with parameters (e.g.,
new CSSStyleSheet(rules)) to make them usable in Node.js without a full browser environment. - Parsing: Standard CSSOM does not expose static parsing methods on these classes. We use the
Parserclass (see below) to bridge this gap. CSSImportRule.styleSheet: Hardcoded tonullbecause the library is a static, offline parser and does not perform network fetches or local I/O to load external imported stylesheets.
These APIs are defined in newer Houdini drafts and are intended to expose lower-level parsing and typed values.
- CSS Typed OM:
submodules/css-houdini-drafts/css-typed-om/Overview.bs - CSS Parser API: Based on the WICG CSS Parser API draft.
CSS.parseStylesheet()CSS.parseRuleList()CSS.parseRule()CSS.parseDeclarationList()CSS.parseDeclaration()CSS.parseValue()CSS.parseValueList()CSS.parseCommaValueList()CSSParserRule,CSSParserAtRule,CSSParserQualifiedRuleCSSParserDeclaration,CSSParserBlock,CSSParserFunctionCSSNumericValue,CSSUnitValue,CSSMathValue(and subclasses)CSSTransformValue,CSSTransformComponent(and subclasses)StylePropertyMap(Read-Write and Read-Only)
- String Boxing: The spec defines
CSSTokenastypedef (DOMString or CSSStyleValue or CSSParserValue) CSSToken;. We box strings inCSSParserTokeninstead of allowing raw strings directly. - Synchronous Execution:
parseRuleandparseDeclarationListare implemented synchronously instead of returning Promises. - Immutability: Properties like
prelude,body, andargsare mutable arrays instead ofFrozenArray. - Constructor Arguments: The
bodyparameter is mandatory in some constructors (e.g.,CSSParserQualifiedRule) where the spec makes it optional. - Math Functions: We support new math functions from CSS Values 4 (like
sin(),cos(),abs(), etc.) via a customCSSMathFunctionclass. Since the CSS Typed OM 1 spec only defines operators forsum,product,negate,invert,min,max, andclamp,CSSMathFunction.operatorreturns'sum'as a fallback for these new functions to satisfy the type system, which is a known spec gap. - WebIDL Dictionary Bindings: In a browser, the WebIDL bindings layer automatically checks dictionary constraints (like checking that the
nameparameter inCSS.registerProperty()options is present and throwing aTypeError). In our headless Node runtime, we perform these validations manually in JavaScript. CSSTransformComponentInheritance: In the CSS Typed OM Level 1 specification,CSSTransformComponentdoes not inherit fromCSSStyleValue. However, to support properties liketranslateandrotatewhich reify directly to transform components, and to allow them to be returned fromCSSStyleValue.parseAll()andStylePropertyMap.get()(which returnCSSStyleValue), we makeCSSTransformComponentextendCSSStyleValue. This matches the implementation in modern browsers (like Blink/Chrome).- Math Simplification & AST Structure Preservation: In accordance with CSS Values 4 (Calculation Trees), we preserve the raw parsed AST structure of mathematical expressions in
CSSNumericValue.parse()andStylePropertyMapparsing rather than performing eager simplification of compatible units (which is expected by older/Level 1 WPT tests). Eager simplification is deferred to computed-value time or manual.simplify()calls.
These APIs are NOT part of any W3C specification. They exist to make the library usable for static analysis, testing, and in non-browser environments.
Parserclass static utilities:calculateSpecificity(selector): Calculates the specificity of a selector.getCascadedStyle(element, rules): Calculates computed styles against a static DOM (likelinkedom).resolveVariables(style, property, envMap?): Expandsvar()andenv()functions with fallbacks.
- Standalone Utilities:
tokenize(text): Exposes the low-level tokenizer.serialize(ast): Exposes the low-level serializer.StreamingTokenizer: For memory-efficient streaming tokenization.
The public API surface area is locked down and verified by api-surface.test.ts. Any additions or removals of public exports must be reflected in that test to ensure intentional API changes.
- When adding new features, clearly identify which layer they belong to.
- Prefer implementing standard APIs (Houdini or CSSOM) over custom ones whenever possible.
- Cite spec anchors in code comments for all standard implementations.