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 impl->replaceChild( newChild.impl, oldChild.impl, exceptioncode ); 00274 if (exceptioncode) 00275 throw DOMException(exceptioncode); 00276 if (newChild.impl && !newChild.impl->closed()) newChild.impl->close(); 00277 00278 return oldChild; 00279 } 00280 00281 Node Node::removeChild( const Node &oldChild ) 00282 { 00283 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00284 int exceptioncode = 0; 00285 impl->removeChild( oldChild.impl, exceptioncode ); 00286 if (exceptioncode) 00287 throw DOMException(exceptioncode); 00288 00289 return oldChild; 00290 } 00291 00292 Node Node::appendChild( const Node &newChild ) 00293 { 00294 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00295 int exceptioncode = 0; 00296 NodeImpl *r = impl->appendChild( newChild.impl, exceptioncode ); 00297 if (exceptioncode) 00298 throw DOMException(exceptioncode); 00299 if (!newChild.impl->closed()) newChild.impl->close(); 00300 return r; 00301 } 00302 00303 bool Node::hasAttributes() 00304 { 00305 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00306 if (!impl->isElementNode()) return false; 00307 ElementImpl* e = static_cast<ElementImpl*>(impl); 00308 return e->attributes(true) && e->attributes(true)->length(); 00309 } 00310 00311 bool Node::hasChildNodes( ) 00312 { 00313 if (!impl) return false; 00314 return impl->hasChildNodes(); 00315 } 00316 00317 Node Node::cloneNode( bool deep ) 00318 { 00319 if (!impl) return 0; 00320 return impl->cloneNode( deep ); 00321 } 00322 00323 void Node::normalize ( ) 00324 { 00325 if (!impl) return; 00326 impl->normalize(); 00327 } 00328 00329 bool Node::isSupported( const DOMString &feature, 00330 const DOMString & /*version*/ ) const 00331 { 00332 DOMString upFeature = feature.upper(); 00333 return (upFeature == "HTML" || 00334 upFeature == "XML" || 00335 upFeature == "CORE"); 00336 } 00337 00338 DOMString Node::namespaceURI( ) const 00339 { 00340 if (!impl) return DOMString(); 00341 return impl->namespaceURI(); 00342 } 00343 00344 DOMString Node::prefix( ) const 00345 { 00346 if (!impl) return DOMString(); 00347 return impl->prefix(); 00348 } 00349 00350 void Node::setPrefix(const DOMString &prefix ) 00351 { 00352 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00353 int exceptioncode = 0; 00354 impl->setPrefix(prefix,exceptioncode); 00355 if (exceptioncode) 00356 throw DOMException(exceptioncode); 00357 } 00358 00359 DOMString Node::localName( ) const 00360 { 00361 if (!impl) return DOMString(); 00362 return impl->localName(); 00363 } 00364 00365 void Node::addEventListener(const DOMString &type, 00366 EventListener *listener, 00367 const bool useCapture) 00368 { 00369 if (!impl) return; 00370 if (listener) 00371 impl->addEventListener(EventImpl::typeToId(type),listener,useCapture); 00372 } 00373 00374 void Node::removeEventListener(const DOMString &type, 00375 EventListener *listener, 00376 bool useCapture) 00377 { 00378 if (!impl) return; 00379 impl->removeEventListener(EventImpl::typeToId(type),listener,useCapture); 00380 } 00381 00382 bool Node::dispatchEvent(const Event &evt) 00383 { 00384 if (!impl) 00385 throw DOMException(DOMException::INVALID_STATE_ERR); 00386 00387 if (!evt.handle()) 00388 throw DOMException(DOMException::NOT_FOUND_ERR); 00389 00390 int exceptioncode = 0; 00391 impl->dispatchEvent(evt.handle(),exceptioncode); 00392 if (exceptioncode) 00393 throw DOMException(exceptioncode); 00394 return !evt.handle()->defaultPrevented(); 00395 } 00396 00397 00398 unsigned int Node::elementId() const 00399 { 00400 if (!impl) return 0; 00401 return impl->id(); 00402 } 00403 00404 unsigned long Node::index() const 00405 { 00406 if (!impl) return 0; 00407 return impl->nodeIndex(); 00408 } 00409 00410 QString Node::toHTML() 00411 { 00412 if (!impl) return QString::null; 00413 return impl->toString().string(); 00414 } 00415 00416 void Node::applyChanges() 00417 { 00418 if (!impl) return; 00419 impl->recalcStyle( NodeImpl::Inherit ); 00420 } 00421 00422 void Node::getCursor(int offset, int &_x, int &_y, int &height) 00423 { 00424 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00425 int dummy; 00426 impl->getCaret(offset, false, _x, _y, dummy, height); 00427 } 00428 00429 QRect Node::getRect() 00430 { 00431 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00432 return impl->getRect(); 00433 } 00434 00435 DOMString Node::textContent( ) const 00436 { 00437 if(impl) return impl->textContent(); 00438 return DOMString(); 00439 } 00440 00441 void Node::setTextContent(const DOMString &content) const 00442 { 00443 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00444 00445 int exceptioncode = 0; 00446 impl->setTextContent( content, exceptioncode ); 00447 if (exceptioncode) 00448 throw DOMException(exceptioncode); 00449 } 00450 00451 //----------------------------------------------------------------------------- 00452 00453 NodeList::NodeList() 00454 { 00455 impl = 0; 00456 } 00457 00458 NodeList::NodeList(const NodeList &other) 00459 { 00460 impl = other.impl; 00461 if(impl) impl->ref(); 00462 } 00463 00464 NodeList::NodeList(const NodeListImpl *i) 00465 { 00466 impl = const_cast<NodeListImpl *>(i); 00467 if(impl) impl->ref(); 00468 } 00469 00470 NodeList &NodeList::operator = (const NodeList &other) 00471 { 00472 if ( impl != other.impl ) { 00473 if(impl) impl->deref(); 00474 impl = other.impl; 00475 if(impl) impl->ref(); 00476 } 00477 return *this; 00478 } 00479 00480 NodeList::~NodeList() 00481 { 00482 if(impl) impl->deref(); 00483 } 00484 00485 Node NodeList::item( unsigned long index ) const 00486 { 00487 if (!impl) return 0; 00488 return impl->item(index); 00489 } 00490 00491 unsigned long NodeList::length() const 00492 { 00493 if (!impl) return 0; 00494 return impl->length(); 00495 }