script: Expose NodeTraits::owner_global / Window::as_global_scope (#34843)

Expose two new helpers and start using them as much as possible.

- `NodeTraits::owner_global`: which gets the `GlobalScope` that currenty
 owns a `Node`. This may be different than `.global()` in the case that
 the `Node` was adopted by a different `Document`.
- `Window::as_global_scope`: A helper to avoid having to cast so much
  when treating a `Window` like a `GlobalScope`.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-01-07 10:56:02 +01:00 committed by GitHub
parent 17e2ca3f01
commit e42b4b793d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 262 additions and 258 deletions

View file

@ -45,6 +45,7 @@ use style::stylesheets::{Stylesheet, UrlExtraData};
use uuid::Uuid;
use xml5ever::serialize as xml_serialize;
use super::globalscope::GlobalScope;
use crate::document_loader::DocumentLoader;
use crate::dom::attr::Attr;
use crate::dom::bindings::cell::{DomRefCell, Ref, RefMut};
@ -3346,6 +3347,10 @@ pub(crate) trait NodeTraits {
/// differ from the [`Document`] that the node was created in if it was adopted by a
/// different [`Document`] (the owner).
fn owner_window(&self) -> DomRoot<Window>;
/// Get the [`GlobalScope`] of the [`Document`] that owns this node. Note that this may
/// differ from the [`GlobalScope`] that the node was created in if it was adopted by a
/// different [`Document`] (the owner).
fn owner_global(&self) -> DomRoot<GlobalScope>;
/// If this [`Node`] is contained in a [`ShadowRoot`] return it, otherwise `None`.
fn containing_shadow_root(&self) -> Option<DomRoot<ShadowRoot>>;
/// Get the stylesheet owner for this node: either the [`Document`] or the [`ShadowRoot`]
@ -3363,6 +3368,10 @@ impl<T: DerivedFrom<Node> + DomObject> NodeTraits for T {
DomRoot::from_ref(self.owner_document().window())
}
fn owner_global(&self) -> DomRoot<GlobalScope> {
DomRoot::from_ref(self.owner_window().upcast())
}
fn containing_shadow_root(&self) -> Option<DomRoot<ShadowRoot>> {
Node::containing_shadow_root(self.upcast())
}