diff -r 000000000000 -r 4f2f89ce4247 WebCore/html/HTMLScriptRunner.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebCore/html/HTMLScriptRunner.cpp Fri Sep 17 09:02:29 2010 +0300 @@ -0,0 +1,311 @@ +/* + * Copyright (C) 2010 Google, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "HTMLScriptRunner.h" + +#include "Attribute.h" +#include "CachedScript.h" +#include "DocLoader.h" +#include "Element.h" +#include "Event.h" +#include "Frame.h" +#include "HTMLScriptRunnerHost.h" +#include "HTMLInputStream.h" +#include "HTMLNames.h" +#include "NotImplemented.h" +#include "ScriptElement.h" +#include "ScriptSourceCode.h" + +namespace WebCore { + +using namespace HTMLNames; + +class NestScript : public Noncopyable { +public: + NestScript(unsigned& nestingLevel, HTMLInputStream& inputStream) + : m_nestingLevel(&nestingLevel) + , m_savedInsertionPoint(inputStream) + { + ++(*m_nestingLevel); + } + + ~NestScript() + { + --(*m_nestingLevel); + } + +private: + unsigned* m_nestingLevel; + InsertionPointRecord m_savedInsertionPoint; +}; + +HTMLScriptRunner::HTMLScriptRunner(Document* document, HTMLScriptRunnerHost* host) + : m_document(document) + , m_host(host) + , m_scriptNestingLevel(0) + , m_hasScriptsWaitingForStylesheets(false) +{ + ASSERT(m_host); +} + +HTMLScriptRunner::~HTMLScriptRunner() +{ + // FIXME: Should we be passed a "done loading/parsing" callback sooner than destruction? + if (m_parsingBlockingScript.cachedScript() && m_parsingBlockingScript.watchingForLoad()) + stopWatchingForLoad(m_parsingBlockingScript); +} + +static KURL documentURLForScriptExecution(Document* document) +{ + if (!document || !document->frame()) + return KURL(); + + // Use the URL of the currently active document for this frame. + return document->frame()->document()->url(); +} + +inline PassRefPtr createScriptLoadEvent() +{ + return Event::create(eventNames().loadEvent, false, false); +} + +inline PassRefPtr createScriptErrorEvent() +{ + return Event::create(eventNames().errorEvent, true, false); +} + +ScriptSourceCode HTMLScriptRunner::sourceFromPendingScript(const PendingScript& script, bool& errorOccurred) +{ + if (script.cachedScript()) { + errorOccurred = script.cachedScript()->errorOccurred(); + ASSERT(script.cachedScript()->isLoaded()); + return ScriptSourceCode(script.cachedScript()); + } + errorOccurred = false; + return ScriptSourceCode(script.element->textContent(), documentURLForScriptExecution(m_document), script.startingLineNumber); +} + +bool HTMLScriptRunner::isPendingScriptReady(const PendingScript& script) +{ + m_hasScriptsWaitingForStylesheets = !m_document->haveStylesheetsLoaded(); + if (m_hasScriptsWaitingForStylesheets) + return false; + if (script.cachedScript() && !script.cachedScript()->isLoaded()) + return false; + return true; +} + +void HTMLScriptRunner::executePendingScript() +{ + ASSERT(!m_scriptNestingLevel); + ASSERT(m_document->haveStylesheetsLoaded()); + bool errorOccurred = false; + ASSERT(isPendingScriptReady(m_parsingBlockingScript)); + ScriptSourceCode sourceCode = sourceFromPendingScript(m_parsingBlockingScript, errorOccurred); + + // Stop watching loads before executeScript to prevent recursion if the script reloads itself. + if (m_parsingBlockingScript.cachedScript() && m_parsingBlockingScript.watchingForLoad()) + stopWatchingForLoad(m_parsingBlockingScript); + + // Clear the pending script before possible rentrancy from executeScript() + RefPtr scriptElement = m_parsingBlockingScript.releaseElementAndClear(); + { + NestScript nestingLevel(m_scriptNestingLevel, m_host->inputStream()); + if (errorOccurred) + scriptElement->dispatchEvent(createScriptErrorEvent()); + else { + executeScript(scriptElement.get(), sourceCode); + scriptElement->dispatchEvent(createScriptLoadEvent()); + } + } + ASSERT(!m_scriptNestingLevel); +} + +void HTMLScriptRunner::executeScript(Element* element, const ScriptSourceCode& sourceCode) +{ + // FIXME: We do not block inline