HTMLScriptElement: add ScriptType::ImportMap (#37291)

HTMLScriptElement: add `ScriptType::ImportMap`

Part of #37262
This covers most steps that are related to import map.

Testing: Existing WPT on HTMLScriptElement should remain the same.

---------

Signed-off-by: Yu Wei Wu <yuweiwu@YunoMacBook-Air.local>
Co-authored-by: Yu Wei Wu <yuweiwu@YunoMacBook-Air.local>
This commit is contained in:
Ngo Iok Ui (Wu Yu Wei) 2025-06-06 23:38:30 +09:00 committed by GitHub
parent 32cffbc985
commit ba33fd1318
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -275,6 +275,7 @@ pub(crate) static SCRIPT_JS_MIMES: StaticStringVec = &[
pub(crate) enum ScriptType {
Classic,
Module,
ImportMap,
}
#[derive(JSTraceable, MallocSizeOf)]
@ -770,14 +771,15 @@ impl HTMLScriptElement {
// Step 23. Module script credentials mode.
let module_credentials_mode = match script_type {
ScriptType::Classic => CredentialsMode::CredentialsSameOrigin,
ScriptType::Module => reflect_cross_origin_attribute(element).map_or(
CredentialsMode::CredentialsSameOrigin,
|attr| match &*attr {
"use-credentials" => CredentialsMode::Include,
"anonymous" => CredentialsMode::CredentialsSameOrigin,
_ => CredentialsMode::CredentialsSameOrigin,
},
),
ScriptType::Module | ScriptType::ImportMap => reflect_cross_origin_attribute(element)
.map_or(
CredentialsMode::CredentialsSameOrigin,
|attr| match &*attr {
"use-credentials" => CredentialsMode::Include,
"anonymous" => CredentialsMode::CredentialsSameOrigin,
_ => CredentialsMode::CredentialsSameOrigin,
},
),
};
// Step 24. Let cryptographic nonce be el's [[CryptographicNonce]] internal slot's value.
@ -821,7 +823,13 @@ impl HTMLScriptElement {
if let Some(src) = element.get_attribute(&ns!(), &local_name!("src")) {
// Step 31. If el has a src content attribute, then:
// TODO: Step 31.1. If el's type is "importmap".
// Step 31.1. If el's type is "importmap".
if script_type == ScriptType::ImportMap {
// then queue an element task on the DOM manipulation task source
// given el to fire an event named error at el, and return.
self.queue_error_event();
return;
}
// Step 31.2. Let src be the value of el's src attribute.
let src = src.value();
@ -904,7 +912,7 @@ impl HTMLScriptElement {
doc.add_asap_script(self);
};
},
// TODO: Case "importmap"
ScriptType::ImportMap => (),
}
} else {
// Step 32. If el does not have a src content attribute:
@ -913,18 +921,17 @@ impl HTMLScriptElement {
let text_rc = Rc::new(text);
// TODO: Fix step number or match spec text. Is this step 32.1?
let result = Ok(ScriptOrigin::internal(
Rc::clone(&text_rc),
base_url.clone(),
options.clone(),
script_type,
self.global().unminified_js_dir(),
));
// TODO: Fix step number or match spec text. Is this step 32.2?
// Step 32.2: Switch on el's type:
match script_type {
ScriptType::Classic => {
let result = Ok(ScriptOrigin::internal(
Rc::clone(&text_rc),
base_url.clone(),
options.clone(),
script_type,
self.global().unminified_js_dir(),
));
if was_parser_inserted &&
doc.get_current_parser()
.is_some_and(|parser| parser.script_nesting_level() <= 1) &&
@ -958,6 +965,10 @@ impl HTMLScriptElement {
can_gc,
);
},
ScriptType::ImportMap => {
// TODO: Let result be the result of creating an import map
// parse result given source text and base URL.
},
}
}
}
@ -1054,19 +1065,17 @@ impl HTMLScriptElement {
} else {
document.set_current_script(Some(self))
}
},
ScriptType::Module => document.set_current_script(None),
}
match script.type_ {
ScriptType::Classic => {
self.run_a_classic_script(&script, can_gc);
document.set_current_script(old_script.as_deref());
},
ScriptType::Module => {
assert!(document.GetCurrentScript().is_none());
document.set_current_script(None);
self.run_a_module_script(&script, false, can_gc);
},
ScriptType::ImportMap => {
// TODO: Register an import map given el's relevant
// global object and el's result.
},
}
// Step 7.