Skip to content

@lanexio/parser-grammar-css

This page documents @lanexio/parser-grammar-css, the CSS grammar package implementing CSS Syntax Module Level 3 with two-pass tokenization and tree construction.

  • Version: Stable
  • Module name: parser-grammar-css
  • Package: @lanexio/parser-grammar-css
  • Import path: @lanexio/parser-grammar-css
  • Layer: 2 (Grammar)
  • Runtime: Universal
  • Module format: ESM
  • Stability: Stable
  • Primary use case: Parse CSS stylesheets into a flat AST.
  • You need to parse CSS stylesheets into a traversable AST.
  • You need to extract CSS from HTML documents (inline <style> blocks).
  • You need to validate CSS property values.
  • You need a spec-conformant CSS object-model projection (use cssConformanceGrammar).
BoundaryDescription
InputsUint8Array (source bytes) + optional ParseCssOptions
OutputsLexTree (flat AST rooted at CssKind.Stylesheet)
Side effectsNone
DeterminismYes
External dependencies@lanexio/parser-core
Never-throw guaranteeYes
Security surfaceNone (parse only, no output generation)
  1. Install the package.

    Terminal window
    pnpm add @lanexio/parser-grammar-css
  2. Import the named export.

    import { parseCss } from '@lanexio/parser-grammar-css';
RequirementRequiredLayerNotes
@lanexio/parser-coreYes1^1.0.0
import { parseCss } from '@lanexio/parser-grammar-css';
const encoder = new TextEncoder();
const bytes = encoder.encode('body { color: red; font-size: 16px; }');
const tree = parseCss(bytes);
console.log(tree.nodeCount);
console.log(tree.root.kind); // CssKind.Stylesheet
import { parseCss, CssKind } from '@lanexio/parser-grammar-css';
const encoder = new TextEncoder();
const tree = parseCss(encoder.encode('/* header */\nbody { color: red; }'));
for (const node of tree.root.children()) {
if (node.kind === CssKind.Comment) {
console.log('comment found');
}
}
ExportTypeDescription
parseCss(bytes: Uint8Array, options?: ParseCssOptions) => LexTreeParse CSS. Never throws.
normalizeCSSInput(bytes: Uint8Array) => Uint8ArrayNormalize CSS input (CR/FF/CRLF to LF, surrogates to U+FFFD).
extractCSS(tree: LexTree) => CSSExtract[]Extract CSS from HTML document tree (style elements).
parseAllCSS(html: LexTree, cssOptions?: ParseCssOptions) => LexTreeParse all CSS found in HTML document.
validateValues(tree: LexTree) => voidValidate CSS property values against known property list.
CssKindconst objectNumeric kind IDs for all CSS node types.
CssFieldconst objectNumeric field IDs for CSS value slots.
CSS_FIELD_NAMES_BY_IDreadonly string[]Field name lookup by numeric field ID.
CSS_KIND_NAMES_BY_IDreadonly Record<number, string>Kind-name lookup by numeric ID.
cssGrammarLanexioParserPureGrammarTooling grammar (preserves comments, whitespace).
cssConformanceGrammarLanexioParserPureGrammarConformance grammar (strips trivia, conformant CSSOM projection).
cssRegistrationGrammarRegistrationRegistration for the unified grammar registry.
cssReuseOracleReuseOracleIncremental reuse oracle for CSS.
DECL_FLAG_IMPORTANTnumberFlag for !important declarations.
TOKEN_NAMESobjectToken type names for CSS tokenizer.
getTokenMetadata(token: number) => TokenMetaGet metadata for a CSS token type.
FieldTypeRequiredDefaultDescription
conformancebooleanNoundefinedWhen true, produce spec-conformant CSSOM projection (omit comments, whitespace. discard invalid rules).
validatePropertiesbooleanNoundefinedWhen true, flag unknown property names with LEX_NODE_HAS_ERROR.
PropertyTypeDescription
rootLexNodeRoot node (CssKind.Stylesheet).
nodeCountnumberTotal nodes in the tree.
sourceUint8ArrayOriginal parsed bytes (after normalization).
TypePurposeNotes
ParseCssOptionsOptions for parseCss{ conformance?: boolean; validateProperties?: boolean }
CssKindTypeUnion of all CssKind valuesType-safe kind reference
CssFieldTypeUnion of all CssField valuesType-safe field reference
CSSExtractExtracted CSS from HTMLContains CSS source and position info
TokenMetaMetadata for a CSS token typeToken classification

The default cssGrammar preserves comments, whitespace, and unknown at-rules for tooling workflows. Use cssConformanceGrammar for a spec-conformant CSS object model projection that strips trivia and discards invalid rules.

extractCSS finds <style> elements in an HTML tree and returns their CSS source. parseAllCSS combines extraction and parsing in a single step.

  • No direct accessibility surface. The CSS parser produces flat AST data structures.
ConcernStatus
Generated output semanticsNot applicable (stylesheet format)
ARIA attributes in serialized outputNot applicable
Semantic element round-tripNot applicable
  • parseCss never throws on any byte sequence.
  • parseCss does not validate property values against known CSS property lists.
ThreatMitigationStatus
Malformed input byte sequencePanic-free guarantee: all inputs accepted, errors produce CssKind.Error AST nodesImplemented
PackageRelationshipLayerNotes
@lanexio/parser-coreRequires1Provides LexTree, LexNode, LexCursor
@lanexio/parserConsumes6Unified entry point
VersionDateStatusNotable changes
1.0.02026-05-29CurrentInitial stable release. Apache-2.0.
  • None.