2010-05-19  Eric Seidel  <eric@webkit.org>

        Reviewed by Adam Barth.

        Add support for handling basic <script> tags in the HTML5 Parser
        https://bugs.webkit.org/show_bug.cgi?id=39350

        WebKit currently executes scripts from HTMLTokenizer in one giagantic
        hack.  HTML 5 requires that we execute scripts from the tree-builders/parser.

        It will take me a while to re-factor enough of HTMLTokenizer to be able to
        move the script execution logic without breaking things.  In the interest
        of allowing Adam to continue improving the HTML 5 lexer I've added this
        very basic (and very incomplete) <script> support to the HTML 5 parser code
        path so the he can run the HTML5 parser test suite.

        <script> support is tested by most of the layout tests.

        * dom/ScriptElement.cpp:
        (WebCore::useHTML5Parser):
        (WebCore::ScriptElement::finishParsingChildren):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@59778 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/WebCore/dom/ScriptElement.cpp b/WebCore/dom/ScriptElement.cpp
index 22d6981..b457f5d 100644
--- a/WebCore/dom/ScriptElement.cpp
+++ b/WebCore/dom/ScriptElement.cpp
@@ -32,9 +32,11 @@
 #include "HTMLNames.h"
 #include "HTMLScriptElement.h"
 #include "MIMETypeRegistry.h"
+#include "Page.h"
 #include "ScriptController.h"
 #include "ScriptSourceCode.h"
 #include "ScriptValue.h"
+#include "Settings.h"
 #include "StringHash.h"
 #include "Text.h"
 #include <wtf/StdLibExtras.h>
@@ -82,12 +84,28 @@
         data.evaluateScript(ScriptSourceCode(data.scriptContent(), element->document()->url())); // FIXME: Provide a real starting line number here
 }
 
+static inline bool useHTML5Parser(Document* document)
+{
+    ASSERT(document);
+    Settings* settings = document->page() ? document->page()->settings() : 0;
+    return settings && settings->html5ParserEnabled();
+}
+
 void ScriptElement::finishParsingChildren(ScriptElementData& data, const String& sourceUrl)
 {
     // The parser just reached </script>. If we have no src and no text,
     // allow dynamic loading later.
     if (sourceUrl.isEmpty() && data.scriptContent().isEmpty())
         data.setCreatedByParser(false);
+    // HTML5 Requires that we execute scripts from the parser, not from
+    // HTMLTokenizer like we currently do.
+    // FIXME: It may not be safe to execute scripts from here if
+    // HTMLParser::popOneBlockCommon is not reentrant.
+    else if (useHTML5Parser(data.element()->document())) {
+        // This is currently an incomplete implementation, see:
+        // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#parsing-main-incdata
+        data.evaluateScript(ScriptSourceCode(data.scriptContent(), data.element()->document()->url())); // FIXME: Provide a real starting line number here
+    }
 }
 
 void ScriptElement::handleSourceAttribute(ScriptElementData& data, const String& sourceUrl)