mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Move the script type and language checks into HTMLScriptElement.
This is IMO a more sensible place for them to live, and prepares for the demise of hubbub_html_parser.rs, as we move to html5ever.
This commit is contained in:
parent
a5965442b5
commit
ee39685051
2 changed files with 79 additions and 61 deletions
|
@ -2,19 +2,22 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
|
||||||
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding;
|
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding;
|
||||||
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding::HTMLScriptElementMethods;
|
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding::HTMLScriptElementMethods;
|
||||||
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
||||||
use dom::bindings::codegen::InheritTypes::HTMLScriptElementDerived;
|
use dom::bindings::codegen::InheritTypes::HTMLScriptElementDerived;
|
||||||
use dom::bindings::codegen::InheritTypes::{ElementCast, NodeCast};
|
use dom::bindings::codegen::InheritTypes::{ElementCast, NodeCast};
|
||||||
use dom::bindings::js::{JSRef, Temporary};
|
use dom::bindings::js::{JSRef, Temporary, OptionalRootable};
|
||||||
use dom::bindings::utils::{Reflectable, Reflector};
|
use dom::bindings::utils::{Reflectable, Reflector};
|
||||||
use dom::document::Document;
|
use dom::document::Document;
|
||||||
use dom::element::{HTMLScriptElementTypeId, Element, AttributeHandlers};
|
use dom::element::{HTMLScriptElementTypeId, Element, AttributeHandlers};
|
||||||
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
|
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
|
||||||
use dom::htmlelement::HTMLElement;
|
use dom::htmlelement::HTMLElement;
|
||||||
use dom::node::{Node, NodeHelpers, ElementNodeTypeId};
|
use dom::node::{Node, NodeHelpers, ElementNodeTypeId};
|
||||||
use servo_util::str::DOMString;
|
|
||||||
|
use servo_util::namespace::Null;
|
||||||
|
use servo_util::str::{DOMString, HTML_SPACE_CHARACTERS, StaticStringVec};
|
||||||
|
|
||||||
#[deriving(Encodable)]
|
#[deriving(Encodable)]
|
||||||
pub struct HTMLScriptElement {
|
pub struct HTMLScriptElement {
|
||||||
|
@ -40,6 +43,67 @@ impl HTMLScriptElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait HTMLScriptElementHelpers {
|
||||||
|
/// Prepare a script (<http://www.whatwg.org/html/#prepare-a-script>),
|
||||||
|
/// steps 6 and 7.
|
||||||
|
fn is_javascript(&self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Supported script types as defined by
|
||||||
|
/// <http://whatwg.org/html/#support-the-scripting-language>.
|
||||||
|
static SCRIPT_JS_MIMES: StaticStringVec = &[
|
||||||
|
"application/ecmascript",
|
||||||
|
"application/javascript",
|
||||||
|
"application/x-ecmascript",
|
||||||
|
"application/x-javascript",
|
||||||
|
"text/ecmascript",
|
||||||
|
"text/javascript",
|
||||||
|
"text/javascript1.0",
|
||||||
|
"text/javascript1.1",
|
||||||
|
"text/javascript1.2",
|
||||||
|
"text/javascript1.3",
|
||||||
|
"text/javascript1.4",
|
||||||
|
"text/javascript1.5",
|
||||||
|
"text/jscript",
|
||||||
|
"text/livescript",
|
||||||
|
"text/x-ecmascript",
|
||||||
|
"text/x-javascript",
|
||||||
|
];
|
||||||
|
|
||||||
|
impl<'a> HTMLScriptElementHelpers for JSRef<'a, HTMLScriptElement> {
|
||||||
|
fn is_javascript(&self) -> bool {
|
||||||
|
let element: &JSRef<Element> = ElementCast::from_ref(self);
|
||||||
|
match element.get_attribute(Null, "type").root().map(|s| s.Value()) {
|
||||||
|
Some(ref s) if s.is_empty() => {
|
||||||
|
// type attr exists, but empty means js
|
||||||
|
debug!("script type empty, inferring js");
|
||||||
|
true
|
||||||
|
},
|
||||||
|
Some(ref s) => {
|
||||||
|
debug!("script type={:s}", *s);
|
||||||
|
SCRIPT_JS_MIMES.contains(&s.as_slice().trim_chars(HTML_SPACE_CHARACTERS))
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
debug!("no script type");
|
||||||
|
match element.get_attribute(Null, "language").root().map(|s| s.Value()) {
|
||||||
|
Some(ref s) if s.is_empty() => {
|
||||||
|
debug!("script language empty, inferring js");
|
||||||
|
true
|
||||||
|
},
|
||||||
|
Some(ref s) => {
|
||||||
|
debug!("script language={:s}", *s);
|
||||||
|
SCRIPT_JS_MIMES.contains(&"text/".to_string().append(s.as_slice()).as_slice())
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
debug!("no script type or language, inferring js");
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> HTMLScriptElementMethods for JSRef<'a, HTMLScriptElement> {
|
impl<'a> HTMLScriptElementMethods for JSRef<'a, HTMLScriptElement> {
|
||||||
fn Src(&self) -> DOMString {
|
fn Src(&self) -> DOMString {
|
||||||
let element: &JSRef<Element> = ElementCast::from_ref(self);
|
let element: &JSRef<Element> = ElementCast::from_ref(self);
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
|
|
||||||
use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
|
use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
|
||||||
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
||||||
use dom::bindings::codegen::InheritTypes::{NodeBase, NodeCast, TextCast, ElementCast};
|
use dom::bindings::codegen::InheritTypes::{NodeBase, NodeCast, TextCast};
|
||||||
|
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLScriptElementCast};
|
||||||
use dom::bindings::js::{JS, JSRef, Temporary, OptionalRootable, Root};
|
use dom::bindings::js::{JS, JSRef, Temporary, OptionalRootable, Root};
|
||||||
use dom::bindings::utils::Reflectable;
|
use dom::bindings::utils::Reflectable;
|
||||||
use dom::document::{Document, DocumentHelpers};
|
use dom::document::{Document, DocumentHelpers};
|
||||||
|
@ -12,6 +13,7 @@ use dom::element::{AttributeHandlers, HTMLLinkElementTypeId};
|
||||||
use dom::htmlelement::HTMLElement;
|
use dom::htmlelement::HTMLElement;
|
||||||
use dom::htmlheadingelement::{Heading1, Heading2, Heading3, Heading4, Heading5, Heading6};
|
use dom::htmlheadingelement::{Heading1, Heading2, Heading3, Heading4, Heading5, Heading6};
|
||||||
use dom::htmlformelement::HTMLFormElement;
|
use dom::htmlformelement::HTMLFormElement;
|
||||||
|
use dom::htmlscriptelement::HTMLScriptElementHelpers;
|
||||||
use dom::node::{ElementNodeTypeId, NodeHelpers};
|
use dom::node::{ElementNodeTypeId, NodeHelpers};
|
||||||
use dom::types::*;
|
use dom::types::*;
|
||||||
use html::cssparse::{StylesheetProvenance, UrlProvenance, spawn_css_parser};
|
use html::cssparse::{StylesheetProvenance, UrlProvenance, spawn_css_parser};
|
||||||
|
@ -23,7 +25,7 @@ use servo_net::resource_task::{Load, LoadData, Payload, Done, ResourceTask, load
|
||||||
use servo_util::atom::Atom;
|
use servo_util::atom::Atom;
|
||||||
use servo_util::namespace;
|
use servo_util::namespace;
|
||||||
use servo_util::namespace::{Namespace, Null};
|
use servo_util::namespace::{Namespace, Null};
|
||||||
use servo_util::str::{DOMString, HTML_SPACE_CHARACTERS, StaticStringVec};
|
use servo_util::str::{DOMString, HTML_SPACE_CHARACTERS};
|
||||||
use servo_util::task::spawn_named;
|
use servo_util::task::spawn_named;
|
||||||
use std::ascii::StrAsciiExt;
|
use std::ascii::StrAsciiExt;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
@ -291,57 +293,6 @@ pub fn build_element_from_tag(tag: DOMString, ns: Namespace, document: &JSRef<Do
|
||||||
return ElementCast::from_temporary(HTMLUnknownElement::new(tag, document));
|
return ElementCast::from_temporary(HTMLUnknownElement::new(tag, document));
|
||||||
}
|
}
|
||||||
|
|
||||||
// List found at http://whatwg.org/html#support-the-scripting-language
|
|
||||||
static SCRIPT_JS_MIMES: StaticStringVec = &[
|
|
||||||
"application/ecmascript",
|
|
||||||
"application/javascript",
|
|
||||||
"application/x-ecmascript",
|
|
||||||
"application/x-javascript",
|
|
||||||
"text/ecmascript",
|
|
||||||
"text/javascript",
|
|
||||||
"text/javascript1.0",
|
|
||||||
"text/javascript1.1",
|
|
||||||
"text/javascript1.2",
|
|
||||||
"text/javascript1.3",
|
|
||||||
"text/javascript1.4",
|
|
||||||
"text/javascript1.5",
|
|
||||||
"text/jscript",
|
|
||||||
"text/livescript",
|
|
||||||
"text/x-ecmascript",
|
|
||||||
"text/x-javascript",
|
|
||||||
];
|
|
||||||
|
|
||||||
fn is_javascript(script: &JSRef<Element>) -> bool {
|
|
||||||
match script.get_attribute(Null, "type").root().map(|s| s.Value()) {
|
|
||||||
Some(ref s) if s.is_empty() => {
|
|
||||||
// type attr exists, but empty means js
|
|
||||||
debug!("script type empty, inferring js");
|
|
||||||
true
|
|
||||||
},
|
|
||||||
Some(ref s) => {
|
|
||||||
debug!("script type={:s}", *s);
|
|
||||||
SCRIPT_JS_MIMES.contains(&s.as_slice().trim_chars(HTML_SPACE_CHARACTERS))
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
debug!("no script type");
|
|
||||||
match script.get_attribute(Null, "language").root().map(|s| s.Value()) {
|
|
||||||
Some(ref s) if s.is_empty() => {
|
|
||||||
debug!("script language empty, inferring js");
|
|
||||||
true
|
|
||||||
},
|
|
||||||
Some(ref s) => {
|
|
||||||
debug!("script language={:s}", *s);
|
|
||||||
SCRIPT_JS_MIMES.contains(&"text/".to_string().append(s.as_slice()).as_slice())
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
debug!("no script type or language, inferring js");
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_html(page: &Page,
|
pub fn parse_html(page: &Page,
|
||||||
document: &JSRef<Document>,
|
document: &JSRef<Document>,
|
||||||
url: Url,
|
url: Url,
|
||||||
|
@ -544,13 +495,16 @@ pub fn parse_html(page: &Page,
|
||||||
},
|
},
|
||||||
complete_script: |script| {
|
complete_script: |script| {
|
||||||
unsafe {
|
unsafe {
|
||||||
let script: &JSRef<Element> = &*from_hubbub_node(script).root();
|
let script = from_hubbub_node::<Node>(script).root();
|
||||||
|
let script: Option<&JSRef<HTMLScriptElement>> =
|
||||||
|
HTMLScriptElementCast::to_ref(&*script);
|
||||||
|
let script = match script {
|
||||||
|
Some(script) if script.is_javascript() => script,
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
|
||||||
if !is_javascript(script) {
|
let script_element: &JSRef<Element> = ElementCast::from_ref(script);
|
||||||
return;
|
match script_element.get_attribute(Null, "src").root() {
|
||||||
}
|
|
||||||
|
|
||||||
match script.get_attribute(Null, "src").root() {
|
|
||||||
Some(src) => {
|
Some(src) => {
|
||||||
debug!("found script: {:s}", src.deref().Value());
|
debug!("found script: {:s}", src.deref().Value());
|
||||||
match UrlParser::new().base_url(base_url)
|
match UrlParser::new().base_url(base_url)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue