MutationRecord.cpp [plain text]
#include "config.h"
#include "MutationRecord.h"
#include "CharacterData.h"
#include "StaticNodeList.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/StdLibExtras.h>
namespace WebCore {
namespace {
class ChildListRecord : public MutationRecord {
public:
ChildListRecord(ContainerNode& target, Ref<NodeList>&& added, Ref<NodeList>&& removed, RefPtr<Node>&& previousSibling, RefPtr<Node>&& nextSibling)
: m_target(target)
, m_addedNodes(WTFMove(added))
, m_removedNodes(WTFMove(removed))
, m_previousSibling(WTFMove(previousSibling))
, m_nextSibling(WTFMove(nextSibling))
{
}
private:
const AtomicString& type() override;
Node* target() override { return m_target.ptr(); }
NodeList* addedNodes() override { return m_addedNodes.get(); }
NodeList* removedNodes() override { return m_removedNodes.get(); }
Node* previousSibling() override { return m_previousSibling.get(); }
Node* nextSibling() override { return m_nextSibling.get(); }
Ref<ContainerNode> m_target;
RefPtr<NodeList> m_addedNodes;
RefPtr<NodeList> m_removedNodes;
RefPtr<Node> m_previousSibling;
RefPtr<Node> m_nextSibling;
};
class RecordWithEmptyNodeLists : public MutationRecord {
public:
RecordWithEmptyNodeLists(Node& target, const String& oldValue)
: m_target(target)
, m_oldValue(oldValue)
{
}
private:
Node* target() override { return m_target.ptr(); }
String oldValue() override { return m_oldValue; }
NodeList* addedNodes() override { return lazilyInitializeEmptyNodeList(m_addedNodes); }
NodeList* removedNodes() override { return lazilyInitializeEmptyNodeList(m_removedNodes); }
static NodeList* lazilyInitializeEmptyNodeList(RefPtr<NodeList>& nodeList)
{
if (!nodeList)
nodeList = StaticNodeList::create();
return nodeList.get();
}
Ref<Node> m_target;
String m_oldValue;
RefPtr<NodeList> m_addedNodes;
RefPtr<NodeList> m_removedNodes;
};
class AttributesRecord : public RecordWithEmptyNodeLists {
public:
AttributesRecord(Element& target, const QualifiedName& name, const AtomicString& oldValue)
: RecordWithEmptyNodeLists(target, oldValue)
, m_attributeName(name.localName())
, m_attributeNamespace(name.namespaceURI())
{
}
private:
const AtomicString& type() override;
const AtomicString& attributeName() override { return m_attributeName; }
const AtomicString& attributeNamespace() override { return m_attributeNamespace; }
AtomicString m_attributeName;
AtomicString m_attributeNamespace;
};
class CharacterDataRecord : public RecordWithEmptyNodeLists {
public:
CharacterDataRecord(CharacterData& target, const String& oldValue)
: RecordWithEmptyNodeLists(target, oldValue)
{
}
private:
const AtomicString& type() override;
};
class MutationRecordWithNullOldValue : public MutationRecord {
public:
MutationRecordWithNullOldValue(MutationRecord& record)
: m_record(record)
{
}
private:
const AtomicString& type() override { return m_record->type(); }
Node* target() override { return m_record->target(); }
NodeList* addedNodes() override { return m_record->addedNodes(); }
NodeList* removedNodes() override { return m_record->removedNodes(); }
Node* previousSibling() override { return m_record->previousSibling(); }
Node* nextSibling() override { return m_record->nextSibling(); }
const AtomicString& attributeName() override { return m_record->attributeName(); }
const AtomicString& attributeNamespace() override { return m_record->attributeNamespace(); }
String oldValue() override { return String(); }
Ref<MutationRecord> m_record;
};
const AtomicString& ChildListRecord::type()
{
static NeverDestroyed<AtomicString> childList("childList", AtomicString::ConstructFromLiteral);
return childList;
}
const AtomicString& AttributesRecord::type()
{
static NeverDestroyed<AtomicString> attributes("attributes", AtomicString::ConstructFromLiteral);
return attributes;
}
const AtomicString& CharacterDataRecord::type()
{
static NeverDestroyed<AtomicString> characterData("characterData", AtomicString::ConstructFromLiteral);
return characterData;
}
}
Ref<MutationRecord> MutationRecord::createChildList(ContainerNode& target, Ref<NodeList>&& added, Ref<NodeList>&& removed, RefPtr<Node>&& previousSibling, RefPtr<Node>&& nextSibling)
{
return adoptRef(static_cast<MutationRecord&>(*new ChildListRecord(target, WTFMove(added), WTFMove(removed), WTFMove(previousSibling), WTFMove(nextSibling))));
}
Ref<MutationRecord> MutationRecord::createAttributes(Element& target, const QualifiedName& name, const AtomicString& oldValue)
{
return adoptRef(static_cast<MutationRecord&>(*new AttributesRecord(target, name, oldValue)));
}
Ref<MutationRecord> MutationRecord::createCharacterData(CharacterData& target, const String& oldValue)
{
return adoptRef(static_cast<MutationRecord&>(*new CharacterDataRecord(target, oldValue)));
}
Ref<MutationRecord> MutationRecord::createWithNullOldValue(MutationRecord& record)
{
return adoptRef(static_cast<MutationRecord&>(*new MutationRecordWithNullOldValue(record)));
}
MutationRecord::~MutationRecord()
{
}
}