Lightpanda: the headless browser designed for AI and automation
mem: switch xpath descent from recursive to iterative
Karl Seguin29942959a9d4
diff --git a/src/browser/tests/xpath/xpath_evaluator.html b/src/browser/tests/xpath/xpath_evaluator.htmlindex 6cb6a8867..d18d83d68 100644--- a/src/browser/tests/xpath/xpath_evaluator.html+++ b/src/browser/tests/xpath/xpath_evaluator.html@@ -101,3 +101,38 @@testing.expectEqual(document, ev.createNSResolver(document));}</script>++<script id=deep_nesting>+{+ // The descendant, following and preceding axes used to recurse per level.+ const depth = 100000;+ // Built bottom-up: appending under a deep parent walks its ancestors.+ const chain = (prefix) => {+ let top = document.createElement('div');+ top.id = prefix + '_deepest';+ for (let i = 1; i < depth; i++) {+ const div = document.createElement('div');+ div.appendChild(top);+ top = div;+ }+ return top;+ };++ const root = document.createElement('section');+ root.appendChild(chain('before'));+ const mid = root.appendChild(document.createElement('span'));+ root.appendChild(chain('after'));++ const num = (expr, ctx) => document.evaluate(expr, ctx, null, XPathResult.NUMBER_TYPE, null).numberValue;+ const first = (expr, ctx) => document.evaluate(expr, ctx, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;++ // .//div takes the fused descendant fast path, descendant::div the axis.+ testing.expectEqual(depth * 2, num('count(.//div)', root));+ testing.expectEqual(depth * 2, num('count(descendant::div)', root));+ testing.expectEqual(depth, num('count(preceding::div)', mid));+ testing.expectEqual(depth, num('count(following::div)', mid));+ // preceding is a reverse axis: position 1 is the nearest node, the deepest one.+ testing.expectEqual('before_deepest', first('preceding::div[1]', mid).id);+ testing.expectEqual('after_deepest', first('following::div[last()]', mid).id);+}+</script>diff --git a/src/browser/xpath/Evaluator.zig b/src/browser/xpath/Evaluator.zigindex 597f8a194..88884dec0 100644--- a/src/browser/xpath/Evaluator.zig+++ b/src/browser/xpath/Evaluator.zig@@ -31,6 +31,7 @@ const std = @import("std");const lp = @import("lightpanda");const Node = @import("../webapi/Node.zig");+const TreeWalker = @import("../webapi/TreeWalker.zig");const ast = @import("ast.zig");const Parser = @import("Parser.zig");@@ -241,8 +242,8 @@ fn fusedDescend(lowered_name: ?[]const u8,out: *std.ArrayList(*Node),) Error!void {- var it = parent.childrenIterator();- while (it.next()) |c| {+ var tw = TreeWalker.FullExcludeSelf.init(parent, .{});+ while (tw.next()) |c| {if (matchTest(c, target.node_test, target.axis, lowered_name)) {var ok = true;for (target.predicates) |pred| {@@ -257,7 +258,6 @@ fn fusedDescend(}if (ok) try out.append(self.arena, c);}- try self.fusedDescend(c, target, lowered_name, out);}}@@ -488,10 +488,9 @@ fn axisNodes(self: *Evaluator, node: *Node, axis: ast.Axis) Error![]const *Node}fn appendDescendants(self: *Evaluator, node: *Node, out: *std.ArrayList(*Node)) Error!void {- var it = node.childrenIterator();- while (it.next()) |c| {- try out.append(self.arena, c);- try self.appendDescendants(c, out);+ var tw = TreeWalker.FullExcludeSelf.init(node, .{});+ while (tw.next()) |n| {+ try out.append(self.arena, n);}}@@ -500,19 +499,22 @@ fn appendFollowing(self: *Evaluator, start: *Node, out: *std.ArrayList(*Node)) Ewhile (n) |cur| : (n = cur.parentNode()) {var s = cur.nextSibling();while (s) |sn| : (s = sn.nextSibling()) {- try out.append(self.arena, sn);- try self.appendDescendants(sn, out);+ var tw = TreeWalker.Full.init(sn, .{});+ while (tw.next()) |node| {+ try out.append(self.arena, node);+ }}}}fn appendPrecedingSubtree(self: *Evaluator, n: *Node, out: *std.ArrayList(*Node)) Error!void {- // Reverse document order: deepest-last children first, then self.- var c = n.lastChild();- while (c) |child| : (c = child.previousSibling()) {- try self.appendPrecedingSubtree(child, out);+ // Reverse document order is the subtree's pre-order, reversed.+ const start = out.items.len;+ var tw = TreeWalker.Full.init(n, .{});+ while (tw.next()) |node| {+ try out.append(self.arena, node);}- try out.append(self.arena, n);+ std.mem.reverse(*Node, out.items[start..]);}fn appendPreceding(self: *Evaluator, start: *Node, out: *std.ArrayList(*Node)) Error!void {