Home | History | Annotate | Download | only in editing
      1 /*
      2  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple 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
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef MarkupAccumulator_h
     27 #define MarkupAccumulator_h
     28 
     29 #include "core/editing/markup.h"
     30 #include "wtf/HashMap.h"
     31 #include "wtf/Vector.h"
     32 #include "wtf/text/StringBuilder.h"
     33 
     34 namespace WebCore {
     35 
     36 class Attribute;
     37 class DocumentType;
     38 class Element;
     39 class Node;
     40 class Range;
     41 
     42 typedef HashMap<StringImpl*, StringImpl*> Namespaces;
     43 
     44 enum EntityMask {
     45     EntityAmp = 0x0001,
     46     EntityLt = 0x0002,
     47     EntityGt = 0x0004,
     48     EntityQuot = 0x0008,
     49     EntityNbsp = 0x0010,
     50 
     51     // Non-breaking space needs to be escaped in innerHTML for compatibility reason. See http://trac.webkit.org/changeset/32879
     52     // However, we cannot do this in a XML document because it does not have the entity reference defined (See the bug 19215).
     53     EntityMaskInCDATA = 0,
     54     EntityMaskInPCDATA = EntityAmp | EntityLt | EntityGt,
     55     EntityMaskInHTMLPCDATA = EntityMaskInPCDATA | EntityNbsp,
     56     EntityMaskInAttributeValue = EntityAmp | EntityLt | EntityGt | EntityQuot,
     57     EntityMaskInHTMLAttributeValue = EntityMaskInAttributeValue | EntityNbsp,
     58 };
     59 
     60 struct EntityDescription {
     61     UChar entity;
     62     const String& reference;
     63     EntityMask mask;
     64 };
     65 
     66 // FIXME: Noncopyable?
     67 class MarkupAccumulator {
     68 public:
     69     MarkupAccumulator(Vector<Node*>*, EAbsoluteURLs, const Range* = 0);
     70     virtual ~MarkupAccumulator();
     71 
     72     String serializeNodes(Node* targetNode, EChildrenOnly);
     73     String serializeNodes(Node* targetNode, EChildrenOnly, Vector<QualifiedName>* tagNamesToSkip);
     74 
     75     static void appendComment(StringBuilder&, const String&);
     76 
     77     static void appendCharactersReplacingEntities(StringBuilder&, const String&, unsigned, unsigned, EntityMask);
     78 
     79 protected:
     80     virtual void appendString(const String&);
     81     void appendStartTag(Node*, Namespaces* = 0);
     82     virtual void appendEndTag(Node*);
     83     static size_t totalLength(const Vector<String>&);
     84     size_t length() const { return m_markup.length(); }
     85     void concatenateMarkup(StringBuilder&);
     86     void appendAttributeValue(StringBuilder&, const String&, bool);
     87     virtual void appendCustomAttributes(StringBuilder&, Element*, Namespaces*);
     88     void appendNodeValue(StringBuilder&, const Node*, const Range*, EntityMask);
     89     bool shouldAddNamespaceElement(const Element*);
     90     bool shouldAddNamespaceAttribute(const Attribute&, Namespaces&);
     91     void appendNamespace(StringBuilder&, const AtomicString& prefix, const AtomicString& namespaceURI, Namespaces&);
     92     EntityMask entityMaskForText(Text*) const;
     93     virtual void appendText(StringBuilder&, Text*);
     94     void appendXMLDeclaration(StringBuilder&, const Document*);
     95     void appendDocumentType(StringBuilder&, const DocumentType*);
     96     void appendProcessingInstruction(StringBuilder&, const String& target, const String& data);
     97     virtual void appendElement(StringBuilder&, Element*, Namespaces*);
     98     void appendOpenTag(StringBuilder&, Element*, Namespaces*);
     99     void appendCloseTag(StringBuilder&, Element*);
    100     void appendAttribute(StringBuilder&, Element*, const Attribute&, Namespaces*);
    101     void appendCDATASection(StringBuilder&, const String&);
    102     void appendStartMarkup(StringBuilder&, const Node*, Namespaces*);
    103     bool shouldSelfClose(const Node*);
    104     bool elementCannotHaveEndTag(const Node*);
    105     void appendEndMarkup(StringBuilder&, const Node*);
    106 
    107     Vector<Node*>* const m_nodes;
    108     const Range* const m_range;
    109 
    110 private:
    111     String resolveURLIfNeeded(const Element*, const String&) const;
    112     void appendQuotedURLAttributeValue(StringBuilder&, const Element*, const Attribute&);
    113     void serializeNodesWithNamespaces(Node* targetNode, EChildrenOnly, const Namespaces*, Vector<QualifiedName>* tagNamesToSkip);
    114 
    115     StringBuilder m_markup;
    116     const EAbsoluteURLs m_resolveURLsMethod;
    117 };
    118 
    119 }
    120 
    121 #endif
    122