Add querySelect and querySelectorAll to DocumentFragment

This commit is contained in:
Karl Seguin
2025-07-08 19:24:35 +08:00
parent 2a5c7d139f
commit d7fba81f8f
3 changed files with 28 additions and 4 deletions

View File

@@ -237,7 +237,7 @@ pub const Document = struct {
pub fn _querySelector(self: *parser.Document, selector: []const u8, page: *Page) !?ElementUnion { pub fn _querySelector(self: *parser.Document, selector: []const u8, page: *Page) !?ElementUnion {
if (selector.len == 0) return null; 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; 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 { 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 { pub fn _prepend(self: *parser.Document, nodes: []const Node.NodeOrText) !void {

View File

@@ -16,8 +16,12 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const css = @import("css.zig");
const parser = @import("../netsurf.zig"); const parser = @import("../netsurf.zig");
const Page = @import("../page.zig").Page; 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; 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 { pub fn _replaceChildren(self: *parser.DocumentFragment, nodes: []const Node.NodeOrText) !void {
return Node.replaceChildren(parser.documentFragmentToNode(self), nodes); 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"); const testing = @import("../../testing.zig");
@@ -83,5 +101,11 @@ test "Browser.DOM.DocumentFragment" {
.{ "document.getElementsByTagName('body')[0].append(f.cloneNode(true));", null }, .{ "document.getElementsByTagName('body')[0].append(f.cloneNode(true));", null },
.{ "document.getElementById('x') != null;", "true" }, .{ "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" },
}, .{}); }, .{});
} }

View File

@@ -335,7 +335,7 @@ pub const Element = struct {
pub fn _querySelector(self: *parser.Element, selector: []const u8, page: *Page) !?Union { pub fn _querySelector(self: *parser.Element, selector: []const u8, page: *Page) !?Union {
if (selector.len == 0) return null; 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; 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 { 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 { pub fn _prepend(self: *parser.Element, nodes: []const Node.NodeOrText) !void {