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