|
16 | 16 | // You should have received a copy of the GNU Affero General Public License |
17 | 17 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | 18 |
|
| 19 | +const css = @import("css.zig"); |
19 | 20 | const parser = @import("../netsurf.zig"); |
20 | 21 | const Page = @import("../page.zig").Page; |
| 22 | +const NodeList = @import("nodelist.zig").NodeList; |
| 23 | +const Element = @import("element.zig").Element; |
| 24 | +const ElementUnion = @import("element.zig").Union; |
21 | 25 |
|
22 | 26 | const Node = @import("node.zig").Node; |
23 | 27 |
|
@@ -53,6 +57,20 @@ pub const DocumentFragment = struct { |
53 | 57 | pub fn _replaceChildren(self: *parser.DocumentFragment, nodes: []const Node.NodeOrText) !void { |
54 | 58 | return Node.replaceChildren(parser.documentFragmentToNode(self), nodes); |
55 | 59 | } |
| 60 | + |
| 61 | + pub fn _querySelector(self: *parser.DocumentFragment, selector: []const u8, page: *Page) !?ElementUnion { |
| 62 | + if (selector.len == 0) return null; |
| 63 | + |
| 64 | + const n = try css.querySelector(page.call_arena, parser.documentFragmentToNode(self), selector); |
| 65 | + |
| 66 | + if (n == null) return null; |
| 67 | + |
| 68 | + return try Element.toInterface(parser.nodeToElement(n.?)); |
| 69 | + } |
| 70 | + |
| 71 | + pub fn _querySelectorAll(self: *parser.DocumentFragment, selector: []const u8, page: *Page) !NodeList { |
| 72 | + return css.querySelectorAll(page.arena, parser.documentFragmentToNode(self), selector); |
| 73 | + } |
56 | 74 | }; |
57 | 75 |
|
58 | 76 | const testing = @import("../../testing.zig"); |
@@ -83,5 +101,11 @@ test "Browser.DOM.DocumentFragment" { |
83 | 101 |
|
84 | 102 | .{ "document.getElementsByTagName('body')[0].append(f.cloneNode(true));", null }, |
85 | 103 | .{ "document.getElementById('x') != null;", "true" }, |
| 104 | + |
| 105 | + .{ "document.querySelector('.hello')", "null" }, |
| 106 | + .{ "document.querySelectorAll('.hello').length", "0" }, |
| 107 | + |
| 108 | + .{ "document.querySelector('#x').id", "x" }, |
| 109 | + .{ "document.querySelectorAll('#x')[0].id", "x" }, |
86 | 110 | }, .{}); |
87 | 111 | } |
0 commit comments