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