Skip to content

Commit bdc49a6

Browse files
authored
Merge pull request #859 from lightpanda-io/document_fragment_query_selector
Add querySelect and querySelectorAll to DocumentFragment
2 parents b8f3b19 + 73d82dd commit bdc49a6

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/browser/dom/document.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ pub const Document = struct {
237237
pub fn _querySelector(self: *parser.Document, selector: []const u8, page: *Page) !?ElementUnion {
238238
if (selector.len == 0) return null;
239239

240-
const n = try css.querySelector(page.arena, parser.documentToNode(self), selector);
240+
const n = try css.querySelector(page.call_arena, parser.documentToNode(self), selector);
241241

242242
if (n == null) return null;
243243

src/browser/dom/document_fragment.zig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
// You should have received a copy of the GNU Affero General Public License
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

19+
const css = @import("css.zig");
1920
const parser = @import("../netsurf.zig");
2021
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;
2125

2226
const Node = @import("node.zig").Node;
2327

@@ -53,6 +57,20 @@ pub const DocumentFragment = struct {
5357
pub fn _replaceChildren(self: *parser.DocumentFragment, nodes: []const Node.NodeOrText) !void {
5458
return Node.replaceChildren(parser.documentFragmentToNode(self), nodes);
5559
}
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+
}
5674
};
5775

5876
const testing = @import("../../testing.zig");
@@ -83,5 +101,11 @@ test "Browser.DOM.DocumentFragment" {
83101

84102
.{ "document.getElementsByTagName('body')[0].append(f.cloneNode(true));", null },
85103
.{ "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" },
86110
}, .{});
87111
}

src/browser/dom/element.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ pub const Element = struct {
335335
pub fn _querySelector(self: *parser.Element, selector: []const u8, page: *Page) !?Union {
336336
if (selector.len == 0) return null;
337337

338-
const n = try css.querySelector(page.arena, parser.elementToNode(self), selector);
338+
const n = try css.querySelector(page.call_arena, parser.elementToNode(self), selector);
339339

340340
if (n == null) return null;
341341

0 commit comments

Comments
 (0)