Repository navigation
Conversation
XPath's data model has no DOCTYPE node, and libxml2's XPath never selects one, but node() matched it. So /node(), //node(), count(/node()), positions such as /node()[2], the sibling axes and preceding:: and following:: all included the DOCTYPE. Every HTML document has one, because parse_html adds one when the input doesn't. node() no longer matches a DOCTYPE, except on the self axis. That axis only reaches a DOCTYPE that is itself the context node, and libxml2's `.` keeps it there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
node()matches DOCTYPE nodes, so they turn up in XPath results. XPath's data model has no DOCTYPE node, and libxml2 never returns one:/node()<?p?>,<r/><?p?>,<r/>/node()[1]<?p?>count(/node())The same goes for
//node(), both sibling axes,preceding::andfollowing::. Every HTML document is affected, becauseparse_htmladds a DOCTYPE when the input has none.The change
node()no longer matches a DOCTYPE, except on the self axis. That axis only reaches a DOCTYPE that is itself the context node, and there libxml2's.returns it. xmloxide parses.asself::node(), so filtering the self axis as well would break.. One small difference remains: with a DOCTYPE as the context node, libxml2 returns nothing for an explicitself::node(), and this returns the DOCTYPE.Tests
Three new tests in
xpath/eval.rs. Two failed on main and pass now. They cover/node(),/node()[2],count(/node()), both sibling axes,preceding::(from an element and from an attribute),following::,/descendant-or-self::node(),//node(), andparse_htmlwith and without a DOCTYPE. The third, which passed before as well, checks that.and..still work with a DOCTYPE as the context node. Every expectation was checked against libxml2 2.14.6 and 2.15.4.All 1,670 tests pass, as do W3C 1727/1727 and libxml2 119/119. Clippy and docs are clean.
Cost
Instructions per query, counted with callgrind on
parser_bench's XPath fixture:parser_bench's six XPath queries: unchanged, within ±0.01%.node()on every node pay about 5 instructions per node for the extra check, as libxml2 does://node()+6.4%,count(//node())+6.0%,//book[40]/following::node()+1.2%,//text()/..+0.3%.//node()+4.8%,count(//node())+3.7%,//book/title−0.2%.Related, not in this PR
When entities aren't substituted, libxml2's XPath also skips entity-reference nodes and doesn't descend into them. xmloxide selects both: with
<!DOCTYPE r [<!ENTITY e "x<i/>">]><r>a&e;b</r>,count(//i)is 1 where libxml2 gives 0. I can open an issue if you'd like that to match too.This came up while comparing against libxml2 for the Rust Nokogiri experiment mentioned in #84.
🤖 Generated with Claude Code