Skip to content

🐛 fix(dom): keep the DOM within the 512 depth cap - #2548

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:fix/depth-cap-overshoot
Nov 21, 2026
Merged

gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:fix/depth-cap-overshoot

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

The parser documents a 512-level DOM cap, but three paths built nodes one to three levels past it:

  • Adoption agency move. The agency nests the moved subtree under a formatting clone with plain appends, so nodes the cap kept at its last level end up deeper. Example: <x> 509 times, then <b><p>t</b>. After the move, cap_moved_subtree walks 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.
  • Mid-stack removals. The agency's inner loop, a second <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 :has memo bound (stack_depth_bound) is unchanged.
  • Option copy into <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.py assert 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-formatting at +0.31%, then parse-afe at +0.21%. Every other row is within ±0.02%.

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.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 21, 2026
@codspeed

codspeed Bot commented Nov 21, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 580 untouched benchmarks
⏩ 91 skipped benchmarks1


Comparing gaborbernat:fix/depth-cap-overshoot (463b65b) with main (1e0639f)2

Open in CodSpeed

Footnotes

  1. 91 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (2248f64) during the generation of this report, so 1e0639f was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@gaborbernat
gaborbernat merged commit bad9a0c into tox-dev:main Nov 21, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant