aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom/htmlscriptelement.rs
diff options
context:
space:
mode:
authorMs2ger <ms2ger@gmail.com>2014-09-02 09:00:38 +0200
committerMs2ger <ms2ger@gmail.com>2014-09-02 09:00:41 +0200
commitee39685051a0be24c5c9699c971ba4de8be59c98 (patch)
treefcbb5ba1e491770aadc199eb29f5e7626beee314 /src/components/script/dom/htmlscriptelement.rs
parenta5965442b5c886bdf1553d65aded1a379f812dc8 (diff)
downloadservo-ee39685051a0be24c5c9699c971ba4de8be59c98.tar.gz
servo-ee39685051a0be24c5c9699c971ba4de8be59c98.zip
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.
Diffstat (limited to 'src/components/script/dom/htmlscriptelement.rs')
-rw-r--r--src/components/script/dom/htmlscriptelement.rs68
1 files changed, 66 insertions, 2 deletions
diff --git a/src/components/script/dom/htmlscriptelement.rs b/src/components/script/dom/htmlscriptelement.rs
index af4c6a5ded6..3c189791b94 100644
--- a/src/components/script/dom/htmlscriptelement.rs
+++ b/src/components/script/dom/htmlscriptelement.rs
@@ -2,19 +2,22 @@
* 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/. */
+use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding;
use dom::bindings::codegen::Bindings::HTMLScriptElementBinding::HTMLScriptElementMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::InheritTypes::HTMLScriptElementDerived;
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::document::Document;
use dom::element::{HTMLScriptElementTypeId, Element, AttributeHandlers};
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
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)]
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> {
fn Src(&self) -> DOMString {
let element: &JSRef<Element> = ElementCast::from_ref(self);