mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
support the composed option of getRootNode()
This commit is contained in:
@@ -360,18 +360,30 @@ pub const Node = struct {
|
|||||||
node: Union,
|
node: Union,
|
||||||
};
|
};
|
||||||
pub fn _getRootNode(self: *parser.Node, options: ?struct { composed: bool = false }, page: *Page) !GetRootNodeResult {
|
pub fn _getRootNode(self: *parser.Node, options: ?struct { composed: bool = false }, page: *Page) !GetRootNodeResult {
|
||||||
if (options) |options_| if (options_.composed) {
|
const composed = if (options) |opts| opts.composed else false;
|
||||||
log.warn(.web_api, "not implemented", .{ .feature = "getRootNode composed" });
|
|
||||||
};
|
|
||||||
|
|
||||||
const root = parser.nodeGetRootNode(self);
|
var current_root = parser.nodeGetRootNode(self);
|
||||||
if (page.getNodeState(root)) |state| {
|
|
||||||
if (state.shadow_root) |sr| {
|
while (true) {
|
||||||
return .{ .shadow_root = sr };
|
const node_type = parser.nodeType(current_root);
|
||||||
|
|
||||||
|
if (node_type == .document_fragment) {
|
||||||
|
if (parser.documentFragmentGetHost(@ptrCast(current_root))) |host| {
|
||||||
|
if (page.getNodeState(host)) |state| {
|
||||||
|
if (state.shadow_root) |sr| {
|
||||||
|
if (!composed) {
|
||||||
|
return .{ .shadow_root = sr };
|
||||||
|
}
|
||||||
|
current_root = parser.nodeGetRootNode(@ptrCast(sr.host));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return .{ .node = try Node.toInterface(root) };
|
return .{ .node = try Node.toInterface(current_root) };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn _hasChildNodes(self: *parser.Node) bool {
|
pub fn _hasChildNodes(self: *parser.Node) bool {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
<p id="para"> And</p>
|
<p id="para"> And</p>
|
||||||
<!--comment-->
|
<!--comment-->
|
||||||
</div>
|
</div>
|
||||||
|
<div id="rootNodeComposed"></div>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
<script src="../testing.js"></script>
|
<script src="../testing.js"></script>
|
||||||
@@ -36,6 +37,26 @@ let first_child = content.firstChild.nextSibling; // nextSibling because of line
|
|||||||
testing.expectEqual('HTMLDocument', content.getRootNode().__proto__.constructor.name);
|
testing.expectEqual('HTMLDocument', content.getRootNode().__proto__.constructor.name);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<script id=getRootNodeComposed>
|
||||||
|
const testContainer = $('#rootNodeComposed');
|
||||||
|
const shadowHost = document.createElement('div');
|
||||||
|
testContainer.appendChild(shadowHost);
|
||||||
|
const shadowRoot = shadowHost.attachShadow({ mode: 'open' });
|
||||||
|
const shadowChild = document.createElement('span');
|
||||||
|
shadowRoot.appendChild(shadowChild);
|
||||||
|
|
||||||
|
testing.expectEqual('ShadowRoot', shadowChild.getRootNode().__proto__.constructor.name);
|
||||||
|
testing.expectEqual('ShadowRoot', shadowChild.getRootNode({ composed: false }).__proto__.constructor.name);
|
||||||
|
testing.expectEqual('HTMLDocument', shadowChild.getRootNode({ composed: true }).__proto__.constructor.name);
|
||||||
|
testing.expectEqual('HTMLDocument', shadowHost.getRootNode().__proto__.constructor.name);
|
||||||
|
|
||||||
|
const disconnected = document.createElement('div');
|
||||||
|
const disconnectedChild = document.createElement('span');
|
||||||
|
disconnected.appendChild(disconnectedChild);
|
||||||
|
testing.expectEqual('HTMLDivElement', disconnectedChild.getRootNode().__proto__.constructor.name);
|
||||||
|
testing.expectEqual('HTMLDivElement', disconnectedChild.getRootNode({ composed: true }).__proto__.constructor.name);
|
||||||
|
</script>
|
||||||
|
|
||||||
<script id=firstChild>
|
<script id=firstChild>
|
||||||
let body_first_child = document.body.firstChild;
|
let body_first_child = document.body.firstChild;
|
||||||
testing.expectEqual('div', body_first_child.localName);
|
testing.expectEqual('div', body_first_child.localName);
|
||||||
|
|||||||
Reference in New Issue
Block a user