Skip to content

Commit 08487b0

Browse files
authored
Merge pull request #891 from lightpanda-io/reattach_shadowroot
Add childElementCount and children to DocumentFragment
2 parents b084dde + 74ad9ec commit 08487b0

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/browser/dom/document_fragment.zig

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const Page = @import("../page.zig").Page;
2222
const NodeList = @import("nodelist.zig").NodeList;
2323
const Element = @import("element.zig").Element;
2424
const ElementUnion = @import("element.zig").Union;
25+
const collection = @import("html_collection.zig");
2526

2627
const Node = @import("node.zig").Node;
2728

@@ -71,6 +72,15 @@ pub const DocumentFragment = struct {
7172
pub fn _querySelectorAll(self: *parser.DocumentFragment, selector: []const u8, page: *Page) !NodeList {
7273
return css.querySelectorAll(page.arena, parser.documentFragmentToNode(self), selector);
7374
}
75+
76+
pub fn get_childElementCount(self: *parser.DocumentFragment) !u32 {
77+
var children = try get_children(self);
78+
return children.get_length();
79+
}
80+
81+
pub fn get_children(self: *parser.DocumentFragment) !collection.HTMLCollection {
82+
return collection.HTMLCollectionChildren(parser.documentFragmentToNode(self), false);
83+
}
7484
};
7585

7686
const testing = @import("../../testing.zig");
@@ -93,10 +103,13 @@ test "Browser.DOM.DocumentFragment" {
93103
try runner.testCases(&.{
94104
.{ "let f = document.createDocumentFragment()", null },
95105
.{ "let d = document.createElement('div');", null },
106+
.{ "d.childElementCount", "0" },
107+
96108
.{ "d.id = 'x';", null },
97109
.{ "document.getElementById('x') == null;", "true" },
98-
99110
.{ "f.append(d);", null },
111+
.{ "f.childElementCount", "1" },
112+
.{ "f.children[0].id", "x" },
100113
.{ "document.getElementById('x') == null;", "true" },
101114

102115
.{ "document.getElementsByTagName('body')[0].append(f.cloneNode(true));", null },

src/browser/dom/element.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ pub const Element = struct {
477477
return error.NotSupportedError;
478478
}
479479

480-
// TODO: the existing shadow root should be cleared!
480+
try Node.removeChildren(@alignCast(@ptrCast(sr.proto)));
481481
return sr;
482482
}
483483

src/browser/dom/shadow_root.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ test "Browser.DOM.ShadowRoot" {
5555
.{ "div1.shadowRoot == sr1", "true" },
5656

5757
.{ "try { div1.attachShadow({mode: 'closed'}) } catch (e) { e }", "Error: NotSupportedError" },
58+
59+
.{ " sr1.append(document.createElement('div'))", null },
60+
.{ " sr1.append(document.createElement('span'))", null },
61+
.{ "sr1.childElementCount", "2" },
62+
// re-attaching clears it
63+
.{ "div1.attachShadow({mode: 'open'}) == sr1", "true" },
64+
.{ "sr1.childElementCount", "0" },
5865
}, .{});
5966

6067
try runner.testCases(&.{

0 commit comments

Comments
 (0)