Title: [91008] trunk/Source/WebCore
Revision
91008
Author
vse...@chromium.org
Date
2011-07-14 10:15:04 -0700 (Thu, 14 Jul 2011)

Log Message

Move XML errors handling out from XMLDocumentParser (refactoring).
https://bugs.webkit.org/show_bug.cgi?id=64305

XML errors handling logic is moved out from XMLDocumentParser to allow using it for XSLT error handling as well.

Reviewed by Adam Barth.

No tests needed as there is no new functionality added.

* CMakeLists.txt:
* GNUmakefile.list.am:
* WebCore.gypi:
* WebCore.pro:
* WebCore.vcproj/WebCore.vcproj:
* WebCore.xcodeproj/project.pbxproj:
* xml/XMLErrors.cpp: Added.
(WebCore::XMLErrors::XMLErrors):
(WebCore::XMLErrors::handleError):
(WebCore::XMLErrors::appendErrorMessage):
(WebCore::createXHTMLParserErrorHeader):
(WebCore::XMLErrors::insertErrorMessageBlock):
* xml/XMLErrors.h: Added.
* xml/parser/XMLDocumentParser.cpp:
(WebCore::XMLDocumentParser::pushCurrentNode):
(WebCore::XMLDocumentParser::handleError):
(WebCore::XMLDocumentParser::insertErrorMessageBlock):
* xml/parser/XMLDocumentParser.h:
* xml/parser/XMLDocumentParserLibxml2.cpp:
(WebCore::PendingCallbacks::appendErrorCallback):
(WebCore::XMLDocumentParser::XMLDocumentParser):
(WebCore::XMLDocumentParser::doWrite):
(WebCore::XMLDocumentParser::error):
(WebCore::warningHandler):
(WebCore::fatalErrorHandler):
(WebCore::normalErrorHandler):
* xml/parser/XMLDocumentParserQt.cpp:
(WebCore::XMLDocumentParser::XMLDocumentParser):
(WebCore::XMLDocumentParser::doWrite):
(WebCore::XMLDocumentParser::doEnd):
(WebCore::XMLDocumentParser::parse):
(WebCore::XMLDocumentParser::parseStartElement):
(WebCore::XMLDocumentParser::parseDtd):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/CMakeLists.txt (91007 => 91008)


--- trunk/Source/WebCore/CMakeLists.txt	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/CMakeLists.txt	2011-07-14 17:15:04 UTC (rev 91008)
@@ -1363,6 +1363,7 @@
 
     xml/DOMParser.cpp
     xml/NativeXPathNSResolver.cpp
+    xml/XMLErrors.cpp
     xml/XMLHttpRequest.cpp
     xml/XMLHttpRequestProgressEventThrottle.cpp
     xml/XMLHttpRequestUpload.cpp

Modified: trunk/Source/WebCore/ChangeLog (91007 => 91008)


--- trunk/Source/WebCore/ChangeLog	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/ChangeLog	2011-07-14 17:15:04 UTC (rev 91008)
@@ -1,5 +1,50 @@
 2011-07-14  Vsevolod Vlasov  <vse...@chromium.org>
 
+        Move XML errors handling out from XMLDocumentParser (refactoring).
+        https://bugs.webkit.org/show_bug.cgi?id=64305
+
+        XML errors handling logic is moved out from XMLDocumentParser to allow using it for XSLT error handling as well.
+
+        Reviewed by Adam Barth.
+
+        No tests needed as there is no new functionality added.
+
+        * CMakeLists.txt:
+        * GNUmakefile.list.am:
+        * WebCore.gypi:
+        * WebCore.pro:
+        * WebCore.vcproj/WebCore.vcproj:
+        * WebCore.xcodeproj/project.pbxproj:
+        * xml/XMLErrors.cpp: Added.
+        (WebCore::XMLErrors::XMLErrors):
+        (WebCore::XMLErrors::handleError):
+        (WebCore::XMLErrors::appendErrorMessage):
+        (WebCore::createXHTMLParserErrorHeader):
+        (WebCore::XMLErrors::insertErrorMessageBlock):
+        * xml/XMLErrors.h: Added.
+        * xml/parser/XMLDocumentParser.cpp:
+        (WebCore::XMLDocumentParser::pushCurrentNode):
+        (WebCore::XMLDocumentParser::handleError):
+        (WebCore::XMLDocumentParser::insertErrorMessageBlock):
+        * xml/parser/XMLDocumentParser.h:
+        * xml/parser/XMLDocumentParserLibxml2.cpp:
+        (WebCore::PendingCallbacks::appendErrorCallback):
+        (WebCore::XMLDocumentParser::XMLDocumentParser):
+        (WebCore::XMLDocumentParser::doWrite):
+        (WebCore::XMLDocumentParser::error):
+        (WebCore::warningHandler):
+        (WebCore::fatalErrorHandler):
+        (WebCore::normalErrorHandler):
+        * xml/parser/XMLDocumentParserQt.cpp:
+        (WebCore::XMLDocumentParser::XMLDocumentParser):
+        (WebCore::XMLDocumentParser::doWrite):
+        (WebCore::XMLDocumentParser::doEnd):
+        (WebCore::XMLDocumentParser::parse):
+        (WebCore::XMLDocumentParser::parseStartElement):
+        (WebCore::XMLDocumentParser::parseDtd):
+
+2011-07-14  Vsevolod Vlasov  <vse...@chromium.org>
+
         Web Inspector: InspectorResourceAgent should generate unique identifier and save cachedResource for resources from memory cache.
         https://bugs.webkit.org/show_bug.cgi?id=64356
 

