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 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 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 "markup.h" 30#include <wtf/HashMap.h> 31#include <wtf/text/StringBuilder.h> 32 33namespace WebCore { 34 35class Attribute; 36class DocumentType; 37class Element; 38class Node; 39class Range; 40 41typedef HashMap<AtomicString, AtomicStringImpl*> Namespaces; 42 43enum EntityMask { 44 EntityAmp = 0x0001, 45 EntityLt = 0x0002, 46 EntityGt = 0x0004, 47 EntityQuot = 0x0008, 48 EntityNbsp = 0x0010, 49 50 // Non-breaking space needs to be escaped in innerHTML for compatibility reason. See http://trac.webkit.org/changeset/32879 51 // However, we cannot do this in a XML document because it does not have the entity reference defined (See the bug 19215). 52 EntityMaskInCDATA = 0, 53 EntityMaskInPCDATA = EntityAmp | EntityLt | EntityGt, 54 EntityMaskInHTMLPCDATA = EntityMaskInPCDATA | EntityNbsp, 55 EntityMaskInAttributeValue = EntityAmp | EntityLt | EntityGt | EntityQuot, 56 EntityMaskInHTMLAttributeValue = EntityMaskInAttributeValue | EntityNbsp, 57}; 58 59// FIXME: Noncopyable? 60class MarkupAccumulator { 61public: 62 MarkupAccumulator(Vector<Node*>*, EAbsoluteURLs, const Range* = 0, EFragmentSerialization = HTMLFragmentSerialization); 63 virtual ~MarkupAccumulator(); 64 65 String serializeNodes(Node& targetNode, Node* nodeToSkip, EChildrenOnly, Vector<QualifiedName>* tagNamesToSkip = nullptr); 66 67 static void appendCharactersReplacingEntities(StringBuilder&, const String&, unsigned, unsigned, EntityMask); 68 69protected: 70 static size_t totalLength(const Vector<String>&); 71 size_t length() const { return m_markup.length(); } 72 73 void concatenateMarkup(StringBuilder&); 74 75 virtual void appendString(const String&); 76 virtual void appendEndTag(const Node&); 77 virtual void appendCustomAttributes(StringBuilder&, const Element&, Namespaces*); 78 virtual void appendText(StringBuilder&, const Text&); 79 virtual void appendElement(StringBuilder&, const Element&, Namespaces*); 80 81 void appendStartTag(const Node&, Namespaces* = 0); 82 83 void appendOpenTag(StringBuilder&, const Element&, Namespaces*); 84 void appendCloseTag(StringBuilder&, const Element&); 85 86 void appendStartMarkup(StringBuilder&, const Node&, Namespaces*); 87 void appendEndMarkup(StringBuilder&, const Node&); 88 89 void appendAttributeValue(StringBuilder&, const String&, bool); 90 void appendNodeValue(StringBuilder&, const Node&, const Range*, EntityMask); 91 void appendNamespace(StringBuilder&, const AtomicString& prefix, const AtomicString& namespaceURI, Namespaces&, bool allowEmptyDefaultNS = false); 92 void appendXMLDeclaration(StringBuilder&, const Document&); 93 void appendDocumentType(StringBuilder&, const DocumentType&); 94 void appendProcessingInstruction(StringBuilder&, const String& target, const String& data); 95 void appendAttribute(StringBuilder&, const Element&, const Attribute&, Namespaces*); 96 void appendCDATASection(StringBuilder&, const String&); 97 98 bool shouldAddNamespaceElement(const Element&); 99 bool shouldAddNamespaceAttribute(const Attribute&, Namespaces&); 100 bool shouldSelfClose(const Node&); 101 bool elementCannotHaveEndTag(const Node&); 102 EntityMask entityMaskForText(const Text&) const; 103 104 Vector<Node*>* const m_nodes; 105 const Range* const m_range; 106 107private: 108 String resolveURLIfNeeded(const Element&, const String&) const; 109 void appendQuotedURLAttributeValue(StringBuilder&, const Element&, const Attribute&); 110 void serializeNodesWithNamespaces(Node& targetNode, Node* nodeToSkip, EChildrenOnly, const Namespaces*, Vector<QualifiedName>* tagNamesToSkip); 111 bool inXMLFragmentSerialization() const { return m_fragmentSerialization == XMLFragmentSerialization; } 112 void generateUniquePrefix(QualifiedName&, const Namespaces&); 113 114 StringBuilder m_markup; 115 const EAbsoluteURLs m_resolveURLsMethod; 116 EFragmentSerialization m_fragmentSerialization; 117 unsigned m_prefixLevel; 118}; 119 120} 121 122#endif 123