initial import
[vuplus_webkit] / Source / WebCore / xml / XMLErrors.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20  * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "XMLErrors.h"
31
32 #include "Document.h"
33 #include "Element.h"
34 #include "Frame.h"
35 #include "HTMLNames.h"
36 #include "Text.h"
37 #include <wtf/text/WTFString.h>
38
39 #if ENABLE(SVG)
40 #include "SVGNames.h"
41 #endif
42
43 namespace WebCore {
44
45 using namespace HTMLNames;
46
47 const int maxErrors = 25;
48
49 XMLErrors::XMLErrors(Document* document)
50     : m_document(document)
51     , m_errorCount(0)
52     , m_lastErrorPosition(TextPosition1::belowRangePosition())
53 {
54 }
55
56 void XMLErrors::handleError(ErrorType type, const char* message, int lineNumber, int columnNumber)
57 {
58     handleError(type, message, TextPosition1(WTF::OneBasedNumber::fromOneBasedInt(lineNumber), WTF::OneBasedNumber::fromOneBasedInt(columnNumber)));
59 }
60
61 void XMLErrors::handleError(ErrorType type, const char* message, TextPosition1 position)
62 {
63     if (type == fatal || (m_errorCount < maxErrors && m_lastErrorPosition.m_line != position.m_line && m_lastErrorPosition.m_column != position.m_column)) {
64         switch (type) {
65         case warning:
66             appendErrorMessage("warning", position, message);
67             break;
68         case fatal:
69         case nonFatal:
70             appendErrorMessage("error", position, message);
71         }
72
73         m_lastErrorPosition = position;
74         ++m_errorCount;
75     }
76 }
77
78 void XMLErrors::appendErrorMessage(const String& typeString, TextPosition1 position, const char* message)
79 {
80     // <typeString> on line <lineNumber> at column <columnNumber>: <message>
81     m_errorMessages.append(typeString);
82     m_errorMessages.append(" on line ");
83     m_errorMessages.append(String::number(position.m_line.oneBasedInt()));
84     m_errorMessages.append(" at column ");
85     m_errorMessages.append(String::number(position.m_column.oneBasedInt()));
86     m_errorMessages.append(": ");
87     m_errorMessages.append(message);
88 }
89
90 static inline RefPtr<Element> createXHTMLParserErrorHeader(Document* doc, const String& errorMessages)
91 {
92     RefPtr<Element> reportElement = doc->createElement(QualifiedName(nullAtom, "parsererror", xhtmlNamespaceURI), false);
93     reportElement->setAttribute(styleAttr, "display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black");
94
95     ExceptionCode ec = 0;
96     RefPtr<Element> h3 = doc->createElement(h3Tag, false);
97     reportElement->appendChild(h3.get(), ec);
98     h3->appendChild(doc->createTextNode("This page contains the following errors:"), ec);
99
100     RefPtr<Element> fixed = doc->createElement(divTag, false);
101     reportElement->appendChild(fixed.get(), ec);
102     fixed->setAttribute(styleAttr, "font-family:monospace;font-size:12px");
103     fixed->appendChild(doc->createTextNode(errorMessages), ec);
104
105     h3 = doc->createElement(h3Tag, false);
106     reportElement->appendChild(h3.get(), ec);
107     h3->appendChild(doc->createTextNode("Below is a rendering of the page up to the first error."), ec);
108
109     return reportElement;
110 }
111
112 void XMLErrors::insertErrorMessageBlock()
113 {
114     // One or more errors occurred during parsing of the code. Display an error block to the user above
115     // the normal content (the DOM tree is created manually and includes line/col info regarding
116     // where the errors are located)
117
118     // Create elements for display
119     ExceptionCode ec = 0;
120     RefPtr<Element> documentElement = m_document->documentElement();
121     if (!documentElement) {
122         RefPtr<Element> rootElement = m_document->createElement(htmlTag, false);
123         m_document->appendChild(rootElement, ec);
124         RefPtr<Element> body = m_document->createElement(bodyTag, false);
125         rootElement->appendChild(body, ec);
126         documentElement = body.get();
127     }
128 #if ENABLE(SVG)
129     else if (documentElement->namespaceURI() == SVGNames::svgNamespaceURI) {
130         RefPtr<Element> rootElement = m_document->createElement(htmlTag, false);
131         RefPtr<Element> body = m_document->createElement(bodyTag, false);
132         rootElement->appendChild(body, ec);
133         body->appendChild(documentElement, ec);
134         m_document->appendChild(rootElement.get(), ec);
135         documentElement = body.get();
136     }
137 #endif
138     String errorMessages = m_errorMessages.toString();
139     RefPtr<Element> reportElement = createXHTMLParserErrorHeader(m_document, errorMessages);
140     documentElement->insertBefore(reportElement, documentElement->firstChild(), ec);
141 #if ENABLE(XSLT)
142     if (m_document->transformSourceDocument()) {
143         RefPtr<Element> paragraph = m_document->createElement(pTag, false);
144         paragraph->setAttribute(styleAttr, "white-space: normal");
145         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);
146         reportElement->appendChild(paragraph.release(), ec);
147     }
148 #endif
149     m_document->updateStyleIfNeeded();
150 }
151
152 } // namespace WebCore