Repository navigation
🐛 fix(dom): keep the DOM within the 512 depth cap - #2548
Merged
Merged
Conversation
The cap checks appends against the open-element stack depth, but two paths built nodes past 512 levels: - The adoption agency nests the moved subtree under a formatting clone with plain appends, pushing nodes the cap kept at its last level one or more levels deeper. After the move, a node at the cap hands its children to its own parent, as a refused append would have. - Removing an entry from the middle of the stack (the agency's inner loop, a second <a>, a closed form) leaves its node an ancestor of the elements above it, so the stack undercounts the DOM depth. The tree now counts those removals and adds them to the depth the cap checks.
Merging this PR will not alter performance
Comparing Footnotes
|
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.
The parser documents a 512-level DOM cap, but three paths built nodes one to three levels past it:
<x>509 times, then<b><p>t</b>. After the move,cap_moved_subtreewalks the subtree, and any node at the cap hands its children to its own parent, right after itself. A refused append would put them in the same place, so text order holds.<a>, and a closed<form>take an entry out of the middle of the open-element stack. Its node stays an ancestor of everything above it, so the stack undercounts DOM depth, and the cap check never fires. Example: 600 repeats of<form><div></form>reach depth 1019. The tree now counts those removals (stack_slack) and adds them to the bound the cap checks. The:hasmemo bound (stack_depth_bound) is unchanged.<selectedcontent>. The copy uses plain appends, so a<selectedcontent>deeper than its option pushes the copy past the cap. The same walk caps the copy.Tests in
tests/dom/test_depth_cap_order.pyassert a maximum depth of 512 and the source text order for each path. All of them fail on main.Instruction counts on the CodSpeed parse, tree-builder and stream rows (callgrind, release + LTO, turbohtml's own code, against main): the worst row is
parse-formattingat +0.31%, thenparse-afeat +0.21%. Every other row is within ±0.02%.