Reset line numbers for scripts generated with document.write.
https://bugs.webkit.org/show_bug.cgi?id=71099

Reviewed by Yury Semikhatsky.

Source/JavaScriptCore:

* wtf/text/TextPosition.h:
(WTF::OrdinalNumber::OrdinalNumber):

Source/WebCore:

Test: http/tests/inspector-enabled/document-write.html

* dom/Document.h:
(WebCore::Document::isInDocumentWrite):
* dom/ScriptElement.cpp:
(WebCore::ScriptElement::prepareScript):

LayoutTests:

* http/tests/inspector-enabled/document-write-expected.txt: Added.
* http/tests/inspector-enabled/document-write.html: Added.
* platform/chromium-win/fast/dom/nested-script-exceptions-expected.txt:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@98724 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/dom/ScriptElement.cpp b/Source/WebCore/dom/ScriptElement.cpp
index ca47d5f..839fb63 100644
--- a/Source/WebCore/dom/ScriptElement.cpp
+++ b/Source/WebCore/dom/ScriptElement.cpp
@@ -198,14 +198,15 @@
     m_alreadyStarted = true;
 
     // FIXME: If script is parser inserted, verify it's still in the original document.
+    Document* document = m_element->document();
 
     // 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())
+    if (!document->frame())
         return false;
 
-    if (!m_element->document()->frame()->script()->canExecuteScripts(AboutToExecuteScript))
+    if (!document->frame()->script()->canExecuteScripts(AboutToExecuteScript))
         return false;
 
     Node* ancestor = m_element->parentNode();
@@ -223,7 +224,7 @@
     if (!charsetAttributeValue().isEmpty())
         m_characterEncoding = charsetAttributeValue();
     else
-        m_characterEncoding = m_element->document()->charset();
+        m_characterEncoding = document->charset();
 
     if (hasSourceAttribute())
         if (!requestScript(sourceAttributeValue()))
@@ -234,17 +235,20 @@
         m_willBeParserExecuted = true;
     } else if (hasSourceAttribute() && m_parserInserted && !asyncAttributeValue())
         m_willBeParserExecuted = true;
-    else if (!hasSourceAttribute() && m_parserInserted && !m_element->document()->haveStylesheetsLoaded()) {
+    else if (!hasSourceAttribute() && m_parserInserted && !document->haveStylesheetsLoaded()) {
         m_willBeParserExecuted = true;
         m_readyToBeParserExecuted = true;
     } else if (hasSourceAttribute() && !asyncAttributeValue() && !m_forceAsync) {
         m_willExecuteInOrder = true;
-        m_element->document()->scriptRunner()->queueScriptForExecution(this, m_cachedScript, ScriptRunner::IN_ORDER_EXECUTION);
+        document->scriptRunner()->queueScriptForExecution(this, m_cachedScript, ScriptRunner::IN_ORDER_EXECUTION);
         m_cachedScript->addClient(this);
     } else if (hasSourceAttribute())
         m_cachedScript->addClient(this);
-    else
-        executeScript(ScriptSourceCode(scriptContent(), m_element->document()->url(), scriptStartPosition));
+    else {
+        // Reset line numbering for nested writes.
+        TextPosition position = document->isInDocumentWrite() ? TextPosition() : scriptStartPosition;
+        executeScript(ScriptSourceCode(scriptContent(), document->url(), position));
+    }
 
     return true;
 }