-
Notifications
You must be signed in to change notification settings - Fork 60
fix(dom): reduce transient allocation during serialize (PER-10135) #2349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rishigupta1599
wants to merge
3
commits into
master
Choose a base branch
from
fix/per-10135-dom-serialize-memory
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+179
−6
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9eb405c
fix(dom): reduce transient allocation during serialize (PER-10135)
rishigupta1599 fc7b9a0
fix(dom): gate serialize fast path on domTransformation, keep clone r…
rishigupta1599 6ddd033
test(dom): make the marker un-mangling test actually exercise both ma…
rishigupta1599 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { getOuterHTML } from '../src/clone-dom'; | ||
|
|
||
| describe('getOuterHTML', () => { | ||
| // A detached <html> element standing in for a clone's documentElement. Built | ||
| // fresh per call so the fast path's `textContent = ''` can't leak between | ||
| // assertions. | ||
| function makeTree() { | ||
| let doc = document.implementation.createHTMLDocument(''); | ||
| doc.body.innerHTML = '<div id="host"><p>hello</p><span title="$& $1">chars</span></div>'; | ||
| return doc.documentElement; | ||
| } | ||
|
|
||
| // The reassembly the fast path replaces, kept verbatim so the equality test | ||
| // below is a real comparison and not a restatement of the new code. | ||
| function reassemble(docElement, shadowRootElements) { | ||
| let innerHTML = docElement.getHTML({ serializableShadowRoots: true, shadowRoots: shadowRootElements }); | ||
| docElement.textContent = ''; | ||
| return docElement.outerHTML.replace('</html>', () => `${innerHTML}</html>`); | ||
| } | ||
|
|
||
| it('produces byte-identical output to the reassembled form with no shadow roots', () => { | ||
| expect(getOuterHTML(makeTree(), { shadowRootElements: [] })) | ||
| .toBe(reassemble(makeTree(), [])); | ||
| }); | ||
|
|
||
| it('releases the clone node graph on the fast path', () => { | ||
| // The reassembly path empties docElement as a side effect; the fast path | ||
| // has to do the same or the whole clone stays reachable while the caller | ||
| // builds the doctype concatenation and any stringified copy on top of it. | ||
| let docElement = makeTree(); | ||
| getOuterHTML(docElement, { shadowRootElements: [] }); | ||
| expect(docElement.childNodes.length).toBe(0); | ||
| }); | ||
|
|
||
| it('serializes shadow roots absent from shadowRootElements when told the clone may hold them', () => { | ||
| // getHTML's `serializableShadowRoots: true` picks up roots attached with | ||
| // `serializable: true` even when they are not in the explicit array, so an | ||
| // empty array alone must not gate the fast path. | ||
| let docElement = makeTree(); | ||
| let shadow = docElement.querySelector('#host').attachShadow({ mode: 'open', serializable: true }); | ||
| shadow.innerHTML = '<b>inside shadow</b>'; | ||
|
|
||
| let html = getOuterHTML(docElement, { | ||
| shadowRootElements: [], | ||
| cloneMayHaveUncountedShadowRoots: true | ||
| }); | ||
|
|
||
| expect(html).toContain('inside shadow'); | ||
| expect(html).toContain('<template shadowrootmode="open"'); | ||
| }); | ||
|
|
||
| it('returns outerHTML directly when forceShadowAsLightDOM is set', () => { | ||
| let docElement = makeTree(); | ||
| expect(getOuterHTML(docElement, { shadowRootElements: [], forceShadowAsLightDOM: true })) | ||
| .toBe(docElement.outerHTML); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Low] Merged regex silently widens the tag branch to case-insensitive
The old pair was asymmetric: the custom-element pass was case-sensitive (
/(<\/?)data-percy-custom-element-/g) and only the attribute pass hadi. Folding them under a single/giextendsito the tag branch.I ran a differential fuzz (70k generated inputs plus hand-written edge cases —
$&/$1in attribute values, both markers adjacent,</closers, empty/hyphen/colon attribute names) against the old two-pass implementation. This is the only divergence found: with lowercase markers the two are byte-identical across 50k cases, but<DATA-PERCY-CUSTOM-ELEMENT-X>is now rewritten to<X>where it previously passed through untouched.Not reachable from Percy's own output —
clone-dom.js:36builds the tag throughdocument.createElement, which ASCII-lowercases in an HTML document. The exposure is customer page content that literally contains<DATA-PERCY-CUSTOM-ELEMENT-…in a text node,<pre>,<script>, or attribute value, which would now be corrupted in the snapshot.Suggestion: both markers are always emitted lowercase, so
ibuys nothing and only widens the blast radius — drop it:If you'd rather keep the old attribute-branch tolerance exactly, keep
ibut note it in the comment so the widening is a recorded decision rather than a side effect of merging.Reviewer: stack:pr-review (orchestrator verification)