mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Implement element.shadowRoot
attribute (#34306)
* Implement Element.shadowRoot attribute Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> * Remove comments about shadowdom not being exposed for web content This is obviously not the case anymore. Signed-off-by: Simon Wülker <simon.wuelker@arcor.de> * Update WPT expectations 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:
parent
c5cf2621b6
commit
527e2d426d
15 changed files with 209 additions and 65 deletions
|
@ -73,11 +73,13 @@ use crate::dom::attr::{Attr, AttrHelpersForLayout};
|
|||
use crate::dom::bindings::cell::{ref_filter_map, DomRefCell, Ref, RefMut};
|
||||
use crate::dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::ElementBinding::{ElementMethods, ShadowRootInit};
|
||||
use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
|
||||
use crate::dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::ShadowRoot_Binding::ShadowRootMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::{
|
||||
ShadowRootMethods, ShadowRootMode,
|
||||
};
|
||||
use crate::dom::bindings::codegen::Bindings::WindowBinding::{
|
||||
ScrollBehavior, ScrollToOptions, WindowMethods,
|
||||
};
|
||||
|
@ -507,9 +509,11 @@ impl Element {
|
|||
}
|
||||
|
||||
/// <https://dom.spec.whatwg.org/#dom-element-attachshadow>
|
||||
/// XXX This is not exposed to web content yet. It is meant to be used
|
||||
/// for UA widgets only.
|
||||
pub fn attach_shadow(&self, is_ua_widget: IsUserAgentWidget) -> Fallible<DomRoot<ShadowRoot>> {
|
||||
pub fn attach_shadow(
|
||||
&self,
|
||||
is_ua_widget: IsUserAgentWidget,
|
||||
mode: ShadowRootMode,
|
||||
) -> Fallible<DomRoot<ShadowRoot>> {
|
||||
// Step 1.
|
||||
if self.namespace != ns!(html) {
|
||||
return Err(Error::NotSupported);
|
||||
|
@ -546,7 +550,7 @@ impl Element {
|
|||
}
|
||||
|
||||
// Steps 4, 5 and 6.
|
||||
let shadow_root = ShadowRoot::new(self, &self.node.owner_doc());
|
||||
let shadow_root = ShadowRoot::new(self, &self.node.owner_doc(), mode);
|
||||
self.ensure_rare_data().shadow_root = Some(Dom::from_ref(&*shadow_root));
|
||||
shadow_root
|
||||
.upcast::<Node>()
|
||||
|
@ -3050,11 +3054,29 @@ impl ElementMethods for Element {
|
|||
doc.enter_fullscreen(self, can_gc)
|
||||
}
|
||||
|
||||
// XXX Hidden under dom.shadowdom.enabled pref. Only exposed to be able
|
||||
// to test partial Shadow DOM support for UA widgets.
|
||||
// https://dom.spec.whatwg.org/#dom-element-attachshadow
|
||||
fn AttachShadow(&self) -> Fallible<DomRoot<ShadowRoot>> {
|
||||
self.attach_shadow(IsUserAgentWidget::No)
|
||||
fn AttachShadow(&self, init: &ShadowRootInit) -> Fallible<DomRoot<ShadowRoot>> {
|
||||
// Step 1. Run attach a shadow root with this, init["mode"], init["clonable"], init["serializable"],
|
||||
// init["delegatesFocus"], and init["slotAssignment"].
|
||||
let shadow_root = self.attach_shadow(IsUserAgentWidget::No, init.mode)?;
|
||||
|
||||
// Step 2. Return this’s shadow root.
|
||||
Ok(shadow_root)
|
||||
}
|
||||
|
||||
/// <https://dom.spec.whatwg.org/#dom-element-shadowroot>
|
||||
fn GetShadowRoot(&self) -> Option<DomRoot<ShadowRoot>> {
|
||||
// Step 1. Let shadow be this’s shadow root.
|
||||
let shadow_or_none = self.shadow_root();
|
||||
|
||||
// Step 2. If shadow is null or its mode is "closed", then return null.
|
||||
let shadow = shadow_or_none?;
|
||||
if shadow.Mode() == ShadowRootMode::Closed {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Step 3. Return shadow.
|
||||
Some(shadow)
|
||||
}
|
||||
|
||||
fn GetRole(&self) -> Option<DOMString> {
|
||||
|
|
|
@ -54,6 +54,7 @@ use crate::dom::bindings::codegen::Bindings::MediaErrorBinding::MediaErrorConsta
|
|||
use crate::dom::bindings::codegen::Bindings::MediaErrorBinding::MediaErrorMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::NavigatorBinding::Navigator_Binding::NavigatorMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::ShadowRootMode;
|
||||
use crate::dom::bindings::codegen::Bindings::TextTrackBinding::{TextTrackKind, TextTrackMode};
|
||||
use crate::dom::bindings::codegen::Bindings::URLBinding::URLMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
|
||||
|
@ -1908,7 +1909,9 @@ impl HTMLMediaElement {
|
|||
// if we are already showing the controls.
|
||||
return;
|
||||
}
|
||||
let shadow_root = element.attach_shadow(IsUserAgentWidget::Yes).unwrap();
|
||||
let shadow_root = element
|
||||
.attach_shadow(IsUserAgentWidget::Yes, ShadowRootMode::Closed)
|
||||
.unwrap();
|
||||
let document = document_from_node(self);
|
||||
let script = HTMLScriptElement::new(
|
||||
local_name!("script"),
|
||||
|
|
|
@ -36,7 +36,7 @@ pub enum IsUserAgentWidget {
|
|||
Yes,
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#interface-shadowroot
|
||||
/// <https://dom.spec.whatwg.org/#interface-shadowroot>
|
||||
#[dom_struct]
|
||||
pub struct ShadowRoot {
|
||||
document_fragment: DocumentFragment,
|
||||
|
@ -48,11 +48,14 @@ pub struct ShadowRoot {
|
|||
author_styles: DomRefCell<AuthorStyles<StyleSheetInDocument>>,
|
||||
stylesheet_list: MutNullableDom<StyleSheetList>,
|
||||
window: Dom<Window>,
|
||||
|
||||
/// <https://dom.spec.whatwg.org/#dom-shadowroot-mode>
|
||||
mode: ShadowRootMode,
|
||||
}
|
||||
|
||||
impl ShadowRoot {
|
||||
#[allow(crown::unrooted_must_root)]
|
||||
fn new_inherited(host: &Element, document: &Document) -> ShadowRoot {
|
||||
fn new_inherited(host: &Element, document: &Document, mode: ShadowRootMode) -> ShadowRoot {
|
||||
let document_fragment = DocumentFragment::new_inherited(document);
|
||||
let node = document_fragment.upcast::<Node>();
|
||||
node.set_flag(NodeFlags::IS_IN_SHADOW_TREE, true);
|
||||
|
@ -60,6 +63,7 @@ impl ShadowRoot {
|
|||
NodeFlags::IS_CONNECTED,
|
||||
host.upcast::<Node>().is_connected(),
|
||||
);
|
||||
|
||||
ShadowRoot {
|
||||
document_fragment,
|
||||
document_or_shadow_root: DocumentOrShadowRoot::new(document.window()),
|
||||
|
@ -68,12 +72,13 @@ impl ShadowRoot {
|
|||
author_styles: DomRefCell::new(AuthorStyles::new()),
|
||||
stylesheet_list: MutNullableDom::new(None),
|
||||
window: Dom::from_ref(document.window()),
|
||||
mode,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(host: &Element, document: &Document) -> DomRoot<ShadowRoot> {
|
||||
pub fn new(host: &Element, document: &Document, mode: ShadowRootMode) -> DomRoot<ShadowRoot> {
|
||||
reflect_dom_object(
|
||||
Box::new(ShadowRoot::new_inherited(host, document)),
|
||||
Box::new(ShadowRoot::new_inherited(host, document, mode)),
|
||||
document.window(),
|
||||
)
|
||||
}
|
||||
|
@ -226,7 +231,7 @@ impl ShadowRootMethods for ShadowRoot {
|
|||
|
||||
/// <https://dom.spec.whatwg.org/#dom-shadowroot-mode>
|
||||
fn Mode(&self) -> ShadowRootMode {
|
||||
ShadowRootMode::Closed
|
||||
self.mode
|
||||
}
|
||||
|
||||
/// <https://dom.spec.whatwg.org/#dom-shadowroot-host>
|
||||
|
|
|
@ -83,7 +83,16 @@ interface Element : Node {
|
|||
[CEReactions, Throws]
|
||||
undefined insertAdjacentHTML(DOMString position, DOMString html);
|
||||
|
||||
[Throws, Pref="dom.shadowdom.enabled"] ShadowRoot attachShadow();
|
||||
[Throws, Pref="dom.shadowdom.enabled"] ShadowRoot attachShadow(ShadowRootInit init);
|
||||
readonly attribute ShadowRoot? shadowRoot;
|
||||
};
|
||||
|
||||
dictionary ShadowRootInit {
|
||||
required ShadowRootMode mode;
|
||||
// boolean delegatesFocus = false;
|
||||
// SlotAssignmentMode slotAssignment = "named";
|
||||
// boolean clonable = false;
|
||||
// boolean serializable = false;
|
||||
};
|
||||
|
||||
// http://dev.w3.org/csswg/cssom-view/#extensions-to-the-element-interface
|
||||
|
|
|
@ -13,5 +13,6 @@ interface ShadowRoot : DocumentFragment {
|
|||
};
|
||||
|
||||
enum ShadowRootMode { "open", "closed"};
|
||||
// enum SlotAssignmentMode { "manual", "named" };
|
||||
|
||||
ShadowRoot includes DocumentOrShadowRoot;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue