KHTML
dom2_range.cpp
Go to the documentation of this file.
00001 00026 #include "dom/dom_exception.h" 00027 #include "xml/dom_docimpl.h" 00028 #include "xml/dom2_rangeimpl.h" 00029 00030 using namespace DOM; 00031 00032 DOMString RangeException::codeAsString() const 00033 { 00034 return codeAsString(code); 00035 } 00036 00037 bool RangeException::isRangeExceptionCode(int exceptioncode) 00038 { 00039 return exceptioncode >= _EXCEPTION_OFFSET && exceptioncode < _EXCEPTION_MAX; 00040 } 00041 00042 DOMString RangeException::codeAsString(int code) 00043 { 00044 switch ( code ) { 00045 case BAD_BOUNDARYPOINTS_ERR: 00046 return DOMString( "BAD_BOUNDARYPOINTS_ERR" ); 00047 case INVALID_NODE_TYPE_ERR: 00048 return DOMString( "INVALID_NODE_TYPE_ERR" ); 00049 default: 00050 return DOMString( "(unknown exception code)" ); 00051 } 00052 } 00053 00054 Range::Range() 00055 { 00056 // a range can't exist by itself - it must be associated with a document 00057 impl = 0; 00058 } 00059 00060 Range::Range(const Document rootContainer) 00061 { 00062 if(rootContainer.handle()) 00063 { 00064 impl = new RangeImpl(rootContainer.handle()->docPtr()); 00065 impl->ref(); 00066 } 00067 else 00068 impl = 0; 00069 } 00070 00071 Range::Range(const Range &other) 00072 { 00073 impl = other.impl; 00074 if (impl) impl->ref(); 00075 } 00076 00077 Range::Range(const Node startContainer, const long startOffset, const Node endContainer, const long endOffset) 00078 { 00079 if (startContainer.isNull() || endContainer.isNull()) { 00080 throw DOMException(DOMException::NOT_FOUND_ERR); 00081 } 00082 00083 if (!startContainer.handle()->document() || 00084 startContainer.handle()->document() != endContainer.handle()->document()) { 00085 throw DOMException(DOMException::WRONG_DOCUMENT_ERR); 00086 } 00087 00088 impl = new RangeImpl(startContainer.handle()->docPtr(),startContainer.handle(),startOffset,endContainer.handle(),endOffset); 00089 impl->ref(); 00090 } 00091 00092 Range::Range(RangeImpl *i) 00093 { 00094 impl = i; 00095 if (impl) impl->ref(); 00096 } 00097 00098 Range &Range::operator = (const Range &other) 00099 { 00100 if ( impl != other.impl ) { 00101 if (impl) impl->deref(); 00102 impl = other.impl; 00103 if (impl) impl->ref(); 00104 } 00105 return *this; 00106 } 00107 00108 Range::~Range() 00109 { 00110 if (impl) impl->deref(); 00111 } 00112 00113 Node Range::startContainer() const 00114 { 00115 if (!impl) 00116 throw DOMException(DOMException::INVALID_STATE_ERR); 00117 00118 int exceptioncode = 0; 00119 NodeImpl *r = impl->startContainer(exceptioncode); 00120 throwException(exceptioncode); 00121 return r; 00122 } 00123 00124 long Range::startOffset() const 00125 { 00126 if (!impl) 00127 throw DOMException(DOMException::INVALID_STATE_ERR); 00128 00129 int exceptioncode = 0; 00130 long r = impl->startOffset(exceptioncode); 00131 throwException(exceptioncode); 00132 return r; 00133 00134 } 00135 00136 Node Range::endContainer() const 00137 { 00138 if (!impl) 00139 throw DOMException(DOMException::INVALID_STATE_ERR); 00140 00141 int exceptioncode = 0; 00142 NodeImpl *r = impl->endContainer(exceptioncode); 00143 throwException(exceptioncode); 00144 return r; 00145 } 00146 00147 long Range::endOffset() const 00148 { 00149 if (!impl) 00150 throw DOMException(DOMException::INVALID_STATE_ERR); 00151 00152 int exceptioncode = 0; 00153 long r = impl->endOffset(exceptioncode); 00154 throwException(exceptioncode); 00155 return r; 00156 } 00157 00158 bool Range::collapsed() const 00159 { 00160 if (!impl) 00161 throw DOMException(DOMException::INVALID_STATE_ERR); 00162 00163 int exceptioncode = 0; 00164 bool r = impl->collapsed(exceptioncode); 00165 throwException(exceptioncode); 00166 return r; 00167 } 00168 00169 Node Range::commonAncestorContainer() 00170 { 00171 if (!impl) 00172 throw DOMException(DOMException::INVALID_STATE_ERR); 00173 00174 int exceptioncode = 0; 00175 NodeImpl *r = impl->commonAncestorContainer(exceptioncode); 00176 throwException(exceptioncode); 00177 return r; 00178 } 00179 00180 void Range::setStart( const Node &refNode, long offset ) 00181 { 00182 if (!impl) 00183 throw DOMException(DOMException::INVALID_STATE_ERR); 00184 00185 int exceptioncode = 0; 00186 impl->setStart(refNode.handle(),offset,exceptioncode); 00187 throwException(exceptioncode); 00188 } 00189 00190 void Range::setEnd( const Node &refNode, long offset ) 00191 { 00192 if (!impl) 00193 throw DOMException(DOMException::INVALID_STATE_ERR); 00194 00195 int exceptioncode = 0; 00196 impl->setEnd(refNode.handle(),offset,exceptioncode); 00197 throwException(exceptioncode); 00198 } 00199 00200 void Range::setStartBefore( const Node &refNode ) 00201 { 00202 if (!impl) 00203 throw DOMException(DOMException::INVALID_STATE_ERR); 00204 00205 00206 int exceptioncode = 0; 00207 impl->setStartBefore(refNode.handle(),exceptioncode); 00208 throwException(exceptioncode); 00209 } 00210 00211 void Range::setStartAfter( const Node &refNode ) 00212 { 00213 if (!impl) 00214 throw DOMException(DOMException::INVALID_STATE_ERR); 00215 00216 int exceptioncode = 0; 00217 impl->setStartAfter(refNode.handle(),exceptioncode); 00218 throwException(exceptioncode); 00219 } 00220 00221 void Range::setEndBefore( const Node &refNode ) 00222 { 00223 if (!impl) 00224 throw DOMException(DOMException::INVALID_STATE_ERR); 00225 00226 int exceptioncode = 0; 00227 impl->setEndBefore(refNode.handle(),exceptioncode); 00228 throwException(exceptioncode); 00229 } 00230 00231 void Range::setEndAfter( const Node &refNode ) 00232 { 00233 if (!impl) 00234 throw DOMException(DOMException::INVALID_STATE_ERR); 00235 00236 int exceptioncode = 0; 00237 impl->setEndAfter(refNode.handle(),exceptioncode); 00238 throwException(exceptioncode); 00239 } 00240 00241 void Range::collapse( bool toStart ) 00242 { 00243 if (!impl) 00244 throw DOMException(DOMException::INVALID_STATE_ERR); 00245 00246 int exceptioncode = 0; 00247 impl->collapse(toStart,exceptioncode); 00248 throwException(exceptioncode); 00249 } 00250 00251 void Range::selectNode( const Node &refNode ) 00252 { 00253 if (!impl) 00254 throw DOMException(DOMException::INVALID_STATE_ERR); 00255 00256 int exceptioncode = 0; 00257 impl->selectNode(refNode.handle(),exceptioncode); 00258 throwException(exceptioncode); 00259 } 00260 00261 void Range::selectNodeContents( const Node &refNode ) 00262 { 00263 if (!impl) 00264 throw DOMException(DOMException::INVALID_STATE_ERR); 00265 00266 int exceptioncode = 0; 00267 impl->selectNodeContents(refNode.handle(),exceptioncode); 00268 throwException(exceptioncode); 00269 } 00270 00271 short Range::compareBoundaryPoints( CompareHow how, const Range &sourceRange ) 00272 { 00273 if (!impl) 00274 throw DOMException(DOMException::INVALID_STATE_ERR); 00275 00276 int exceptioncode = 0; 00277 short r = impl->compareBoundaryPoints(how,sourceRange.handle(),exceptioncode); 00278 throwException(exceptioncode); 00279 return r; 00280 } 00281 00282 bool Range::boundaryPointsValid( ) 00283 { 00284 if (!impl) 00285 throw DOMException(DOMException::INVALID_STATE_ERR); 00286 00287 return impl->boundaryPointsValid(); 00288 } 00289 00290 void Range::deleteContents( ) 00291 { 00292 if (!impl) 00293 throw DOMException(DOMException::INVALID_STATE_ERR); 00294 00295 int exceptioncode = 0; 00296 impl->deleteContents(exceptioncode); 00297 throwException(exceptioncode); 00298 } 00299 00300 DocumentFragment Range::extractContents( ) 00301 { 00302 if (!impl) 00303 throw DOMException(DOMException::INVALID_STATE_ERR); 00304 00305 int exceptioncode = 0; 00306 DocumentFragmentImpl *r = impl->extractContents(exceptioncode); 00307 throwException(exceptioncode); 00308 return r; 00309 } 00310 00311 DocumentFragment Range::cloneContents( ) 00312 { 00313 if (!impl) 00314 throw DOMException(DOMException::INVALID_STATE_ERR); 00315 00316 int exceptioncode = 0; 00317 DocumentFragmentImpl *r = impl->cloneContents(exceptioncode); 00318 throwException(exceptioncode); 00319 return r; 00320 } 00321 00322 void Range::insertNode( const Node &newNode ) 00323 { 00324 if (!impl) 00325 throw DOMException(DOMException::INVALID_STATE_ERR); 00326 00327 int exceptioncode = 0; 00328 impl->insertNode(newNode.handle(),exceptioncode); 00329 throwException(exceptioncode); 00330 } 00331 00332 void Range::surroundContents( const Node &newParent ) 00333 { 00334 if (!impl) 00335 throw DOMException(DOMException::INVALID_STATE_ERR); 00336 00337 int exceptioncode = 0; 00338 impl->surroundContents(newParent.handle(),exceptioncode); 00339 throwException(exceptioncode); 00340 } 00341 00342 Range Range::cloneRange( ) 00343 { 00344 if (!impl) 00345 throw DOMException(DOMException::INVALID_STATE_ERR); 00346 00347 int exceptioncode = 0; 00348 RangeImpl *r = impl->cloneRange(exceptioncode); 00349 throwException(exceptioncode); 00350 return r; 00351 } 00352 00353 DOMString Range::toString( ) 00354 { 00355 if (!impl) 00356 throw DOMException(DOMException::INVALID_STATE_ERR); 00357 00358 int exceptioncode = 0; 00359 DOMString r = impl->toString(exceptioncode); 00360 throwException(exceptioncode); 00361 return r; 00362 00363 } 00364 00365 DOMString Range::toHTML( ) 00366 { 00367 if (!impl) 00368 throw DOMException(DOMException::INVALID_STATE_ERR); 00369 int exceptioncode = 0; 00370 DOMString r = impl->toHTML(exceptioncode); 00371 throwException(exceptioncode); 00372 return r; 00373 } 00374 00375 DocumentFragment Range::createContextualFragment( const DOMString &html ) 00376 { 00377 if (!impl) 00378 throw DOMException(DOMException::INVALID_STATE_ERR); 00379 00380 int exceptioncode = 0; 00381 DocumentFragment r = impl->createContextualFragment(html, exceptioncode); 00382 throwException(exceptioncode); 00383 return r; 00384 } 00385 00386 00387 void Range::detach( ) 00388 { 00389 if (!impl) 00390 throw DOMException(DOMException::INVALID_STATE_ERR); 00391 00392 int exceptioncode = 0; 00393 impl->detach(exceptioncode); 00394 throwException(exceptioncode); 00395 } 00396 00397 bool Range::isDetached() const 00398 { 00399 if (!impl) 00400 throw DOMException(DOMException::INVALID_STATE_ERR); 00401 00402 return impl->isDetached(); 00403 } 00404 00405 RangeImpl *Range::handle() const 00406 { 00407 return impl; 00408 } 00409 00410 bool Range::isNull() const 00411 { 00412 return (impl == 0); 00413 } 00414 00415 void Range::throwException(int exceptioncode) const 00416 { 00417 if (!exceptioncode) 00418 return; 00419 00420 // ### also check for CSS & other exceptions? 00421 if (exceptioncode >= RangeException::_EXCEPTION_OFFSET && exceptioncode <= RangeException::_EXCEPTION_MAX) 00422 throw RangeException(static_cast<RangeException::RangeExceptionCode>(exceptioncode-RangeException::_EXCEPTION_OFFSET)); 00423 else 00424 throw DOMException(exceptioncode); 00425 } 00426 00427 00428
KDE 4.6 API Reference