khtml Library API Documentation

dom_element.cpp

00001 
00024 #include "dom/dom_exception.h"
00025 #include "xml/dom_docimpl.h"
00026 #include "xml/dom_elementimpl.h"
00027 #include "html/html_formimpl.h"
00028 #include <kdebug.h>
00029 
00030 using namespace DOM;
00031 
00032 Attr::Attr() : Node()
00033 {
00034 }
00035 
00036 Attr::Attr(const Attr &other) : Node(other)
00037 {
00038 }
00039 
00040 Attr::Attr( AttrImpl *_impl )
00041 {
00042     impl= _impl;
00043     if (impl) impl->ref();
00044 }
00045 
00046 Attr &Attr::operator = (const Node &other)
00047 {
00048     NodeImpl* ohandle = other.handle();
00049     if ( impl != ohandle ) {
00050         if (!ohandle || !ohandle->isAttributeNode()) {
00051             if (impl) impl->deref();
00052             impl = 0;
00053         } else {
00054             Node::operator =(other);
00055         }
00056     }
00057     return *this;
00058 }
00059 
00060 Attr &Attr::operator = (const Attr &other)
00061 {
00062     Node::operator =(other);
00063     return *this;
00064 }
00065 
00066 Attr::~Attr()
00067 {
00068 }
00069 
00070 DOMString Attr::name() const
00071 {
00072     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00073     return impl->getDocument()->attrName(
00074         static_cast<AttrImpl*>(impl)->attrImpl()->id());
00075 }
00076 
00077 
00078 bool Attr::specified() const
00079 {
00080   if (impl) return ((AttrImpl *)impl)->specified();
00081   return 0;
00082 }
00083 
00084 Element Attr::ownerElement() const
00085 {
00086   if (!impl) return 0;
00087   return static_cast<AttrImpl*>(impl)->ownerElement();
00088 }
00089 
00090 DOMString Attr::value() const
00091 {
00092     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00093     return impl->nodeValue();
00094 }
00095 
00096 void Attr::setValue( const DOMString &newValue )
00097 {
00098   if (!impl)
00099     return;
00100 
00101   int exceptioncode = 0;
00102   ((AttrImpl *)impl)->setValue(newValue,exceptioncode);
00103   if (exceptioncode)
00104     throw DOMException(exceptioncode);
00105 }
00106 
00107 // ---------------------------------------------------------------------------
00108 
00109 Element::Element() : Node()
00110 {
00111 }
00112 
00113 Element::Element(const Element &other) : Node(other)
00114 {
00115 }
00116 
00117 Element::Element(ElementImpl *impl) : Node(impl)
00118 {
00119 }
00120 
00121 Element &Element::operator = (const Node &other)
00122 {
00123     NodeImpl* ohandle = other.handle();
00124     if ( impl != ohandle ) {
00125         if (!ohandle || !ohandle->isElementNode()) {
00126             if (impl) impl->deref();
00127             impl = 0;
00128         } else {
00129             Node::operator =(other);
00130         }
00131     }
00132     return *this;
00133 }
00134 
00135 Element &Element::operator = (const Element &other)
00136 {
00137     Node::operator =(other);
00138     return *this;
00139 }
00140 
00141 Element::~Element()
00142 {
00143 }
00144 
00145 DOMString Element::tagName() const
00146 {
00147     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00148     return static_cast<ElementImpl*>(impl)->tagName();
00149 }
00150 
00151 DOMString Element::getAttribute( const DOMString &name )
00152 {
00153     return getAttributeNS(DOMString(), name);
00154 }
00155 
00156 void Element::setAttribute( const DOMString &name, const DOMString &value )
00157 {
00158     setAttributeNS(DOMString(), name, value);
00159 }
00160 
00161 void Element::removeAttribute( const DOMString &name )
00162 {
00163     removeAttributeNS(DOMString(), name);
00164 }
00165 
00166 Attr Element::getAttributeNode( const DOMString &name )
00167 {
00168     return getAttributeNodeNS(DOMString(), name);
00169 }
00170 
00171 Attr Element::setAttributeNode( const Attr &newAttr )
00172 {
00173     return setAttributeNodeNS(newAttr);
00174 }
00175 
00176 Attr Element::removeAttributeNode( const Attr &oldAttr )
00177 {
00178     if (!impl || oldAttr.isNull() || oldAttr.ownerElement().handle() != impl)
00179         throw DOMException(DOMException::NOT_FOUND_ERR);
00180     if (impl->getDocument() != oldAttr.handle()->getDocument())
00181         throw DOMException(DOMException::WRONG_DOCUMENT_ERR);
00182 
00183     int exceptioncode = 0;
00184 
00185     NodeImpl::Id id = static_cast<AttrImpl *>(oldAttr.handle())->attrImpl()->id();
00186     Attr r = static_cast<ElementImpl*>(impl)->attributes(true)->removeNamedItem(id, exceptioncode);
00187     if ( exceptioncode )
00188         throw DOMException( exceptioncode );
00189     return r;
00190 }
00191 
00192 NodeList Element::getElementsByTagName( const DOMString &name )
00193 {
00194     if (!impl) return 0;
00195     return static_cast<ElementImpl*>(impl)->
00196         getElementsByTagNameNS(0, name.implementation());
00197 }
00198 
00199 NodeList Element::getElementsByTagNameNS( const DOMString &namespaceURI,
00200                                           const DOMString &localName )
00201 {
00202     if (!impl) return 0;
00203     return static_cast<ElementImpl*>(impl)->
00204         getElementsByTagNameNS(namespaceURI.implementation(), localName.implementation());
00205 }
00206 
00207 DOMString Element::getAttributeNS( const DOMString &namespaceURI,
00208                                    const DOMString &localName)
00209 {
00210     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00211     NodeImpl::Id id = impl->getDocument()->attrId(namespaceURI.implementation(),
00212                                                   localName.implementation(), true,
00213                                                   0);
00214     if (!id) return DOMString();
00215     return static_cast<ElementImpl*>(impl)->getAttribute(id);
00216 }
00217 
00218 void Element::setAttributeNS( const DOMString &namespaceURI,
00219                               const DOMString &qualifiedName,
00220                               const DOMString &value)
00221 {
00222     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00223 
00224     int colonpos = qualifiedName.find(':');
00225     DOMString localName = qualifiedName;
00226     if (colonpos >= 0) {
00227         localName.remove(0, colonpos+1);
00228         // ### extract and set new prefix
00229     }
00230     int exceptioncode = 0;
00231     NodeImpl::Id id = impl->getDocument()->attrId(namespaceURI.implementation(),
00232                                                   localName.implementation(), false /* allocate */,
00233                                                   &exceptioncode);
00234     if (exceptioncode)
00235         throw DOMException(exceptioncode);
00236     static_cast<ElementImpl*>(impl)->setAttribute(id, value.implementation(), exceptioncode);
00237     if (exceptioncode)
00238         throw DOMException(exceptioncode);
00239 }
00240 
00241 void Element::removeAttributeNS( const DOMString &namespaceURI,
00242                                  const DOMString &localName )
00243 {
00244     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00245     NodeImpl::Id id = impl->getDocument()->attrId(namespaceURI.implementation(),
00246                                                   localName.implementation(), true,
00247                                                   0);
00248     if (!id) return;
00249 
00250     int exceptioncode = 0;
00251     ((ElementImpl *)impl)->removeAttribute(id, exceptioncode);
00252     if ( exceptioncode )
00253         throw DOMException( exceptioncode );
00254 }
00255 
00256 Attr Element::getAttributeNodeNS( const DOMString &namespaceURI,
00257                                   const DOMString &localName )
00258 {
00259     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
00260     NodeImpl::Id id = impl->getDocument()->attrId(namespaceURI.implementation(),
00261                                                   localName.implementation(), true,
00262                                                   0);
00263     if (!id) return 0;
00264 
00265     ElementImpl* e = static_cast<ElementImpl*>(impl);
00266     if (!e->attributes()) return 0; // exception ?
00267 
00268     return e->attributes()->getNamedItem(id);
00269 }
00270 
00271 Attr Element::setAttributeNodeNS( const Attr &newAttr )
00272 {
00273     if (!impl || newAttr.isNull())
00274         throw DOMException(DOMException::NOT_FOUND_ERR);
00275 
00276     int exceptioncode = 0;
00277     Attr r = static_cast<ElementImpl*>(impl)->attributes(false)->setNamedItem(newAttr.handle(), exceptioncode);
00278     if ( exceptioncode )
00279         throw DOMException( exceptioncode );
00280     static_cast<AttrImpl *>(newAttr.handle())->setOwnerElement( static_cast<ElementImpl*>(impl) );
00281     return r;
00282 }
00283 
00284 
00285 bool Element::hasAttribute( const DOMString& name )
00286 {
00287     return hasAttributeNS(DOMString(), name);
00288 }
00289 
00290 bool Element::hasAttributeNS( const DOMString &namespaceURI,
00291                               const DOMString &localName )
00292 {
00293     if (!impl || !static_cast<ElementImpl*>(impl)->attributes()) return false; // ### throw ?
00294     NodeImpl::Id id = impl->getDocument()->attrId(namespaceURI.implementation(),
00295                                                   localName.implementation(), true,
00296                                                   0);
00297     if (!id) return false;
00298 
00299     if (!static_cast<ElementImpl*>(impl)->attributes(true /*readonly*/)) return false;
00300     return static_cast<ElementImpl*>(impl)->attributes(true)->getAttributeItem(id) != 0;
00301 }
00302 
00303 bool Element::isHTMLElement() const
00304 {
00305     if(!impl) return false;
00306     return ((ElementImpl *)impl)->isHTMLElement();
00307 }
00308 
00309 Element Element::form() const
00310 {
00311     if (!impl || !impl->isGenericFormElement()) return 0;
00312     return static_cast<HTMLGenericFormElementImpl*>(impl)->form();
00313 }
00314 
00315 CSSStyleDeclaration Element::style()
00316 {
00317     if (impl) return ((ElementImpl *)impl)->styleRules();
00318     return 0;
00319 }
00320 
00321 bool Element::khtmlValidAttrName(const DOMString &name)
00322 {
00323     // Check if name is valid
00324     // http://www.w3.org/TR/2000/REC-xml-20001006#NT-Name
00325     DOMStringImpl* _name = name.implementation();
00326     QChar ch = _name->s[0];
00327     if ( !ch.isLetter() && ch != '_' && ch != ':' )
00328         return false; // first char isn't valid
00329     for ( uint i = 0; i < _name->l; ++i )
00330     {
00331         ch = _name->s[i];
00332         if ( !ch.isLetter() && !ch.isDigit() && ch != '.'
00333              && ch != '-' && ch != '_' && ch != ':'
00334              && ch.category() != QChar::Mark_SpacingCombining
00335              /* no idea what "extender is" */ )
00336             return false;
00337     }
00338     return true;
00339 }
00340 
00341 bool Element::khtmlValidPrefix(const DOMString &name)
00342 {
00343     // Null prefix is ok. If not null, reuse code from khtmlValidAttrName
00344     return !name.implementation() || khtmlValidAttrName(name);
00345 }
00346 
00347 bool Element::khtmlValidQualifiedName(const DOMString &name)
00348 {
00349     return khtmlValidAttrName(name);
00350 }
00351 
00352 bool Element::khtmlMalformedQualifiedName(const DOMString &name)
00353 {
00354     // #### Not clearly defined in the DOM spec...
00355     // But we know for sure that a null qualified name is malformed
00356     return name.isNull();
00357 }
00358 
00359 bool Element::khtmlMalformedPrefix(const DOMString &/*name*/)
00360 {
00361     // ####
00362     return false;
00363 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:33:42 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001