Modified: trunk/Source/WebCore/GNUmakefile.list.am (91007 => 91008)


--- trunk/Source/WebCore/GNUmakefile.list.am	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/GNUmakefile.list.am	2011-07-14 17:15:04 UTC (rev 91008)
@@ -3807,6 +3807,8 @@
 	Source/WebCore/xml/DOMParser.h \
 	Source/WebCore/xml/NativeXPathNSResolver.cpp \
 	Source/WebCore/xml/NativeXPathNSResolver.h \
+	Source/WebCore/xml/XMLErrors.cpp \
+	Source/WebCore/xml/XMLErrors.h \
 	Source/WebCore/xml/XMLHttpRequest.cpp \
 	Source/WebCore/xml/XMLHttpRequestException.h \
 	Source/WebCore/xml/XMLHttpRequest.h \

Modified: trunk/Source/WebCore/WebCore.gypi (91007 => 91008)


--- trunk/Source/WebCore/WebCore.gypi	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/WebCore.gypi	2011-07-14 17:15:04 UTC (rev 91008)
@@ -6111,6 +6111,8 @@
             'xml/DOMParser.h',
             'xml/NativeXPathNSResolver.cpp',
             'xml/NativeXPathNSResolver.h',
+            'xml/XMLErrors.cpp',
+            'xml/XMLErrors.h',
             'xml/XMLHttpRequest.cpp',
             'xml/XMLHttpRequest.h',
             'xml/XMLHttpRequestException.h',

Modified: trunk/Source/WebCore/WebCore.pro (91007 => 91008)


--- trunk/Source/WebCore/WebCore.pro	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/WebCore.pro	2011-07-14 17:15:04 UTC (rev 91008)
@@ -1195,6 +1195,7 @@
     xml/XMLHttpRequest.cpp \
     xml/XMLHttpRequestProgressEventThrottle.cpp \
     xml/XMLHttpRequestUpload.cpp \
+    xml/XMLErrors.cpp \
     xml/XMLSerializer.cpp \
     xml/parser/XMLDocumentParser.cpp 
 
@@ -2497,6 +2498,7 @@
     xml/NativeXPathNSResolver.h \
     xml/XMLHttpRequest.h \
     xml/XMLHttpRequestUpload.h \
+    xml/XMLErrors.h \
     xml/XMLSerializer.h \
     xml/XPathEvaluator.h \
     xml/XPathExpression.h \

Modified: trunk/Source/WebCore/WebCore.vcproj/WebCore.vcproj (91007 => 91008)


--- trunk/Source/WebCore/WebCore.vcproj/WebCore.vcproj	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/WebCore.vcproj/WebCore.vcproj	2011-07-14 17:15:04 UTC (rev 91008)
@@ -42060,6 +42060,14 @@
 				>
 			</File>
 			<File
+				RelativePath="..\xml\XMLErrors.cpp"
+				>
+			</File>
+			<File
+				RelativePath="..\xml\XMLErrors.h"
+				>
+			</File>
+			<File
 				RelativePath="..\xml\XMLHttpRequest.cpp"
 				>
 			</File>

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (91007 => 91008)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2011-07-14 17:15:04 UTC (rev 91008)
@@ -1467,6 +1467,8 @@
 		59BC393F11054A1300FD85DB /* JavaStringJSC.h in Headers */ = {isa = PBXBuildFile; fileRef = 59BC393E11054A1300FD85DB /* JavaStringJSC.h */; };
 		59C27F05138D28C10079B7E2 /* NetworkResourcesData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59C27F04138D28C10079B7E2 /* NetworkResourcesData.cpp */; };
 		59C27F07138D28CF0079B7E2 /* NetworkResourcesData.h in Headers */ = {isa = PBXBuildFile; fileRef = 59C27F06138D28CF0079B7E2 /* NetworkResourcesData.h */; };
