From cad810f21b803229eb11403f9209855525a25d57 Mon Sep 17 00:00:00 2001 From: Steve Block Date: Fri, 6 May 2011 11:45:16 +0100 Subject: Merge WebKit at r75315: Initial merge by git. Change-Id: I570314b346ce101c935ed22a626b48c2af266b84 --- Source/WebCore/dom/ScriptElement.cpp | 357 +++++++++++++++++++++++++++++++++++ 1 file changed, 357 insertions(+) create mode 100644 Source/WebCore/dom/ScriptElement.cpp (limited to 'Source/WebCore/dom/ScriptElement.cpp') diff --git a/Source/WebCore/dom/ScriptElement.cpp b/Source/WebCore/dom/ScriptElement.cpp new file mode 100644 index 0000000..747f7a1 --- /dev/null +++ b/Source/WebCore/dom/ScriptElement.cpp @@ -0,0 +1,357 @@ +/* + * Copyright (C) 1999 Lars Knoll (knoll@kde.org) + * (C) 1999 Antti Koivisto (koivisto@kde.org) + * (C) 2001 Dirk Mueller (mueller@kde.org) + * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2008 Nikolas Zimmermann + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "config.h" +#include "ScriptElement.h" + +#include "AsyncScriptRunner.h" +#include "CachedScript.h" +#include "CachedResourceLoader.h" +#include "Document.h" +#include "DocumentParser.h" +#include "Frame.h" +#include "FrameLoader.h" +#include "HTMLNames.h" +#include "HTMLScriptElement.h" +#include "IgnoreDestructiveWriteCountIncrementer.h" +#include "MIMETypeRegistry.h" +#include "Page.h" +#include "ScriptController.h" +#include "ScriptSourceCode.h" +#include "ScriptValue.h" +#include "Settings.h" +#include "Text.h" +#include +#include + +#if ENABLE(SVG) +#include "SVGNames.h" +#include "SVGScriptElement.h" +#endif + +namespace WebCore { + +ScriptElement::ScriptElement(Element* element, bool wasInsertedByParser, bool wasAlreadyStarted) + : m_element(element) + , m_cachedScript(0) + , m_wasInsertedByParser(wasInsertedByParser) + , m_isExternalScript(false) + , m_wasAlreadyStarted(wasAlreadyStarted) + , m_haveFiredLoad(false) +{ + ASSERT(m_element); +} + +ScriptElement::~ScriptElement() +{ + stopLoadRequest(); +} + +void ScriptElement::insertedIntoDocument(const String& sourceUrl) +{ + if (wasInsertedByParser() && !isAsynchronous()) + return; + + // http://www.whatwg.org/specs/web-apps/current-work/#script + + if (!sourceUrl.isEmpty()) { + requestScript(sourceUrl); + return; + } + + // If there's an empty script node, we shouldn't evaluate the script + // because if a script is inserted afterwards (by setting text or innerText) + // it should be evaluated, and evaluateScript only evaluates a script once. + evaluateScript(ScriptSourceCode(scriptContent(), element()->document()->url())); // FIXME: Provide a real starting line number here. +} + +void ScriptElement::removedFromDocument() +{ + // Eventually stop loading any not-yet-finished content + stopLoadRequest(); +} + +void ScriptElement::childrenChanged() +{ + if (wasInsertedByParser()) + return; + + // If a node is inserted as a child of the script element + // and the script element has been inserted in the document + // we evaluate the script. + if (m_element->inDocument() && m_element->firstChild()) + evaluateScript(ScriptSourceCode(scriptContent(), m_element->document()->url())); // FIXME: Provide a real starting line number here +} + +void ScriptElement::finishParsingChildren(const String& sourceUrl) +{ + // The parser just reached . If we have no src and no text, + // allow dynamic loading later. + if (sourceUrl.isEmpty() && scriptContent().isEmpty()) + m_wasInsertedByParser = false; +} + +void ScriptElement::handleSourceAttribute(const String& sourceUrl) +{ + if (ignoresLoadRequest() || sourceUrl.isEmpty()) + return; + + requestScript(sourceUrl); +} + +// Helper function +static bool isSupportedJavaScriptLanguage(const String& language) +{ + typedef HashSet LanguageSet; + DEFINE_STATIC_LOCAL(LanguageSet, languages, ()); + if (languages.isEmpty()) { + languages.add("javascript"); + languages.add("javascript"); + languages.add("javascript1.0"); + languages.add("javascript1.1"); + languages.add("javascript1.2"); + languages.add("javascript1.3"); + languages.add("javascript1.4"); + languages.add("javascript1.5"); + languages.add("javascript1.6"); + languages.add("javascript1.7"); + languages.add("livescript"); + languages.add("ecmascript"); + languages.add("jscript"); + } + + return languages.contains(language); +} + +void ScriptElement::requestScript(const String& sourceUrl) +{ + // FIXME: Eventually we'd like to evaluate scripts which are inserted into a + // viewless document but this'll do for now. + // See http://bugs.webkit.org/show_bug.cgi?id=5727 + if (!m_element->document()->frame()) + return; + + RefPtr originalDocument = m_element->document(); + if (!m_element->dispatchBeforeLoadEvent(sourceUrl)) + return; + if (!m_element->inDocument() || m_element->document() != originalDocument) + return; + + ASSERT(!m_cachedScript); + m_cachedScript = m_element->document()->cachedResourceLoader()->requestScript(sourceUrl, scriptCharset()); + m_isExternalScript = true; + + // m_wasInsertedByParser is never reset - always resied at the initial value set while parsing. + // m_wasAlreadyStarted is left untouched as well to avoid script reexecution, if a