Don't register unconnected shadow roots with their owner document (#34361)

* Don't falsely register Shadow Roots as connected

Previously, a shadowroot would be registered as connected
during the shadow hosts bind_to_tree call, even if the host
was being bound to an element that was not itself
connected to a document.

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>

* Update WPT expectations

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>

* Move bind/unbind methods into a VirtualMethod impl

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>

* Add DocumentFragment/Shadowroot to vtable_for

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>

---------

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
Simon Wülker 2024-12-02 19:33:25 +01:00 committed by GitHub
parent 888a93af47
commit f1e89c58a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 161 additions and 27 deletions

View file

@ -24,8 +24,11 @@ use crate::dom::document::Document;
use crate::dom::documentfragment::DocumentFragment;
use crate::dom::documentorshadowroot::{DocumentOrShadowRoot, StyleSheetInDocument};
use crate::dom::element::Element;
use crate::dom::node::{Node, NodeDamage, NodeFlags, ShadowIncluding, UnbindContext};
use crate::dom::node::{
document_from_node, BindContext, Node, NodeDamage, NodeFlags, ShadowIncluding, UnbindContext,
};
use crate::dom::stylesheetlist::{StyleSheetList, StyleSheetListOwner};
use crate::dom::virtualmethods::VirtualMethods;
use crate::dom::window::Window;
use crate::script_runtime::CanGc;
use crate::stylesheet_set::StylesheetSetRef;
@ -280,6 +283,48 @@ impl ShadowRootMethods<crate::DomTypeHolder> for ShadowRoot {
}
}
impl VirtualMethods for ShadowRoot {
fn super_type(&self) -> Option<&dyn VirtualMethods> {
Some(self.upcast::<DocumentFragment>() as &dyn VirtualMethods)
}
fn bind_to_tree(&self, context: &BindContext) {
if let Some(s) = self.super_type() {
s.bind_to_tree(context);
}
if context.tree_connected {
let document = document_from_node(self);
document.register_shadow_root(self);
}
let shadow_root = self.upcast::<Node>();
shadow_root.set_flag(NodeFlags::IS_CONNECTED, context.tree_connected);
for node in shadow_root.children() {
node.set_flag(NodeFlags::IS_CONNECTED, context.tree_connected);
node.bind_to_tree(context);
}
}
fn unbind_from_tree(&self, context: &UnbindContext) {
if let Some(s) = self.super_type() {
s.unbind_from_tree(context);
}
if context.tree_connected {
let document = document_from_node(self);
document.unregister_shadow_root(self);
}
let shadow_root = self.upcast::<Node>();
shadow_root.set_flag(NodeFlags::IS_CONNECTED, false);
for node in shadow_root.children() {
node.set_flag(NodeFlags::IS_CONNECTED, false);
node.unbind_from_tree(context);
}
}
}
#[allow(unsafe_code)]
pub trait LayoutShadowRootHelpers<'dom> {
fn get_host_for_layout(self) -> LayoutDom<'dom, Element>;