+		59C28045138DC2410079B7E2 /* XMLErrors.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59C28043138DC2410079B7E2 /* XMLErrors.cpp */; };
+		59C28046138DC2410079B7E2 /* XMLErrors.h in Headers */ = {isa = PBXBuildFile; fileRef = 59C28044138DC2410079B7E2 /* XMLErrors.h */; };
 		59D1C10411EB5DCF00B638C8 /* DeviceOrientation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59D1C10311EB5DCF00B638C8 /* DeviceOrientation.cpp */; };
 		59E560A71105336600AA1258 /* JavaClassJSC.h in Headers */ = {isa = PBXBuildFile; fileRef = 59E560A61105336600AA1258 /* JavaClassJSC.h */; };
 		59E560A91105336F00AA1258 /* JavaClassJSC.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59E560A81105336F00AA1258 /* JavaClassJSC.cpp */; };
@@ -8006,6 +8008,8 @@
 		59BC393E11054A1300FD85DB /* JavaStringJSC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JavaStringJSC.h; path = jsc/JavaStringJSC.h; sourceTree = "<group>"; };
 		59C27F04138D28C10079B7E2 /* NetworkResourcesData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = NetworkResourcesData.cpp; sourceTree = "<group>"; };
 		59C27F06138D28CF0079B7E2 /* NetworkResourcesData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkResourcesData.h; sourceTree = "<group>"; };
+		59C28043138DC2410079B7E2 /* XMLErrors.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XMLErrors.cpp; sourceTree = "<group>"; };
+		59C28044138DC2410079B7E2 /* XMLErrors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XMLErrors.h; sourceTree = "<group>"; };
 		59D1C10311EB5DCF00B638C8 /* DeviceOrientation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DeviceOrientation.cpp; sourceTree = "<group>"; };
 		59E560A61105336600AA1258 /* JavaClassJSC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JavaClassJSC.h; path = jsc/JavaClassJSC.h; sourceTree = "<group>"; };
 		59E560A81105336F00AA1258 /* JavaClassJSC.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = JavaClassJSC.cpp; path = jsc/JavaClassJSC.cpp; sourceTree = "<group>"; };
@@ -18968,6 +18972,8 @@
 				E10B9B6B0B747599003ED890 /* NativeXPathNSResolver.cpp */,
 				E10B9B6A0B747599003ED890 /* NativeXPathNSResolver.h */,
 				A833C7F60A2CF1D800D57664 /* xmlattrs.in */,
+				59C28043138DC2410079B7E2 /* XMLErrors.cpp */,
+				59C28044138DC2410079B7E2 /* XMLErrors.h */,
 				BC772C440C4EB2C60083285F /* XMLHttpRequest.cpp */,
 				BC772C450C4EB2C60083285F /* XMLHttpRequest.h */,
 				BC348BA70DB7F1B8004ABAB9 /* XMLHttpRequest.idl */,
@@ -23044,6 +23050,7 @@
 				6565820209D1508D000E61D7 /* XLinkNames.h in Headers */,
 				00B9318813BA8DBA0035A948 /* XMLDocumentParser.h in Headers */,
 				00B9318C13BA8DCC0035A948 /* XMLDocumentParserScope.h in Headers */,
+				59C28046138DC2410079B7E2 /* XMLErrors.h in Headers */,
 				BC772C470C4EB2C60083285F /* XMLHttpRequest.h in Headers */,
 				BC60D9C90D2A29E500B9918F /* XMLHttpRequestException.h in Headers */,
 				F9F0ED7A0DB50CA200D16DB9 /* XMLHttpRequestProgressEvent.h in Headers */,
@@ -25813,6 +25820,7 @@
 				00B9318713BA8DB30035A948 /* XMLDocumentParser.cpp in Sources */,
 				00B9318913BA8DBC0035A948 /* XMLDocumentParserLibxml2.cpp in Sources */,
 				00B9318B13BA8DC90035A948 /* XMLDocumentParserScope.cpp in Sources */,
+				59C28045138DC2410079B7E2 /* XMLErrors.cpp in Sources */,
 				BC772C460C4EB2C60083285F /* XMLHttpRequest.cpp in Sources */,
 				A136A00C1134DBD200CC8D50 /* XMLHttpRequestProgressEventThrottle.cpp in Sources */,
 				BCDFD48F0E305290009D10AD /* XMLHttpRequestUpload.cpp in Sources */,

Added: trunk/Source/WebCore/xml/XMLErrors.cpp (0 => 91008)


