Skip to content

fix(extract-static-html): escape JSX output and preserve unitless styles - #108

Open
JasonOA888 wants to merge 1 commit into
google-labs-code:mainfrom
JasonOA888:fix/extract-inline-html-serialization
Open

JasonOA888 wants to merge 1 commit into
google-labs-code:mainfrom
JasonOA888:fix/extract-inline-html-serialization

Conversation

@JasonOA888

Copy link
Copy Markdown

extract_inline_html.ts writes JSX string values straight into the HTML it emits. That works for most mocks, but a few ordinary inputs give you a page that doesn't match what React would render:

  • Text children aren't escaped. Babel already decodes entities in JSXText, so <p>a &lt; b</p> in a mock comes out as <p>a < b</p>. Anything that looks like a tag starts parsing as markup.
  • Attribute values aren't escaped either, so alt='Say "hi"' closes the attribute early, and any " in a string style breaks the style attribute.
  • Every non-zero numeric style gets px. opacity: 0.5, zIndex: 10, flexGrow: 1 and lineHeight: 1.5 turn into 0.5px, 10px, 1px and 1.5px. The browser ignores the first three and the last one changes meaning.
  • Template literals use raw instead of cooked, so {`\u2014`} shows the escape sequence instead of the character.

What changed:

  • Text and attribute values are escaped on output. <style> and <script> children are left as raw text, with the same </style / </script guard React DOM uses.
  • Numeric styles follow React's unitless list, and custom properties (--foo) are passed through without a unit.
  • Templates use cooked.
  • embedImages now reads URLs back out of the HTML after they've been escaped, so it decodes them before fetching. Without that, https://images.unsplash.com/...?w=800&q=80 would be requested as ...&amp;q=80. Inline style="..." CSS is decoded before url() scanning and re-escaped afterwards. <link> hrefs taken from index.html are decoded so they aren't escaped twice.

The escaping and unit helpers are in a small dependency-free html_serialize.ts, so tests/html-serialize.test.mjs runs under the existing validate-helper-scripts job with plain node --test. The CLI itself needs the Babel packages, which that job doesn't install.

Testing:

  • find plugins -name '*.test.mjs' -print0 | xargs -0 node --test on Node 24: 13/13 pass.
  • I ran the full CLI with Babel installed outside the repo:
    • A plain mock with no special characters produces byte-identical output before and after.
    • Escaping and unitless-style fixtures checked in Chrome: the computed opacity, z-index and flex-grow now apply, and markup inside strings renders as text.
    • A local image server that only answers the exact ?w=800&q=80 query: src, poster, inline background and <style> URLs all embed.
    • <style>/<script>/<textarea>/<title> output compared against react-dom/server 19 renderToStaticMarkup.

Escape HTML text and attributes while preserving inline CSS and scripts. Decode resource URLs before embedding, keep unitless numeric styles, and use cooked template strings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant