Repository navigation
fix(extract-static-html): escape JSX output and preserve unitless styles - #108
Open
JasonOA888 wants to merge 1 commit into
Open
JasonOA888 wants to merge 1 commit into
JasonOA888 wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
extract_inline_html.tswrites 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:JSXText, so<p>a < b</p>in a mock comes out as<p>a < b</p>. Anything that looks like a tag starts parsing as markup.alt='Say "hi"'closes the attribute early, and any"in a string style breaks thestyleattribute.px.opacity: 0.5,zIndex: 10,flexGrow: 1andlineHeight: 1.5turn into0.5px,10px,1pxand1.5px. The browser ignores the first three and the last one changes meaning.rawinstead ofcooked, so{`\u2014`}shows the escape sequence instead of the character.What changed:
<style>and<script>children are left as raw text, with the same</style/</scriptguard React DOM uses.--foo) are passed through without a unit.cooked.embedImagesnow 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=80would be requested as...&q=80. Inlinestyle="..."CSS is decoded beforeurl()scanning and re-escaped afterwards.<link>hrefs taken fromindex.htmlare decoded so they aren't escaped twice.The escaping and unit helpers are in a small dependency-free
html_serialize.ts, sotests/html-serialize.test.mjsruns under the existingvalidate-helper-scriptsjob with plainnode --test. The CLI itself needs the Babel packages, which that job doesn't install.Testing:
find plugins -name '*.test.mjs' -print0 | xargs -0 node --teston Node 24: 13/13 pass.opacity,z-indexandflex-grownow apply, and markup inside strings renders as text.?w=800&q=80query:src,poster, inlinebackgroundand<style>URLs all embed.<style>/<script>/<textarea>/<title>output compared againstreact-dom/server19renderToStaticMarkup.