blob: e0ac13e5ad267d75dbd5f4689729b84decca7471 [file] [log] [blame]
adamk@chromium.orgd02d7602011-10-21 19:19:56 +00001/*
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
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000033#include "ChildListMutationScope.h"
34
35#include "DocumentFragment.h"
adamk@chromium.orgc3168fb2011-12-16 00:59:39 +000036#include "MutationObserverInterestGroup.h"
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000037#include "MutationRecord.h"
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000038#include "StaticNodeList.h"
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +000039#include <wtf/StdLibExtras.h>
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000040
41namespace WebCore {
42
weinig@apple.com2e067212013-09-23 03:40:47 +000043typedef HashMap<ContainerNode*, ChildListMutationAccumulator*> AccumulatorMap;
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +000044static AccumulatorMap& accumulatorMap()
45{
svillar@igalia.com5b31eef2014-03-14 08:30:55 +000046 DEPRECATED_DEFINE_STATIC_LOCAL(AccumulatorMap, map, ());
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +000047 return map;
48}
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000049
zandobersek@gmail.com9cf5edb2014-02-09 08:33:19 +000050ChildListMutationAccumulator::ChildListMutationAccumulator(ContainerNode& target, std::unique_ptr<MutationObserverInterestGroup> observers)
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000051 : m_target(target)
zandobersek@gmail.com9cf5edb2014-02-09 08:33:19 +000052 , m_lastAdded(nullptr)
dbates@webkit.org0cefe4f2014-07-03 22:13:54 +000053 , m_observers(WTF::move(observers))
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000054{
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000055}
56
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +000057ChildListMutationAccumulator::~ChildListMutationAccumulator()
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000058{
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +000059 if (!isEmpty())
60 enqueueMutationRecord();
cdumez@apple.com78141732014-11-04 23:00:48 +000061 accumulatorMap().remove(m_target.ptr());
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000062}
63
weinig@apple.com2e067212013-09-23 03:40:47 +000064PassRefPtr<ChildListMutationAccumulator> ChildListMutationAccumulator::getOrCreate(ContainerNode& target)
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +000065{
weinig@apple.com2e067212013-09-23 03:40:47 +000066 AccumulatorMap::AddResult result = accumulatorMap().add(&target, nullptr);
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +000067 RefPtr<ChildListMutationAccumulator> accumulator;
68 if (!result.isNewEntry)
benjamin@webkit.orgee554052012-10-07 23:12:07 +000069 accumulator = result.iterator->value;
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +000070 else {
71 accumulator = adoptRef(new ChildListMutationAccumulator(target, MutationObserverInterestGroup::createForChildListMutation(target)));
benjamin@webkit.orgee554052012-10-07 23:12:07 +000072 result.iterator->value = accumulator.get();
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +000073 }
74 return accumulator.release();
75}
76
weinig@apple.com2e067212013-09-23 03:40:47 +000077inline bool ChildListMutationAccumulator::isAddedNodeInOrder(Node& child)
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000078{
weinig@apple.com2e067212013-09-23 03:40:47 +000079 return isEmpty() || (m_lastAdded == child.previousSibling() && m_nextSibling == child.nextSibling());
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000080}
81
weinig@apple.com2e067212013-09-23 03:40:47 +000082void ChildListMutationAccumulator::childAdded(Node& childRef)
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000083{
adamk@chromium.orgc2896142012-09-22 01:27:52 +000084 ASSERT(hasObservers());
85
weinig@apple.com2e067212013-09-23 03:40:47 +000086 Ref<Node> child(childRef);
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000087
cdumez@apple.com2f661202014-11-04 07:22:18 +000088 if (!isAddedNodeInOrder(child))
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000089 enqueueMutationRecord();
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000090
adamk@chromium.org9daf50d2012-04-05 00:05:24 +000091 if (isEmpty()) {
commit-queue@webkit.org159674f2012-04-06 19:37:20 +000092 m_previousSibling = child->previousSibling();
93 m_nextSibling = child->nextSibling();
adamk@chromium.org9daf50d2012-04-05 00:05:24 +000094 }
95
cdumez@apple.com78141732014-11-04 23:00:48 +000096 m_lastAdded = child.ptr();
weinig@apple.com2e067212013-09-23 03:40:47 +000097 m_addedNodes.append(child.get());
adamk@chromium.orgd02d7602011-10-21 19:19:56 +000098}
99
weinig@apple.com2e067212013-09-23 03:40:47 +0000100inline bool ChildListMutationAccumulator::isRemovedNodeInOrder(Node& child)
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000101{
weinig@apple.com2e067212013-09-23 03:40:47 +0000102 return isEmpty() || m_nextSibling == &child;
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000103}
104
weinig@apple.com2e067212013-09-23 03:40:47 +0000105void ChildListMutationAccumulator::willRemoveChild(Node& childRef)
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000106{
adamk@chromium.orgc2896142012-09-22 01:27:52 +0000107 ASSERT(hasObservers());
108
weinig@apple.com2e067212013-09-23 03:40:47 +0000109 Ref<Node> child(childRef);
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000110
cdumez@apple.com2f661202014-11-04 07:22:18 +0000111 if (!m_addedNodes.isEmpty() || !isRemovedNodeInOrder(child))
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000112 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
weinig@apple.com2e067212013-09-23 03:40:47 +0000121 m_removedNodes.append(child.get());
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000122}
123
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +0000124void ChildListMutationAccumulator::enqueueMutationRecord()
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000125{
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +0000126 ASSERT(hasObservers());
127 ASSERT(!isEmpty());
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000128
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +0000129 RefPtr<NodeList> addedNodes = StaticNodeList::adopt(m_addedNodes);
130 RefPtr<NodeList> removedNodes = StaticNodeList::adopt(m_removedNodes);
cdumez@apple.com2f661202014-11-04 07:22:18 +0000131 RefPtr<MutationRecord> record = MutationRecord::createChildList(m_target, addedNodes.release(), removedNodes.release(), m_previousSibling.release(), m_nextSibling.release());
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +0000132 m_observers->enqueueMutationRecord(record.release());
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000133 m_lastAdded = 0;
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +0000134 ASSERT(isEmpty());
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000135}
136
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +0000137bool ChildListMutationAccumulator::isEmpty()
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000138{
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +0000139 bool result = m_removedNodes.isEmpty() && m_addedNodes.isEmpty();
adamk@chromium.orga5b2e472011-12-14 01:03:01 +0000140#ifndef NDEBUG
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +0000141 if (result) {
142 ASSERT(!m_previousSibling);
143 ASSERT(!m_nextSibling);
144 ASSERT(!m_lastAdded);
145 }
adamk@chromium.orga5b2e472011-12-14 01:03:01 +0000146#endif
adamk@chromium.orgb49e0c62012-09-22 00:30:48 +0000147 return result;
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000148}
149
adamk@chromium.orgd02d7602011-10-21 19:19:56 +0000150} // namespace WebCore