MutationObserverRegistration.cpp [plain text]
#include "config.h"
#include "MutationObserverRegistration.h"
#include "Document.h"
#include "QualifiedName.h"
namespace WebCore {
MutationObserverRegistration::MutationObserverRegistration(MutationObserver& observer, Node& node, MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter)
: m_observer(observer)
, m_node(node)
, m_options(options)
, m_attributeFilter(attributeFilter)
{
m_observer->observationStarted(*this);
}
MutationObserverRegistration::~MutationObserverRegistration()
{
clearTransientRegistrations();
m_observer->observationEnded(*this);
}
void MutationObserverRegistration::resetObservation(MutationObserverOptions options, const HashSet<AtomicString>& attributeFilter)
{
clearTransientRegistrations();
m_options = options;
m_attributeFilter = attributeFilter;
}
void MutationObserverRegistration::observedSubtreeNodeWillDetach(Node& node)
{
if (!isSubtree())
return;
node.registerTransientMutationObserver(*this);
m_observer->setHasTransientRegistration();
if (!m_transientRegistrationNodes) {
m_transientRegistrationNodes = std::make_unique<HashSet<RefPtr<Node>>>();
ASSERT(!m_nodeKeptAlive);
m_nodeKeptAlive = &m_node; }
m_transientRegistrationNodes->add(&node);
}
void MutationObserverRegistration::clearTransientRegistrations()
{
if (!m_transientRegistrationNodes) {
ASSERT(!m_nodeKeptAlive);
return;
}
for (auto& node : *m_transientRegistrationNodes)
node->unregisterTransientMutationObserver(*this);
m_transientRegistrationNodes = nullptr;
ASSERT(m_nodeKeptAlive);
m_nodeKeptAlive = nullptr; }
bool MutationObserverRegistration::shouldReceiveMutationFrom(Node& node, MutationObserver::MutationType type, const QualifiedName* attributeName) const
{
ASSERT((type == MutationObserver::Attributes && attributeName) || !attributeName);
if (!(m_options & type))
return false;
if (&m_node != &node && !isSubtree())
return false;
if (type != MutationObserver::Attributes || !(m_options & MutationObserver::AttributeFilter))
return true;
if (!attributeName->namespaceURI().isNull())
return false;
return m_attributeFilter.contains(attributeName->localName());
}
void MutationObserverRegistration::addRegistrationNodesToSet(HashSet<Node*>& nodes) const
{
nodes.add(&m_node);
if (!m_transientRegistrationNodes)
return;
for (auto& node : *m_transientRegistrationNodes)
nodes.add(node.get());
}
}