Add WTF::move()
[WebKit-https.git] / Source / WebCore / dom / ChildListMutationScope.cpp
1 /*
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 "ChildListMutationScope.h"
34
35 #include "DocumentFragment.h"
36 #include "MutationObserverInterestGroup.h"
37 #include "MutationRecord.h"
38 #include "StaticNodeList.h"
39 #include <wtf/StdLibExtras.h>
40
41 namespace WebCore {
42
43 typedef HashMap<ContainerNode*, ChildListMutationAccumulator*> AccumulatorMap;
44 static AccumulatorMap& accumulatorMap()
45 {
46     DEPRECATED_DEFINE_STATIC_LOCAL(AccumulatorMap, map, ());
47     return map;
48 }
49
50 ChildListMutationAccumulator::ChildListMutationAccumulator(ContainerNode& target, std::unique_ptr<MutationObserverInterestGroup> observers)
51     : m_target(target)
52     , m_lastAdded(nullptr)
53     , m_observers(WTF::move(observers))
54 {
55 }
56
57 ChildListMutationAccumulator::~ChildListMutationAccumulator()
58 {
59     if (!isEmpty())
60         enqueueMutationRecord();
61     accumulatorMap().remove(&m_target.get());
62 }
63
64 PassRefPtr<ChildListMutationAccumulator> ChildListMutationAccumulator::getOrCreate(ContainerNode& target)
65 {
66     AccumulatorMap::AddResult result = accumulatorMap().add(&target, nullptr);
67     RefPtr<ChildListMutationAccumulator> accumulator;
68     if (!result.isNewEntry)
69         accumulator = result.iterator->value;
70     else {
71         accumulator = adoptRef(new ChildListMutationAccumulator(target, MutationObserverInterestGroup::createForChildListMutation(target)));
72         result.iterator->value = accumulator.get();
73     }
74     return accumulator.release();
75 }
76
77 inline bool ChildListMutationAccumulator::isAddedNodeInOrder(Node& child)
78 {
79     return isEmpty() || (m_lastAdded == child.previousSibling() && m_nextSibling == child.nextSibling());
80 }
81
82 void ChildListMutationAccumulator::childAdded(Node& childRef)
83 {
84     ASSERT(hasObservers());
85
86     Ref<Node> child(childRef);
87
88     if (!isAddedNodeInOrder(child.get()))
89         enqueueMutationRecord();
90
91     if (isEmpty()) {
92         m_previousSibling = child->previousSibling();
93         m_nextSibling = child->nextSibling();
94     }
95
96     m_lastAdded = &child.get();
97     m_addedNodes.append(child.get());
98 }
99
100 inline bool ChildListMutationAccumulator::isRemovedNodeInOrder(Node& child)
101 {
102     return isEmpty() || m_nextSibling == &child;
103 }
104
105 void ChildListMutationAccumulator::willRemoveChild(Node& childRef)
106 {
107     ASSERT(hasObservers());
108
109     Ref<Node> child(childRef);
110
111     if (!m_addedNodes.isEmpty() || !isRemovedNodeInOrder(child.get()))
112         enqueueMutationRecord();
113
114     if (isEmpty()) {
115         m_previousSibling = child->previousSibling();
116         m_nextSibling = child->nextSibling();
117         m_lastAdded = child->previousSibling();
118     } else
119         m_nextSibling = child->nextSibling();
120
121     m_removedNodes.append(child.get());
122 }
123
124 void ChildListMutationAccumulator::enqueueMutationRecord()
125 {
126     ASSERT(hasObservers());
127     ASSERT(!isEmpty());
128
129     RefPtr<NodeList> addedNodes = StaticNodeList::adopt(m_addedNodes);
130     RefPtr<NodeList> removedNodes = StaticNodeList::adopt(m_removedNodes);
131     RefPtr<MutationRecord> record = MutationRecord::createChildList(m_target.get(), addedNodes.release(), removedNodes.release(), m_previousSibling.release(), m_nextSibling.release());
132     m_observers->enqueueMutationRecord(record.release());
133     m_lastAdded = 0;
134     ASSERT(isEmpty());
135 }
136
137 bool ChildListMutationAccumulator::isEmpty()
138 {
139     bool result = m_removedNodes.isEmpty() && m_addedNodes.isEmpty();
140 #ifndef NDEBUG
141     if (result) {
142         ASSERT(!m_previousSibling);
143         ASSERT(!m_nextSibling);
144         ASSERT(!m_lastAdded);
145     }
146 #endif
147     return result;
148 }
149
150 } // namespace WebCore