dom_element.cpp
#include "dom/dom_exception.h"
#include "xml/dom_docimpl.h"
#include "xml/dom_elementimpl.h"
#include "html/html_formimpl.h"
using namespace DOM;
Attr::Attr() : Node()
{
}
Attr::Attr(const Attr &other) : Node(other)
{
}
Attr::Attr( AttrImpl *_impl )
{
impl= _impl;
if (impl) impl->ref();
}
Attr &Attr::operator = (const Node &other)
{
NodeImpl* ohandle = other.handle();
if ( impl != ohandle ) {
if (!ohandle || !ohandle->isAttributeNode()) {
if (impl) impl->deref();
impl = 0;
} else {
Node::operator =(other);
}
}
return *this;
}
Attr &Attr::operator = (const Attr &other)
{
Node::operator =(other);
return *this;
}
Attr::~Attr()
{
}
DOMString Attr::name() const
{
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
return ((AttrImpl *)impl)->name();
}
bool Attr::specified() const
{
if (impl) return ((AttrImpl *)impl)->specified();
return 0;
}
Element Attr::ownerElement() const
{
if (!impl) return 0;
return static_cast<AttrImpl*>(impl)->ownerElement();
}
DOMString Attr::value() const
{
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
return impl->nodeValue();
}
void Attr::setValue( const DOMString &newValue )
{
if (!impl)
return;
int exceptioncode = 0;
((AttrImpl *)impl)->setValue(newValue,exceptioncode);
if (exceptioncode)
throw DOMException(exceptioncode);
}
// ---------------------------------------------------------------------------
Element::Element() : Node()
{
}
Element::Element(const Element &other) : Node(other)
{
}
Element::Element(ElementImpl *impl) : Node(impl)
{
}
Element &Element::operator = (const Node &other)
{
NodeImpl* ohandle = other.handle();
if ( impl != ohandle ) {
if (!ohandle || !ohandle->isElementNode()) {
if (impl) impl->deref();
impl = 0;
} else {
Node::operator =(other);
}
}
return *this;
}
Element &Element::operator = (const Element &other)
{
Node::operator =(other);
return *this;
}
Element::~Element()
{
}
DOMString Element::tagName() const
{
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
return static_cast<ElementImpl*>(impl)->tagName();
}
DOMString Element::getAttribute( const DOMString &name )
{
// ### getAttribute() and getAttributeNS() are supposed to return the empty string if the attribute
// does not exist. However, there are a number of places around khtml that expect a null string
// for nonexistent attributes. These need to be changed to use hasAttribute() instead.
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
if (!name.implementation()) throw DOMException(DOMException::NOT_FOUND_ERR);
NodeImpl::Id id = impl->getDocument()->getId(NodeImpl::AttributeId,name.implementation(),true,true);
if (!id) return DOMString();
ElementImpl* e = static_cast<ElementImpl*>(impl);
return e->getAttribute(id, false, name);
}
void Element::setAttribute( const DOMString &name, const DOMString &value )
{
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
int exceptioncode = 0;
NodeImpl::Id id = impl->getDocument()->getId(NodeImpl::AttributeId, name.implementation(), false /* allocate */,
true, &exceptioncode);
static_cast<ElementImpl*>(impl)->setAttribute(id, value, name, exceptioncode);
if ( exceptioncode )
throw DOMException( exceptioncode );
}
void Element::removeAttribute( const DOMString &name )
{
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
NodeImpl::Id id = impl->getDocument()->getId(NodeImpl::AttributeId, name.implementation(), true, true);
if (!id) return;
int exceptioncode = 0;
NamedNodeMapImpl *attributes = static_cast<ElementImpl*>(impl)->attributes(false);
attributes->removeNamedItem(id, false, name.implementation(), exceptioncode);
// it's allowed to remove attributes that don't exist.
if ( exceptioncode && exceptioncode != DOMException::NOT_FOUND_ERR )
throw DOMException( exceptioncode );
}
Attr Element::getAttributeNode( const DOMString &name )
{
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
if (!name.implementation()) throw DOMException(DOMException::NOT_FOUND_ERR);
NodeImpl::Id id = impl->getDocument()->getId(NodeImpl::AttributeId, name.implementation(), true, true);
if (!id) return 0;
ElementImpl* e = static_cast<ElementImpl*>(impl);
if (!e->attributes()) return 0;
return static_cast<AttrImpl*>(e->attributes()->getNamedItem(id, false, name.implementation()));
}
Attr Element::setAttributeNode( const Attr &newAttr )
{
if (!impl || newAttr.isNull())
throw DOMException(DOMException::NOT_FOUND_ERR);
// WRONG_DOCUMENT_ERR and INUSE_ATTRIBUTE_ERR are already tested & thrown by setNamedItem
int exceptioncode = 0;
Attr r = static_cast<ElementImpl*>(impl)->attributes(false)->setNamedItem(newAttr.handle(), false,
newAttr.handle()->nodeName().implementation(), exceptioncode);
if ( exceptioncode )
throw DOMException( exceptioncode );
static_cast<AttrImpl *>(newAttr.handle())->setOwnerElement( static_cast<ElementImpl*>(impl) );
return r;
}
Attr Element::removeAttributeNode( const Attr &oldAttr )
{
if (!impl || oldAttr.isNull() || oldAttr.ownerElement().handle() != impl)
throw DOMException(DOMException::NOT_FOUND_ERR);
if (impl->isReadOnly())
throw DOMException(DOMException::NO_MODIFICATION_ALLOWED_ERR);
if (!static_cast<ElementImpl*>(impl)->attributes(true))
throw DOMException(DOMException::NOT_FOUND_ERR);
NamedAttrMapImpl *attributes = static_cast<ElementImpl*>(impl)->attributes(false);
return attributes->removeAttr(static_cast<AttrImpl*>(static_cast<AttrImpl*>(oldAttr.handle())));
}
NodeList Element::getElementsByTagName( const DOMString &tagName )
{
if (!impl) return 0;
NodeImpl::Id id;
if ( tagName == "*" )
id = 0;
else
id = impl->getDocument()->getId(NodeImpl::ElementId, tagName.implementation(), false, true);
return new TagNodeListImpl( impl, id );
}
NodeList Element::getElementsByTagNameNS( const DOMString &namespaceURI,
const DOMString &localName )
{
if (!impl) return 0;
return new TagNodeListImpl( impl, namespaceURI, localName );
}
DOMString Element::getAttributeNS( const DOMString &namespaceURI,
const DOMString &localName)
{
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
if (!localName.implementation()) throw DOMException(DOMException::NOT_FOUND_ERR);
NodeImpl::Id id = impl->getDocument()->getId(NodeImpl::AttributeId, namespaceURI.implementation(), 0/*prefix*/, localName.implementation(), true, true);
ElementImpl* e = static_cast<ElementImpl*>(impl);
return e->getAttribute(id, true);
}
void Element::setAttributeNS( const DOMString &namespaceURI,
const DOMString &qualifiedName,
const DOMString &value)
{
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
int exceptioncode = 0;
static_cast<ElementImpl*>(impl)->setAttributeNS(namespaceURI, qualifiedName, value, exceptioncode);
if ( exceptioncode )
throw DOMException( exceptioncode );
}
void Element::removeAttributeNS( const DOMString &namespaceURI,
const DOMString &localName )
{
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
int exceptioncode = 0;
NamedNodeMapImpl *attributes = static_cast<ElementImpl*>(impl)->attributes(false);
NodeImpl::Id id = impl->getDocument()->getId(NodeImpl::AttributeId, namespaceURI.implementation(), 0/*prefix*/, localName.implementation(), false, true);
attributes->removeNamedItem(id, true, 0, exceptioncode);
if ( exceptioncode )
throw DOMException( exceptioncode );
}
Attr Element::getAttributeNodeNS( const DOMString &namespaceURI,
const DOMString &localName )
{
if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
if (!localName.implementation()) throw DOMException(DOMException::NOT_FOUND_ERR);
NodeImpl::Id id = impl->getDocument()->getId(NodeImpl::AttributeId, namespaceURI.implementation(),
0/*prefix*/, localName.implementation(), true, true);
ElementImpl* e = static_cast<ElementImpl*>(impl);
if (!e->attributes()) return 0;
return static_cast<AttrImpl*>(e->attributes()->getNamedItem(id, true));
}
Attr Element::setAttributeNodeNS( const Attr &newAttr )
{
if (!impl || newAttr.isNull())
throw DOMException(DOMException::NOT_FOUND_ERR);
// WRONG_DOCUMENT_ERR and INUSE_ATTRIBUTE_ERR are already tested & thrown by setNamedItem
int exceptioncode = 0;
Attr r = static_cast<ElementImpl*>(impl)->attributes(false)->setNamedItem(newAttr.handle(), true, 0, exceptioncode);
if ( exceptioncode )
throw DOMException( exceptioncode );
static_cast<AttrImpl *>(newAttr.handle())->setOwnerElement( static_cast<ElementImpl*>(impl) );
return r;
}
bool Element::hasAttribute( const DOMString& name )
{
if (!impl || !static_cast<ElementImpl*>(impl)->attributes()) return false; // ### throw ?
NodeImpl::Id id = impl->getDocument()->getId(NodeImpl::AttributeId, name.implementation(), true, true);
if (!id) return false;
if (!static_cast<ElementImpl*>(impl)->attributes(true /*readonly*/)) return false;
return static_cast<ElementImpl*>(impl)->attributes(true)->getValue(id, false, name.implementation()) != 0;
}
bool Element::hasAttributeNS( const DOMString &namespaceURI,
const DOMString &localName )
{
if (!impl || !static_cast<ElementImpl*>(impl)->attributes()) return false; // ### throw ?
if (!static_cast<ElementImpl*>(impl)->attributes(true /*readonly*/)) return false;
NodeImpl::Id id = impl->getDocument()->getId(NodeImpl::AttributeId,namespaceURI.implementation(),
0/*prefix*/, localName.implementation(), true, true);
return static_cast<ElementImpl*>(impl)->attributes(true)->getValue(id, true) != 0;
}
bool Element::isHTMLElement() const
{
if(!impl) return false;
return ((ElementImpl *)impl)->isHTMLElement();
}
Element Element::form() const
{
if (!impl || !impl->isGenericFormElement()) return 0;
return static_cast<HTMLGenericFormElementImpl*>(impl)->form();
ElementImpl* f = static_cast<HTMLGenericFormElementImpl*>( impl )->form();
if( f && f->implicitNode() )
return 0;
return f;
}
CSSStyleDeclaration Element::style()
{
if (impl) return ((ElementImpl *)impl)->styleRules();
return 0;
}
bool Element::contentEditable() const {
if(!impl) return false;
return static_cast<ElementImpl *>(impl)->contentEditable();
}
void Element::setContentEditable(bool enabled) {
if(!impl)
throw DOMException(DOMException::INVALID_STATE_ERR);
static_cast<ElementImpl *>(impl)->setContentEditable(enabled);
}
bool Element::khtmlValidAttrName(const DOMString &name)
{
// Check if name is valid
// http://www.w3.org/TR/2000/REC-xml-20001006#NT-Name
DOMStringImpl* _name = name.implementation();
QChar ch = _name->s[0];
if ( !ch.isLetter() && ch != '_' && ch != ':' )
return false; // first char isn't valid
for ( uint i = 0; i < _name->l; ++i )
{
ch = _name->s[i];
if ( !ch.isLetter() && !ch.isDigit() && ch != '.'
&& ch != '-' && ch != '_' && ch != ':'
&& ch.category() != QChar::Mark_SpacingCombining
/* no idea what "extender is" */ )
return false;
}
return true;
}
bool Element::khtmlValidPrefix(const DOMString &name)
{
// Null prefix is ok. If not null, reuse code from khtmlValidAttrName
return !name.implementation() || khtmlValidAttrName(name);
}
bool Element::khtmlValidQualifiedName(const DOMString &name)
{
return khtmlValidAttrName(name);
}
bool Element::khtmlMalformedQualifiedName(const DOMString &name)
{
// #### Not clearly defined in the DOM spec...
// But we know for sure that a null qualified name is malformed
return name.isNull();
}
bool Element::khtmlMalformedPrefix(const DOMString &/*name*/)
{
// ####
return false;
}
DOM::Element::getElementsByTagNameNS
NodeList getElementsByTagNameNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2 Returns a NodeList of all the descendant Elements with a given local name a...
Definition: dom_element.cpp:242
DOM::Element::tagName
DOMString tagName() const
The name of the element.
Definition: dom_element.cpp:141
DOM::Node
The Node interface is the primary datatype for the entire Document Object Model.
Definition: dom_node.h:274
DOM::NamedNodeMap::removeNamedItem
Node removeNamedItem(const DOMString &name)
Removes a node specified by name.
Definition: dom_node.cpp:86
DOM::Element::style
CSSStyleDeclaration style()
Introduced in DOM Level 2 This method is from the CSSStyleDeclaration interface.
Definition: dom_element.cpp:349
DOM::Element::getAttributeNodeNS
Attr getAttributeNodeNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2.
Definition: dom_element.cpp:284
DOM::CSSStyleDeclaration
The CSSStyleDeclaration interface represents a single CSS declaration block .
Definition: css_value.h:60
DOM::Attr::value
DOMString value() const
On retrieval, the value of the attribute is returned as a string.
Definition: dom_element.cpp:86
DOM::Element::contentEditable
bool contentEditable() const
not part of the official DOM
Definition: dom_element.cpp:355
DOM::Node::attributes
NamedNodeMap attributes() const
A NamedNodeMap containing the attributes of this node (if it is an Element ) or null otherwise...
Definition: dom_node.cpp:241
DOM::Element::setAttributeNS
void setAttributeNS(const DOMString &namespaceURI, const DOMString &qualifiedName, const DOMString &value)
Introduced in DOM Level 2.
Definition: dom_element.cpp:259
DOM::DOMException
DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation is impos...
Definition: dom_exception.h:57
DOM::Element::getElementsByTagName
NodeList getElementsByTagName(const DOMString &name)
Returns a NodeList of all descendant elements with a given tag name, in the order in which they would...
Definition: dom_element.cpp:231
DOM::Element::setAttributeNodeNS
Attr setAttributeNodeNS(const Attr &newAttr)
Introduced in DOM Level 2.
Definition: dom_element.cpp:297
DOM::Element::removeAttribute
void removeAttribute(const DOMString &name)
Removes an attribute by name.
Definition: dom_element.cpp:174
DOM::Element::setAttribute
void setAttribute(const DOMString &name, const DOMString &value)
Adds a new attribute.
Definition: dom_element.cpp:162
DOM::Element
By far the vast majority of objects (apart from text) that authors encounter when traversing a docume...
Definition: dom_element.h:210
DOM::Attr
The Attr interface represents an attribute in an Element object.
Definition: dom_element.h:89
DOM::Element::hasAttributeNS
bool hasAttributeNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2.
Definition: dom_element.cpp:322
DOM::Node::isNull
bool isNull() const
tests if this Node is 0.
Definition: dom_node.h:892
DOM::DOMString
This class implements the basic string we use in the DOM.
Definition: dom_string.h:43
DOM::Attr::setValue
void setValue(const DOMString &)
see value
Definition: dom_element.cpp:92
DOM::Attr::specified
bool specified() const
If this attribute was explicitly given a value in the original document, this is true ; otherwise...
Definition: dom_element.cpp:74
DOM::NodeList
The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented.
Definition: dom_node.h:931
DOM::Attr::ownerElement
Element ownerElement() const
Introduced in DOM Level 2.
Definition: dom_element.cpp:80
DOM::Element::removeAttributeNS
void removeAttributeNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2.
Definition: dom_element.cpp:271
DOM::Element::getAttribute
DOMString getAttribute(const DOMString &name)
Retrieves an attribute value by name.
Definition: dom_element.cpp:147
DOM::Element::getAttributeNS
DOMString getAttributeNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2.
Definition: dom_element.cpp:249
DOM::Element::form
Element form() const
KHTML extension to DOM This method returns the associated form element.
Definition: dom_element.cpp:338
DOM::Element::hasAttribute
bool hasAttribute(const DOMString &name)
Returns true when an attribute with a given name is specified on this element or has a default value...
Definition: dom_element.cpp:312
DOM::Attr::name
DOMString name() const
Returns the name of this attribute.
Definition: dom_element.cpp:68
DOM::Element::setAttributeNode
Attr setAttributeNode(const Attr &newAttr)
Adds a new attribute.
Definition: dom_element.cpp:201
DOM::Element::getAttributeNode
Attr getAttributeNode(const DOMString &name)
Retrieves an Attr node by name.
Definition: dom_element.cpp:188
DOM::Element::setContentEditable
void setContentEditable(bool enabled)
not part of the official DOM
Definition: dom_element.cpp:360
DOM::NamedNodeMap::setNamedItem
Node setNamedItem(const Node &arg)
Adds a node using its nodeName attribute.
Definition: dom_node.cpp:74
DOM::Element::removeAttributeNode
Attr removeAttributeNode(const Attr &oldAttr)
Removes the specified attribute.
Definition: dom_element.cpp:216

KDE Home | KDE Accessibility Home | Description of Access Keys