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