diff --git a/src/browser/dom/element.zig b/src/browser/dom/element.zig index 5043c4cb..5482dc65 100644 --- a/src/browser/dom/element.zig +++ b/src/browser/dom/element.zig @@ -560,6 +560,17 @@ pub const Element = struct { state.shadow_root = sr; parser.documentFragmentSetHost(sr.proto, @ptrCast(@alignCast(self))); + // Storing the ShadowRoot on the element makes sense, it's the ShadowRoot's + // parent. When we render, we go top-down, so we'll have the element, get + // its shadowroot, and go on. that's what the above code does. + // But we sometimes need to go bottom-up, e.g when we have a slot element + // and want to find the containing parent. Unforatunately , we don't have + // that link, so we need to create it. In the DOM, the ShadowRoot is + // represented by this DocumentFragment (it's the ShadowRoot's base prototype) + // So we can also store the ShadowRoot in the DocumentFragment's state. + const fragment_state = try page.getOrCreateNodeState(@ptrCast(@alignCast(fragment))); + fragment_state.shadow_root = sr; + return sr; } diff --git a/src/browser/dom/node.zig b/src/browser/dom/node.zig index aa3933d0..5dc171b4 100644 --- a/src/browser/dom/node.zig +++ b/src/browser/dom/node.zig @@ -37,6 +37,7 @@ const DocumentFragment = @import("document_fragment.zig").DocumentFragment; const HTMLCollection = @import("html_collection.zig").HTMLCollection; const HTMLAllCollection = @import("html_collection.zig").HTMLAllCollection; const HTMLCollectionIterator = @import("html_collection.zig").HTMLCollectionIterator; +const ShadowRoot = @import("shadow_root.zig").ShadowRoot; const Walker = @import("walker.zig").WalkerDepthFirst; // HTML @@ -354,11 +355,23 @@ pub const Node = struct { // - An Element inside a standard web page will return an HTMLDocument object representing the entire page (or