1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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, 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::namespace::Null;
use servo_util::str::{DOMString, HTML_SPACE_CHARACTERS, StaticStringVec};
#[deriving(Encodable)]
#[must_root]
pub struct HTMLScriptElement {
pub htmlelement: HTMLElement,
}
impl HTMLScriptElementDerived for EventTarget {
fn is_htmlscriptelement(&self) -> bool {
self.type_id == NodeTargetTypeId(ElementNodeTypeId(HTMLScriptElementTypeId))
}
}
impl HTMLScriptElement {
pub fn new_inherited(localName: DOMString, document: &JSRef<Document>) -> HTMLScriptElement {
HTMLScriptElement {
htmlelement: HTMLElement::new_inherited(HTMLScriptElementTypeId, localName, document)
}
}
#[allow(unrooted_must_root)]
pub fn new(localName: DOMString, document: &JSRef<Document>) -> Temporary<HTMLScriptElement> {
let element = HTMLScriptElement::new_inherited(localName, document);
Node::reflect_node(box element, document, HTMLScriptElementBinding::Wrap)
}
}
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);
element.get_url_attribute("src")
}
// http://www.whatwg.org/html/#dom-script-text
fn Text(&self) -> DOMString {
let node: &JSRef<Node> = NodeCast::from_ref(self);
Node::collect_text_contents(node.children())
}
// http://www.whatwg.org/html/#dom-script-text
fn SetText(&self, value: DOMString) {
let node: &JSRef<Node> = NodeCast::from_ref(self);
node.SetTextContent(Some(value))
}
}
impl Reflectable for HTMLScriptElement {
fn reflector<'a>(&'a self) -> &'a Reflector {
self.htmlelement.reflector()
}
}
|