Skip to content

🐛 fix(dom): move live ranges on normalize - #2447

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:fix/range-normalize-2445
Nov 17, 2026
Merged

gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:fix/range-normalize-2445

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Range boundaries kept their container and offset when normalize() merged text nodes, and when the whitespace collapse shortened a text node. compare_point() then reported positions that no longer matched the text.

from turbohtml import Range, parse

doc = parse("<p>a<i></i>b</p>")
p = doc.find("p")
r = Range(p, 2)
p.children[1].extract()
p.normalize()
# r.start_container is now the merged text "ab", at offset 1

normalize() moves every boundary into the merged text before any sibling is removed, including a boundary on the parent at a merged sibling's index. The whitespace collapse applies the replace-data rule to each run it drops, right to left, so each offset stays on the text it was in.

The expected containers and offsets in the new tests come from the DOM Standard's rules, worked out by hand, not from the output of the code.

Fixes #2445

Range boundaries kept their container and offset when normalize() merged
text nodes, and when the whitespace collapse shortened a text node, so
compare_point() reported positions that no longer matched the text.

normalize() moves every boundary into the merged text before any sibling
is removed, including a boundary on the parent at a merged sibling's
index. The whitespace collapse applies the replace-data rule to each run
it drops, right to left, so each offset is its position in the original
text.

Fixes tox-dev#2445
@gaborbernat gaborbernat added the bug Something isn't working label Nov 17, 2026
@gaborbernat
gaborbernat enabled auto-merge (squash) November 17, 2026 14:51
@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 581 untouched benchmarks
⏩ 90 skipped benchmarks1


Comparing gaborbernat:fix/range-normalize-2445 (951be27) with main (4f37baf)

Open in CodSpeed

Footnotes

  1. 90 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. ↩

The live-range moves for normalize() ran inside the merge loop, and the
merge loop unlinked empty siblings only after the copy. The empty-tail
benchmark regressed by about 14 percent in instruction count. The merge
loop is main's again, and the moves run in a pre-pass that only executes
when the tree has live Ranges. Measured under callgrind on the same
50,000 empty-tail input, the normalize() call costs 59.9M instructions
against 60.8M on main.

The whitespace collapse checks for live Ranges once per call and applies
its replace-data updates only then.

Range tests compare containers with ==. Node equality compares the node
itself, and wrapper identity is not kept on free-threaded builds.

Refs tox-dev#2445
@gaborbernat
gaborbernat merged commit b256e37 into tox-dev:main Nov 17, 2026
65 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.

Range boundaries stay stale after normalize() and whitespace collapse

1 participant