1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/*
* 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(TextPosition::belowRangePosition())
{
}
void XMLErrors::handleError(ErrorType type, const char* message, int lineNumber, int columnNumber)
{
handleError(type, message, TextPosition(OrdinalNumber::fromOneBasedInt(lineNumber), OrdinalNumber::fromOneBasedInt(columnNumber)));
}
void XMLErrors::handleError(ErrorType type, const char* message, TextPosition 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, TextPosition position, const char* message)
{
// <typeString> on line <lineNumber> at column <columnNumber>: <message>
m_errorMessages.append(typeString);
m_errorMessages.appendLiteral(" on line ");
m_errorMessages.appendNumber(position.m_line.oneBasedInt());
m_errorMessages.appendLiteral(" at column ");
m_errorMessages.appendNumber(position.m_column.oneBasedInt());
m_errorMessages.appendLiteral(": ");
m_errorMessages.append(message);
}
static inline PassRefPtr<Element> createXHTMLParserErrorHeader(Document* doc, const String& errorMessages)
{
RefPtr<Element> reportElement = doc->createElement(QualifiedName(nullAtom, "parsererror", xhtmlNamespaceURI), true);
Vector<Attribute> reportAttributes;
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"));
reportElement->parserSetAttributes(reportAttributes);
RefPtr<Element> h3 = doc->createElement(h3Tag, true);
reportElement->parserAppendChild(h3.get());
h3->parserAppendChild(doc->createTextNode("This page contains the following errors:"));
RefPtr<Element> fixed = doc->createElement(divTag, true);
Vector<Attribute> fixedAttributes;
fixedAttributes.append(Attribute(styleAttr, "font-family:monospace;font-size:12px"));
fixed->parserSetAttributes(fixedAttributes);
reportElement->parserAppendChild(fixed.get());
fixed->parserAppendChild(doc->createTextNode(errorMessages));
h3 = doc->createElement(h3Tag, true);
reportElement->parserAppendChild(h3.get());
h3->parserAppendChild(doc->createTextNode("Below is a rendering of the page up to the first error."));
return reportElement.release();
}
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
RefPtr<Element> documentElement = m_document->documentElement();
if (!documentElement) {
RefPtr<Element> rootElement = m_document->createElement(htmlTag, true);
RefPtr<Element> body = m_document->createElement(bodyTag, true);
rootElement->parserAppendChild(body);
m_document->parserAppendChild(rootElement);
if (m_document->attached() && !rootElement->attached())
rootElement->attach();
documentElement = body.get();
}
#if ENABLE(SVG)
else if (documentElement->namespaceURI() == SVGNames::svgNamespaceURI) {
RefPtr<Element> rootElement = m_document->createElement(htmlTag, true);
RefPtr<Element> body = m_document->createElement(bodyTag, true);
rootElement->parserAppendChild(body);
documentElement->parentNode()->parserRemoveChild(documentElement.get());
if (documentElement->attached())
documentElement->detach();
body->parserAppendChild(documentElement);
m_document->parserAppendChild(rootElement.get());
if (m_document->attached())
// In general, rootElement shouldn't be attached right now, but it will be if there is a style element
// in the SVG content.
rootElement->reattach();
documentElement = body.get();
}
#endif
String errorMessages = m_errorMessages.toString();
RefPtr<Element> reportElement = createXHTMLParserErrorHeader(m_document, errorMessages);
#if ENABLE(XSLT)
if (m_document->transformSourceDocument()) {
Vector<Attribute> attributes;
attributes.append(Attribute(styleAttr, "white-space: normal"));
RefPtr<Element> paragraph = m_document->createElement(pTag, true);
paragraph->parserSetAttributes(attributes);
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."));
reportElement->parserAppendChild(paragraph.release());
}
#endif
Node* firstChild = documentElement->firstChild();
if (firstChild)
documentElement->parserInsertBefore(reportElement, documentElement->firstChild());
else
documentElement->parserAppendChild(reportElement);
if (documentElement->attached() && !reportElement->attached())
reportElement->attach();
m_document->updateStyleIfNeeded();
}
} // namespace WebCore
|