# khtml Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# dom_node.cpp

    00001
    00023 #include "dom/dom_doc.h"
    00024 #include "dom/dom_exception.h"
    00025 #include "dom/dom2_events.h"
    00026 #include "xml/dom_docimpl.h"
    00027 #include "xml/dom_elementimpl.h"
    00028 #include "xml/dom2_eventsimpl.h"
    00029
    00030 #include <qrect.h>
    00031
    00032 using namespace DOM;
    00033
    00034 NamedNodeMap::NamedNodeMap()
    00035 {
    00036     impl = 0;
    00037 }
    00038
    00039 NamedNodeMap::NamedNodeMap(const NamedNodeMap &other)
    00040 {
    00041     impl = other.impl;
    00042     if (impl) impl->ref();
    00043 }
    00044
    00045 NamedNodeMap::NamedNodeMap(NamedNodeMapImpl *i)
    00046 {
    00047     impl = i;
    00048     if (impl) impl->ref();
    00049 }
    00050
    00051 NamedNodeMap &NamedNodeMap::operator = (const NamedNodeMap &other)
    00052 {
    00053     if ( impl != other.impl ) {
    00054         if(impl) impl->deref();
    00055         impl = other.impl;
    00056         if(impl) impl->ref();
    00057     }
    00058     return *this;
    00059 }
    00060
    00061 NamedNodeMap::~NamedNodeMap()
    00062 {
    00063     if(impl) impl->deref();
    00064 }
    00065
    00066 Node NamedNodeMap::getNamedItem( const DOMString &name ) const
    00067 {
    00068     if (!impl) return 0;
    00069     NodeImpl::Id nid = impl->mapId(0, name.implementation(), true);
    00070     if (!nid) return 0;
    00071     return impl->getNamedItem(nid, false, name.implementation());
    00072 }
    00073
    00074 Node NamedNodeMap::setNamedItem( const Node &arg )
    00075 {
    00076     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00077     if (!arg.impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00078     int exceptioncode = 0;
    00079     Node r = impl->setNamedItem(arg.impl, false,
    00080                        arg.impl->nodeName().implementation(), exceptioncode);
    00081     if (exceptioncode)
    00082         throw DOMException(exceptioncode);
    00083     return r;
    00084 }
    00085
    00086 Node NamedNodeMap::removeNamedItem( const DOMString &name )
    00087 {
    00088     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00089     int exceptioncode = 0;
    00090     Node r = impl->removeNamedItem(impl->mapId(0, name.implementation(), false),
    00091                                    false, name.implementation(), exceptioncode);
    00092     if (exceptioncode)
    00093         throw DOMException(exceptioncode);
    00094     return r;
    00095 }
    00096
    00097 Node NamedNodeMap::item( unsigned long index ) const
    00098 {
    00099     if (!impl) return 0;
    00100     return impl->item(index);
    00101 }
    00102
    00103 Node NamedNodeMap::getNamedItemNS( const DOMString &namespaceURI, const DOMString &localName ) const
    00104 {
    00105     if (!impl) return 0;
    00106     NodeImpl::Id nid = impl->mapId( namespaceURI.implementation(), localName.implementation(), true );
    00107     return impl->getNamedItem(nid, true);
    00108 }
    00109
    00110 Node NamedNodeMap::setNamedItemNS( const Node &arg )
    00111 {
    00112     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00113     int exceptioncode = 0;
    00114     Node r = impl->setNamedItem(arg.impl, true, 0, exceptioncode);
    00115     if (exceptioncode)
    00116         throw DOMException(exceptioncode);
    00117     return r;
    00118 }
    00119
    00120 Node NamedNodeMap::removeNamedItemNS( const DOMString &namespaceURI, const DOMString &localName )
    00121 {
    00122     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00123     int exceptioncode = 0;
    00124     NodeImpl::Id nid = impl->mapId( namespaceURI.implementation(), localName.implementation(), false );
    00125     Node r = impl->removeNamedItem(nid, true, 0, exceptioncode);
    00126     if (exceptioncode)
    00127         throw DOMException(exceptioncode);
    00128     return r;
    00129 }
    00130
    00131 unsigned long NamedNodeMap::length() const
    00132 {
    00133     if (!impl) return 0;
    00134     return impl->length();
    00135 }
    00136
    00137 // ---------------------------------------------------------------------------
    00138
    00139 Node::Node(const Node &other)
    00140 {
    00141     impl = other.impl;
    00142     if(impl) impl->ref();
    00143 }
    00144
    00145 Node::Node( NodeImpl *i )
    00146 {
    00147     impl = i;
    00148     if(impl) impl->ref();
    00149 }
    00150
    00151 Node &Node::operator = (const Node &other)
    00152 {
    00153     if(impl != other.impl) {
    00154         if(impl) impl->deref();
    00155         impl = other.impl;
    00156         if(impl) impl->ref();
    00157     }
    00158     return *this;
    00159 }
    00160
    00161 bool Node::operator == (const Node &other) const
    00162 {
    00163     return (impl == other.impl);
    00164 }
    00165
    00166 bool Node::operator != (const Node &other) const
    00167 {
    00168     return !(impl == other.impl);
    00169 }
    00170
    00171 Node::~Node()
    00172 {
    00173     if(impl) impl->deref();
    00174 }
    00175
    00176 DOMString Node::nodeName() const
    00177 {
    00178     if(impl) return impl->nodeName();
    00179     return DOMString();
    00180 }
    00181
    00182 DOMString Node::nodeValue() const
    00183 {
    00184     // ### should throw exception on plain node ?
    00185     if(impl) return impl->nodeValue();
    00186     return DOMString();
    00187 }
    00188
    00189 void Node::setNodeValue( const DOMString &_str )
    00190 {
    00191     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00192
    00193     int exceptioncode = 0;
    00194     if(impl) impl->setNodeValue( _str,exceptioncode );
    00195     if (exceptioncode)
    00196     throw DOMException(exceptioncode);
    00197 }
    00198
    00199 unsigned short Node::nodeType() const
    00200 {
    00201     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00202     return impl->nodeType();
    00203 }
    00204
    00205 Node Node::parentNode() const
    00206 {
    00207     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00208     return impl->parentNode();
    00209 }
    00210
    00211 NodeList Node::childNodes() const
    00212 {
    00213     if (!impl) return 0;
    00214     return impl->childNodes();
    00215 }
    00216
    00217 Node Node::firstChild() const
    00218 {
    00219     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00220     return impl->firstChild();
    00221 }
    00222
    00223 Node Node::lastChild() const
    00224 {
    00225     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00226     return impl->lastChild();
    00227 }
    00228
    00229 Node Node::previousSibling() const
    00230 {
    00231     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00232     return impl->previousSibling();
    00233 }
    00234
    00235 Node Node::nextSibling() const
    00236 {
    00237     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00238     return impl->nextSibling();
    00239 }
    00240
    00241 NamedNodeMap Node::attributes() const
    00242 {
    00243     if (!impl || !impl->isElementNode()) return 0;
    00244     return static_cast<ElementImpl*>(impl)->attributes();
    00245 }
    00246
    00247 Document Node::ownerDocument() const
    00248 {
    00249     // braindead DOM spec says that ownerDocument
    00250     // should return null if called on the document node
    00251     // we don't do that in the *impl tree to avoid excessive if()'s
    00252     // so we simply hack it here in one central place.
    00253     if (!impl || impl->getDocument() == impl) return Document(false);
    00254
    00255     return impl->getDocument();
    00256 }
    00257
    00258 Node Node::insertBefore( const Node &newChild, const Node &refChild )
    00259 {
    00260     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00261     int exceptioncode = 0;
    00262     NodeImpl *r = impl->insertBefore( newChild.impl, refChild.impl, exceptioncode );
    00263     if (exceptioncode)
    00264     throw DOMException(exceptioncode);
    00265     if (!newChild.impl->closed()) newChild.impl->close();
    00266     return r;
    00267 }
    00268
    00269 Node Node::replaceChild( const Node &newChild, const Node &oldChild )
    00270 {
    00271     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00272     int exceptioncode = 0;
    00273     NodeImpl *r = impl->replaceChild( newChild.impl, oldChild.impl, exceptioncode );
    00274     if (exceptioncode)
    00275     throw DOMException(exceptioncode);
    00276     if (!newChild.impl->closed()) newChild.impl->close();
    00277     return r;
    00278 }
    00279
    00280 Node Node::removeChild( const Node &oldChild )
    00281 {
    00282     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00283     int exceptioncode = 0;
    00284     NodeImpl *r = impl->removeChild( oldChild.impl, exceptioncode );
    00285     if (exceptioncode)
    00286     throw DOMException(exceptioncode);
    00287     return r;
    00288 }
    00289
    00290 Node Node::appendChild( const Node &newChild )
    00291 {
    00292     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00293     int exceptioncode = 0;
    00294     NodeImpl *r = impl->appendChild( newChild.impl, exceptioncode );
    00295     if (exceptioncode)
    00296     throw DOMException(exceptioncode);
    00297     if (!newChild.impl->closed()) newChild.impl->close();
    00298     return r;
    00299 }
    00300
    00301 bool Node::hasAttributes()
    00302 {
    00303     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00304     if (!impl->isElementNode()) return false;
    00305     ElementImpl* e = static_cast<ElementImpl*>(impl);
    00306     return e->attributes(true) && e->attributes(true)->length();
    00307 }
    00308
    00309 bool Node::hasChildNodes(  )
    00310 {
    00311     if (!impl) return false;
    00312     return impl->hasChildNodes();
    00313 }
    00314
    00315 Node Node::cloneNode( bool deep )
    00316 {
    00317     if (!impl) return 0;
    00318     return impl->cloneNode( deep  );
    00319 }
    00320
    00321 void Node::normalize (  )
    00322 {
    00323     if (!impl) return;
    00324     impl->normalize();
    00325 }
    00326
    00327 bool Node::isSupported( const DOMString &feature,
    00328                         const DOMString & /*version*/ ) const
    00329 {
    00330     DOMString upFeature = feature.upper();
    00331     return (upFeature == "HTML" ||
    00332             upFeature == "XML" ||
    00333             upFeature == "CORE");
    00334 }
    00335
    00336 DOMString Node::namespaceURI(  ) const
    00337 {
    00338     if (!impl) return DOMString();
    00339     return impl->namespaceURI();
    00340 }
    00341
    00342 DOMString Node::prefix(  ) const
    00343 {
    00344     if (!impl) return DOMString();
    00345     return impl->prefix();
    00346 }
    00347
    00348 void Node::setPrefix(const DOMString &prefix )
    00349 {
    00350     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00351     int exceptioncode = 0;
    00352     impl->setPrefix(prefix,exceptioncode);
    00353     if (exceptioncode)
    00354         throw DOMException(exceptioncode);
    00355 }
    00356
    00357 DOMString Node::localName(  ) const
    00358 {
    00359     if (!impl) return DOMString();
    00360     return impl->localName();
    00361 }
    00362
    00363 void Node::addEventListener(const DOMString &type,
    00364               EventListener *listener,
    00365               const bool useCapture)
    00366 {
    00367     if (!impl) return;
    00368     if (listener)
    00369         impl->addEventListener(EventImpl::typeToId(type),listener,useCapture);
    00370 }
    00371
    00372 void Node::removeEventListener(const DOMString &type,
    00373                  EventListener *listener,
    00374                  bool useCapture)
    00375 {
    00376     if (!impl) return;
    00377     impl->removeEventListener(EventImpl::typeToId(type),listener,useCapture);
    00378 }
    00379
    00380 bool Node::dispatchEvent(const Event &evt)
    00381 {
    00382     if (!impl)
    00383     throw DOMException(DOMException::INVALID_STATE_ERR);
    00384
    00385     if (!evt.handle())
    00386         throw DOMException(DOMException::NOT_FOUND_ERR);
    00387
    00388     int exceptioncode = 0;
    00389     impl->dispatchEvent(evt.handle(),exceptioncode);
    00390     if (exceptioncode)
    00391     throw DOMException(exceptioncode);
    00392     return !evt.handle()->defaultPrevented();
    00393 }
    00394
    00395
    00396 unsigned int Node::elementId() const
    00397 {
    00398     if (!impl) return 0;
    00399     return impl->id();
    00400 }
    00401
    00402 unsigned long Node::index() const
    00403 {
    00404     if (!impl) return 0;
    00405     return impl->nodeIndex();
    00406 }
    00407
    00408 QString Node::toHTML()
    00409 {
    00410     if (!impl) return QString::null;
    00411     return impl->toString().string();
    00412 }
    00413
    00414 void Node::applyChanges()
    00415 {
    00416     if (!impl) return;
    00417     impl->recalcStyle( NodeImpl::Inherit );
    00418 }
    00419
    00420 void Node::getCursor(int offset, int &_x, int &_y, int &height)
    00421 {
    00422     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00423     int dummy;
    00424     impl->getCaret(offset, false, _x, _y, dummy, height);
    00425 }
    00426
    00427 QRect Node::getRect()
    00428 {
    00429     if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR);
    00430     return impl->getRect();
    00431 }
    00432
    00433 //-----------------------------------------------------------------------------
    00434
    00435 NodeList::NodeList()
    00436 {
    00437     impl = 0;
    00438 }
    00439
    00440 NodeList::NodeList(const NodeList &other)
    00441 {
    00442     impl = other.impl;
    00443     if(impl) impl->ref();
    00444 }
    00445
    00446 NodeList::NodeList(const NodeListImpl *i)
    00447 {
    00448     impl = const_cast<NodeListImpl *>(i);
    00449     if(impl) impl->ref();
    00450 }
    00451
    00452 NodeList &NodeList::operator = (const NodeList &other)
    00453 {
    00454     if ( impl != other.impl ) {
    00455         if(impl) impl->deref();
    00456         impl = other.impl;
    00457         if(impl) impl->ref();
    00458     }
    00459     return *this;
    00460 }
    00461
    00462 NodeList::~NodeList()
    00463 {
    00464     if(impl) impl->deref();
    00465 }
    00466
    00467 Node NodeList::item( unsigned long index ) const
    00468 {
    00469     if (!impl) return 0;
    00470     return impl->item(index);
    00471 }
    00472
    00473 unsigned long NodeList::length() const
    00474 {
    00475     if (!impl) return 0;
    00476     return impl->length();
    00477 }

This file is part of the documentation for khtml Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:33:10 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003