--- trunk/Source/WebCore/xml/XMLErrors.cpp	                        (rev 0)
+++ trunk/Source/WebCore/xml/XMLErrors.cpp	2011-07-14 17:15:04 UTC (rev 91008)
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2011 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 GOOGLE INC. AND ITS CONTRIBUTORS
+ * "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 GOOGLE INC.
+ * OR ITS 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 "XMLErrors.h"
+
+#include "Document.h"
+#include "Element.h"
+#include "Frame.h"
+#include "HTMLNames.h"
+#include "Text.h"
+#include <wtf/text/WTFString.h>
+
+#if ENABLE(SVG)
+#include "SVGNames.h"
+#endif
+
+namespace WebCore {
+
+using namespace HTMLNames;
+
+const int maxErrors = 25;
+
+XMLErrors::XMLErrors(Document* document)
+    : m_document(document)
+    , m_errorCount(0)
+    , m_lastErrorPosition(TextPosition1::belowRangePosition())
+{
+}
+
+void XMLErrors::handleError(ErrorType type, const char* message, int lineNumber, int columnNumber)
+{
+    handleError(type, message, TextPosition1(WTF::OneBasedNumber::fromOneBasedInt(lineNumber), WTF::OneBasedNumber::fromOneBasedInt(columnNumber)));
+}
+
+void XMLErrors::handleError(ErrorType type, const char* message, TextPosition1 position)
+{
+    if (type == fatal || (m_errorCount < maxErrors && m_lastErrorPosition.m_line != position.m_line && m_lastErrorPosition.m_column != position.m_column)) {
+        switch (type) {
+        case warning:
+            appendErrorMessage("warning", position, message);
+            break;
+        case fatal:
+        case nonFatal:
+            appendErrorMessage("error", position, message);
+        }
+
+        m_lastErrorPosition = position;
+        ++m_errorCount;
+    }
+}
+
+void XMLErrors::appendErrorMessage(const String& typeString, TextPosition1 position, const char* message)
+{
+    // <typeString> on line <lineNumber> at column <columnNumber>: <message>
+    m_errorMessages.append(typeString);
+    m_errorMessages.append(" on line ");
+    m_errorMessages.append(String::number(position.m_line.oneBasedInt()));
+    m_errorMessages.append(" at column ");
+    m_errorMessages.append(String::number(position.m_column.oneBasedInt()));
+    m_errorMessages.append(": ");
+    m_errorMessages.append(message);
+}
+
+static inline RefPtr<Element> createXHTMLParserErrorHeader(Document* doc, const String& errorMessages)
+{
+    RefPtr<Element> reportElement = doc->createElement(QualifiedName(nullAtom, "parsererror", xhtmlNamespaceURI), false);
+    reportElement->setAttribute(styleAttr, "display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black");
+
+    ExceptionCode ec = 0;
+    RefPtr<Element> h3 = doc->createElement(h3Tag, false);
+    reportElement->appendChild(h3.get(), ec);
+    h3->appendChild(doc->createTextNode("This page contains the following errors:"), ec);
+
+    RefPtr<Element> fixed = doc->createElement(divTag, false);
+    reportElement->appendChild(fixed.get(), ec);
+    fixed->setAttribute(styleAttr, "font-family:monospace;font-size:12px");
+    fixed->appendChild(doc->createTextNode(errorMessages), ec);
+
+    h3 = doc->createElement(h3Tag, false);
+    reportElement->appendChild(h3.get(), ec);
+    h3->appendChild(doc->createTextNode("Below is a rendering of the page up to the first error."), ec);
+
+    return reportElement;
+}
+
+void XMLErrors::insertErrorMessageBlock()
+{
+    // One or more errors occurred during parsing of the code. Display an error block to the user above
+    // the normal content (the DOM tree is created manually and includes line/col info regarding
+    // where the errors are located)
+
+    // Create elements for display
+    ExceptionCode ec = 0;
+    RefPtr<Element> documentElement = m_document->documentElement();
+    if (!documentElement) {
+        RefPtr<Element> rootElement = m_document->createElement(htmlTag, false);
+        m_document->appendChild(rootElement, ec);
+        RefPtr<Element> body = m_document->createElement(bodyTag, false);
+        rootElement->appendChild(body, ec);
+        documentElement = body.get();
+    }
+#if ENABLE(SVG)
+    else if (documentElement->namespaceURI() == SVGNames::svgNamespaceURI) {
+        RefPtr<Element> rootElement = m_document->createElement(htmlTag, false);
+        RefPtr<Element> body = m_document->createElement(bodyTag, false);
+        rootElement->appendChild(body, ec);
+        body->appendChild(documentElement, ec);
+        m_document->appendChild(rootElement.get(), ec);
+        documentElement = body.get();
+    }
+#endif
+    String errorMessages = m_errorMessages.toString();
+    RefPtr<Element> reportElement = createXHTMLParserErrorHeader(m_document, errorMessages);
+    documentElement->insertBefore(reportElement, documentElement->firstChild(), ec);
+#if ENABLE(XSLT)
+    if (m_document->transformSourceDocument()) {
+        RefPtr<Element> paragraph = m_document->createElement(pTag, false);
+        paragraph->setAttribute(styleAttr, "white-space: normal");
+        paragraph->appendChild(m_document->createTextNode("This document was created as the result of an XSL transformation. The line and column numbers given are from the transformed result."), ec);
+        reportElement->appendChild(paragraph.release(), ec);
+    }
+#endif
+    m_document->updateStyleIfNeeded();
+}
+
+} // namespace WebCore
Property changes on: trunk/Source/WebCore/xml/XMLErrors.cpp
___________________________________________________________________

