Skip to content

Commit 0ba04ad

Browse files
committed
add TreeWalker
1 parent 1ff422a commit 0ba04ad

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

src/browser/dom/document.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const css = @import("css.zig");
3030

3131
const Element = @import("element.zig").Element;
3232
const ElementUnion = @import("element.zig").Union;
33+
const TreeWalker = @import("tree_walker.zig").TreeWalker;
34+
35+
const Env = @import("../env.zig").Env;
3336

3437
const DOMImplementation = @import("implementation.zig").DOMImplementation;
3538

@@ -238,6 +241,10 @@ pub const Document = struct {
238241
pub fn _replaceChildren(self: *parser.Document, nodes: []const Node.NodeOrText) !void {
239242
return Node.replaceChildren(parser.documentToNode(self), nodes);
240243
}
244+
245+
pub fn _createTreeWalker(_: *parser.Document, root: *parser.Node, whatToShow: ?u32, filter: ?Env.Callback) TreeWalker {
246+
return TreeWalker.init(root, whatToShow, filter);
247+
}
241248
};
242249

243250
const testing = @import("../../testing.zig");

src/browser/dom/dom.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const Node = @import("node.zig");
2626
const MutationObserver = @import("mutation_observer.zig");
2727
const IntersectionObserver = @import("intersection_observer.zig");
2828
const DOMParser = @import("dom_parser.zig").DOMParser;
29+
const TreeWalker = @import("tree_walker.zig").TreeWalker;
2930

3031
pub const Interfaces = .{
3132
DOMException,
@@ -39,4 +40,5 @@ pub const Interfaces = .{
3940
MutationObserver.Interfaces,
4041
IntersectionObserver.Interfaces,
4142
DOMParser,
43+
TreeWalker,
4244
};

src/browser/dom/tree_walker.zig

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <[email protected]>
4+
// Pierre Tachoire <[email protected]>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
const std = @import("std");
20+
const parser = @import("../netsurf.zig");
21+
22+
const Env = @import("../env.zig").Env;
23+
24+
// https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker
25+
pub const TreeWalker = struct {
26+
root: *parser.Node,
27+
currentNode: *parser.Node,
28+
whatToShow: u32,
29+
filter: ?Env.Callback,
30+
31+
pub fn init(node: *parser.Node, whatToShow: ?u32, filter: ?Env.Callback) TreeWalker {
32+
return .{
33+
.root = node,
34+
.currentNode = node,
35+
.whatToShow = whatToShow orelse std.math.maxInt(u32),
36+
.filter = filter,
37+
};
38+
}
39+
40+
pub fn get_root(self: *TreeWalker) *parser.Node {
41+
return self.root;
42+
}
43+
44+
pub fn get_currentNode(self: *TreeWalker) *parser.Node {
45+
return self.currentNode;
46+
}
47+
48+
pub fn get_whatToShow(self: *TreeWalker) u32 {
49+
return self.whatToShow;
50+
}
51+
52+
pub fn get_filter(self: *TreeWalker) ?Env.Callback {
53+
return self.filter;
54+
}
55+
56+
pub fn _firstChild(self: *TreeWalker) ?*parser.Node {
57+
const first_child = parser.nodeFirstChild(self.currentNode) catch return null;
58+
self.currentNode = first_child orelse return null;
59+
return first_child;
60+
}
61+
62+
pub fn _lastChild(self: *TreeWalker) ?*parser.Node {
63+
const last_child = parser.nodeLastChild(self.currentNode) catch return null;
64+
self.currentNode = last_child orelse return null;
65+
return last_child;
66+
}
67+
68+
pub fn _nextNode(self: *TreeWalker) ?*parser.Node {
69+
return self._firstChild();
70+
}
71+
72+
pub fn _nextSibling(self: *TreeWalker) ?*parser.Node {
73+
const next_sibling = parser.nodeNextSibling(self.currentNode) catch return null;
74+
self.currentNode = next_sibling orelse return null;
75+
return next_sibling;
76+
}
77+
78+
pub fn _parentNode(self: *TreeWalker) ?*parser.Node {
79+
const parent = parser.nodeParentNode(self.currentNode) catch return null;
80+
self.currentNode = parent orelse return null;
81+
return parent;
82+
}
83+
84+
pub fn _previousNode(self: *TreeWalker) ?*parser.Node {
85+
return self._parentNode();
86+
}
87+
88+
pub fn _previousSibling(self: *TreeWalker) ?*parser.Node {
89+
const previous_sibling = parser.nodePreviousSibling(self.currentNode) catch return null;
90+
self.currentNode = previous_sibling orelse return null;
91+
return previous_sibling;
92+
}
93+
};

0 commit comments

Comments
 (0)