Repository navigation
🐛 fix(etree): root from_lxml copies in a document - #2553
Merged
Merged
Conversation
gaborbernat
force-pushed
the
fix/etree-from-lxml-root
branch
from
November 21, 2026 22:21
ed3cc0a to
e136fd5
Compare
Merging this PR will not alter performance
Comparing Footnotes
|
This was referenced Nov 21, 2026
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.
A
from_lxmlcopy was a detached tree, so XPath resolved absolute paths from the copied element rather than from a document. Inside a predicate,//h1searched only below the predicate's context node, and/htmlmatched 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()staysNoneand serialization is unchanged.from_lxml(<html><body><div><h2/></div><h1/></body></html>).//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.