Added: svn:eol-style

Added: trunk/Source/WebCore/xml/XMLErrors.h (0 => 91008)


--- trunk/Source/WebCore/xml/XMLErrors.h	                        (rev 0)
+++ trunk/Source/WebCore/xml/XMLErrors.h	2011-07-14 17:15:04 UTC (rev 91008)
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2011 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 GOOGLE INC. AND ITS CONTRIBUTORS
+ * "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 GOOGLE INC.
+ * OR ITS 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.
+ */
+
+#ifndef XMLErrors_h
+#define XMLErrors_h
+
+#include <wtf/text/StringBuilder.h>
+#include <wtf/text/TextPosition.h>
+
+#if !USE(QXMLSTREAM)
+#include <libxml/parser.h>
+#endif
+
+namespace WebCore {
+
+class Document;
+
+class XMLErrors {
+public:
+    explicit XMLErrors(Document*);
+
+    // Exposed for callbacks:
+    enum ErrorType { warning, nonFatal, fatal };
+    void handleError(ErrorType, const char* message, int lineNumber, int columnNumber);
+    void handleError(ErrorType, const char* message, TextPosition1);
+
+    void insertErrorMessageBlock();
+
+private:
+    void appendErrorMessage(const String& typeString, TextPosition1, const char* message);
+
+    Document* m_document;
+
+    int m_errorCount;
+    TextPosition1 m_lastErrorPosition;
+    StringBuilder m_errorMessages;
+};
+
+} // namespace WebCore
+
+#endif // XMLErrors_h
Property changes on: trunk/Source/WebCore/xml/XMLErrors.h
___________________________________________________________________

Added: svn:eol-style

Modified: trunk/Source/WebCore/xml/parser/XMLDocumentParser.cpp (91007 => 91008)


--- trunk/Source/WebCore/xml/parser/XMLDocumentParser.cpp	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/xml/parser/XMLDocumentParser.cpp	2011-07-14 17:15:04 UTC (rev 91008)
@@ -50,7 +50,7 @@
 #include "ScriptValue.h"
 #include "TextResourceDecoder.h"
 #include "TreeDepthLimit.h"
-#include <wtf/text/WTFString.h>
+#include "XMLErrors.h"
 #include <wtf/StringExtras.h>
 #include <wtf/Threading.h>
 #include <wtf/Vector.h>
@@ -66,8 +66,6 @@
 
 using namespace HTMLNames;
 
-const int maxErrors = 25;
-
 void XMLDocumentParser::pushCurrentNode(ContainerNode* n)
 {
     ASSERT(n);
@@ -77,7 +75,7 @@
     m_currentNodeStack.append(m_currentNode);
     m_currentNode = n;
     if (m_currentNodeStack.size() > maxDOMTreeDepth)
-        handleError(fatal, "Excessive node nesting.", lineNumber(), columnNumber());
+        handleError(XMLErrors::fatal, "Excessive node nesting.", lineNumber(), columnNumber());
 }
 
 void XMLDocumentParser::popCurrentNode()
@@ -135,31 +133,17 @@
     ImageLoader::dispatchPendingBeforeLoadEvents();
 }
 
-void XMLDocumentParser::handleError(ErrorType type, const char* m, int lineNumber, int columnNumber)
+void XMLDocumentParser::handleError(XMLErrors::ErrorType type, const char* m, int lineNumber, int columnNumber)
 {
     handleError(type, m, TextPosition1(WTF::OneBasedNumber::fromOneBasedInt(lineNumber), WTF::OneBasedNumber::fromOneBasedInt(columnNumber)));
 }
 
