#ifndef ChildListMutationScope_h
#define ChildListMutationScope_h
#include "core/dom/Document.h"
#include "core/dom/MutationObserver.h"
#include "core/dom/Node.h"
#include "wtf/Noncopyable.h"
#include "wtf/OwnPtr.h"
#include "wtf/RefCounted.h"
namespace WebCore {
class MutationObserverInterestGroup;
class ChildListMutationAccumulator : public RefCounted<ChildListMutationAccumulator> {
public:
static PassRefPtr<ChildListMutationAccumulator> getOrCreate(Node&);
~ChildListMutationAccumulator();
void childAdded(PassRefPtr<Node>);
void willRemoveChild(PassRefPtr<Node>);
bool hasObservers() const { return m_observers; }
private:
ChildListMutationAccumulator(PassRefPtr<Node>, PassOwnPtr<MutationObserverInterestGroup>);
void enqueueMutationRecord();
bool isEmpty();
bool isAddedNodeInOrder(Node*);
bool isRemovedNodeInOrder(Node*);
RefPtr<Node> m_target;
Vector<RefPtr<Node> > m_removedNodes;
Vector<RefPtr<Node> > m_addedNodes;
RefPtr<Node> m_previousSibling;
RefPtr<Node> m_nextSibling;
Node* m_lastAdded;
OwnPtr<MutationObserverInterestGroup> m_observers;
};
class ChildListMutationScope {
WTF_MAKE_NONCOPYABLE(ChildListMutationScope);
public:
explicit ChildListMutationScope(Node& target)
{
if (target.document().hasMutationObserversOfType(MutationObserver::ChildList))
m_accumulator = ChildListMutationAccumulator::getOrCreate(target);
}
void childAdded(Node& child)
{
if (m_accumulator && m_accumulator->hasObservers())
m_accumulator->childAdded(PassRefPtr<Node>(child));
}
void willRemoveChild(Node& child)
{
if (m_accumulator && m_accumulator->hasObservers())
m_accumulator->willRemoveChild(PassRefPtr<Node>(child));
}
private:
RefPtr<ChildListMutationAccumulator> m_accumulator;
};
}
#endif