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>
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.
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.
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.
25 #include "ScriptElement.h"
27 #include "CachedScript.h"
28 #include "DocLoader.h"
31 #include "FrameLoader.h"
32 #include "MIMETypeRegistry.h"
33 #include "ScriptController.h"
34 #include "StringHash.h"
39 void ScriptElement::insertedIntoDocument(ScriptElementData& data, const String& sourceUrl)
41 if (data.createdByParser())
44 if (!sourceUrl.isEmpty()) {
45 data.requestScript(sourceUrl);
49 // If there's an empty script node, we shouldn't evaluate the script
50 // because if a script is inserted afterwards (by setting text or innerText)
51 // it should be evaluated, and evaluateScript only evaluates a script once.
52 data.evaluateScript(data.element()->document()->url().string(), data.scriptContent());
55 void ScriptElement::removedFromDocument(ScriptElementData& data)
57 // Eventually stop loading any not-yet-finished content
58 data.stopLoadRequest();
61 void ScriptElement::childrenChanged(ScriptElementData& data)
63 if (data.createdByParser())
66 Element* element = data.element();
68 // If a node is inserted as a child of the script element
69 // and the script element has been inserted in the document
70 // we evaluate the script.
71 if (element->inDocument() && element->firstChild())
72 data.evaluateScript(element->document()->url().string(), data.scriptContent());
75 void ScriptElement::finishParsingChildren(ScriptElementData& data, const String& sourceUrl)
77 // The parser just reached </script>. If we have no src and no text,
78 // allow dynamic loading later.
79 if (sourceUrl.isEmpty() && data.scriptContent().isEmpty())
80 data.setCreatedByParser(false);
83 void ScriptElement::handleSourceAttribute(ScriptElementData& data, const String& sourceUrl)
85 if (data.ignoresLoadRequest() || sourceUrl.isEmpty())
88 data.requestScript(sourceUrl);
92 static bool isSupportedJavaScriptLanguage(const String& language)
94 static HashSet<String, CaseFoldingHash> languages;
95 if (languages.isEmpty()) {
96 languages.add("javascript");
97 languages.add("javascript");
98 languages.add("javascript1.0");
99 languages.add("javascript1.1");
100 languages.add("javascript1.2");
101 languages.add("javascript1.3");
102 languages.add("javascript1.4");
103 languages.add("javascript1.5");
104 languages.add("javascript1.6");
105 languages.add("javascript1.7");
106 languages.add("livescript");
107 languages.add("ecmascript");
108 languages.add("jscript");
111 return languages.contains(language);
115 ScriptElementData::ScriptElementData(ScriptElement* scriptElement, Element* element)
116 : m_scriptElement(scriptElement)
119 , m_createdByParser(false)
123 ASSERT(m_scriptElement);
127 ScriptElementData::~ScriptElementData()
132 void ScriptElementData::requestScript(const String& sourceUrl)
134 Document* document = m_element->document();
136 // FIXME: Eventually we'd like to evaluate scripts which are inserted into a
137 // viewless document but this'll do for now.
138 // See http://bugs.webkit.org/show_bug.cgi?id=5727
139 if (!document->frame())
142 ASSERT(!m_cachedScript);
143 m_cachedScript = document->docLoader()->requestScript(sourceUrl, scriptCharset());
145 // m_createdByParser is never reset - always resied at the initial value set while parsing.
146 // m_evaluated is left untouched as well to avoid script reexecution, if a <script> element
147 // is removed and reappended to the document.
150 if (m_cachedScript) {
151 m_cachedScript->addClient(this);
155 m_scriptElement->dispatchErrorEvent();
158 void ScriptElementData::evaluateScript(const String& sourceUrl, const String& content)
160 if (m_evaluated || content.isEmpty() || !shouldExecuteAsJavaScript())
163 if (Frame* frame = m_element->document()->frame()) {
164 if (!frame->script()->isEnabled())
169 // FIXME: This starting line number will be incorrect for evaluation triggered
170 // from insertedIntoDocument or childrenChanged.
171 frame->script()->evaluate(sourceUrl, 1, content);
172 Document::updateDocumentsRendering();
176 void ScriptElementData::stopLoadRequest()
178 if (m_cachedScript) {
179 m_cachedScript->removeClient(this);
184 void ScriptElementData::notifyFinished(CachedResource* o)
186 CachedScript* cs = static_cast<CachedScript*>(o);
187 ASSERT(cs == m_cachedScript);
189 if (cs->errorOccurred())
190 m_scriptElement->dispatchErrorEvent();
192 RefPtr<Element> protector(m_element);
193 evaluateScript(cs->url(), cs->script());
194 m_scriptElement->dispatchLoadEvent();
200 bool ScriptElementData::ignoresLoadRequest() const
202 return m_evaluated || m_cachedScript || m_createdByParser || !m_element->inDocument();
205 bool ScriptElementData::shouldExecuteAsJavaScript() const
208 Mozilla 1.8 accepts javascript1.0 - javascript1.7, but WinIE 7 accepts only javascript1.1 - javascript1.3.
209 Mozilla 1.8 and WinIE 7 both accept javascript and livescript.
210 WinIE 7 accepts ecmascript and jscript, but Mozilla 1.8 doesn't.
211 Neither Mozilla 1.8 nor WinIE 7 accept leading or trailing whitespace.
212 We want to accept all the values that either of these browsers accept, but not other values.
214 String type = m_scriptElement->typeAttributeValue();
216 return MIMETypeRegistry::isSupportedJavaScriptMIMEType(type.stripWhiteSpace().lower());
218 String language = m_scriptElement->languageAttributeValue();
219 if (!language.isEmpty())
220 return isSupportedJavaScriptLanguage(language);
222 // No type or language is specified, so we assume the script to be JavaScript
226 String ScriptElementData::scriptCharset() const
228 // First we try to get encoding from charset attribute.
229 String charset = m_scriptElement->charsetAttributeValue().stripWhiteSpace();
231 // If charset has not been declared in script tag, fall back to frame encoding.
232 if (charset.isEmpty()) {
233 if (Frame* frame = m_element->document()->frame())
234 charset = frame->loader()->encoding();
240 String ScriptElementData::scriptContent() const
243 Text* firstTextNode = 0;
244 bool foundMultipleTextNodes = false;
246 for (Node* n = m_element->firstChild(); n; n = n->nextSibling()) {
247 if (!n->isTextNode())
250 Text* t = static_cast<Text*>(n);
251 if (foundMultipleTextNodes)
252 append(val, t->data());
253 else if (firstTextNode) {
254 append(val, firstTextNode->data());
255 append(val, t->data());
256 foundMultipleTextNodes = true;
261 if (firstTextNode && !foundMultipleTextNodes)
262 return firstTextNode->data();
264 return String::adopt(val);