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.
Layer 2 dependency rules
This package depends only on Layer 1 core packages. It does not import from query, bridge, devtools, or entry packages. Grammar packs never depend on other grammar packs.
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).
Boundary Description Inputs Uint8Array (source bytes) + optional ParseCssOptionsOutputs LexTree (flat AST rooted at CssKind.Stylesheet)Side effects None Determinism Yes External dependencies @lanexio/parser-coreNever-throw guarantee Yes Security surface None (parse only, no output generation)
Install the package.
pnpm add @lanexio/parser-grammar-css
npm install @lanexio/parser-grammar-css
yarn add @lanexio/parser-grammar-css
Import the named export.
import { parseCss } from ' @lanexio/parser-grammar-css ' ;
Requirement Required Layer Notes @lanexio/parser-coreYes 1 ^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 */ \n body { color: red; } ' ));
for ( const node of tree . root . children ()) {
if (node . kind === CssKind . Comment ) {
console . log ( ' comment found ' );
Export Type Description 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.
Field Type Required Default Description conformancebooleanNo undefinedWhen true, produce spec-conformant CSSOM projection (omit comments, whitespace. discard invalid rules). validatePropertiesbooleanNo undefinedWhen true, flag unknown property names with LEX_NODE_HAS_ERROR.
Property Type Description rootLexNodeRoot node (CssKind.Stylesheet). nodeCountnumberTotal nodes in the tree. sourceUint8ArrayOriginal parsed bytes (after normalization).
Type Purpose Notes ParseCssOptionsOptions for parseCss { conformance?: boolean; validateProperties?: boolean }CssKindTypeUnion of all CssKind values Type-safe kind reference CssFieldTypeUnion of all CssField values Type-safe field reference CSSExtractExtracted CSS from HTML Contains CSS source and position info TokenMetaMetadata for a CSS token type Token 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.
Concern Status Generated output semantics Not applicable (stylesheet format) ARIA attributes in serialized output Not applicable Semantic element round-trip Not applicable
parseCss never throws on any byte sequence.
parseCss does not validate property values against known CSS property lists.
Threat Mitigation Status Malformed input byte sequence Panic-free guarantee: all inputs accepted, errors produce CssKind.Error AST nodes Implemented
Package Relationship Layer Notes @lanexio/parser-coreRequires 1 Provides LexTree, LexNode, LexCursor @lanexio/parserConsumes 6 Unified entry point
Version Date Status Notable changes 1.0.02026-05-29Current Initial stable release. Apache-2.0.
Parsing CSS Guide: parse CSS with options and kind constants. Flat AST How the 16-byte node layout works.