-void XMLDocumentParser::handleError(ErrorType type, const char* m, TextPosition1 position)
+void XMLDocumentParser::handleError(XMLErrors::ErrorType type, const char* m, TextPosition1 position)
 {
-    if (type == fatal || (m_errorCount < maxErrors && m_lastErrorPosition.m_line != position.m_line && m_lastErrorPosition.m_column != position.m_column)) {
-        switch (type) {
-            case warning:
-                m_errorMessages += "warning on line " + String::number(position.m_line.oneBasedInt()) + " at column " + String::number(position.m_column.oneBasedInt()) + ": " + m;
-                break;
-            case fatal:
-            case nonFatal:
-                m_errorMessages += "error on line " + String::number(position.m_line.oneBasedInt()) + " at column " + String::number(position.m_column.oneBasedInt()) + ": " + m;
-        }
-
-        m_lastErrorPosition = position;
-        ++m_errorCount;
-    }
-
-    if (type != warning)
+    m_xmlErrors.handleError(type, m, position);
+    if (type != XMLErrors::warning)
         m_sawError = true;
-
-    if (type == fatal)
+    if (type == XMLErrors::fatal)
         stopParsing();
 }
 
@@ -251,70 +235,14 @@
     return m_finishCalled;
 }
 
-static inline RefPtr<Element> createXHTMLParserErrorHeader(Document* doc, const String& errorMessages)
-{
-    RefPtr<Element> reportElement = doc->createElement(QualifiedName(nullAtom, "parsererror", xhtmlNamespaceURI), false);
-    reportElement->setAttribute(styleAttr, "display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black");
-
-    ExceptionCode ec = 0;
-    RefPtr<Element> h3 = doc->createElement(h3Tag, false);
-    reportElement->appendChild(h3.get(), ec);
-    h3->appendChild(doc->createTextNode("This page contains the following errors:"), ec);
-
-    RefPtr<Element> fixed = doc->createElement(divTag, false);
-    reportElement->appendChild(fixed.get(), ec);
-    fixed->setAttribute(styleAttr, "font-family:monospace;font-size:12px");
-    fixed->appendChild(doc->createTextNode(errorMessages), ec);
-
-    h3 = doc->createElement(h3Tag, false);
-    reportElement->appendChild(h3.get(), ec);
-    h3->appendChild(doc->createTextNode("Below is a rendering of the page up to the first error."), ec);
-
-    return reportElement;
-}
-
 void XMLDocumentParser::insertErrorMessageBlock()
 {
 #if USE(QXMLSTREAM)
     if (m_parsingFragment)
         return;
 #endif
-    // One or more errors occurred during parsing of the code. Display an error block to the user above
-    // the normal content (the DOM tree is created manually and includes line/col info regarding
-    // where the errors are located)
 
-    // Create elements for display
-    ExceptionCode ec = 0;
-    Document* document = this->document();
-    RefPtr<Element> documentElement = document->documentElement();
-    if (!documentElement) {
-        RefPtr<Element> rootElement = document->createElement(htmlTag, false);
-        document->appendChild(rootElement, ec);
-        RefPtr<Element> body = document->createElement(bodyTag, false);
-        rootElement->appendChild(body, ec);
-        documentElement = body.get();
-    }
-#if ENABLE(SVG)
-    else if (documentElement->namespaceURI() == SVGNames::svgNamespaceURI) {
-        RefPtr<Element> rootElement = document->createElement(htmlTag, false);
-        RefPtr<Element> body = document->createElement(bodyTag, false);
-        rootElement->appendChild(body, ec);
-        body->appendChild(documentElement, ec);
-        document->appendChild(rootElement.get(), ec);
-        documentElement = body.get();
-    }
-#endif
-    RefPtr<Element> reportElement = createXHTMLParserErrorHeader(document, m_errorMessages);
-    documentElement->insertBefore(reportElement, documentElement->firstChild(), ec);
-#if ENABLE(XSLT)
-    if (document->transformSourceDocument()) {
-        RefPtr<Element> paragraph = document->createElement(pTag, false);
-        paragraph->setAttribute(styleAttr, "white-space: normal");
-        paragraph->appendChild(document->createTextNode("This document was created as the result of an XSL transformation. The line and column numbers given are from the transformed result."), ec);
-        reportElement->appendChild(paragraph.release(), ec);
-    }
-#endif
-    document->updateStyleIfNeeded();
+    m_xmlErrors.insertErrorMessageBlock();
 }
 
 void XMLDocumentParser::notifyFinished(CachedResource* unusedResource)

Modified: trunk/Source/WebCore/xml/parser/XMLDocumentParser.h (91007 => 91008)


--- trunk/Source/WebCore/xml/parser/XMLDocumentParser.h	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/xml/parser/XMLDocumentParser.h	2011-07-14 17:15:04 UTC (rev 91008)
@@ -30,6 +30,7 @@
 #include "FragmentScriptingPermission.h"
 #include "ScriptableDocumentParser.h"
 #include "SegmentedString.h"
+#include "XMLErrors.h"
 #include <wtf/HashMap.h>
 #include <wtf/OwnPtr.h>
 #include <wtf/text/CString.h>
@@ -86,9 +87,8 @@
         ~XMLDocumentParser();
 
         // Exposed for callbacks:
