KHtml

dom2_range.cpp
1 /**
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright 1999 Lars Knoll ([email protected])
5  * Copyright 2000 Gunnstein Lye ([email protected])
6  * Copyright 2000 Frederik Holljen ([email protected])
7  * Copyright 2001 Peter Kelly ([email protected])
8  * Copyright 2003 Apple Computer, Inc.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public License
21  * along with this library; see the file COPYING.LIB. If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  *
25  */
26 #include "dom/dom_exception.h"
27 #include "xml/dom_docimpl.h"
28 #include "xml/dom2_rangeimpl.h"
29 
30 using namespace DOM;
31 
32 DOMString RangeException::codeAsString() const
33 {
34  return codeAsString(code);
35 }
36 
37 bool RangeException::isRangeExceptionCode(int exceptioncode)
38 {
39  return exceptioncode >= _EXCEPTION_OFFSET && exceptioncode < _EXCEPTION_MAX;
40 }
41 
42 DOMString RangeException::codeAsString(int code)
43 {
44  switch (code) {
45  case BAD_BOUNDARYPOINTS_ERR:
46  return DOMString("BAD_BOUNDARYPOINTS_ERR");
47  case INVALID_NODE_TYPE_ERR:
48  return DOMString("INVALID_NODE_TYPE_ERR");
49  default:
50  return DOMString("(unknown exception code)");
51  }
52 }
53 
54 Range::Range()
55 {
56  // a range can't exist by itself - it must be associated with a document
57  impl = nullptr;
58 }
59 
60 Range::Range(const Document rootContainer)
61 {
62  if (rootContainer.handle()) {
63  impl = new RangeImpl(rootContainer.handle()->docPtr());
64  impl->ref();
65  } else {
66  impl = nullptr;
67  }
68 }
69 
70 Range::Range(const Range &other)
71 {
72  impl = other.impl;
73  if (impl) {
74  impl->ref();
75  }
76 }
77 
78 Range::Range(const Node startContainer, const long startOffset, const Node endContainer, const long endOffset)
79 {
80  if (startContainer.isNull() || endContainer.isNull()) {
81  throw DOMException(DOMException::NOT_FOUND_ERR);
82  }
83 
84  if (!startContainer.handle()->document() ||
85  startContainer.handle()->document() != endContainer.handle()->document()) {
86  throw DOMException(DOMException::WRONG_DOCUMENT_ERR);
87  }
88 
89  impl = new RangeImpl(startContainer.handle()->docPtr(), startContainer.handle(), startOffset, endContainer.handle(), endOffset);
90  impl->ref();
91 }
92 
93 Range::Range(RangeImpl *i)
94 {
95  impl = i;
96  if (impl) {
97  impl->ref();
98  }
99 }
100 
101 Range &Range::operator = (const Range &other)
102 {
103  if (impl != other.impl) {
104  if (impl) {
105  impl->deref();
106  }
107  impl = other.impl;
108  if (impl) {
109  impl->ref();
110  }
111  }
112  return *this;
113 }
114 
115 Range::~Range()
116 {
117  if (impl) {
118  impl->deref();
119  }
120 }
121 
122 Node Range::startContainer() const
123 {
124  if (!impl) {
125  throw DOMException(DOMException::INVALID_STATE_ERR);
126  }
127 
128  int exceptioncode = 0;
129  NodeImpl *r = impl->startContainer(exceptioncode);
130  throwException(exceptioncode);
131  return r;
132 }
133 
134 long Range::startOffset() const
135 {
136  if (!impl) {
137  throw DOMException(DOMException::INVALID_STATE_ERR);
138  }
139 
140  int exceptioncode = 0;
141  long r = impl->startOffset(exceptioncode);
142  throwException(exceptioncode);
143  return r;
144 
145 }
146 
147 Node Range::endContainer() const
148 {
149  if (!impl) {
150  throw DOMException(DOMException::INVALID_STATE_ERR);
151  }
152 
153  int exceptioncode = 0;
154  NodeImpl *r = impl->endContainer(exceptioncode);
155  throwException(exceptioncode);
156  return r;
157 }
158 
159 long Range::endOffset() const
160 {
161  if (!impl) {
162  throw DOMException(DOMException::INVALID_STATE_ERR);
163  }
164 
165  int exceptioncode = 0;
166  long r = impl->endOffset(exceptioncode);
167  throwException(exceptioncode);
168  return r;
169 }
170 
171 bool Range::collapsed() const
172 {
173  if (!impl) {
174  throw DOMException(DOMException::INVALID_STATE_ERR);
175  }
176 
177  int exceptioncode = 0;
178  bool r = impl->collapsed(exceptioncode);
179  throwException(exceptioncode);
180  return r;
181 }
182 
183 Node Range::commonAncestorContainer()
184 {
185  if (!impl) {
186  throw DOMException(DOMException::INVALID_STATE_ERR);
187  }
188 
189  int exceptioncode = 0;
190  NodeImpl *r = impl->commonAncestorContainer(exceptioncode);
191  throwException(exceptioncode);
192  return r;
193 }
194 
195 void Range::setStart(const Node &refNode, long offset)
196 {
197  if (!impl) {
198  throw DOMException(DOMException::INVALID_STATE_ERR);
199  }
200 
201  int exceptioncode = 0;
202  impl->setStart(refNode.handle(), offset, exceptioncode);
203  throwException(exceptioncode);
204 }
205 
206 void Range::setEnd(const Node &refNode, long offset)
207 {
208  if (!impl) {
209  throw DOMException(DOMException::INVALID_STATE_ERR);
210  }
211 
212  int exceptioncode = 0;
213  impl->setEnd(refNode.handle(), offset, exceptioncode);
214  throwException(exceptioncode);
215 }
216 
217 void Range::setStartBefore(const Node &refNode)
218 {
219  if (!impl) {
220  throw DOMException(DOMException::INVALID_STATE_ERR);
221  }
222 
223  int exceptioncode = 0;
224  impl->setStartBefore(refNode.handle(), exceptioncode);
225  throwException(exceptioncode);
226 }
227 
228 void Range::setStartAfter(const Node &refNode)
229 {
230  if (!impl) {
231  throw DOMException(DOMException::INVALID_STATE_ERR);
232  }
233 
234  int exceptioncode = 0;
235  impl->setStartAfter(refNode.handle(), exceptioncode);
236  throwException(exceptioncode);
237 }
238 
239 void Range::setEndBefore(const Node &refNode)
240 {
241  if (!impl) {
242  throw DOMException(DOMException::INVALID_STATE_ERR);
243  }
244 
245  int exceptioncode = 0;
246  impl->setEndBefore(refNode.handle(), exceptioncode);
247  throwException(exceptioncode);
248 }
249 
250 void Range::setEndAfter(const Node &refNode)
251 {
252  if (!impl) {
253  throw DOMException(DOMException::INVALID_STATE_ERR);
254  }
255 
256  int exceptioncode = 0;
257  impl->setEndAfter(refNode.handle(), exceptioncode);
258  throwException(exceptioncode);
259 }
260 
261 void Range::collapse(bool toStart)
262 {
263  if (!impl) {
264  throw DOMException(DOMException::INVALID_STATE_ERR);
265  }
266 
267  int exceptioncode = 0;
268  impl->collapse(toStart, exceptioncode);
269  throwException(exceptioncode);
270 }
271 
272 void Range::selectNode(const Node &refNode)
273 {
274  if (!impl) {
275  throw DOMException(DOMException::INVALID_STATE_ERR);
276  }
277 
278  int exceptioncode = 0;
279  impl->selectNode(refNode.handle(), exceptioncode);
280  throwException(exceptioncode);
281 }
282 
283 void Range::selectNodeContents(const Node &refNode)
284 {
285  if (!impl) {
286  throw DOMException(DOMException::INVALID_STATE_ERR);
287  }
288 
289  int exceptioncode = 0;
290  impl->selectNodeContents(refNode.handle(), exceptioncode);
291  throwException(exceptioncode);
292 }
293 
294 short Range::compareBoundaryPoints(CompareHow how, const Range &sourceRange)
295 {
296  if (!impl) {
297  throw DOMException(DOMException::INVALID_STATE_ERR);
298  }
299 
300  int exceptioncode = 0;
301  short r = impl->compareBoundaryPoints(how, sourceRange.handle(), exceptioncode);
302  throwException(exceptioncode);
303  return r;
304 }
305 
306 bool Range::boundaryPointsValid()
307 {
308  if (!impl) {
309  throw DOMException(DOMException::INVALID_STATE_ERR);
310  }
311 
312  return impl->boundaryPointsValid();
313 }
314 
315 void Range::deleteContents()
316 {
317  if (!impl) {
318  throw DOMException(DOMException::INVALID_STATE_ERR);
319  }
320 
321  int exceptioncode = 0;
322  impl->deleteContents(exceptioncode);
323  throwException(exceptioncode);
324 }
325 
326 DocumentFragment Range::extractContents()
327 {
328  if (!impl) {
329  throw DOMException(DOMException::INVALID_STATE_ERR);
330  }
331 
332  int exceptioncode = 0;
333  DocumentFragmentImpl *r = impl->extractContents(exceptioncode);
334  throwException(exceptioncode);
335  return r;
336 }
337 
338 DocumentFragment Range::cloneContents()
339 {
340  if (!impl) {
341  throw DOMException(DOMException::INVALID_STATE_ERR);
342  }
343 
344  int exceptioncode = 0;
345  DocumentFragmentImpl *r = impl->cloneContents(exceptioncode);
346  throwException(exceptioncode);
347  return r;
348 }
349 
350 void Range::insertNode(const Node &newNode)
351 {
352  if (!impl) {
353  throw DOMException(DOMException::INVALID_STATE_ERR);
354  }
355 
356  int exceptioncode = 0;
357  impl->insertNode(newNode.handle(), exceptioncode);
358  throwException(exceptioncode);
359 }
360 
361 void Range::surroundContents(const Node &newParent)
362 {
363  if (!impl) {
364  throw DOMException(DOMException::INVALID_STATE_ERR);
365  }
366 
367  int exceptioncode = 0;
368  impl->surroundContents(newParent.handle(), exceptioncode);
369  throwException(exceptioncode);
370 }
371 
372 Range Range::cloneRange()
373 {
374  if (!impl) {
375  throw DOMException(DOMException::INVALID_STATE_ERR);
376  }
377 
378  int exceptioncode = 0;
379  RangeImpl *r = impl->cloneRange(exceptioncode);
380  throwException(exceptioncode);
381  return r;
382 }
383 
384 DOMString Range::toString()
385 {
386  if (!impl) {
387  throw DOMException(DOMException::INVALID_STATE_ERR);
388  }
389 
390  int exceptioncode = 0;
391  DOMString r = impl->toString(exceptioncode);
392  throwException(exceptioncode);
393  return r;
394 
395 }
396 
397 DOMString Range::toHTML()
398 {
399  if (!impl) {
400  throw DOMException(DOMException::INVALID_STATE_ERR);
401  }
402  int exceptioncode = 0;
403  DOMString r = impl->toHTML(exceptioncode);
404  throwException(exceptioncode);
405  return r;
406 }
407 
408 DocumentFragment Range::createContextualFragment(const DOMString &html)
409 {
410  if (!impl) {
411  throw DOMException(DOMException::INVALID_STATE_ERR);
412  }
413 
414  int exceptioncode = 0;
415  DocumentFragment r = impl->createContextualFragment(html, exceptioncode);
416  throwException(exceptioncode);
417  return r;
418 }
419 
420 void Range::detach()
421 {
422  if (!impl) {
423  throw DOMException(DOMException::INVALID_STATE_ERR);
424  }
425 
426  int exceptioncode = 0;
427  impl->detach(exceptioncode);
428  throwException(exceptioncode);
429 }
430 
431 bool Range::isDetached() const
432 {
433  if (!impl) {
434  throw DOMException(DOMException::INVALID_STATE_ERR);
435  }
436 
437  return impl->isDetached();
438 }
439 
440 RangeImpl *Range::handle() const
441 {
442  return impl;
443 }
444 
445 bool Range::isNull() const
446 {
447  return (impl == nullptr);
448 }
449 
450 void Range::throwException(int exceptioncode) const
451 {
452  if (!exceptioncode) {
453  return;
454  }
455 
456  // ### also check for CSS & other exceptions?
457  if (exceptioncode >= RangeException::_EXCEPTION_OFFSET && exceptioncode <= RangeException::_EXCEPTION_MAX) {
458  throw RangeException(static_cast<RangeException::RangeExceptionCode>(exceptioncode - RangeException::_EXCEPTION_OFFSET));
459  } else {
460  throw DOMException(exceptioncode);
461  }
462 }
463 
The Node interface is the primary datatype for the entire Document Object Model.
Definition: dom_node.h:278
DocumentFragment is a "lightweight" or "minimal" Document object.
Definition: dom_doc.h:1042
DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation is impos...
Definition: dom_exception.h:58
bool isNull() const
tests if this Node is 0.
Definition: dom_node.h:928
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
The Document interface represents the entire HTML or XML document.
Definition: dom_doc.h:246
This library provides a full-featured HTML parser and widget.
NodeImpl * handle() const
Definition: dom_node.h:936
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:13 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.