#include "config.h"
#include "NamedAttrMap.h"
#include "Document.h"
#include "ExceptionCode.h"
#include "HTMLNames.h"
namespace WebCore {
using namespace HTMLNames;
static inline bool inHTMLDocument(const Element* e)
{
return e && e->document()->isHTMLDocument();
}
NamedAttrMap::NamedAttrMap(Element *e)
: element(e)
, attrs(0)
, len(0)
{
}
NamedAttrMap::~NamedAttrMap()
{
NamedAttrMap::clearAttributes(); }
bool NamedAttrMap::isMappedAttributeMap() const
{
return false;
}
PassRefPtr<Node> NamedAttrMap::getNamedItem(const String& name) const
{
String localName = inHTMLDocument(element) ? name.lower() : name;
Attribute* a = getAttributeItem(localName);
if (!a)
return 0;
return a->createAttrIfNeeded(element);
}
PassRefPtr<Node> NamedAttrMap::getNamedItemNS(const String& namespaceURI, const String& localName) const
{
return getNamedItem(QualifiedName(nullAtom, localName.impl(), namespaceURI.impl()));
}
PassRefPtr<Node> NamedAttrMap::removeNamedItem(const String& name, ExceptionCode& ec)
{
String localName = inHTMLDocument(element) ? name.lower() : name;
Attribute* a = getAttributeItem(localName);
if (!a) {
ec = NOT_FOUND_ERR;
return 0;
}
return removeNamedItem(a->name(), ec);
}
PassRefPtr<Node> NamedAttrMap::removeNamedItemNS(const String& namespaceURI, const String& localName, ExceptionCode& ec)
{
return removeNamedItem(QualifiedName(nullAtom, localName.impl(), namespaceURI.impl()), ec);
}
PassRefPtr<Node> NamedAttrMap::getNamedItem(const QualifiedName& name) const
{
Attribute* a = getAttributeItem(name);
if (!a)
return 0;
return a->createAttrIfNeeded(element);
}
PassRefPtr<Node> NamedAttrMap::setNamedItem(Node* arg, ExceptionCode& ec)
{
if (!element) {
ec = NOT_FOUND_ERR;
return 0;
}
if (isReadOnlyNode()) {
ec = NO_MODIFICATION_ALLOWED_ERR;
return 0;
}
if (arg->document() != 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)
element->updateId(old ? old->value() : nullAtom, a->value());
RefPtr<Node> r;
if (old) {
r = old->createAttrIfNeeded(element);
removeAttribute(a->name());
}
addAttribute(a);
return r.release();
}
PassRefPtr<Node> NamedAttrMap::removeNamedItem(const QualifiedName& name, ExceptionCode& ec)
{
if (isReadOnlyNode()) {
ec = NO_MODIFICATION_ALLOWED_ERR;
return 0;
}
Attribute* a = getAttributeItem(name);
if (!a) {
ec = NOT_FOUND_ERR;
return 0;
}
RefPtr<Node> r = a->createAttrIfNeeded(element);
if (name == idAttr)
element->updateId(a->value(), nullAtom);
removeAttribute(name);
return r.release();
}
PassRefPtr<Node> NamedAttrMap::item ( unsigned index ) const
{
if (index >= len)
return 0;
return attrs[index]->createAttrIfNeeded(element);
}
Attribute* NamedAttrMap::getAttributeItem(const String& name) const
{
for (unsigned i = 0; i < len; ++i) {
if (!attrs[i]->name().hasPrefix() &&
attrs[i]->name().localName() == name)
return attrs[i];
if (attrs[i]->name().toString() == name)
return attrs[i];
}
return 0;
}
Attribute* NamedAttrMap::getAttributeItem(const QualifiedName& name) const
{
for (unsigned i = 0; i < len; ++i) {
if (attrs[i]->name().matches(name))
return attrs[i];
}
return 0;
}
void NamedAttrMap::clearAttributes()
{
if (attrs) {
for (unsigned i = 0; i < len; i++) {
if (attrs[i]->attr())
attrs[i]->attr()->m_element = 0;
attrs[i]->deref();
}
fastFree(attrs);
attrs = 0;
}
len = 0;
}
void NamedAttrMap::detachFromElement()
{
element = 0;
clearAttributes();
}
NamedAttrMap& NamedAttrMap::operator=(const NamedAttrMap& other)
{
if (!element)
return *this;
Attribute *oldId = getAttributeItem(idAttr);
Attribute *newId = other.getAttributeItem(idAttr);
if (oldId || newId)
element->updateId(oldId ? oldId->value() : nullAtom, newId ? newId->value() : nullAtom);
clearAttributes();
len = other.len;
attrs = static_cast<Attribute **>(fastMalloc(len * sizeof(Attribute *)));
for (unsigned i = 0; i < len; i++) {
attrs[i] = other.attrs[i]->clone();
attrs[i]->ref();
}
for(unsigned i = 0; i < len; i++)
element->attributeChanged(attrs[i], true);
return *this;
}
void NamedAttrMap::addAttribute(Attribute *attribute)
{
Attribute **newAttrs = static_cast<Attribute **>(fastMalloc((len + 1) * sizeof(Attribute *)));
if (attrs) {
for (unsigned i = 0; i < len; i++)
newAttrs[i] = attrs[i];
fastFree(attrs);
}
attrs = newAttrs;
attrs[len++] = attribute;
attribute->ref();
Attr * const attr = attribute->attr();
if (attr)
attr->m_element = element;
if (element) {
RefPtr<Attribute> a = attribute;
element->attributeChanged(a.get());
if (a->name() != styleAttr) {
element->dispatchAttrAdditionEvent(a.get());
element->dispatchSubtreeModifiedEvent(false);
}
}
}
void NamedAttrMap::removeAttribute(const QualifiedName& name)
{
unsigned index = len+1;
for (unsigned i = 0; i < len; ++i)
if (attrs[i]->name().matches(name)) {
index = i;
break;
}
if (index >= len) return;
Attribute* attr = attrs[index];
if (attrs[index]->attr())
attrs[index]->attr()->m_element = 0;
if (len == 1) {
fastFree(attrs);
attrs = 0;
len = 0;
} else {
Attribute **newAttrs = static_cast<Attribute **>(fastMalloc((len - 1) * sizeof(Attribute *)));
unsigned i;
for (i = 0; i < unsigned(index); i++)
newAttrs[i] = attrs[i];
len--;
for (; i < len; i++)
newAttrs[i] = attrs[i+1];
fastFree(attrs);
attrs = newAttrs;
}
if (element && !attr->m_value.isNull()) {
AtomicString value = attr->m_value;
attr->m_value = nullAtom;
element->attributeChanged(attr);
attr->m_value = value;
}
if (element) {
element->dispatchAttrRemovalEvent(attr);
element->dispatchSubtreeModifiedEvent(false);
}
attr->deref();
}
bool NamedAttrMap::mapsEquivalent(const NamedAttrMap* otherMap) const
{
if (!otherMap)
return false;
if (length() != otherMap->length())
return false;
for (unsigned i = 0; i < length(); i++) {
Attribute *attr = attributeItem(i);
Attribute *otherAttr = otherMap->getAttributeItem(attr->name());
if (!otherAttr || attr->value() != otherAttr->value())
return false;
}
return true;
}
}