AI-native design editor. Open-source Figma alternative.
docs(dom-css): audit CSS parser boundaries
Danila Poyarkov8f7702f0e7f9
diff --git a/packages/docs/development/dom-css-parser-audit.md b/packages/docs/development/dom-css-parser-audit.mdnew file mode 100644index 000000000..86f846879--- /dev/null+++ b/packages/docs/development/dom-css-parser-audit.md@@ -0,0 +1,34 @@+# DOM/CSS parser audit++OpenPencil's DOM/CSS compatibility layer should not grow hand-rolled CSS parsing. Browser conversion should use native DOM/CSSOM and `getComputedStyle()`. Headless conversion may keep narrow approximations only as temporary test/CLI support and should prefer dependency-backed parsers or browser-oracle paths for new CSS behavior.++## Current dependency-backed pieces++- HTML parsing: `parse5` in the headless runtime, native `DOMParser` in the browser runtime.+- Stylesheet parsing: `@acemir/cssom` in the headless runtime, native CSSOM in the browser runtime.+- Tailwind generation: Tailwind v4 `compile()` / `build()`.+- Color parsing: `@open-pencil/core/color` (`culori`-backed).++## Current hand-rolled approximations++These are intentionally limited and should not be expanded without replacing them or proving a dependency cannot cover the use case.++| Area | File | Current behavior | Preferred direction |+|---|---|---|---|+| Selector matching/specificity | `packages/dom-css/src/headless-css.ts` | Supports simple tag/id/class selectors plus descendant and child combinators. Rejects pseudo/classes and attributes. | Replace with a selector engine over DesignDOM or run browser/runtime oracle for complex CSS. |+| Shorthand expansion | `packages/dom-css/src/headless-css.ts` | Expands simple margin/padding boxes, border color/width, and background color. | Use parsed declarations from CSSOM/native computed style; avoid adding new shorthand parsers. |+| `calc()` / custom properties | `packages/dom-css/src/headless-css.ts` | Resolves variables by direct lookup and only handles `calc(<number><unit> * <number>)`. | Browser runtime for real computed values; do not add arithmetic or fallback parsing manually. |+| Inline style strings | `packages/dom-css/src/style-attribute.ts`, `packages/dom-css/src/jsx/runtime.ts` | Splits simple `style="a: b;"` text into declarations. | Use native `CSSStyleDeclaration` in browser paths; keep only for simple authored test data or replace with a CSS declaration parser. |+| Shadow values | `packages/dom-css/src/css-values.ts` | Extracts one color and numeric offsets for simple shadows. | Replace with a CSS value parser before supporting multiple shadows, inset, color functions, or spread edge cases. |+| Numeric lengths | `packages/dom-css/src/css-values.ts` | Parses px/rem-ish numbers with `Number.parseFloat`. | Consume browser-computed pixel values where available; keep headless numeric parsing narrow. |++## Rule for new mapping work++- Do not add regex/string parsers for CSS grammar such as gradients, transforms, filters, complex shadows, selectors, variable fallback, or calc arithmetic.+- Prefer browser `getComputedStyle()` fixtures as oracle coverage.+- If headless support is required, first look for a maintained parser/runtime dependency and document why it was chosen.+- If a temporary approximation is unavoidable, keep it narrow, add tests that define its limits, and list it in this audit.++## Recently rejected++Linear-gradient parsing was started with manual comma/direction parsing and removed. Gradient support should be implemented only through a proper CSS value parser or browser-native extracted data that can be mapped without parsing CSS grammar manually.diff --git a/packages/docs/development/package-split.md b/packages/docs/development/package-split.mdindex e36ce2dbf..09f290ea5 100644--- a/packages/docs/development/package-split.md+++ b/packages/docs/development/package-split.md@@ -16,7 +16,7 @@ OpenPencil is splitting stable compatibility layers into independently publishab- `@open-pencil/kiwi` owns pure Kiwi schema/runtime code plus low-level Figma Kiwi codec, container, and parse helpers.- App, CLI, MCP, and Vue SDK consume packages through public workspace exports only.-See [`kiwi-package-plan.md`](./kiwi-package-plan.md) for the detailed `@open-pencil/kiwi` inventory, package-local test plan, and remaining split boundaries.+See [`kiwi-package-plan.md`](./kiwi-package-plan.md) for the detailed `@open-pencil/kiwi` inventory, package-local test plan, and remaining split boundaries. See [`dom-css-parser-audit.md`](./dom-css-parser-audit.md) for the DOM/CSS rule against expanding hand-rolled CSS parsing.## Candidate packagesdiff --git a/packages/dom-css/README.md b/packages/dom-css/README.mdindex c4f99b114..6f33279c8 100644--- a/packages/dom-css/README.md+++ b/packages/dom-css/README.md@@ -45,7 +45,7 @@ const document = runtime.parseHTML('<article class="card">OpenPencil</article>')const styled = await runtime.computeStyles(document, '.card { width: calc(10rem + 16px); }')```-The headless runtime is useful for Bun/Node tests, CLI flows, and fast approximate conversion. It supports common selectors, inheritance, shorthands, CSSOM grouping rules, and simple variable/calc values, but it is not a browser replacement. Do not use it as an oracle for browser-only CSS behavior such as layout-dependent computed values, full custom-property fallback behavior, modern color serialization, or UA defaults.+The headless runtime is useful for Bun/Node tests, CLI flows, and fast approximate conversion. It supports common selectors, inheritance, shorthands, CSSOM grouping rules, and simple variable/calc values, but it is not a browser replacement. Do not use it as an oracle for browser-only CSS behavior such as layout-dependent computed values, full custom-property fallback behavior, modern color serialization, or UA defaults. Do not expand headless CSS parsing with ad hoc regex/string parsers; add a maintained parser/runtime dependency or use the browser runtime instead.```tsimport { createHeadlessCSSRuntime } from '@open-pencil/dom-css'@@ -216,6 +216,6 @@ const graph = await tailwindHTMLToSceneGraph(## Roadmap- Expand reusable fixtures: inputs, badges, nav/menu rows, dialog shells, and richer cards-- Map more computed CSS properties to scene graph fields: richer shadows, typography details, position constraints, borders, and grid once OpenPencil's grid support matures+- Map more computed CSS properties to scene graph fields through browser-native computed style or dependency-backed parsers: richer shadows, typography details, position constraints, borders, gradients, and grid once OpenPencil's grid support matures- Improve SceneGraph → CSS export so generated HTML/CSS is useful for JSX, Tailwind, and web export- Keep `@open-pencil/dom-css` stable before splitting lower-level file-format packages such as future `@open-pencil/kiwi` and `@open-pencil/fig`