Recognize module as script type

This commit is contained in:
CYBAI 2019-05-12 21:33:38 +09:00
parent 96ed5ac84d
commit 47d8c572ce

View file

@ -125,6 +125,11 @@ static SCRIPT_JS_MIMES: StaticStringVec = &[
"text/x-javascript", "text/x-javascript",
]; ];
pub enum ScriptType {
Classic,
Module,
}
#[derive(JSTraceable, MallocSizeOf)] #[derive(JSTraceable, MallocSizeOf)]
pub struct ClassicScript { pub struct ClassicScript {
text: DOMString, text: DOMString,
@ -375,10 +380,12 @@ impl HTMLScriptElement {
return; return;
} }
// Step 7. let _script_type = if let Some(ty) = self.get_script_type() {
if !self.is_javascript() { ty
} else {
// Step 7.
return; return;
} };
// Step 8. // Step 8.
if was_parser_inserted { 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 element = self.upcast::<Element>();
let type_attr = element.get_attribute(&ns!(), &local_name!("type")); let type_attr = element.get_attribute(&ns!(), &local_name!("type"));
let is_js = match type_attr.as_ref().map(|s| s.value()) { let language_attr = element.get_attribute(&ns!(), &local_name!("language"));
Some(ref s) if s.is_empty() => {
// type attr exists, but empty means js 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"); debug!("script type empty, inferring js");
true Some(ScriptType::Classic)
}, },
Some(s) => { (None, Some(ref lang)) if lang.is_empty() => {
debug!("script type={}", &**s); debug!("script type empty, inferring js");
SCRIPT_JS_MIMES Some(ScriptType::Classic)
.contains(&s.to_ascii_lowercase().trim_matches(HTML_SPACE_CHARACTERS))
}, },
None => { (None, None) => {
debug!("no script type"); debug!("script type empty, inferring js");
let language_attr = element.get_attribute(&ns!(), &local_name!("language")); Some(ScriptType::Classic)
let is_js = match language_attr.as_ref().map(|s| s.value()) { },
Some(ref s) if s.is_empty() => { (None, Some(ref lang)) => {
debug!("script language empty, inferring js"); debug!("script language={}", &***lang);
true let language = format!("text/{}", &***lang);
},
Some(s) => { if SCRIPT_JS_MIMES.contains(&language.to_ascii_lowercase().as_str()) {
debug!("script language={}", &**s); Some(ScriptType::Classic)
let mut language = format!("text/{}", &**s); } else {
language.make_ascii_lowercase(); None
SCRIPT_JS_MIMES.contains(&&*language) }
}, },
None => { (Some(ref ty), _) => {
debug!("no script type or language, inferring js"); debug!("script type={}", &***ty);
true
}, if &***ty == String::from("module") {
}; return Some(ScriptType::Module);
// https://github.com/rust-lang/rust/issues/21114 }
is_js
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 // https://github.com/rust-lang/rust/issues/21114
is_js script_type
} }
pub fn set_parser_inserted(&self, parser_inserted: bool) { pub fn set_parser_inserted(&self, parser_inserted: bool) {