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 "SVGNames.h"
37#include "Text.h"
38#include <wtf/text/WTFString.h>
39
40namespace WebCore {
41
42using namespace HTMLNames;
43
44const int maxErrors = 25;
45
46XMLErrors::XMLErrors(Document* document)
47 : m_document(document)
48 , m_errorCount(0)
49 , m_lastErrorPosition(TextPosition::belowRangePosition())
50{
51}
52
53void XMLErrors::handleError(ErrorType type, const char* message, int lineNumber, int columnNumber)
54{
55 handleError(type, message, TextPosition(OrdinalNumber::fromOneBasedInt(lineNumber), OrdinalNumber::fromOneBasedInt(columnNumber)));
56}
57
58void XMLErrors::handleError(ErrorType type, const char* message, TextPosition position)
59{
60 if (type == fatal || (m_errorCount < maxErrors && m_lastErrorPosition.m_line != position.m_line && m_lastErrorPosition.m_column != position.m_column)) {
61 switch (type) {
62 case warning:
63 appendErrorMessage("warning", position, message);
64 break;
65 case fatal:
66 case nonFatal:
67 appendErrorMessage("error", position, message);
68 }
69
70 m_lastErrorPosition = position;
71 ++m_errorCount;
72 }
73}
74
75void XMLErrors::appendErrorMessage(const String& typeString, TextPosition position, const char* message)
76{
77 // <typeString> on line <lineNumber> at column <columnNumber>: <message>
78 m_errorMessages.append(typeString);
79 m_errorMessages.appendLiteral(" on line ");
80 m_errorMessages.appendNumber(position.m_line.oneBasedInt());
81 m_errorMessages.appendLiteral(" at column ");
82 m_errorMessages.appendNumber(position.m_column.oneBasedInt());
83 m_errorMessages.appendLiteral(": ");
84 m_errorMessages.append(message);
85}
86
87static inline Ref<Element> createXHTMLParserErrorHeader(Document* doc, const String& errorMessages)
88{
89 Ref<Element> reportElement = doc->createElement(QualifiedName(nullAtom, "parsererror", xhtmlNamespaceURI), true);
90
91 Vector<Attribute> reportAttributes;
92 reportAttributes.append(Attribute(styleAttr, "display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"));
93 reportElement->parserSetAttributes(reportAttributes);
94
95 auto h3 = doc->createElement(h3Tag, true);
96 reportElement->parserAppendChild(h3.copyRef());
97 h3->parserAppendChild(doc->createTextNode("This page contains the following errors:"));
98
99 auto fixed = doc->createElement(divTag, true);
100 Vector<Attribute> fixedAttributes;
101 fixedAttributes.append(Attribute(styleAttr, "font-family:monospace;font-size:12px"));
102 fixed->parserSetAttributes(fixedAttributes);
103 reportElement->parserAppendChild(fixed.copyRef());
104
105 fixed->parserAppendChild(doc->createTextNode(errorMessages));
106
107 h3 = doc->createElement(h3Tag, true);
108 reportElement->parserAppendChild(h3.get());
109 h3->parserAppendChild(doc->createTextNode("Below is a rendering of the page up to the first error."));
110
111 return reportElement;
112}
113
114void XMLErrors::insertErrorMessageBlock()
115{
116 // One or more errors occurred during parsing of the code. Display an error block to the user above
117 // the normal content (the DOM tree is created manually and includes line/col info regarding
118 // where the errors are located)
119
120 // Create elements for display
121 RefPtr<Element> documentElement = m_document->documentElement();
122 if (!documentElement) {
123 auto rootElement = m_document->createElement(htmlTag, true);
124 auto body = m_document->createElement(bodyTag, true);
125 rootElement->parserAppendChild(body.copyRef());
126 m_document->parserAppendChild(rootElement.copyRef());
127 documentElement = WTFMove(body);
128 }
129 else if (documentElement->namespaceURI() == SVGNames::svgNamespaceURI) {
130 auto rootElement = m_document->createElement(htmlTag, true);
131 auto head = m_document->createElement(headTag, true);
132 auto style = m_document->createElement(styleTag, true);
133 head->parserAppendChild(style.copyRef());
134 style->parserAppendChild(m_document->createTextNode("html, body { height: 100% } parsererror + svg { width: 100%; height: 100% }"));
135 style->finishParsingChildren();
136 rootElement->parserAppendChild(head.copyRef());
137 auto body = m_document->createElement(bodyTag, true);
138 rootElement->parserAppendChild(body.copyRef());
139
140 m_document->parserRemoveChild(*documentElement);
141
142 body->parserAppendChild(*documentElement);
143 m_document->parserAppendChild(WTFMove(rootElement));
144
145 documentElement = WTFMove(body);
146 }
147
148 String errorMessages = m_errorMessages.toString();
149 auto reportElement = createXHTMLParserErrorHeader(m_document, errorMessages);
150
151#if ENABLE(XSLT)
152 if (m_document->transformSourceDocument()) {
153 Vector<Attribute> attributes;
154 attributes.append(Attribute(styleAttr, "white-space: normal"));
155 auto paragraph = m_document->createElement(pTag, true);
156 paragraph->parserSetAttributes(attributes);
157 paragraph->parserAppendChild(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."));
158 reportElement->parserAppendChild(WTFMove(paragraph));
159 }
160#endif
161
162 Node* firstChild = documentElement->firstChild();
163 if (firstChild)
164 documentElement->parserInsertBefore(WTFMove(reportElement), *firstChild);
165 else
166 documentElement->parserAppendChild(WTFMove(reportElement));
167
168 m_document->updateStyleIfNeeded();
169}
170
171} // namespace WebCore
172