@monochromatic-dev/module-css-edit #
Byte-preserving CSS read,
edit,
and write utility.
Free-function API over an immutable CssEditState;
a stylesheet-structure CST over the
@csstools/css-tokenizer
spec tokenizer,
with comments and formatting kept as first-class token data.
Member of the workspace's format-editing family
(jsonc-edit,
toml-edit):
immutable state,
every edit returns a fresh tree,
unchanged nodes shared by reference.
Why this exists #
The 2026-07 parser survey
(recorded in doc/troubleshooting/css-tooling.md)
found no established pure-JS CSS parser that is simultaneously
lossless,
nesting-correct,
and maintained:
css-tree corrupts relaxed nesting and drops comments,
@adobe/css-tools throws on custom-property block values,
@projectwallace/css-parser cannot emit CSS,
and postcss carries process-global reach-ins and a 2013-era mutable AST.
The @csstools tokenizer is spec-exact and byte-lossless but deliberately headless;
this package supplies the head:
the CSS Syntax Module Level 3 section 5 structure layer.
Design #
- Byte fidelity:
tokens keep their source representation;
stringifyCssover an unedited state reproduces the input byte-exactly. Comments and whitespace live in the tree astrivianodes. - Unified block contents:
every block accepts declarations,
nested rules
(
&and relaxed), and at-rules, so unknown at-rules like@mixinparse structurally. Declaration-versus-rule classification follows the spec's restart-as-rule step (span:hover { ... }reclassifies;--x: { ... };stays a declaration). - Strict:
malformed input throws a positioned
CssParseErrorinstead of producing recovery nodes; a build pipeline wants loud failures. - Immutable transforms:
transformNodes/transformStylesheetrebuild bottom-up under a visitor; a visitor returns a node (keep or replace) or a node array (splice; empty removes). Untouched subtrees keep reference identity. - No value parsing:
preludes and declaration values stay token slices.
Consumers needing token-level detail use the re-exported guards
(
isTokenString,isTokenURL,isTokenIdent) andtokenData.
Usage #
import {
asCssSource,
isCssAtRule,
parseCss,
stringifyCss,
transformStylesheet,
} from '@monochromatic-dev/module-css-edit';
const state = parseCss({ source: asCssSource('.btn { @apply --card; }',), },);
const withoutApplies = transformStylesheet({
root: state.root,
visit: (node,) => (isCssAtRule(node,) && (node.name === 'apply')) ? [] : node,
pruneTriviaBeforeRemoved: true,
},);
stringifyCss({ state, },); // byte-identical to the input
Node kinds #
stylesheet: root, ordered childrenatRule: at-keyword token, unescapedname, prelude token slice, optionalblockorsemicolonTokenrule: selector prelude token slice plusblockblock:{/}tokens plus unified childrendeclaration: raw token run including any trailing;trivia: whitespace, comments, CDO/CDC
Sidecars #
css-edit.fuzz: fast-check properties (byte round-trip, totality, structural sharing) plus a postcss differential oraclecss-edit.bench: mitata benchmark against postcss and css-treecss-edit.conformance: curated css-parsing-tests-style corpus with context amplification
Testing #
mise run //package/module/css-edit:buildAndTest