Skip to content

🐛 fix(etree): root from_lxml copies in a document - #2553

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/etree-from-lxml-root
Nov 21, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/etree-from-lxml-root

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

A from_lxml copy was a detached tree, so XPath resolved absolute paths from the copied element rather than from a document. Inside a predicate, //h1 searched only below the predicate's context node, and /html matched nothing. trafilatura's .//div[not(.//h2[not(//h1)])] returned [] where lxml returns the div, which blocks trafilatura#939 from dropping its Python adapter.

lxml roots every tree in a document. The copy now does the same: it replaces the document element of an empty parsed document. getparent() stays None and serialization is unchanged.

Query on from_lxml(<html><body><div><h2/></div><h1/></body></html>) Before Now (= lxml)
.//div[not(.//h2[not(//h1)])] [] [div]
.//h2[/html] [] [h2]
/html/body/h1 [] [h1]
.//h2[count(//h1) = 1] [] [h2]

Only the subtree of the element passed in is copied. For an element that isn't the root of its lxml tree, the copy's document element is that element, while lxml's absolute paths still see the whole lxml document. trafilatura passes the whole document, so it matches lxml exactly.

@gaborbernat gaborbernat added the bug Something isn't working label Nov 21, 2026
@gaborbernat
gaborbernat force-pushed the fix/etree-from-lxml-root branch from ed3cc0a to e136fd5 Compare November 21, 2026 22:21
@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/etree-from-lxml-root (e136fd5) 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 f1541ac 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