aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/htmlscriptelement.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/script/dom/htmlscriptelement.rs')
-rw-r--r--components/script/dom/htmlscriptelement.rs88
1 files changed, 54 insertions, 34 deletions
diff --git a/components/script/dom/htmlscriptelement.rs b/components/script/dom/htmlscriptelement.rs
index 8b5a2b2e6ca..c7931bfdecc 100644
--- a/components/script/dom/htmlscriptelement.rs
+++ b/components/script/dom/htmlscriptelement.rs
@@ -125,6 +125,11 @@ static SCRIPT_JS_MIMES: StaticStringVec = &[
"text/x-javascript",
];
+pub enum ScriptType {
+ Classic,
+ Module,
+}
+
#[derive(JSTraceable, MallocSizeOf)]
pub struct ClassicScript {
text: DOMString,
@@ -375,10 +380,12 @@ impl HTMLScriptElement {
return;
}
- // Step 7.
- if !self.is_javascript() {
+ let _script_type = if let Some(ty) = self.get_script_type() {
+ ty
+ } else {
+ // Step 7.
return;
- }
+ };
// Step 8.
if was_parser_inserted {
@@ -695,45 +702,58 @@ impl HTMLScriptElement {
);
}
- pub fn is_javascript(&self) -> bool {
+ // https://html.spec.whatwg.org/multipage/#prepare-a-script Step 7.
+ pub fn get_script_type(&self) -> Option<ScriptType> {
let element = self.upcast::<Element>();
+
let type_attr = element.get_attribute(&ns!(), &local_name!("type"));
- let is_js = match type_attr.as_ref().map(|s| s.value()) {
- Some(ref s) if s.is_empty() => {
- // type attr exists, but empty means js
+ let language_attr = element.get_attribute(&ns!(), &local_name!("language"));
+
+ let script_type = match (
+ type_attr.as_ref().map(|t| t.value()),
+ language_attr.as_ref().map(|l| l.value()),
+ ) {
+ (Some(ref ty), _) if ty.is_empty() => {
+ debug!("script type empty, inferring js");
+ Some(ScriptType::Classic)
+ },
+ (None, Some(ref lang)) if lang.is_empty() => {
debug!("script type empty, inferring js");
- true
+ Some(ScriptType::Classic)
},
- Some(s) => {
- debug!("script type={}", &**s);
- SCRIPT_JS_MIMES
- .contains(&s.to_ascii_lowercase().trim_matches(HTML_SPACE_CHARACTERS))
+ (None, None) => {
+ debug!("script type empty, inferring js");
+ Some(ScriptType::Classic)
},
- None => {
- debug!("no script type");
- let language_attr = element.get_attribute(&ns!(), &local_name!("language"));
- let is_js = match language_attr.as_ref().map(|s| s.value()) {
- Some(ref s) if s.is_empty() => {
- debug!("script language empty, inferring js");
- true
- },
- Some(s) => {
- debug!("script language={}", &**s);
- let mut language = format!("text/{}", &**s);
- language.make_ascii_lowercase();
- SCRIPT_JS_MIMES.contains(&&*language)
- },
- None => {
- debug!("no script type or language, inferring js");
- true
- },
- };
- // https://github.com/rust-lang/rust/issues/21114
- is_js
+ (None, Some(ref lang)) => {
+ debug!("script language={}", &***lang);
+ let language = format!("text/{}", &***lang);
+
+ if SCRIPT_JS_MIMES.contains(&language.to_ascii_lowercase().as_str()) {
+ Some(ScriptType::Classic)
+ } else {
+ None
+ }
+ },
+ (Some(ref ty), _) => {
+ debug!("script type={}", &***ty);
+
+ if &***ty == String::from("module") {
+ return Some(ScriptType::Module);
+ }
+
+ if SCRIPT_JS_MIMES
+ .contains(&ty.to_ascii_lowercase().trim_matches(HTML_SPACE_CHARACTERS))
+ {
+ Some(ScriptType::Classic)
+ } else {
+ None
+ }
},
};
+
// https://github.com/rust-lang/rust/issues/21114
- is_js
+ script_type
}
pub fn set_parser_inserted(&self, parser_inserted: bool) {