From d7fba81f8f71afa152f36981d99b74f2074e620d Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Tue, 8 Jul 2025 19:24:35 +0800 Subject: [PATCH] Add querySelect and querySelectorAll to DocumentFragment --- src/browser/dom/document.zig | 4 ++-- src/browser/dom/document_fragment.zig | 24 ++++++++++++++++++++++++ src/browser/dom/element.zig | 4 ++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/browser/dom/document.zig b/src/browser/dom/document.zig index 47e151bb..22eece0d 100644 --- a/src/browser/dom/document.zig +++ b/src/browser/dom/document.zig @@ -237,7 +237,7 @@ pub const Document = struct { pub fn _querySelector(self: *parser.Document, selector: []const u8, page: *Page) !?ElementUnion { if (selector.len == 0) return null; - const n = try css.querySelector(page.arena, parser.documentToNode(self), selector); + const n = try css.querySelector(page.call_arena, parser.documentToNode(self), selector); if (n == null) return null; @@ -245,7 +245,7 @@ pub const Document = struct { } pub fn _querySelectorAll(self: *parser.Document, selector: []const u8, page: *Page) !NodeList { - return css.querySelectorAll(page.arena, parser.documentToNode(self), selector); + return css.querySelectorAll(page.call_arena, parser.documentToNode(self), selector); } pub fn _prepend(self: *parser.Document, nodes: []const Node.NodeOrText) !void { diff --git a/src/browser/dom/document_fragment.zig b/src/browser/dom/document_fragment.zig index 6ed43cac..d4d8badd 100644 --- a/src/browser/dom/document_fragment.zig +++ b/src/browser/dom/document_fragment.zig @@ -16,8 +16,12 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +const css = @import("css.zig"); const parser = @import("../netsurf.zig"); const Page = @import("../page.zig").Page; +const NodeList = @import("nodelist.zig").NodeList; +const Element = @import("element.zig").Element; +const ElementUnion = @import("element.zig").Union; const Node = @import("node.zig").Node; @@ -53,6 +57,20 @@ pub const DocumentFragment = struct { pub fn _replaceChildren(self: *parser.DocumentFragment, nodes: []const Node.NodeOrText) !void { return Node.replaceChildren(parser.documentFragmentToNode(self), nodes); } + + pub fn _querySelector(self: *parser.DocumentFragment, selector: []const u8, page: *Page) !?ElementUnion { + if (selector.len == 0) return null; + + const n = try css.querySelector(page.call_arena, parser.documentFragmentToNode(self), selector); + + if (n == null) return null; + + return try Element.toInterface(parser.nodeToElement(n.?)); + } + + pub fn _querySelectorAll(self: *parser.DocumentFragment, selector: []const u8, page: *Page) !NodeList { + return css.querySelectorAll(page.arena, parser.documentFragmentToNode(self), selector); + } }; const testing = @import("../../testing.zig"); @@ -83,5 +101,11 @@ test "Browser.DOM.DocumentFragment" { .{ "document.getElementsByTagName('body')[0].append(f.cloneNode(true));", null }, .{ "document.getElementById('x') != null;", "true" }, + + .{ "document.querySelector('.hello')", "null" }, + .{ "document.querySelectorAll('.hello').length", "0" }, + + .{ "document.querySelector('#x').id", "x" }, + .{ "document.querySelectorAll('#x')[0].id", "x" }, }, .{}); } diff --git a/src/browser/dom/element.zig b/src/browser/dom/element.zig index 45cce475..6682228e 100644 --- a/src/browser/dom/element.zig +++ b/src/browser/dom/element.zig @@ -335,7 +335,7 @@ pub const Element = struct { pub fn _querySelector(self: *parser.Element, selector: []const u8, page: *Page) !?Union { if (selector.len == 0) return null; - const n = try css.querySelector(page.arena, parser.elementToNode(self), selector); + const n = try css.querySelector(page.call_arena, parser.elementToNode(self), selector); if (n == null) return null; @@ -343,7 +343,7 @@ pub const Element = struct { } pub fn _querySelectorAll(self: *parser.Element, selector: []const u8, page: *Page) !NodeList { - return css.querySelectorAll(page.arena, parser.elementToNode(self), selector); + return css.querySelectorAll(page.call_arena, parser.elementToNode(self), selector); } pub fn _prepend(self: *parser.Element, nodes: []const Node.NodeOrText) !void {