KHtml

dom_node.cpp
1 /**
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright 1999 Lars Knoll ([email protected])
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "dom/dom_doc.h"
24 #include "dom/dom_exception.h"
25 #include "dom/dom2_events.h"
26 #include "xml/dom_docimpl.h"
27 #include "xml/dom_elementimpl.h"
28 #include "xml/dom2_eventsimpl.h"
29 
30 #include <QRect>
31 
32 using namespace DOM;
33 
34 NamedNodeMap::NamedNodeMap()
35 {
36  impl = nullptr;
37 }
38 
39 NamedNodeMap::NamedNodeMap(const NamedNodeMap &other)
40 {
41  impl = other.impl;
42  if (impl) {
43  impl->ref();
44  }
45 }
46 
47 NamedNodeMap::NamedNodeMap(NamedNodeMapImpl *i)
48 {
49  impl = i;
50  if (impl) {
51  impl->ref();
52  }
53 }
54 
55 NamedNodeMap &NamedNodeMap::operator = (const NamedNodeMap &other)
56 {
57  if (impl != other.impl) {
58  if (impl) {
59  impl->deref();
60  }
61  impl = other.impl;
62  if (impl) {
63  impl->ref();
64  }
65  }
66  return *this;
67 }
68 
69 NamedNodeMap::~NamedNodeMap()
70 {
71  if (impl) {
72  impl->deref();
73  }
74 }
75 
77 {
78  if (!impl) {
79  return nullptr;
80  }
81  return impl->getNamedItem(name);
82 }
83 
85 {
86  if (!impl) {
87  throw DOMException(DOMException::NOT_FOUND_ERR);
88  }
89 
90  int exceptioncode = 0;
91  Node r = impl->setNamedItem(arg, exceptioncode);
92  if (exceptioncode) {
93  throw DOMException(exceptioncode);
94  }
95  return r;
96 }
97 
99 {
100  if (!impl) {
101  throw DOMException(DOMException::NOT_FOUND_ERR);
102  }
103  int exceptioncode = 0;
104  Node r = impl->removeNamedItem(name, exceptioncode);
105  if (exceptioncode) {
106  throw DOMException(exceptioncode);
107  }
108  return r;
109 }
110 
111 Node NamedNodeMap::item(unsigned long index) const
112 {
113  if (!impl) {
114  return nullptr;
115  }
116  return impl->item(index);
117 }
118 
119 Node NamedNodeMap::getNamedItemNS(const DOMString &namespaceURI, const DOMString &localName) const
120 {
121  if (!impl) {
122  return nullptr;
123  }
124  return impl->getNamedItemNS(namespaceURI, localName);
125 }
126 
128 {
129  if (!impl) {
130  throw DOMException(DOMException::NOT_FOUND_ERR);
131  }
132  int exceptioncode = 0;
133  Node r = impl->setNamedItemNS(arg, exceptioncode);
134  if (exceptioncode) {
135  throw DOMException(exceptioncode);
136  }
137  return r;
138 }
139 
140 Node NamedNodeMap::removeNamedItemNS(const DOMString &namespaceURI, const DOMString &localName)
141 {
142  if (!impl) {
143  throw DOMException(DOMException::NOT_FOUND_ERR);
144  }
145  int exceptioncode = 0;
146  Node r = impl->removeNamedItemNS(namespaceURI, localName, exceptioncode);
147  if (exceptioncode) {
148  throw DOMException(exceptioncode);
149  }
150  return r;
151 }
152 
153 unsigned long NamedNodeMap::length() const
154 {
155  if (!impl) {
156  return 0;
157  }
158  return impl->length();
159 }
160 
161 // ---------------------------------------------------------------------------
162 
163 Node::Node(const Node &other)
164 {
165  impl = other.impl;
166  if (impl) {
167  impl->ref();
168  }
169 }
170 
171 Node::Node(NodeImpl *i)
172 {
173  impl = i;
174  if (impl) {
175  impl->ref();
176  }
177 }
178 
179 Node &Node::operator = (const Node &other)
180 {
181  if (impl != other.impl) {
182  if (impl) {
183  impl->deref();
184  }
185  impl = other.impl;
186  if (impl) {
187  impl->ref();
188  }
189  }
190  return *this;
191 }
192 
193 bool Node::operator == (const Node &other) const
194 {
195  return (impl == other.impl);
196 }
197 
198 bool Node::operator != (const Node &other) const
199 {
200  return !(impl == other.impl);
201 }
202 
203 Node::~Node()
204 {
205  if (impl) {
206  impl->deref();
207  }
208 }
209 
211 {
212  if (impl) {
213  return impl->nodeName();
214  }
215  return DOMString();
216 }
217 
219 {
220  // ### should throw exception on plain node ?
221  if (impl) {
222  return impl->nodeValue();
223  }
224  return DOMString();
225 }
226 
227 void Node::setNodeValue(const DOMString &_str)
228 {
229  if (!impl) {
230  throw DOMException(DOMException::NOT_FOUND_ERR);
231  }
232 
233  int exceptioncode = 0;
234  if (impl) {
235  impl->setNodeValue(_str, exceptioncode);
236  }
237  if (exceptioncode) {
238  throw DOMException(exceptioncode);
239  }
240 }
241 
242 unsigned short Node::nodeType() const
243 {
244  if (!impl) {
245  throw DOMException(DOMException::NOT_FOUND_ERR);
246  }
247  return impl->nodeType();
248 }
249 
251 {
252  if (!impl) {
253  throw DOMException(DOMException::NOT_FOUND_ERR);
254  }
255  return impl->parentNode();
256 }
257 
259 {
260  if (!impl) {
261  return nullptr;
262  }
263  return impl->childNodes().get();
264 }
265 
267 {
268  if (!impl) {
269  throw DOMException(DOMException::NOT_FOUND_ERR);
270  }
271  return impl->firstChild();
272 }
273 
275 {
276  if (!impl) {
277  throw DOMException(DOMException::NOT_FOUND_ERR);
278  }
279  return impl->lastChild();
280 }
281 
283 {
284  if (!impl) {
285  throw DOMException(DOMException::NOT_FOUND_ERR);
286  }
287  return impl->previousSibling();
288 }
289 
291 {
292  if (!impl) {
293  throw DOMException(DOMException::NOT_FOUND_ERR);
294  }
295  return impl->nextSibling();
296 }
297 
299 {
300  if (!impl || !impl->isElementNode()) {
301  return nullptr;
302  }
303  return static_cast<ElementImpl *>(impl)->attributes();
304 }
305 
307 {
308  if (!impl || !impl->ownerDocument()) {
309  return Document(false);
310  }
311  return impl->ownerDocument();
312 }
313 
314 Node Node::insertBefore(const Node &newChild, const Node &refChild)
315 {
316  if (!impl) {
317  throw DOMException(DOMException::NOT_FOUND_ERR);
318  }
319  int exceptioncode = 0;
320  NodeImpl *r = impl->insertBefore(newChild.impl, refChild.impl, exceptioncode);
321  if (exceptioncode) {
322  throw DOMException(exceptioncode);
323  }
324  return r;
325 }
326 
327 Node Node::replaceChild(const Node &newChild, const Node &oldChild)
328 {
329  if (!impl) {
330  throw DOMException(DOMException::NOT_FOUND_ERR);
331  }
332  int exceptioncode = 0;
333  impl->replaceChild(newChild.impl, oldChild.impl, exceptioncode);
334  if (exceptioncode) {
335  throw DOMException(exceptioncode);
336  }
337  return oldChild;
338 }
339 
340 Node Node::removeChild(const Node &oldChild)
341 {
342  if (!impl) {
343  throw DOMException(DOMException::NOT_FOUND_ERR);
344  }
345  int exceptioncode = 0;
346  impl->removeChild(oldChild.impl, exceptioncode);
347  if (exceptioncode) {
348  throw DOMException(exceptioncode);
349  }
350 
351  return oldChild;
352 }
353 
354 Node Node::appendChild(const Node &newChild)
355 {
356  if (!impl) {
357  throw DOMException(DOMException::NOT_FOUND_ERR);
358  }
359  int exceptioncode = 0;
360  NodeImpl *r = impl->appendChild(newChild.impl, exceptioncode);
361  if (exceptioncode) {
362  throw DOMException(exceptioncode);
363  }
364  return r;
365 }
366 
368 {
369  if (!impl) {
370  throw DOMException(DOMException::NOT_FOUND_ERR);
371  }
372  return impl->hasAttributes();
373 }
374 
376 {
377  if (!impl) {
378  return false;
379  }
380  return impl->hasChildNodes();
381 }
382 
384 {
385  if (!impl) {
386  return nullptr;
387  }
388  return impl->cloneNode(deep).get();
389 }
390 
392 {
393  if (!impl) {
394  return;
395  }
396  impl->normalize();
397 }
398 
399 bool Node::isSupported(const DOMString &feature,
400  const DOMString &version) const
401 {
402  return NodeImpl::isSupported(feature, version);
403 }
404 
406 {
407  if (!impl) {
408  return DOMString();
409  }
410  return impl->namespaceURI();
411 }
412 
414 {
415  if (!impl) {
416  return DOMString();
417  }
418  return impl->prefix();
419 }
420 
421 void Node::setPrefix(const DOMString &prefix)
422 {
423  if (!impl) {
424  throw DOMException(DOMException::NOT_FOUND_ERR);
425  }
426  int exceptioncode = 0;
427  impl->setPrefix(prefix, exceptioncode);
428  if (exceptioncode) {
429  throw DOMException(exceptioncode);
430  }
431 }
432 
434 {
435  if (!impl) {
436  return DOMString();
437  }
438  return impl->localName();
439 }
440 
442  EventListener *listener,
443  const bool useCapture)
444 {
445  if (!impl) {
446  return;
447  }
448  if (listener) {
449  impl->addEventListener(EventName::fromString(type), listener, useCapture);
450  }
451 }
452 
454  EventListener *listener,
455  bool useCapture)
456 {
457  if (!impl) {
458  return;
459  }
460  impl->removeEventListener(EventName::fromString(type), listener, useCapture);
461 }
462 
463 bool Node::dispatchEvent(const Event &evt)
464 {
465  if (!impl) {
466  throw DOMException(DOMException::INVALID_STATE_ERR);
467  }
468 
469  if (!evt.handle()) {
470  throw DOMException(DOMException::NOT_FOUND_ERR);
471  }
472 
473  int exceptioncode = 0;
474  impl->dispatchEvent(evt.handle(), exceptioncode);
475  if (exceptioncode) {
476  throw DOMException(exceptioncode);
477  }
478  return !evt.handle()->defaultPrevented();
479 }
480 
482 {
483  if (!impl) {
484  return DOMString();
485  }
486  return impl->textContent();
487 }
488 
489 void Node::setTextContent(const DOMString &content)
490 {
491  if (!impl) {
492  throw DOMException(DOMException::NOT_FOUND_ERR);
493  }
494  int exceptioncode = 0;
495  impl->setTextContent(content, exceptioncode);
496  if (exceptioncode) {
497  throw DOMException(exceptioncode);
498  }
499 }
500 
501 unsigned Node::compareDocumentPosition(const Node &other)
502 {
503  if (!impl || !other.impl) {
504  throw DOMException(DOMException::NOT_FOUND_ERR);
505  }
506  return impl->compareDocumentPosition(other.impl);
507 }
508 
509 unsigned int Node::elementId() const
510 {
511  if (!impl) {
512  return 0;
513  }
514  return impl->id();
515 }
516 
517 unsigned long Node::index() const
518 {
519  if (!impl) {
520  return 0;
521  }
522  return impl->nodeIndex();
523 }
524 
525 #ifndef KHTML_NO_DEPRECATED
526 QString Node::toHTML()
527 {
528  if (!impl) {
529  return QString();
530  }
531  return impl->toString().string();
532 }
533 #endif
534 
535 void Node::applyChanges()
536 {
537  if (!impl) {
538  return;
539  }
540  impl->recalcStyle(NodeImpl::Inherit);
541 }
542 
543 #ifndef KHTML_NO_DEPRECATED
544 void Node::getCursor(int offset, int &_x, int &_y, int &height)
545 {
546  if (!impl) {
547  throw DOMException(DOMException::NOT_FOUND_ERR);
548  }
549  int dummy;
550  impl->getCaret(offset, false, _x, _y, dummy, height);
551 }
552 #endif
553 
555 {
556  if (!impl) {
557  throw DOMException(DOMException::NOT_FOUND_ERR);
558  }
559  return impl->getRect();
560 }
561 
562 //-----------------------------------------------------------------------------
563 
564 NodeList::NodeList()
565 {
566  impl = nullptr;
567 }
568 
569 NodeList::NodeList(const NodeList &other)
570 {
571  impl = other.impl;
572  if (impl) {
573  impl->ref();
574  }
575 }
576 
577 NodeList::NodeList(const NodeListImpl *i)
578 {
579  impl = const_cast<NodeListImpl *>(i);
580  if (impl) {
581  impl->ref();
582  }
583 }
584 
585 NodeList &NodeList::operator = (const NodeList &other)
586 {
587  if (impl != other.impl) {
588  if (impl) {
589  impl->deref();
590  }
591  impl = other.impl;
592  if (impl) {
593  impl->ref();
594  }
595  }
596  return *this;
597 }
598 
599 NodeList::~NodeList()
600 {
601  if (impl) {
602  impl->deref();
603  }
604 }
605 
606 Node NodeList::item(unsigned long index) const
607 {
608  if (!impl) {
609  return nullptr;
610  }
611  return impl->item(index);
612 }
613 
614 unsigned long NodeList::length() const
615 {
616  if (!impl) {
617  return 0;
618  }
619  return impl->length();
620 }
621 
622 //-----------------------------------------------------------------------------
623 
625 {
626  return codeAsString(code);
627 }
628 
630 {
631  switch (code) {
632  case INDEX_SIZE_ERR:
633  return DOMString("INDEX_SIZE_ERR");
634  case DOMSTRING_SIZE_ERR:
635  return DOMString("DOMSTRING_SIZE_ERR");
636  case HIERARCHY_REQUEST_ERR:
637  return DOMString("HIERARCHY_REQUEST_ERR");
638  case WRONG_DOCUMENT_ERR:
639  return DOMString("WRONG_DOCUMENT_ERR");
640  case INVALID_CHARACTER_ERR:
641  return DOMString("INVALID_CHARACTER_ERR");
642  case NO_DATA_ALLOWED_ERR:
643  return DOMString("NO_DATA_ALLOWED_ERR");
644  case NO_MODIFICATION_ALLOWED_ERR:
645  return DOMString("NO_MODIFICATION_ALLOWED_ERR");
646  case NOT_FOUND_ERR:
647  return DOMString("NOT_FOUND_ERR");
648  case NOT_SUPPORTED_ERR:
649  return DOMString("NOT_SUPPORTED_ERR");
650  case INUSE_ATTRIBUTE_ERR:
651  return DOMString("INUSE_ATTRIBUTE_ERR");
652  case INVALID_STATE_ERR:
653  return DOMString("INVALID_STATE_ERR");
654  case SYNTAX_ERR:
655  return DOMString("SYNTAX_ERR");
656  case INVALID_MODIFICATION_ERR:
657  return DOMString("INVALID_MODIFICATION_ERR");
658  case NAMESPACE_ERR:
659  return DOMString("NAMESPACE_ERR");
660  case INVALID_ACCESS_ERR:
661  return DOMString("INVALID_ACCESS_ERR");
662  case VALIDATION_ERR:
663  return DOMString("VALIDATION_ERR");
664  case TYPE_MISMATCH_ERR:
665  return DOMString("TYPE_MISMATCH_ERR");
666  case SECURITY_ERR:
667  return DOMString("SECURITY_ERR");
668  case NETWORK_ERR:
669  return DOMString("NETWORK_ERR");
670  case ABORT_ERR:
671  return DOMString("ABORT_ERR");
672  case URL_MISMATCH_ERR:
673  return DOMString("URL_MISMATCH_ERR");
674  case QUOTA_EXCEEDED_ERR:
675  return DOMString("QUOTA_EXCEEDED_ERR");
676  case TIMEOUT_ERR:
677  return DOMString("TIMEOUT_ERR");
678  case NOT_READABLE_ERR:
679  return DOMString("NOT_READABLE_ERR");
680  case DATA_CLONE_ERR:
681  return DOMString("DATA_CLONE_ERR");
682  case ENCODING_ERR:
683  return DOMString("ENCODING_ERR");
684  default:
685  return DOMString("(unknown exception code)");
686  }
687 }
688 
689 bool DOMException::isDOMExceptionCode(int exceptioncode)
690 {
691  return exceptioncode < 100;
692 }
693 
void setNodeValue(const DOMString &)
see nodeValue
Definition: dom_node.cpp:227
NodeList childNodes() const
A NodeList that contains all children of this node.
Definition: dom_node.cpp:258
Node setNamedItemNS(const Node &arg)
Introduced in DOM Level 2.
Definition: dom_node.cpp:127
Document ownerDocument() const
The Document object associated with this node.
Definition: dom_node.cpp:306
void removeEventListener(const DOMString &type, EventListener *listener, bool useCapture)
Introduced in DOM Level 2 This method is from the EventTarget interface.
Definition: dom_node.cpp:453
DOMString nodeValue() const
The value of this node, depending on its type; see the table above.
Definition: dom_node.cpp:218
KHTML_DEPRECATED void getCursor(int offset, int &_x, int &_y, int &height)
Definition: dom_node.cpp:544
quint32 elementId() const
Definition: dom_node.cpp:509
Introduced in DOM Level 2.
Definition: dom2_events.h:116
unsigned long length() const
The number of nodes in the map.
Definition: dom_node.cpp:153
Introduced in DOM Level 2.
Definition: dom2_events.h:69
bool hasChildNodes()
This is a convenience method to allow easy determination of whether a node has any children.
Definition: dom_node.cpp:375
QRect getRect()
not part of the DOM.
Definition: dom_node.cpp:554
static bool isDOMExceptionCode(int exceptioncode)
Definition: dom_node.cpp:689
Node replaceChild(const Node &newChild, const Node &oldChild)
Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node...
Definition: dom_node.cpp:327
bool dispatchEvent(const Event &evt)
Introduced in DOM Level 2 This method is from the EventTarget interface.
Definition: dom_node.cpp:463
Node lastChild() const
The last child of this node.
Definition: dom_node.cpp:274
Node removeNamedItemNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2.
Definition: dom_node.cpp:140
Node appendChild(const Node &newChild)
Adds the node newChild to the end of the list of children of this node.
Definition: dom_node.cpp:354
unsigned long length() const
The number of nodes in the list.
Definition: dom_node.cpp:614
This library provides a full-featured HTML parser and widget.
void setTextContent(const DOMString &text)
see textContent()
Definition: dom_node.cpp:489
Node setNamedItem(const Node &arg)
Adds a node using its nodeName attribute.
Definition: dom_node.cpp:84
Node removeChild(const Node &oldChild)
Removes the child node indicated by oldChild from the list of children, and returns it.
Definition: dom_node.cpp:340
DOMString nodeName() const
The name of this node, depending on its type; see the table above.
Definition: dom_node.cpp:210
Node removeNamedItem(const DOMString &name)
Removes a node specified by name.
Definition: dom_node.cpp:98
DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation is impos...
Definition: dom_exception.h:58
void addEventListener(const DOMString &type, EventListener *listener, const bool useCapture)
Introduced in DOM Level 2 This method is from the EventTarget interface.
Definition: dom_node.cpp:441
DOMString namespaceURI() const
Introduced in DOM Level 2.
Definition: dom_node.cpp:405
NamedNodeMap attributes() const
A NamedNodeMap containing the attributes of this node (if it is an Element ) or null otherwise.
Definition: dom_node.cpp:298
The Document interface represents the entire HTML or XML document.
Definition: dom_doc.h:246
Node cloneNode(bool deep)
Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes.
Definition: dom_node.cpp:383
unsigned long index() const
Definition: dom_node.cpp:517
DOMString codeAsString() const
Returns the name of this error.
Definition: dom_node.cpp:624
Node getNamedItemNS(const DOMString &namespaceURI, const DOMString &localName) const
Introduced in DOM Level 2.
Definition: dom_node.cpp:119
Node parentNode() const
The parent of this node.
Definition: dom_node.cpp:250
DOMString textContent() const
Introduced in DOM Level 3.
Definition: dom_node.cpp:481
Node getNamedItem(const DOMString &name) const
Retrieves a node specified by name.
Definition: dom_node.cpp:76
void normalize()
Modified in DOM Level 2.
Definition: dom_node.cpp:391
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
DOMString localName() const
Introduced in DOM Level 2.
Definition: dom_node.cpp:433
bool hasAttributes()
Returns whether this node (if it is an element) has any attributes.
Definition: dom_node.cpp:367
The NodeList interface provides the abstraction of an ordered collection of nodes,...
Definition: dom_node.h:976
void setPrefix(const DOMString &prefix)
see prefix
Definition: dom_node.cpp:421
Node item(unsigned long index) const
Returns the index th item in the collection.
Definition: dom_node.cpp:606
Objects implementing the NamedNodeMap interface are used to represent collections of nodes that can b...
Definition: dom_node.h:64
The Node interface is the primary datatype for the entire Document Object Model.
Definition: dom_node.h:278
DOMString prefix() const
Introduced in DOM Level 2.
Definition: dom_node.cpp:413
unsigned short nodeType() const
A code representing the type of the underlying object, as defined above.
Definition: dom_node.cpp:242
Node nextSibling() const
The node immediately following this node.
Definition: dom_node.cpp:290
Node item(unsigned long index) const
Returns the index th item in the map.
Definition: dom_node.cpp:111
EventImpl * handle() const
bool isSupported(const DOMString &feature, const DOMString &version) const
Introduced in DOM Level 2.
Definition: dom_node.cpp:399
Node insertBefore(const Node &newChild, const Node &refChild)
Inserts the node newChild before the existing child node refChild .
Definition: dom_node.cpp:314
Node previousSibling() const
The node immediately preceding this node.
Definition: dom_node.cpp:282
Node firstChild() const
The first child of this node.
Definition: dom_node.cpp:266
unsigned compareDocumentPosition(const DOM::Node &other)
Introduced in DOM Level 3.
Definition: dom_node.cpp:501
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Dec 11 2023 04:06:48 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.