-        enum ErrorType { warning, nonFatal, fatal };
-        void handleError(ErrorType, const char* message, int lineNumber, int columnNumber);
-        void handleError(ErrorType, const char* message, TextPosition1);
+        void handleError(XMLErrors::ErrorType, const char* message, int lineNumber, int columnNumber);
+        void handleError(XMLErrors::ErrorType, const char* message, TextPosition1);
 
         void setIsXHTMLDocument(bool isXHTML) { m_isXHTMLDocument = isXHTML; }
         bool isXHTMLDocument() const { return m_isXHTMLDocument; }
@@ -148,7 +148,7 @@
 #else
 public:
         // callbacks from parser SAX
-        void error(ErrorType, const char* message, va_list args) WTF_ATTRIBUTE_PRINTF(3, 0);
+        void error(XMLErrors::ErrorType, const char* message, va_list args) WTF_ATTRIBUTE_PRINTF(3, 0);
         void startElementNs(const xmlChar* xmlLocalName, const xmlChar* xmlPrefix, const xmlChar* xmlURI, int nb_namespaces,
                             const xmlChar** namespaces, int nb_attributes, int nb_defaulted, const xmlChar** libxmlAttributes);
         void endElementNs();
@@ -207,9 +207,7 @@
         bool m_requestingScript;
         bool m_finishCalled;
 
-        int m_errorCount;
-        TextPosition1 m_lastErrorPosition;
-        String m_errorMessages;
+        XMLErrors m_xmlErrors;
 
         CachedResourceHandle<CachedScript> m_pendingScript;
         RefPtr<Element> m_scriptElement;

Modified: trunk/Source/WebCore/xml/parser/XMLDocumentParserLibxml2.cpp (91007 => 91008)


--- trunk/Source/WebCore/xml/parser/XMLDocumentParserLibxml2.cpp	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/xml/parser/XMLDocumentParserLibxml2.cpp	2011-07-14 17:15:04 UTC (rev 91008)
@@ -172,7 +172,7 @@
         m_callbacks.append(callback.release());
     }
 
-    void appendErrorCallback(XMLDocumentParser::ErrorType type, const xmlChar* message, int lineNumber, int columnNumber)
+    void appendErrorCallback(XMLErrors::ErrorType type, const xmlChar* message, int lineNumber, int columnNumber)
     {
         OwnPtr<PendingErrorCallback> callback = adoptPtr(new PendingErrorCallback);
 
@@ -328,7 +328,7 @@
             parser->handleError(type, reinterpret_cast<char*>(message), lineNumber, columnNumber);
         }
 
-        XMLDocumentParser::ErrorType type;
+        XMLErrors::ErrorType type;
         xmlChar* message;
         int lineNumber;
         int columnNumber;
@@ -562,8 +562,7 @@
     , m_parserPaused(false)
     , m_requestingScript(false)
     , m_finishCalled(false)
-    , m_errorCount(0)
-    , m_lastErrorPosition(TextPosition1::belowRangePosition())
+    , m_xmlErrors(document)
     , m_pendingScript(0)
     , m_scriptStartPosition(TextPosition1::belowRangePosition())
     , m_parsingFragment(false)
@@ -589,8 +588,7 @@
     , m_parserPaused(false)
     , m_requestingScript(false)
     , m_finishCalled(false)
-    , m_errorCount(0)
-    , m_lastErrorPosition(TextPosition1::belowRangePosition())
+    , m_xmlErrors(fragment->document())
     , m_pendingScript(0)
     , m_scriptStartPosition(TextPosition1::belowRangePosition())
     , m_parsingFragment(true)
@@ -675,7 +673,7 @@
     // FIXME: Why is this here?  And why is it after we process the passed source?
     if (document()->decoder() && document()->decoder()->sawError()) {
         // If the decoder saw an error, report it as fatal (stops parsing)
-        handleError(fatal, "Encoding error", context->context()->input->line, context->context()->input->col);
+        handleError(XMLErrors::fatal, "Encoding error", context->context()->input->line, context->context()->input->col);
     }
 }
 
@@ -935,7 +933,7 @@
     m_bufferedText.append(s, len);
 }
 
