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 "PlatformString.h" 30 #include "markup.h" 31 #include <wtf/HashMap.h> 32 #include <wtf/Vector.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<AtomicStringImpl*, AtomicStringImpl*> 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*>* nodes, EAbsoluteURLs shouldResolveURLs, const Range* range = 0); 70 virtual ~MarkupAccumulator(); 71 72 String serializeNodes(Node* node, Node* nodeToSkip, EChildrenOnly childrenOnly); 73 74 protected: 75 virtual void appendString(const String&); 76 void appendStartTag(Node*, Namespaces* = 0); 77 virtual void appendEndTag(Node*); 78 static size_t totalLength(const Vector<String>&); length()79 size_t length() const { return totalLength(m_succeedingMarkup); } 80 void concatenateMarkup(Vector<UChar>& out); 81 void appendAttributeValue(Vector<UChar>& result, const String& attribute, bool documentIsHTML); 82 virtual void appendCustomAttributes(Vector<UChar>&, Element*, Namespaces*); 83 void appendQuotedURLAttributeValue(Vector<UChar>& result, const String& urlString); 84 void appendNodeValue(Vector<UChar>& out, const Node*, const Range*, EntityMask); 85 bool shouldAddNamespaceElement(const Element*); 86 bool shouldAddNamespaceAttribute(const Attribute&, Namespaces&); 87 void appendNamespace(Vector<UChar>& result, const AtomicString& prefix, const AtomicString& namespaceURI, Namespaces&); 88 EntityMask entityMaskForText(Text*) const; 89 virtual void appendText(Vector<UChar>& out, Text*); 90 void appendComment(Vector<UChar>& out, const String& comment); 91 void appendDocumentType(Vector<UChar>& result, const DocumentType*); 92 void appendProcessingInstruction(Vector<UChar>& out, const String& target, const String& data); 93 virtual void appendElement(Vector<UChar>& out, Element*, Namespaces*); 94 void appendOpenTag(Vector<UChar>& out, Element*, Namespaces*); 95 void appendCloseTag(Vector<UChar>& out, Element*); 96 void appendAttribute(Vector<UChar>& out, Element*, const Attribute&, Namespaces*); 97 void appendCDATASection(Vector<UChar>& out, const String& section); 98 void appendStartMarkup(Vector<UChar>& result, const Node*, Namespaces*); 99 bool shouldSelfClose(const Node*); 100 bool elementCannotHaveEndTag(const Node* node); 101 void appendEndMarkup(Vector<UChar>& result, const Node*); 102 shouldResolveURLs()103 bool shouldResolveURLs() { return m_shouldResolveURLs == AbsoluteURLs; } 104 105 Vector<Node*>* const m_nodes; 106 const Range* const m_range; 107 108 private: 109 void serializeNodesWithNamespaces(Node*, Node* nodeToSkip, EChildrenOnly, const Namespaces*); 110 111 Vector<String> m_succeedingMarkup; 112 const bool m_shouldResolveURLs; 113 }; 114 115 // FIXME: This method should be integrated with MarkupAccumulator. 116 void appendCharactersReplacingEntities(Vector<UChar>& out, const UChar* content, size_t length, EntityMask entityMask); 117 118 } 119 120 #endif 121