Merge pull request #476 from mnaza/fix/treewalker-parentnode-475

fix(js): implement DOM parentNode() traversal in TreeWalker (#475)
This commit is contained in:
SGavrl authored and GitHub committed 2026-07-24 14:29:57 +00:00
commit a9c25976b2
2 files changed
+79 -4

No files matched your search

+10 -4
View File
@@ -2724,11 +2724,17 @@ class Document extends Node {
}
return null;
},
// DOM 6.1 "parentNode" (issue #475). The old version looked only at the
// immediate parent, so it couldn't climb past a skipped ancestor; it also
// excluded `root` as a result yet stepped to root's own parent when
// currentNode was root — returning a node OUTSIDE the walker's subtree.
// The loop's `node !== this.root` guard is what keeps the walk inside
// root while still allowing root itself to be returned.
parentNode() {
let parent = this.currentNode.parentNode;
if (parent && parent !== this.root && this._accept(parent)) {
this.currentNode = parent;
return parent;
let node = this.currentNode;
while (node && node !== this.root) {
node = node.parentNode;
if (node && this._accept(node)) { this.currentNode = node; return node; }
}
return null;
},