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

@ -9,10 +9,8 @@ use std::rc::Rc;
use js::jsval::UndefinedValue;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlheadelement::HTMLHeadElement;
use crate::dom::htmlscriptelement::SourceCode;
use crate::dom::node::NodeTraits;
@ -25,9 +23,9 @@ pub fn load_script(head: &HTMLHeadElement) {
Some(p) => p,
None => return,
};
let win = Trusted::new(doc.window());
let window = Trusted::new(doc.window());
doc.add_delayed_task(task!(UserScriptExecute: move || {
let win = win.root();
let win = window.root();
let cx = win.get_cx();
rooted!(in(*cx) let mut rval = UndefinedValue());
@ -47,14 +45,15 @@ pub fn load_script(head: &HTMLHeadElement) {
let script_text = SourceCode::Text(
Rc::new(DOMString::from_string(String::from_utf8_lossy(&contents).to_string()))
);
let global = win.upcast::<GlobalScope>();
global.evaluate_script_on_global_with_result(
let global_scope = win.as_global_scope();
global_scope.evaluate_script_on_global_with_result(
&script_text,
&file.to_string_lossy(),
rval.handle_mut(),
1,
ScriptFetchOptions::default_classic_script(global),
global.api_base_url(),
ScriptFetchOptions::default_classic_script(global_scope),
global_scope.api_base_url(),
CanGc::note(),
);
}