#include "config.h"
#include "Attr.h"
#include "Document.h"
#include "Element.h"
#include "ExceptionCode.h"
#include "Text.h"
namespace WebCore {
Attr::Attr(Element* element, Document* docPtr, Attribute* a)
: ContainerNode(docPtr),
m_element(element),
m_attribute(a),
m_ignoreChildrenChanged(0)
{
assert(!m_attribute->attr());
m_attribute->m_impl = this;
m_specified = true;
}
Attr::~Attr()
{
assert(m_attribute->attr() == this);
m_attribute->m_impl = 0;
}
void Attr::createTextChild()
{
assert(refCount());
if (!m_attribute->value().isEmpty()) {
ExceptionCode ec = 0;
m_ignoreChildrenChanged++;
appendChild(document()->createTextNode(m_attribute->value().impl()), ec);
m_ignoreChildrenChanged--;
}
}
String Attr::nodeName() const
{
return name();
}
Node::NodeType Attr::nodeType() const
{
return ATTRIBUTE_NODE;
}
const AtomicString& Attr::localName() const
{
return m_attribute->localName();
}
const AtomicString& Attr::namespaceURI() const
{
return m_attribute->namespaceURI();
}
const AtomicString& Attr::prefix() const
{
return m_attribute->prefix();
}
void Attr::setPrefix(const AtomicString &_prefix, ExceptionCode& ec)
{
checkSetPrefix(_prefix, ec);
if (ec)
return;
m_attribute->setPrefix(_prefix);
}
String Attr::nodeValue() const
{
return value();
}
void Attr::setValue( const String& v, ExceptionCode& ec)
{
ec = 0;
if (isReadOnlyNode()) {
ec = NO_MODIFICATION_ALLOWED_ERR;
return;
}
if (v.isNull()) {
ec = DOMSTRING_SIZE_ERR;
return;
}
int e = 0;
m_ignoreChildrenChanged++;
removeChildren();
appendChild(document()->createTextNode(v.impl()), e);
m_ignoreChildrenChanged--;
m_attribute->setValue(v.impl());
if (m_element)
m_element->attributeChanged(m_attribute.get());
}
void Attr::setNodeValue(const String& v, ExceptionCode& ec)
{
setValue(v, ec);
}
PassRefPtr<Node> Attr::cloneNode(bool )
{
RefPtr<Attr> clone = new Attr(0, document(), m_attribute->clone());
cloneChildNodes(clone.get());
return clone.release();
}
bool Attr::childTypeAllowed(NodeType type)
{
switch (type) {
case TEXT_NODE:
case ENTITY_REFERENCE_NODE:
return true;
default:
return false;
}
}
void Attr::childrenChanged()
{
Node::childrenChanged();
if (m_ignoreChildrenChanged > 0)
return;
String val = "";
for (Node *n = firstChild(); n; n = n->nextSibling()) {
if (n->isTextNode())
val += static_cast<Text *>(n)->data();
}
m_attribute->setValue(val.impl());
if (m_element)
m_element->attributeChanged(m_attribute.get());
}
String Attr::toString() const
{
String result;
result += nodeName();
if (firstChild() != NULL) {
result += "=\"";
for (Node *child = firstChild(); child != NULL; child = child->nextSibling()) {
result += child->toString();
}
result += "\"";
}
return result;
}
}