Implement ElementInternals::shadowRoot (#35923)

* Implement ElementInternals::shadowRoot

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:
Simon Wülker 2025-03-12 01:09:32 +01:00 committed by GitHub
parent f527217bdc
commit 8034216d06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 44 additions and 12 deletions

View file

@ -542,6 +542,16 @@ impl Element {
clonable,
can_gc,
);
// Step 7. If elements custom element state is "precustomized" or "custom",
// then set shadows available to element internals to true.
if matches!(
self.get_custom_element_state(),
CustomElementState::Precustomized | CustomElementState::Custom
) {
shadow_root.set_available_to_element_internals(true);
}
self.ensure_rare_data().shadow_root = Some(Dom::from_ref(&*shadow_root));
shadow_root
.upcast::<Node>()

View file

@ -23,6 +23,7 @@ use crate::dom::htmlelement::HTMLElement;
use crate::dom::htmlformelement::{FormDatum, FormDatumValue, HTMLFormElement};
use crate::dom::node::{Node, NodeTraits};
use crate::dom::nodelist::NodeList;
use crate::dom::shadowroot::ShadowRoot;
use crate::dom::validation::{Validatable, is_barred_by_datalist_ancestor};
use crate::dom::validitystate::{ValidationFlags, ValidityState};
use crate::script_runtime::CanGc;
@ -188,6 +189,22 @@ impl ElementInternals {
}
impl ElementInternalsMethods<crate::DomTypeHolder> for ElementInternals {
/// <https://html.spec.whatwg.org/multipage/#dom-elementinternals-shadowroot>
fn GetShadowRoot(&self) -> Option<DomRoot<ShadowRoot>> {
// Step 1. Let target be this's target element.
// Step 2. If target is not a shadow host, then return null.
// Step 3. Let shadow be target's shadow root.
let shadow = self.target_element.upcast::<Element>().shadow_root()?;
// Step 4. If shadow's available to element internals is false, then return null.
if !shadow.is_available_to_element_internals() {
return None;
}
// Step 5. Return shadow.
Some(shadow)
}
/// <https://html.spec.whatwg.org/multipage#dom-elementinternals-setformvalue>
fn SetFormValue(
&self,

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
@ -72,6 +73,9 @@ pub(crate) struct ShadowRoot {
/// <https://dom.spec.whatwg.org/#dom-shadowroot-clonable>
clonable: bool,
/// <https://dom.spec.whatwg.org/#shadowroot-available-to-element-internals>
available_to_element_internals: Cell<bool>,
slots: DomRefCell<HashMap<DOMString, Vec<Dom<HTMLSlotElement>>>>,
}
@ -103,6 +107,7 @@ impl ShadowRoot {
mode,
slot_assignment_mode,
clonable,
available_to_element_internals: Cell::new(false),
slots: Default::default(),
}
}
@ -252,6 +257,15 @@ impl ShadowRoot {
pub(crate) fn has_slot_descendants(&self) -> bool {
!self.slots.borrow().is_empty()
}
pub(crate) fn set_available_to_element_internals(&self, value: bool) {
self.available_to_element_internals.set(value);
}
/// <https://dom.spec.whatwg.org/#shadowroot-available-to-element-internals>
pub(crate) fn is_available_to_element_internals(&self) -> bool {
self.available_to_element_internals.get()
}
}
impl ShadowRootMethods<crate::DomTypeHolder> for ShadowRoot {

View file

@ -5,6 +5,9 @@
// https://html.spec.whatwg.org/multipage/#elementinternals
[Exposed=Window]
interface ElementInternals {
// Shadow root access
readonly attribute ShadowRoot? shadowRoot;
// Form-associated custom elements
[Throws] undefined setFormValue((File or USVString or FormData)? value,

View file

@ -1,10 +1,4 @@
[element-internals-shadowroot.html]
expected: ERROR
[ElementInternals.shadowRoot allows access to open shadow root]
expected: FAIL
[ElementInternals.shadowRoot allows access to closed shadow root]
expected: FAIL
[ElementInternals.shadowRoot doesn't reveal pre-attached closed shadowRoot]
expected: FAIL

View file

@ -1,3 +0,0 @@
[ElementInternals-target-element-is-held-strongly.html]
[Target element of ElementsInternals is held strongly and doesn't get GCed if there are no other references]
expected: FAIL

View file

@ -4940,9 +4940,6 @@
[OffscreenCanvasRenderingContext2D interface: operation roundRect(unrestricted double, unrestricted double, unrestricted double, unrestricted double, optional (unrestricted double or DOMPointInit or sequence<(unrestricted double or DOMPointInit)>))]
expected: FAIL
[ElementInternals interface: attribute shadowRoot]
expected: FAIL
[ElementInternals interface: attribute states]
expected: FAIL