ChildListMutationScope.h [plain text]
#ifndef ChildListMutationScope_h
#define ChildListMutationScope_h
#include "Document.h"
#include "MutationObserver.h"
#include "Node.h"
#include <memory>
#include <wtf/HashMap.h>
#include <wtf/Noncopyable.h>
#include <wtf/RefCounted.h>
namespace WebCore {
class MutationObserverInterestGroup;
class ChildListMutationAccumulator : public RefCounted<ChildListMutationAccumulator> {
public:
static PassRefPtr<ChildListMutationAccumulator> getOrCreate(ContainerNode&);
~ChildListMutationAccumulator();
void childAdded(Node&);
void willRemoveChild(Node&);
bool hasObservers() const { return !!m_observers; }
private:
ChildListMutationAccumulator(ContainerNode&, std::unique_ptr<MutationObserverInterestGroup>);
void enqueueMutationRecord();
bool isEmpty();
bool isAddedNodeInOrder(Node&);
bool isRemovedNodeInOrder(Node&);
Ref<ContainerNode> m_target;
Vector<Ref<Node>> m_removedNodes;
Vector<Ref<Node>> m_addedNodes;
RefPtr<Node> m_previousSibling;
RefPtr<Node> m_nextSibling;
Node* m_lastAdded;
std::unique_ptr<MutationObserverInterestGroup> m_observers;
};
class ChildListMutationScope {
WTF_MAKE_NONCOPYABLE(ChildListMutationScope);
public:
explicit ChildListMutationScope(ContainerNode& 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(child);
}
void willRemoveChild(Node& child)
{
if (m_accumulator && m_accumulator->hasObservers())
m_accumulator->willRemoveChild(child);
}
private:
RefPtr<ChildListMutationAccumulator> m_accumulator;
};
}
#endif // ChildListMutationScope_h