Home | History | Annotate | Download | only in xml
      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 "core/HTMLNames.h"
     33 #include "core/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 
     39 namespace WebCore {
     40 
     41 using namespace HTMLNames;
     42 
     43 const int maxErrors = 25;
     44 
     45 XMLErrors::XMLErrors(Document* document)
     46     : m_document(document)
     47     , m_errorCount(0)
     48     , m_lastErrorPosition(TextPosition::belowRangePosition())
     49 {
     50 }
     51 
     52 void XMLErrors::trace(Visitor* visitor)
     53 {
     54     visitor->trace(m_document);
     55 }
     56 
     57 void XMLErrors::handleError(ErrorType type, const char* message, int lineNumber, int columnNumber)
     58 {
     59     handleError(type, message, TextPosition(OrdinalNumber::fromOneBasedInt(lineNumber), OrdinalNumber::fromOneBasedInt(columnNumber)));
     60 }
     61 
     62 void XMLErrors::handleError(ErrorType type, const char* message, TextPosition position)
     63 {
     64     if (type == ErrorTypeFatal || (m_errorCount < maxErrors && m_lastErrorPosition.m_line != position.m_line && m_lastErrorPosition.m_column != position.m_column)) {
     65         switch (type) {
     66         case ErrorTypeWarning:
     67             appendErrorMessage("warning", position, message);
     68             break;
     69         case ErrorTypeFatal:
     70         case ErrorTypeNonFatal:
     71             appendErrorMessage("error", position, message);
     72         }
     73 
     74         m_lastErrorPosition = position;
     75         ++m_errorCount;
     76     }
     77 }
     78 
     79 void XMLErrors::appendErrorMessage(const String& typeString, TextPosition position, const char* message)
     80 {
     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);
     89 }
     90 
     91 static inline PassRefPtrWillBeRawPtr<Element> createXHTMLParserErrorHeader(Document* doc, const String& errorMessages)
     92 {
     93     RefPtrWillBeRawPtr<Element> reportElement = doc->createElement(QualifiedName(nullAtom, "parsererror", xhtmlNamespaceURI), true);
     94 
     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);
     98 
     99     RefPtrWillBeRawPtr<Element> h3 = doc->createElement(h3Tag, true);
    100     reportElement->parserAppendChild(h3.get());
    101     h3->parserAppendChild(doc->createTextNode("This page contains the following errors:"));
    102 
    103     RefPtrWillBeRawPtr<Element> fixed = doc->createElement(divTag, true);
    104     Vector<Attribute> fixedAttributes;
    105     fixedAttributes.append(Attribute(styleAttr, "font-family:monospace;font-size:12px"));
    106     fixed->parserSetAttributes(fixedAttributes);
    107     reportElement->parserAppendChild(fixed.get());
    108 
    109     fixed->parserAppendChild(doc->createTextNode(errorMessages));
    110 
    111     h3 = doc->createElement(h3Tag, true);
    112     reportElement->parserAppendChild(h3.get());
    113     h3->parserAppendChild(doc->createTextNode("Below is a rendering of the page up to the first error."));
    114 
    115     return reportElement.release();
    116 }
    117 
    118 void XMLErrors::insertErrorMessageBlock()
    119 {
    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)
    123 
    124     // Create elements for display
    125     RefPtrWillBeRawPtr<Element> documentElement = m_document->documentElement();
    126     if (!documentElement) {
    127         RefPtrWillBeRawPtr<Element> rootElement = m_document->createElement(htmlTag, true);
    128         RefPtrWillBeRawPtr<Element> body = m_document->createElement(bodyTag, true);
    129         rootElement->parserAppendChild(body);
    130         m_document->parserAppendChild(rootElement);
    131         documentElement = body.get();
    132     } else if (documentElement->namespaceURI() == SVGNames::svgNamespaceURI) {
    133         RefPtrWillBeRawPtr<Element> rootElement = m_document->createElement(htmlTag, true);
    134         RefPtrWillBeRawPtr<Element> head = m_document->createElement(headTag, true);
    135         RefPtrWillBeRawPtr<Element> style = m_document->createElement(styleTag, true);
    136         head->parserAppendChild(style);
    137         style->parserAppendChild(m_document->createTextNode("html, body { height: 100% } parsererror + svg { width: 100%; height: 100% }"));
    138         style->finishParsingChildren();
    139         rootElement->parserAppendChild(head);
    140         RefPtrWillBeRawPtr<Element> body = m_document->createElement(bodyTag, true);
    141         rootElement->parserAppendChild(body);
    142 
    143         m_document->parserRemoveChild(*documentElement);
    144 
    145         body->parserAppendChild(documentElement);
    146         m_document->parserAppendChild(rootElement);
    147 
    148         documentElement = body.get();
    149     }
    150 
    151     String errorMessages = m_errorMessages.toString();
    152     RefPtrWillBeRawPtr<Element> reportElement = createXHTMLParserErrorHeader(m_document, errorMessages);
    153 
    154     if (m_document->transformSourceDocument()) {
    155         Vector<Attribute> attributes;
    156         attributes.append(Attribute(styleAttr, "white-space: normal"));
    157         RefPtrWillBeRawPtr<Element> paragraph = m_document->createElement(pTag, true);
    158         paragraph->parserSetAttributes(attributes);
    159         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."));
    160         reportElement->parserAppendChild(paragraph.release());
    161     }
    162 
    163     Node* firstChild = documentElement->firstChild();
    164     if (firstChild)
    165         documentElement->parserInsertBefore(reportElement, *firstChild);
    166     else
    167         documentElement->parserAppendChild(reportElement);
    168 
    169     // FIXME: Why do we need to call this manually?
    170     m_document->updateRenderTreeIfNeeded();
    171 }
    172 
    173 } // namespace WebCore
    174