1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include "config.h"
25 #include "ScriptElement.h"
26
27 #include "CachedScript.h"
28 #include "DocLoader.h"
29 #include "Document.h"
30 #include "Frame.h"
31 #include "FrameLoader.h"
32 #include "HTMLNames.h"
33 #include "HTMLScriptElement.h"
34 #include "MIMETypeRegistry.h"
35 #include "ScriptController.h"
36 #include "ScriptSourceCode.h"
37 #include "ScriptValue.h"
38 #include "StringHash.h"
39 #include "Text.h"
40 #include <wtf/StdLibExtras.h>
41
42 #if ENABLE(SVG)
43 #include "SVGNames.h"
44 #include "SVGScriptElement.h"
45 #endif
46
47 namespace WebCore {
48
insertedIntoDocument(ScriptElementData & data,const String & sourceUrl)49 void ScriptElement::insertedIntoDocument(ScriptElementData& data, const String& sourceUrl)
50 {
51 if (data.createdByParser())
52 return;
53
54 if (!sourceUrl.isEmpty()) {
55 data.requestScript(sourceUrl);
56 return;
57 }
58
59 // If there's an empty script node, we shouldn't evaluate the script
60 // because if a script is inserted afterwards (by setting text or innerText)
61 // it should be evaluated, and evaluateScript only evaluates a script once.
62 data.evaluateScript(ScriptSourceCode(data.scriptContent(), data.element()->document()->url())); // FIXME: Provide a real starting line number here.
63 }
64
removedFromDocument(ScriptElementData & data)65 void ScriptElement::removedFromDocument(ScriptElementData& data)
66 {
67 // Eventually stop loading any not-yet-finished content
68 data.stopLoadRequest();
69 }
70
childrenChanged(ScriptElementData & data)71 void ScriptElement::childrenChanged(ScriptElementData& data)
72 {
73 if (data.createdByParser())
74 return;
75
76 Element* element = data.element();
77
78 // If a node is inserted as a child of the script element
79 // and the script element has been inserted in the document
80 // we evaluate the script.
81 if (element->inDocument() && element->firstChild())
82 data.evaluateScript(ScriptSourceCode(data.scriptContent(), element->document()->url())); // FIXME: Provide a real starting line number here
83 }
84
finishParsingChildren(ScriptElementData & data,const String & sourceUrl)85 void ScriptElement::finishParsingChildren(ScriptElementData& data, const String& sourceUrl)
86 {
87 // The parser just reached </script>. If we have no src and no text,
88 // allow dynamic loading later.
89 if (sourceUrl.isEmpty() && data.scriptContent().isEmpty())
90 data.setCreatedByParser(false);
91 }
92
handleSourceAttribute(ScriptElementData & data,const String & sourceUrl)93 void ScriptElement::handleSourceAttribute(ScriptElementData& data, const String& sourceUrl)
94 {
95 if (data.ignoresLoadRequest() || sourceUrl.isEmpty())
96 return;
97
98 data.requestScript(sourceUrl);
99 }
100
101 // Helper function
isSupportedJavaScriptLanguage(const String & language)102 static bool isSupportedJavaScriptLanguage(const String& language)
103 {
104 typedef HashSet<String, CaseFoldingHash> LanguageSet;
105 DEFINE_STATIC_LOCAL(LanguageSet, languages, ());
106 if (languages.isEmpty()) {
107 languages.add("javascript");
108 languages.add("javascript");
109 languages.add("javascript1.0");
110 languages.add("javascript1.1");
111 languages.add("javascript1.2");
112 languages.add("javascript1.3");
113 languages.add("javascript1.4");
114 languages.add("javascript1.5");
115 languages.add("javascript1.6");
116 languages.add("javascript1.7");
117 languages.add("livescript");
118 languages.add("ecmascript");
119 languages.add("jscript");
120 }
121
122 return languages.contains(language);
123 }
124
125 // ScriptElementData
ScriptElementData(ScriptElement * scriptElement,Element * element)126 ScriptElementData::ScriptElementData(ScriptElement* scriptElement, Element* element)
127 : m_scriptElement(scriptElement)
128 , m_element(element)
129 , m_cachedScript(0)
130 , m_createdByParser(false)
131 , m_requested(false)
132 , m_evaluated(false)
133 , m_firedLoad(false)
134 {
135 ASSERT(m_scriptElement);
136 ASSERT(m_element);
137 }
138
~ScriptElementData()139 ScriptElementData::~ScriptElementData()
140 {
141 stopLoadRequest();
142 }
143
requestScript(const String & sourceUrl)144 void ScriptElementData::requestScript(const String& sourceUrl)
145 {
146 Document* document = m_element->document();
147
148 // FIXME: Eventually we'd like to evaluate scripts which are inserted into a
149 // viewless document but this'll do for now.
150 // See http://bugs.webkit.org/show_bug.cgi?id=5727
151 if (!document->frame())
152 return;
153
154 ASSERT(!m_cachedScript);
155 m_cachedScript = document->docLoader()->requestScript(sourceUrl, scriptCharset());
156 m_requested = true;
157
158 // m_createdByParser is never reset - always resied at the initial value set while parsing.
159 // m_evaluated is left untouched as well to avoid script reexecution, if a <script> element
160 // is removed and reappended to the document.
161 m_firedLoad = false;
162
163 if (m_cachedScript) {
164 m_cachedScript->addClient(this);
165 return;
166 }
167
168 m_scriptElement->dispatchErrorEvent();
169 }
170
evaluateScript(const ScriptSourceCode & sourceCode)171 void ScriptElementData::evaluateScript(const ScriptSourceCode& sourceCode)
172 {
173 if (m_evaluated || sourceCode.isEmpty() || !shouldExecuteAsJavaScript())
174 return;
175
176 if (Frame* frame = m_element->document()->frame()) {
177 if (!frame->script()->isEnabled())
178 return;
179
180 m_evaluated = true;
181
182 frame->script()->evaluate(sourceCode);
183 Document::updateStyleForAllDocuments();
184 }
185 }
186
stopLoadRequest()187 void ScriptElementData::stopLoadRequest()
188 {
189 if (m_cachedScript) {
190 m_cachedScript->removeClient(this);
191 m_cachedScript = 0;
192 }
193 }
194
execute(CachedScript * cachedScript)195 void ScriptElementData::execute(CachedScript* cachedScript)
196 {
197 ASSERT(cachedScript);
198 if (cachedScript->errorOccurred())
199 m_scriptElement->dispatchErrorEvent();
200 else {
201 evaluateScript(ScriptSourceCode(cachedScript));
202 m_scriptElement->dispatchLoadEvent();
203 }
204 cachedScript->removeClient(this);
205 }
206
notifyFinished(CachedResource * o)207 void ScriptElementData::notifyFinished(CachedResource* o)
208 {
209 ASSERT_UNUSED(o, o == m_cachedScript);
210 m_element->document()->executeScriptSoon(this, m_cachedScript);
211 m_cachedScript = 0;
212 }
213
ignoresLoadRequest() const214 bool ScriptElementData::ignoresLoadRequest() const
215 {
216 return m_evaluated || m_requested || m_createdByParser || !m_element->inDocument();
217 }
218
shouldExecuteAsJavaScript() const219 bool ScriptElementData::shouldExecuteAsJavaScript() const
220 {
221 /*
222 Mozilla 1.8 accepts javascript1.0 - javascript1.7, but WinIE 7 accepts only javascript1.1 - javascript1.3.
223 Mozilla 1.8 and WinIE 7 both accept javascript and livescript.
224 WinIE 7 accepts ecmascript and jscript, but Mozilla 1.8 doesn't.
225 Neither Mozilla 1.8 nor WinIE 7 accept leading or trailing whitespace.
226 We want to accept all the values that either of these browsers accept, but not other values.
227 */
228 String type = m_scriptElement->typeAttributeValue();
229 if (!type.isEmpty())
230 return MIMETypeRegistry::isSupportedJavaScriptMIMEType(type.stripWhiteSpace().lower());
231
232 String language = m_scriptElement->languageAttributeValue();
233 if (!language.isEmpty())
234 return isSupportedJavaScriptLanguage(language);
235
236 // No type or language is specified, so we assume the script to be JavaScript.
237 // We don't yet support setting event listeners via the 'for' attribute for scripts.
238 // If there is such an attribute it's likely better to not execute the script than to do so
239 // immediately and unconditionally.
240 // FIXME: After <rdar://problem/4471751> / https://bugs.webkit.org/show_bug.cgi?id=16915 are resolved
241 // and we support the for syntax in script tags, this check can be removed and we should just
242 // return 'true' here.
243 String forAttribute = m_scriptElement->forAttributeValue();
244 return forAttribute.isEmpty();
245 }
246
scriptCharset() const247 String ScriptElementData::scriptCharset() const
248 {
249 // First we try to get encoding from charset attribute.
250 String charset = m_scriptElement->charsetAttributeValue().stripWhiteSpace();
251
252 // If charset has not been declared in script tag, fall back to frame encoding.
253 if (charset.isEmpty()) {
254 if (Frame* frame = m_element->document()->frame())
255 charset = frame->loader()->encoding();
256 }
257
258 return charset;
259 }
260
scriptContent() const261 String ScriptElementData::scriptContent() const
262 {
263 Vector<UChar> val;
264 Text* firstTextNode = 0;
265 bool foundMultipleTextNodes = false;
266
267 for (Node* n = m_element->firstChild(); n; n = n->nextSibling()) {
268 if (!n->isTextNode())
269 continue;
270
271 Text* t = static_cast<Text*>(n);
272 if (foundMultipleTextNodes)
273 append(val, t->data());
274 else if (firstTextNode) {
275 append(val, firstTextNode->data());
276 append(val, t->data());
277 foundMultipleTextNodes = true;
278 } else
279 firstTextNode = t;
280 }
281
282 if (firstTextNode && !foundMultipleTextNodes)
283 return firstTextNode->data();
284
285 return String::adopt(val);
286 }
287
toScriptElement(Element * element)288 ScriptElement* toScriptElement(Element* element)
289 {
290 if (element->isHTMLElement() && element->hasTagName(HTMLNames::scriptTag))
291 return static_cast<HTMLScriptElement*>(element);
292
293 #if ENABLE(SVG)
294 if (element->isSVGElement() && element->hasTagName(SVGNames::scriptTag))
295 return static_cast<SVGScriptElement*>(element);
296 #endif
297
298 return 0;
299 }
300
301 }
302