blob: 5d86f3e9c0d1eeeab8fd8e17b56b8912e3d9b560 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32
33#include "core/dom/MutationRecord.h"
34
35#include "core/dom/Node.h"
36#include "core/dom/NodeList.h"
37#include "core/dom/QualifiedName.h"
38#include "core/dom/StaticNodeList.h"
Ben Murdoch591b9582013-07-10 11:41:44 +010039#include "wtf/StdLibExtras.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010040
41namespace WebCore {
42
43namespace {
44
45class ChildListRecord : public MutationRecord {
46public:
47 ChildListRecord(PassRefPtr<Node> target, PassRefPtr<NodeList> added, PassRefPtr<NodeList> removed, PassRefPtr<Node> previousSibling, PassRefPtr<Node> nextSibling)
48 : m_target(target)
49 , m_addedNodes(added)
50 , m_removedNodes(removed)
51 , m_previousSibling(previousSibling)
52 , m_nextSibling(nextSibling)
53 {
54 }
55
56private:
57 virtual const AtomicString& type() OVERRIDE;
58 virtual Node* target() OVERRIDE { return m_target.get(); }
59 virtual NodeList* addedNodes() OVERRIDE { return m_addedNodes.get(); }
60 virtual NodeList* removedNodes() OVERRIDE { return m_removedNodes.get(); }
61 virtual Node* previousSibling() OVERRIDE { return m_previousSibling.get(); }
62 virtual Node* nextSibling() OVERRIDE { return m_nextSibling.get(); }
63
64 RefPtr<Node> m_target;
65 RefPtr<NodeList> m_addedNodes;
66 RefPtr<NodeList> m_removedNodes;
67 RefPtr<Node> m_previousSibling;
68 RefPtr<Node> m_nextSibling;
69};
70
71class RecordWithEmptyNodeLists : public MutationRecord {
72public:
73 RecordWithEmptyNodeLists(PassRefPtr<Node> target, const String& oldValue)
74 : m_target(target)
75 , m_oldValue(oldValue)
76 {
77 }
78
79private:
80 virtual Node* target() OVERRIDE { return m_target.get(); }
81 virtual String oldValue() OVERRIDE { return m_oldValue; }
82 virtual NodeList* addedNodes() OVERRIDE { return lazilyInitializeEmptyNodeList(m_addedNodes); }
83 virtual NodeList* removedNodes() OVERRIDE { return lazilyInitializeEmptyNodeList(m_removedNodes); }
84
85 static NodeList* lazilyInitializeEmptyNodeList(RefPtr<NodeList>& nodeList)
86 {
87 if (!nodeList)
88 nodeList = StaticNodeList::createEmpty();
89 return nodeList.get();
90 }
91
92 RefPtr<Node> m_target;
93 String m_oldValue;
94 RefPtr<NodeList> m_addedNodes;
95 RefPtr<NodeList> m_removedNodes;
96};
97
98class AttributesRecord : public RecordWithEmptyNodeLists {
99public:
100 AttributesRecord(PassRefPtr<Node> target, const QualifiedName& name, const AtomicString& oldValue)
101 : RecordWithEmptyNodeLists(target, oldValue)
102 , m_attributeName(name.localName())
103 , m_attributeNamespace(name.namespaceURI())
104 {
105 }
106
107private:
108 virtual const AtomicString& type() OVERRIDE;
109 virtual const AtomicString& attributeName() OVERRIDE { return m_attributeName; }
110 virtual const AtomicString& attributeNamespace() OVERRIDE { return m_attributeNamespace; }
111
112 AtomicString m_attributeName;
113 AtomicString m_attributeNamespace;
114};
115
116class CharacterDataRecord : public RecordWithEmptyNodeLists {
117public:
118 CharacterDataRecord(PassRefPtr<Node> target, const String& oldValue)
119 : RecordWithEmptyNodeLists(target, oldValue)
120 {
121 }
122
123private:
124 virtual const AtomicString& type() OVERRIDE;
125};
126
127class MutationRecordWithNullOldValue : public MutationRecord {
128public:
129 MutationRecordWithNullOldValue(PassRefPtr<MutationRecord> record)
130 : m_record(record)
131 {
132 }
133
134private:
135 virtual const AtomicString& type() OVERRIDE { return m_record->type(); }
136 virtual Node* target() OVERRIDE { return m_record->target(); }
137 virtual NodeList* addedNodes() OVERRIDE { return m_record->addedNodes(); }
138 virtual NodeList* removedNodes() OVERRIDE { return m_record->removedNodes(); }
139 virtual Node* previousSibling() OVERRIDE { return m_record->previousSibling(); }
140 virtual Node* nextSibling() OVERRIDE { return m_record->nextSibling(); }
141 virtual const AtomicString& attributeName() OVERRIDE { return m_record->attributeName(); }
142 virtual const AtomicString& attributeNamespace() OVERRIDE { return m_record->attributeNamespace(); }
143
144 virtual String oldValue() OVERRIDE { return String(); }
145
146 RefPtr<MutationRecord> m_record;
147};
148
149const AtomicString& ChildListRecord::type()
150{
151 DEFINE_STATIC_LOCAL(AtomicString, childList, ("childList", AtomicString::ConstructFromLiteral));
152 return childList;
153}
154
155const AtomicString& AttributesRecord::type()
156{
157 DEFINE_STATIC_LOCAL(AtomicString, attributes, ("attributes", AtomicString::ConstructFromLiteral));
158 return attributes;
159}
160
161const AtomicString& CharacterDataRecord::type()
162{
163 DEFINE_STATIC_LOCAL(AtomicString, characterData, ("characterData", AtomicString::ConstructFromLiteral));
164 return characterData;
165}
166
167} // namespace
168
169PassRefPtr<MutationRecord> MutationRecord::createChildList(PassRefPtr<Node> target, PassRefPtr<NodeList> added, PassRefPtr<NodeList> removed, PassRefPtr<Node> previousSibling, PassRefPtr<Node> nextSibling)
170{
171 return adoptRef(static_cast<MutationRecord*>(new ChildListRecord(target, added, removed, previousSibling, nextSibling)));
172}
173
174PassRefPtr<MutationRecord> MutationRecord::createAttributes(PassRefPtr<Node> target, const QualifiedName& name, const AtomicString& oldValue)
175{
176 return adoptRef(static_cast<MutationRecord*>(new AttributesRecord(target, name, oldValue)));
177}
178
179PassRefPtr<MutationRecord> MutationRecord::createCharacterData(PassRefPtr<Node> target, const String& oldValue)
180{
181 return adoptRef(static_cast<MutationRecord*>(new CharacterDataRecord(target, oldValue)));
182}
183
184PassRefPtr<MutationRecord> MutationRecord::createWithNullOldValue(PassRefPtr<MutationRecord> record)
185{
186 return adoptRef(static_cast<MutationRecord*>(new MutationRecordWithNullOldValue(record)));
187}
188
189MutationRecord::~MutationRecord()
190{
191}
192
193} // namespace WebCore