2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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.
30 #include "XMLErrors.h"
34 #include "HTMLBodyElement.h"
35 #include "HTMLDivElement.h"
36 #include "HTMLHeadElement.h"
37 #include "HTMLHeadingElement.h"
38 #include "HTMLHtmlElement.h"
39 #include "HTMLNames.h"
40 #include "HTMLParagraphElement.h"
41 #include "HTMLStyleElement.h"
47 using namespace HTMLNames;
49 const int maxErrors = 25;
51 XMLErrors::XMLErrors(Document& document)
52 : m_document(document)
53 , m_lastErrorPosition(TextPosition::belowRangePosition())
57 void XMLErrors::handleError(ErrorType type, const char* message, int lineNumber, int columnNumber)
59 handleError(type, message, TextPosition(OrdinalNumber::fromOneBasedInt(lineNumber), OrdinalNumber::fromOneBasedInt(columnNumber)));
62 void XMLErrors::handleError(ErrorType type, const char* message, TextPosition position)
64 if (type == fatal || (m_errorCount < maxErrors && m_lastErrorPosition.m_line != position.m_line && m_lastErrorPosition.m_column != position.m_column)) {
67 appendErrorMessage("warning", position, message);
71 appendErrorMessage("error", position, message);
74 m_lastErrorPosition = position;
79 void XMLErrors::appendErrorMessage(const String& typeString, TextPosition position, const char* message)
81 // <typeString> on line <lineNumber> at column <columnNumber>: <message>
82 m_errorMessages.append(typeString);
83 m_errorMessages.appendLiteral(" on line ");
84 m_errorMessages.appendNumber(position.m_line.oneBasedInt());
85 m_errorMessages.appendLiteral(" at column ");
86 m_errorMessages.appendNumber(position.m_column.oneBasedInt());
87 m_errorMessages.appendLiteral(": ");
88 m_errorMessages.append(message);
91 static inline Ref<Element> createXHTMLParserErrorHeader(Document& document, const String& errorMessages)
93 Ref<Element> reportElement = document.createElement(QualifiedName(nullAtom, "parsererror", xhtmlNamespaceURI), true);
95 Vector<Attribute> reportAttributes;
96 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"));
97 reportElement->parserSetAttributes(reportAttributes);
99 auto h3 = HTMLHeadingElement::create(h3Tag, document);
100 reportElement->parserAppendChild(h3);
101 h3->parserAppendChild(Text::create(document, ASCIILiteral("This page contains the following errors:")));
103 auto fixed = HTMLDivElement::create(document);
104 Vector<Attribute> fixedAttributes;
105 fixedAttributes.append(Attribute(styleAttr, "font-family:monospace;font-size:12px"));
106 fixed->parserSetAttributes(fixedAttributes);
107 reportElement->parserAppendChild(fixed);
109 fixed->parserAppendChild(Text::create(document, errorMessages));
111 h3 = HTMLHeadingElement::create(h3Tag, document);
112 reportElement->parserAppendChild(h3);
113 h3->parserAppendChild(Text::create(document, ASCIILiteral("Below is a rendering of the page up to the first error.")));
115 return reportElement;
118 void XMLErrors::insertErrorMessageBlock()
120 // One or more errors occurred during parsing of the code. Display an error block to the user above
121 // the normal content (the DOM tree is created manually and includes line/col info regarding
122 // where the errors are located)
124 // Create elements for display
125 RefPtr<Element> documentElement = m_document.documentElement();
126 if (!documentElement) {
127 auto rootElement = HTMLHtmlElement::create(m_document);
128 auto body = HTMLBodyElement::create(m_document);
129 rootElement->parserAppendChild(body);
130 m_document.parserAppendChild(rootElement);
131 documentElement = WTFMove(body);
132 } else if (documentElement->namespaceURI() == SVGNames::svgNamespaceURI) {
133 auto rootElement = HTMLHtmlElement::create(m_document);
134 auto head = HTMLHeadElement::create(m_document);
135 auto style = HTMLStyleElement::create(m_document);
136 head->parserAppendChild(style);
137 style->parserAppendChild(m_document.createTextNode(ASCIILiteral("html, body { height: 100% } parsererror + svg { width: 100%; height: 100% }")));
138 style->finishParsingChildren();
139 rootElement->parserAppendChild(head);
140 auto body = HTMLBodyElement::create(m_document);
141 rootElement->parserAppendChild(body);
143 m_document.parserRemoveChild(*documentElement);
145 body->parserAppendChild(*documentElement);
146 m_document.parserAppendChild(rootElement);
148 documentElement = WTFMove(body);
151 String errorMessages = m_errorMessages.toString();
152 auto reportElement = createXHTMLParserErrorHeader(m_document, errorMessages);
155 if (m_document.transformSourceDocument()) {
156 Vector<Attribute> attributes;
157 attributes.append(Attribute(styleAttr, "white-space: normal"));
158 auto paragraph = HTMLParagraphElement::create(m_document);
159 paragraph->parserSetAttributes(attributes);
160 paragraph->parserAppendChild(m_document.createTextNode(ASCIILiteral("This document was created as the result of an XSL transformation. The line and column numbers given are from the transformed result.")));
161 reportElement->parserAppendChild(paragraph);
165 Node* firstChild = documentElement->firstChild();
167 documentElement->parserInsertBefore(reportElement, *firstChild);
169 documentElement->parserAppendChild(reportElement);
171 m_document.updateStyleIfNeeded();
174 } // namespace WebCore