This source file includes following definitions.
- accumulatorMap
- m_observers
- getOrCreate
- isAddedNodeInOrder
- childAdded
- isRemovedNodeInOrder
- willRemoveChild
- enqueueMutationRecord
- isEmpty
#include "config.h"
#include "core/dom/ChildListMutationScope.h"
#include "core/dom/MutationObserverInterestGroup.h"
#include "core/dom/MutationRecord.h"
#include "core/dom/StaticNodeList.h"
#include "wtf/HashMap.h"
#include "wtf/StdLibExtras.h"
namespace WebCore {
typedef HashMap<Node*, ChildListMutationAccumulator*> AccumulatorMap;
static AccumulatorMap& accumulatorMap()
{
DEFINE_STATIC_LOCAL(AccumulatorMap, map, ());
return map;
}
ChildListMutationAccumulator::ChildListMutationAccumulator(PassRefPtr<Node> target, PassOwnPtr<MutationObserverInterestGroup> observers)
: m_target(target)
, m_lastAdded(0)
, m_observers(observers)
{
}
ChildListMutationAccumulator::~ChildListMutationAccumulator()
{
if (!isEmpty())
enqueueMutationRecord();
accumulatorMap().remove(m_target.get());
}
PassRefPtr<ChildListMutationAccumulator> ChildListMutationAccumulator::getOrCreate(Node& target)
{
AccumulatorMap::AddResult result = accumulatorMap().add(&target, 0);
RefPtr<ChildListMutationAccumulator> accumulator;
if (!result.isNewEntry)
accumulator = result.storedValue->value;
else {
accumulator = adoptRef(new ChildListMutationAccumulator(PassRefPtr<Node>(target), MutationObserverInterestGroup::createForChildListMutation(target)));
result.storedValue->value = accumulator.get();
}
return accumulator.release();
}
inline bool ChildListMutationAccumulator::isAddedNodeInOrder(Node* child)
{
return isEmpty() || (m_lastAdded == child->previousSibling() && m_nextSibling == child->nextSibling());
}
void ChildListMutationAccumulator::childAdded(PassRefPtr<Node> prpChild)
{
ASSERT(hasObservers());
RefPtr<Node> child = prpChild;
if (!isAddedNodeInOrder(child.get()))
enqueueMutationRecord();
if (isEmpty()) {
m_previousSibling = child->previousSibling();
m_nextSibling = child->nextSibling();
}
m_lastAdded = child.get();
m_addedNodes.append(child.release());
}
inline bool ChildListMutationAccumulator::isRemovedNodeInOrder(Node* child)
{
return isEmpty() || m_nextSibling == child;
}
void ChildListMutationAccumulator::willRemoveChild(PassRefPtr<Node> prpChild)
{
ASSERT(hasObservers());
RefPtr<Node> child = prpChild;
if (!m_addedNodes.isEmpty() || !isRemovedNodeInOrder(child.get()))
enqueueMutationRecord();
if (isEmpty()) {
m_previousSibling = child->previousSibling();
m_nextSibling = child->nextSibling();
m_lastAdded = child->previousSibling();
} else
m_nextSibling = child->nextSibling();
m_removedNodes.append(child.release());
}
void ChildListMutationAccumulator::enqueueMutationRecord()
{
ASSERT(hasObservers());
ASSERT(!isEmpty());
RefPtr<NodeList> addedNodes = StaticNodeList::adopt(m_addedNodes);
RefPtr<NodeList> removedNodes = StaticNodeList::adopt(m_removedNodes);
RefPtr<MutationRecord> record = MutationRecord::createChildList(m_target, addedNodes.release(), removedNodes.release(), m_previousSibling.release(), m_nextSibling.release());
m_observers->enqueueMutationRecord(record.release());
m_lastAdded = 0;
ASSERT(isEmpty());
}
bool ChildListMutationAccumulator::isEmpty()
{
bool result = m_removedNodes.isEmpty() && m_addedNodes.isEmpty();
#ifndef NDEBUG
if (result) {
ASSERT(!m_previousSibling);
ASSERT(!m_nextSibling);
ASSERT(!m_lastAdded);
}
#endif
return result;
}
}