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#pragma once
27
28#include "Element.h"
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<AtomString, AtomStringImpl*> 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 = EntityAmp | EntityQuot | EntityNbsp,
57};
58
59class MarkupAccumulator {
60 WTF_MAKE_NONCOPYABLE(MarkupAccumulator);
61public:
62 MarkupAccumulator(Vector<Node*>*, ResolveURLs, SerializationSyntax = SerializationSyntax::HTML);
63 virtual ~MarkupAccumulator();
64
65 String serializeNodes(Node& targetNode, SerializedNodes, 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 void appendString(const String&);
76 void appendStringView(StringView);
77
78 void startAppendingNode(const Node&, Namespaces* = nullptr);
79 void endAppendingNode(const Node& node)
80 {
81 if (is<Element>(node))
82 appendEndTag(m_markup, downcast<Element>(node));
83 }
84
85 virtual void appendStartTag(StringBuilder&, const Element&, Namespaces*);
86 virtual void appendEndTag(StringBuilder&, const Element&);
87 virtual void appendCustomAttributes(StringBuilder&, const Element&, Namespaces*);
88 virtual void appendText(StringBuilder&, const Text&);
89
90 void appendOpenTag(StringBuilder&, const Element&, Namespaces*);
91 void appendCloseTag(StringBuilder&, const Element&);
92
93 void appendNonElementNode(StringBuilder&, const Node&, Namespaces*);
94 void appendEndMarkup(StringBuilder&, const Element&);
95
96 void appendAttributeValue(StringBuilder&, const String&, bool isSerializingHTML);
97 void appendNamespace(StringBuilder&, const AtomString& prefix, const AtomString& namespaceURI, Namespaces&, bool allowEmptyDefaultNS = false);
98 void appendXMLDeclaration(StringBuilder&, const Document&);
99 void appendDocumentType(StringBuilder&, const DocumentType&);
100 void appendProcessingInstruction(StringBuilder&, const String& target, const String& data);
101 void appendAttribute(StringBuilder&, const Element&, const Attribute&, Namespaces*);
102 void appendCDATASection(StringBuilder&, const String&);
103
104 bool shouldAddNamespaceElement(const Element&);
105 bool shouldAddNamespaceAttribute(const Attribute&, Namespaces&);
106 EntityMask entityMaskForText(const Text&) const;
107
108 Vector<Node*>* const m_nodes;
109
110private:
111 String resolveURLIfNeeded(const Element&, const String&) const;
112 void appendQuotedURLAttributeValue(StringBuilder&, const Element&, const Attribute&);
113 void serializeNodesWithNamespaces(Node& targetNode, SerializedNodes, const Namespaces*, Vector<QualifiedName>* tagNamesToSkip);
114 bool inXMLFragmentSerialization() const { return m_serializationSyntax == SerializationSyntax::XML; }
115 void generateUniquePrefix(QualifiedName&, const Namespaces&);
116
117 StringBuilder m_markup;
118 const ResolveURLs m_resolveURLs;
119 SerializationSyntax m_serializationSyntax;
120 unsigned m_prefixLevel { 0 };
121};
122
123} // namespace WebCore
124