#include "config.h"
#include "NamedNodeMap.h"
#include "Attr.h"
#include "Document.h"
#include "Element.h"
#include "ExceptionCode.h"
#include "HTMLNames.h"
namespace WebCore {
using namespace HTMLNames;
static inline bool shouldIgnoreAttributeCase(const Element* e)
{
return e && e->document()->isHTMLDocument() && e->isHTMLElement();
}
inline void NamedNodeMap::detachAttributesFromElement()
{
size_t size = m_attributes.size();
for (size_t i = 0; i < size; i++) {
if (Attr* attr = m_attributes[i]->attr())
attr->m_element = 0;
}
}
NamedNodeMap::~NamedNodeMap()
{
detachAttributesFromElement();
}
bool NamedNodeMap::isMappedAttributeMap() const
{
return false;
}
PassRefPtr<Node> NamedNodeMap::getNamedItem(const String& name) const
{
Attribute* a = getAttributeItem(name, shouldIgnoreAttributeCase(m_element));
if (!a)
return 0;
return a->createAttrIfNeeded(m_element);
}
PassRefPtr<Node> NamedNodeMap::getNamedItemNS(const String& namespaceURI, const String& localName) const
{
return getNamedItem(QualifiedName(nullAtom, localName, namespaceURI));
}
PassRefPtr<Node> NamedNodeMap::removeNamedItem(const String& name, ExceptionCode& ec)
{
Attribute* a = getAttributeItem(name, shouldIgnoreAttributeCase(m_element));
if (!a) {
ec = NOT_FOUND_ERR;
return 0;
}
return removeNamedItem(a->name(), ec);
}
PassRefPtr<Node> NamedNodeMap::removeNamedItemNS(const String& namespaceURI, const String& localName, ExceptionCode& ec)
{
return removeNamedItem(QualifiedName(nullAtom, localName, namespaceURI), ec);
}
PassRefPtr<Node> NamedNodeMap::getNamedItem(const QualifiedName& name) const
{
Attribute* a = getAttributeItem(name);
if (!a)
return 0;
return a->createAttrIfNeeded(m_element);
}
PassRefPtr<Node> NamedNodeMap::setNamedItem(Node* arg, ExceptionCode& ec)
{
if (!m_element || !arg) {
ec = NOT_FOUND_ERR;
return 0;
}
if (arg->document() != m_element->document()) {
ec = WRONG_DOCUMENT_ERR;
return 0;
}
if (!arg->isAttributeNode()) {
ec = HIERARCHY_REQUEST_ERR;
return 0;
}
Attr *attr = static_cast<Attr*>(arg);
Attribute* a = attr->attr();
Attribute* old = getAttributeItem(a->name());
if (old == a)
return RefPtr<Node>(arg);
if (attr->ownerElement()) {
ec = INUSE_ATTRIBUTE_ERR;
return 0;
}
if (a->name() == idAttr)
m_element->updateId(old ? old->value() : nullAtom, a->value());
RefPtr<Node> r;
if (old) {
r = old->createAttrIfNeeded(m_element);
removeAttribute(a->name());
}
addAttribute(a);
return r.release();
}
PassRefPtr<Node> NamedNodeMap::removeNamedItem(const QualifiedName& name, ExceptionCode& ec)
{
Attribute* a = getAttributeItem(name);
if (!a) {
ec = NOT_FOUND_ERR;
return 0;
}
RefPtr<Node> r = a->createAttrIfNeeded(m_element);
if (name == idAttr)
m_element->updateId(a->value(), nullAtom);
removeAttribute(name);
return r.release();
}
PassRefPtr<Node> NamedNodeMap::item (unsigned index) const
{
if (index >= length())
return 0;
return m_attributes[index]->createAttrIfNeeded(m_element);
}
Attribute* NamedNodeMap::getAttributeItem(const String& name, bool shouldIgnoreAttributeCase) const
{
unsigned len = length();
for (unsigned i = 0; i < len; ++i) {
if (!m_attributes[i]->name().hasPrefix() &&
m_attributes[i]->name().localName() == name)
return m_attributes[i].get();
if (shouldIgnoreAttributeCase ? equalIgnoringCase(m_attributes[i]->name().toString(), name) : name == m_attributes[i]->name().toString())
return m_attributes[i].get();
}
return 0;
}
Attribute* NamedNodeMap::getAttributeItem(const QualifiedName& name) const
{
unsigned len = length();
for (unsigned i = 0; i < len; ++i) {
if (m_attributes[i]->name().matches(name))
return m_attributes[i].get();
}
return 0;
}
void NamedNodeMap::clearAttributes()
{
detachAttributesFromElement();
m_attributes.clear();
}
void NamedNodeMap::detachFromElement()
{
m_element = 0;
detachAttributesFromElement();
}
void NamedNodeMap::setAttributes(const NamedNodeMap& other)
{
if (!m_element)
return;
Attribute *oldId = getAttributeItem(idAttr);
Attribute *newId = other.getAttributeItem(idAttr);
if (oldId || newId)
m_element->updateId(oldId ? oldId->value() : nullAtom, newId ? newId->value() : nullAtom);
clearAttributes();
unsigned newLength = other.length();
m_attributes.resize(newLength);
for (unsigned i = 0; i < newLength; i++)
m_attributes[i] = other.m_attributes[i]->clone();
for (unsigned i = 0; i < newLength; i++)
m_element->attributeChanged(m_attributes[i].get(), true);
}
void NamedNodeMap::addAttribute(PassRefPtr<Attribute> prpAttribute)
{
RefPtr<Attribute> attribute = prpAttribute;
m_attributes.append(attribute);
if (Attr* attr = attribute->attr())
attr->m_element = m_element;
if (m_element) {
m_element->attributeChanged(attribute.get());
if (attribute->name() != styleAttr) {
m_element->dispatchAttrAdditionEvent(attribute.get());
m_element->dispatchSubtreeModifiedEvent();
}
}
}
void NamedNodeMap::removeAttribute(const QualifiedName& name)
{
unsigned len = length();
unsigned index = len + 1;
for (unsigned i = 0; i < len; ++i)
if (m_attributes[i]->name().matches(name)) {
index = i;
break;
}
if (index >= len)
return;
RefPtr<Attribute> attr = m_attributes[index].get();
if (Attr* a = m_attributes[index]->attr())
a->m_element = 0;
m_attributes.remove(index);
if (m_element && !attr->m_value.isNull()) {
AtomicString value = attr->m_value;
attr->m_value = nullAtom;
m_element->attributeChanged(attr.get());
attr->m_value = value;
}
if (m_element) {
m_element->dispatchAttrRemovalEvent(attr.get());
m_element->dispatchSubtreeModifiedEvent();
}
}
bool NamedNodeMap::mapsEquivalent(const NamedNodeMap* otherMap) const
{
if (!otherMap)
return false;
unsigned len = length();
if (len != otherMap->length())
return false;
for (unsigned i = 0; i < len; i++) {
Attribute *attr = attributeItem(i);
Attribute *otherAttr = otherMap->getAttributeItem(attr->name());
if (!otherAttr || attr->value() != otherAttr->value())
return false;
}
return true;
}
}