Skip to content

🐛 fix(etree): match lxml on iter, nsmap, to_lxml - #2552

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:fix/etree-lxml-gaps
Nov 21, 2026
Merged

gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:fix/etree-lxml-gaps

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

turbohtml.etree diverged from lxml.html in three places that block extraction code from using the C views instead of a Python adapter. Each now matches lxml.

ElementTree.iter

getroottree() returned an ElementTree with only getroot(), so tree.getroottree().iter("p") raised AttributeError. It now has iter(*tags), which walks from the root element like lxml's _ElementTree.iter and takes the same tag filters as ElementView.iter:

>>> document_fromstring("<p>a</p><div><p>b</p></div>").find(".//div").getroottree().iter("p")
# before: AttributeError; after: both <p> elements, as lxml.html gives

ElementView.nsmap

The attribute was missing. It now returns a new empty dict, as lxml.html does for every element. Views name foreign elements such as svg by their local name, so there are no namespace declarations to report.

XML-illegal characters in to_lxml

lxml refuses strings holding C0 controls other than tab, LF and CR, surrogates, U+FFFE and U+FFFF. to_lxml and to_lxml_html passed text straight through, so:

Input Before After
text or tail with \x01 ValueError: All strings must be XML compatible the character is removed
<a href="/z\x01">, <img alt="p\x0bq"> href and alt dropped entirely href="/z", alt="pq"

The scan returns the original string when nothing needs stripping, so a clean tree copies as before. No benchmark in tools/bench calls to_lxml, so no CodSpeed row reaches the change.

Not changed: drop_tree tail

lxml leaves the dropped element's tail on it, while turbohtml leaves it only in the former parent. #1935 made that choice on purpose to keep repeated drops linear (#1727), and the reference documents it, so it stays.

ElementTree gains iter(), which walks from the root element as lxml's
_ElementTree.iter does. ElementView gains nsmap, an empty mapping as on
lxml.html elements. to_lxml and to_lxml_html now strip the characters
lxml refuses (C0 controls other than tab, LF and CR, surrogates, U+FFFE,
U+FFFF) from text, tails and attribute values; before, text raised
ValueError and such an attribute was dropped whole.
@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 improve performance by 8.95%

2 warnings

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 2 improved benchmarks
✅ 578 untouched benchmarks
⏩ 91 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_mutating_feature[observe-registrations-unrelated] 1.4 ms 1.3 ms +9.71%
⚡ test_mutating_feature[observe-registrations] 1.3 ms 1.2 ms +8.2%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:fix/etree-lxml-gaps (7ebb602) with main (9fa4a35)

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

@gaborbernat
gaborbernat merged commit ca17e42 into tox-dev:main Nov 21, 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.

1 participant