KHTML
dom_node.cpp
Go to the documentation of this file.
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 <QtCore/QRect> 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 return impl->getNamedItem(name); 00070 } 00071 00072 Node NamedNodeMap::setNamedItem( const Node &arg ) 00073 { 00074 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00075 00076 int exceptioncode = 0; 00077 Node r = impl->setNamedItem(arg, exceptioncode); 00078 if (exceptioncode) 00079 throw DOMException(exceptioncode); 00080 return r; 00081 } 00082 00083 Node NamedNodeMap::removeNamedItem( const DOMString &name ) 00084 { 00085 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00086 int exceptioncode = 0; 00087 Node r = impl->removeNamedItem(name, exceptioncode); 00088 if (exceptioncode) 00089 throw DOMException(exceptioncode); 00090 return r; 00091 } 00092 00093 Node NamedNodeMap::item( unsigned long index ) const 00094 { 00095 if (!impl) return 0; 00096 return impl->item(index); 00097 } 00098 00099 Node NamedNodeMap::getNamedItemNS( const DOMString &namespaceURI, const DOMString &localName ) const 00100 { 00101 if (!impl) return 0; 00102 return impl->getNamedItemNS(namespaceURI, localName); 00103 } 00104 00105 Node NamedNodeMap::setNamedItemNS( const Node &arg ) 00106 { 00107 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00108 int exceptioncode = 0; 00109 Node r = impl->setNamedItemNS(arg, exceptioncode); 00110 if (exceptioncode) 00111 throw DOMException(exceptioncode); 00112 return r; 00113 } 00114 00115 Node NamedNodeMap::removeNamedItemNS( const DOMString &namespaceURI, const DOMString &localName ) 00116 { 00117 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00118 int exceptioncode = 0; 00119 Node r = impl->removeNamedItemNS(namespaceURI, localName, exceptioncode); 00120 if (exceptioncode) 00121 throw DOMException(exceptioncode); 00122 return r; 00123 } 00124 00125 unsigned long NamedNodeMap::length() const 00126 { 00127 if (!impl) return 0; 00128 return impl->length(); 00129 } 00130 00131 // --------------------------------------------------------------------------- 00132 00133 Node::Node(const Node &other) 00134 { 00135 impl = other.impl; 00136 if(impl) impl->ref(); 00137 } 00138 00139 Node::Node( NodeImpl *i ) 00140 { 00141 impl = i; 00142 if(impl) impl->ref(); 00143 } 00144 00145 Node &Node::operator = (const Node &other) 00146 { 00147 if(impl != other.impl) { 00148 if(impl) impl->deref(); 00149 impl = other.impl; 00150 if(impl) impl->ref(); 00151 } 00152 return *this; 00153 } 00154 00155 bool Node::operator == (const Node &other) const 00156 { 00157 return (impl == other.impl); 00158 } 00159 00160 bool Node::operator != (const Node &other) const 00161 { 00162 return !(impl == other.impl); 00163 } 00164 00165 Node::~Node() 00166 { 00167 if(impl) impl->deref(); 00168 } 00169 00170 DOMString Node::nodeName() const 00171 { 00172 if(impl) return impl->nodeName(); 00173 return DOMString(); 00174 } 00175 00176 DOMString Node::nodeValue() const 00177 { 00178 // ### should throw exception on plain node ? 00179 if(impl) return impl->nodeValue(); 00180 return DOMString(); 00181 } 00182 00183 void Node::setNodeValue( const DOMString &_str ) 00184 { 00185 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00186 00187 int exceptioncode = 0; 00188 if(impl) impl->setNodeValue( _str,exceptioncode ); 00189 if (exceptioncode) 00190 throw DOMException(exceptioncode); 00191 } 00192 00193 unsigned short Node::nodeType() const 00194 { 00195 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00196 return impl->nodeType(); 00197 } 00198 00199 Node Node::parentNode() const 00200 { 00201 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00202 return impl->parentNode(); 00203 } 00204 00205 NodeList Node::childNodes() const 00206 { 00207 if (!impl) return 0; 00208 return impl->childNodes(); 00209 } 00210 00211 Node Node::firstChild() const 00212 { 00213 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00214 return impl->firstChild(); 00215 } 00216 00217 Node Node::lastChild() const 00218 { 00219 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00220 return impl->lastChild(); 00221 } 00222 00223 Node Node::previousSibling() const 00224 { 00225 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00226 return impl->previousSibling(); 00227 } 00228 00229 Node Node::nextSibling() const 00230 { 00231 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00232 return impl->nextSibling(); 00233 } 00234 00235 NamedNodeMap Node::attributes() const 00236 { 00237 if (!impl || !impl->isElementNode()) return 0; 00238 return static_cast<ElementImpl*>(impl)->attributes(); 00239 } 00240 00241 Document Node::ownerDocument() const 00242 { 00243 if (!impl || !impl->ownerDocument()) 00244 return Document(false); 00245 return impl->ownerDocument(); 00246 } 00247 00248 Node Node::insertBefore( const Node &newChild, const Node &refChild ) 00249 { 00250 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00251 int exceptioncode = 0; 00252 NodeImpl *r = impl->insertBefore( newChild.impl, refChild.impl, exceptioncode ); 00253 if (exceptioncode) 00254 throw DOMException(exceptioncode); 00255 return r; 00256 } 00257 00258 Node Node::replaceChild( const Node &newChild, const Node &oldChild ) 00259 { 00260 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00261 int exceptioncode = 0; 00262 impl->replaceChild( newChild.impl, oldChild.impl, exceptioncode ); 00263 if (exceptioncode) 00264 throw DOMException(exceptioncode); 00265 return oldChild; 00266 } 00267 00268 Node Node::removeChild( const Node &oldChild ) 00269 { 00270 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00271 int exceptioncode = 0; 00272 impl->removeChild( oldChild.impl, exceptioncode ); 00273 if (exceptioncode) 00274 throw DOMException(exceptioncode); 00275 00276 return oldChild; 00277 } 00278 00279 Node Node::appendChild( const Node &newChild ) 00280 { 00281 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00282 int exceptioncode = 0; 00283 NodeImpl *r = impl->appendChild( newChild.impl, exceptioncode ); 00284 if (exceptioncode) 00285 throw DOMException(exceptioncode); 00286 return r; 00287 } 00288 00289 bool Node::hasAttributes() 00290 { 00291 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00292 return impl->hasAttributes(); 00293 } 00294 00295 bool Node::hasChildNodes( ) 00296 { 00297 if (!impl) return false; 00298 return impl->hasChildNodes(); 00299 } 00300 00301 Node Node::cloneNode( bool deep ) 00302 { 00303 if (!impl) return 0; 00304 return impl->cloneNode( deep ).get(); 00305 } 00306 00307 void Node::normalize ( ) 00308 { 00309 if (!impl) return; 00310 impl->normalize(); 00311 } 00312 00313 bool Node::isSupported( const DOMString &feature, 00314 const DOMString &version ) const 00315 { 00316 return NodeImpl::isSupported(feature, version); 00317 } 00318 00319 DOMString Node::namespaceURI( ) const 00320 { 00321 if (!impl) return DOMString(); 00322 return impl->namespaceURI(); 00323 } 00324 00325 DOMString Node::prefix( ) const 00326 { 00327 if (!impl) return DOMString(); 00328 return impl->prefix(); 00329 } 00330 00331 void Node::setPrefix(const DOMString &prefix ) 00332 { 00333 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00334 int exceptioncode = 0; 00335 impl->setPrefix(prefix,exceptioncode); 00336 if (exceptioncode) 00337 throw DOMException(exceptioncode); 00338 } 00339 00340 DOMString Node::localName( ) const 00341 { 00342 if (!impl) return DOMString(); 00343 return impl->localName(); 00344 } 00345 00346 void Node::addEventListener(const DOMString &type, 00347 EventListener *listener, 00348 const bool useCapture) 00349 { 00350 if (!impl) return; 00351 if (listener) 00352 impl->addEventListener(EventName::fromString(type),listener,useCapture); 00353 } 00354 00355 void Node::removeEventListener(const DOMString &type, 00356 EventListener *listener, 00357 bool useCapture) 00358 { 00359 if (!impl) return; 00360 impl->removeEventListener(EventName::fromString(type),listener,useCapture); 00361 } 00362 00363 bool Node::dispatchEvent(const Event &evt) 00364 { 00365 if (!impl) 00366 throw DOMException(DOMException::INVALID_STATE_ERR); 00367 00368 if (!evt.handle()) 00369 throw DOMException(DOMException::NOT_FOUND_ERR); 00370 00371 int exceptioncode = 0; 00372 impl->dispatchEvent(evt.handle(),exceptioncode); 00373 if (exceptioncode) 00374 throw DOMException(exceptioncode); 00375 return !evt.handle()->defaultPrevented(); 00376 } 00377 00378 DOMString Node::textContent() const 00379 { 00380 if (!impl) return DOMString(); 00381 return impl->textContent(); 00382 } 00383 00384 void Node::setTextContent(const DOMString& content) 00385 { 00386 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00387 int exceptioncode = 0; 00388 impl->setTextContent(content, exceptioncode); 00389 if (exceptioncode) 00390 throw DOMException(exceptioncode); 00391 } 00392 00393 unsigned Node::compareDocumentPosition(const Node& other) 00394 { 00395 if (!impl || !other.impl) 00396 throw DOMException(DOMException::NOT_FOUND_ERR); 00397 return impl->compareDocumentPosition(other.impl); 00398 } 00399 00400 unsigned int Node::elementId() const 00401 { 00402 if (!impl) return 0; 00403 return impl->id(); 00404 } 00405 00406 unsigned long Node::index() const 00407 { 00408 if (!impl) return 0; 00409 return impl->nodeIndex(); 00410 } 00411 00412 #ifndef KDE_NO_DEPRECATED 00413 QString Node::toHTML() 00414 { 00415 if (!impl) return QString(); 00416 return impl->toString().string(); 00417 } 00418 #endif 00419 00420 void Node::applyChanges() 00421 { 00422 if (!impl) return; 00423 impl->recalcStyle( NodeImpl::Inherit ); 00424 } 00425 00426 #ifndef KDE_NO_DEPRECATED 00427 void Node::getCursor(int offset, int &_x, int &_y, int &height) 00428 { 00429 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00430 int dummy; 00431 impl->getCaret(offset, false, _x, _y, dummy, height); 00432 } 00433 #endif 00434 00435 QRect Node::getRect() 00436 { 00437 if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); 00438 return impl->getRect(); 00439 } 00440 00441 //----------------------------------------------------------------------------- 00442 00443 NodeList::NodeList() 00444 { 00445 impl = 0; 00446 } 00447 00448 NodeList::NodeList(const NodeList &other) 00449 { 00450 impl = other.impl; 00451 if(impl) impl->ref(); 00452 } 00453 00454 NodeList::NodeList(const NodeListImpl *i) 00455 { 00456 impl = const_cast<NodeListImpl *>(i); 00457 if(impl) impl->ref(); 00458 } 00459 00460 NodeList &NodeList::operator = (const NodeList &other) 00461 { 00462 if ( impl != other.impl ) { 00463 if(impl) impl->deref(); 00464 impl = other.impl; 00465 if(impl) impl->ref(); 00466 } 00467 return *this; 00468 } 00469 00470 NodeList::~NodeList() 00471 { 00472 if(impl) impl->deref(); 00473 } 00474 00475 Node NodeList::item( unsigned long index ) const 00476 { 00477 if (!impl) return 0; 00478 return impl->item(index); 00479 } 00480 00481 unsigned long NodeList::length() const 00482 { 00483 if (!impl) return 0; 00484 return impl->length(); 00485 } 00486 00487 //----------------------------------------------------------------------------- 00488 00489 DOMString DOMException::codeAsString() const 00490 { 00491 return codeAsString(code); 00492 } 00493 00494 DOMString DOMException::codeAsString(int code) 00495 { 00496 switch ( code ) { 00497 case INDEX_SIZE_ERR: 00498 return DOMString( "INDEX_SIZE_ERR" ); 00499 case DOMSTRING_SIZE_ERR: 00500 return DOMString( "DOMSTRING_SIZE_ERR" ); 00501 case HIERARCHY_REQUEST_ERR: 00502 return DOMString( "HIERARCHY_REQUEST_ERR" ); 00503 case WRONG_DOCUMENT_ERR: 00504 return DOMString( "WRONG_DOCUMENT_ERR" ); 00505 case INVALID_CHARACTER_ERR: 00506 return DOMString( "INVALID_CHARACTER_ERR" ); 00507 case NO_DATA_ALLOWED_ERR: 00508 return DOMString( "NO_DATA_ALLOWED_ERR" ); 00509 case NO_MODIFICATION_ALLOWED_ERR: 00510 return DOMString( "NO_MODIFICATION_ALLOWED_ERR" ); 00511 case NOT_FOUND_ERR: 00512 return DOMString( "NOT_FOUND_ERR" ); 00513 case NOT_SUPPORTED_ERR: 00514 return DOMString( "NOT_SUPPORTED_ERR" ); 00515 case INUSE_ATTRIBUTE_ERR: 00516 return DOMString( "INUSE_ATTRIBUTE_ERR" ); 00517 case INVALID_STATE_ERR: 00518 return DOMString( "INVALID_STATE_ERR" ); 00519 case SYNTAX_ERR: 00520 return DOMString( "SYNTAX_ERR" ); 00521 case INVALID_MODIFICATION_ERR: 00522 return DOMString( "INVALID_MODIFICATION_ERR" ); 00523 case NAMESPACE_ERR: 00524 return DOMString( "NAMESPACE_ERR" ); 00525 case INVALID_ACCESS_ERR: 00526 return DOMString( "INVALID_ACCESS_ERR" ); 00527 case VALIDATION_ERR: 00528 return DOMString( "VALIDATION_ERR" ); 00529 case TYPE_MISMATCH_ERR: 00530 return DOMString( "TYPE_MISMATCH_ERR" ); 00531 case SECURITY_ERR: 00532 return DOMString( "SECURITY_ERR" ); 00533 case NETWORK_ERR: 00534 return DOMString( "NETWORK_ERR" ); 00535 case ABORT_ERR: 00536 return DOMString( "ABORT_ERR" ); 00537 case URL_MISMATCH_ERR: 00538 return DOMString( "URL_MISMATCH_ERR" ); 00539 case QUOTA_EXCEEDED_ERR: 00540 return DOMString( "QUOTA_EXCEEDED_ERR" ); 00541 case TIMEOUT_ERR: 00542 return DOMString( "TIMEOUT_ERR" ); 00543 case NOT_READABLE_ERR: 00544 return DOMString( "NOT_READABLE_ERR" ); 00545 case DATA_CLONE_ERR: 00546 return DOMString( "DATA_CLONE_ERR" ); 00547 case ENCODING_ERR: 00548 return DOMString( "ENCODING_ERR" ); 00549 default: 00550 return DOMString( "(unknown exception code)" ); 00551 } 00552 } 00553 00554 bool DOMException::isDOMExceptionCode(int exceptioncode) 00555 { 00556 return exceptioncode < 100; 00557 } 00558
KDE 4.6 API Reference