-void XMLDocumentParser::error(ErrorType type, const char* message, va_list args)
+void XMLDocumentParser::error(XMLErrors::ErrorType type, const char* message, va_list args)
 {
     if (isStopped())
         return;
@@ -1157,7 +1155,7 @@
 {
     va_list args;
     va_start(args, message);
-    getParser(closure)->error(XMLDocumentParser::warning, message, args);
+    getParser(closure)->error(XMLErrors::warning, message, args);
     va_end(args);
 }
 
@@ -1166,7 +1164,7 @@
 {
     va_list args;
     va_start(args, message);
-    getParser(closure)->error(XMLDocumentParser::fatal, message, args);
+    getParser(closure)->error(XMLErrors::fatal, message, args);
     va_end(args);
 }
 
@@ -1175,7 +1173,7 @@
 {
     va_list args;
     va_start(args, message);
-    getParser(closure)->error(XMLDocumentParser::nonFatal, message, args);
+    getParser(closure)->error(XMLErrors::nonFatal, message, args);
     va_end(args);
 }
 

Modified: trunk/Source/WebCore/xml/parser/XMLDocumentParserQt.cpp (91007 => 91008)


--- trunk/Source/WebCore/xml/parser/XMLDocumentParserQt.cpp	2011-07-14 17:09:53 UTC (rev 91007)
+++ trunk/Source/WebCore/xml/parser/XMLDocumentParserQt.cpp	2011-07-14 17:15:04 UTC (rev 91008)
@@ -101,8 +101,7 @@
     , m_parserPaused(false)
     , m_requestingScript(false)
     , m_finishCalled(false)
-    , m_errorCount(0)
-    , m_lastErrorPosition(TextPosition1::belowRangePosition())
+    , m_xmlErrors(document)
     , m_pendingScript(0)
     , m_scriptStartPosition(TextPosition1::belowRangePosition())
     , m_parsingFragment(false)
@@ -128,8 +127,7 @@
     , m_parserPaused(false)
     , m_requestingScript(false)
     , m_finishCalled(false)
-    , m_errorCount(0)
-    , m_lastErrorPosition(TextPosition1::belowRangePosition())
+    , m_xmlErrors(fragment->document())
     , m_pendingScript(0)
     , m_scriptStartPosition(TextPosition1::belowRangePosition())
     , m_parsingFragment(true)
@@ -185,7 +183,7 @@
 
     if (document()->decoder() && document()->decoder()->sawError()) {
         // If the decoder saw an error, report it as fatal (stops parsing)
-        handleError(fatal, "Encoding error", lineNumber(), columnNumber());
+        handleError(XMLErrors::fatal, "Encoding error", lineNumber(), columnNumber());
         return;
     }
 
@@ -225,7 +223,7 @@
 
     if (m_stream.error() == QXmlStreamReader::PrematureEndOfDocumentError
         || (m_wroteText && !m_sawFirstElement && !m_sawXSLTransform && !m_sawError))
-        handleError(fatal, qPrintable(m_stream.errorString()), lineNumber(), columnNumber());
+        handleError(XMLErrors::fatal, qPrintable(m_stream.errorString()), lineNumber(), columnNumber());
 }
 
 int XMLDocumentParser::lineNumber() const
@@ -382,7 +380,7 @@
         case QXmlStreamReader::StartElement: {
 #if ENABLE(XHTMLMP)
             if (document()->isXHTMLMPDocument() && !m_hasDocTypeDeclaration) {
-                handleError(fatal, "DOCTYPE declaration lost.", lineNumber(), columnNumber());
+                handleError(XMLErrors::fatal, "DOCTYPE declaration lost.", lineNumber(), columnNumber());
                 break;
             }
 #endif
@@ -440,8 +438,8 @@
             break;
         default: {
             if (m_stream.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
-                ErrorType type = (m_stream.error() == QXmlStreamReader::NotWellFormedError) ?
-                                 fatal : warning;
+                XMLErrors::ErrorType type = (m_stream.error() == QXmlStreamReader::NotWellFormedError) ?
+                                 XMLErrors::fatal : XMLErrors::warning;
                 handleError(type, qPrintable(m_stream.errorString()), lineNumber(),
                             columnNumber());
             }
@@ -501,7 +499,7 @@
         // ensure the name of the default namespace on the root elment 'html'
         // MUST be 'http://www.w3.org/1999/xhtml'
         if (localName != HTMLNames::htmlTag.localName()) {
-            handleError(fatal, "XHTMLMP document expects 'html' as root element.", lineNumber(), columnNumber());
+            handleError(XMLErrors::fatal, "XHTMLMP document expects 'html' as root element.", lineNumber(), columnNumber());
             return;
         }
 
@@ -705,7 +703,7 @@
     else if ((publicId == QLatin1String("-//WAPFORUM//DTD XHTML Mobile 1.1//EN"))
              || (publicId == QLatin1String("-//WAPFORUM//DTD XHTML Mobile 1.0//EN"))) {
         if (AtomicString(name) != HTMLNames::htmlTag.localName()) {
-            handleError(fatal, "Invalid DOCTYPE declaration, expected 'html' as root element.", lineNumber(), columnNumber());
+            handleError(XMLErrors::fatal, "Invalid DOCTYPE declaration, expected 'html' as root element.", lineNumber(), columnNumber());
             return;
         }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to