KHtml

dom_element.cpp
1 
23 #include "dom/dom_exception.h"
24 #include "xml/dom_docimpl.h"
25 #include "xml/dom_elementimpl.h"
26 #include "html/html_formimpl.h"
27 
28 using namespace DOM;
29 
30 Attr::Attr() : Node()
31 {
32 }
33 
34 Attr::Attr(const Attr &other) : Node(other)
35 {
36 }
37 
38 Attr::Attr(AttrImpl *_impl)
39 {
40  impl = _impl;
41  if (impl) {
42  impl->ref();
43  }
44 }
45 
46 Attr &Attr::operator = (const Node &other)
47 {
48  NodeImpl *ohandle = other.handle();
49  if (impl != ohandle) {
50  if (!ohandle || !ohandle->isAttributeNode()) {
51  if (impl) {
52  impl->deref();
53  }
54  impl = nullptr;
55  } else {
56  Node::operator =(other);
57  }
58  }
59  return *this;
60 }
61 
62 Attr &Attr::operator = (const Attr &other)
63 {
64  Node::operator =(other);
65  return *this;
66 }
67 
68 Attr::~Attr()
69 {
70 }
71 
73 {
74  if (!impl) {
75  throw DOMException(DOMException::NOT_FOUND_ERR);
76  }
77  return ((AttrImpl *)impl)->name();
78 }
79 
80 bool Attr::specified() const
81 {
82  if (impl) {
83  return ((AttrImpl *)impl)->specified();
84  }
85  return 0;
86 }
87 
89 {
90  if (!impl) {
91  return nullptr;
92  }
93  return static_cast<AttrImpl *>(impl)->ownerElement();
94 }
95 
97 {
98  if (!impl) {
99  throw DOMException(DOMException::NOT_FOUND_ERR);
100  }
101  return impl->nodeValue();
102 }
103 
104 void Attr::setValue(const DOMString &newValue)
105 {
106  if (!impl) {
107  return;
108  }
109 
110  int exceptioncode = 0;
111  ((AttrImpl *)impl)->setValue(newValue, exceptioncode);
112  if (exceptioncode) {
113  throw DOMException(exceptioncode);
114  }
115 }
116 
117 // ---------------------------------------------------------------------------
118 
119 Element::Element() : Node()
120 {
121 }
122 
123 Element::Element(const Element &other) : Node(other)
124 {
125 }
126 
127 Element::Element(ElementImpl *impl) : Node(impl)
128 {
129 }
130 
131 Element &Element::operator = (const Node &other)
132 {
133  NodeImpl *ohandle = other.handle();
134  if (impl != ohandle) {
135  if (!ohandle || !ohandle->isElementNode()) {
136  if (impl) {
137  impl->deref();
138  }
139  impl = nullptr;
140  } else {
141  Node::operator =(other);
142  }
143  }
144  return *this;
145 }
146 
147 Element &Element::operator = (const Element &other)
148 {
149  Node::operator =(other);
150  return *this;
151 }
152 
153 Element::~Element()
154 {
155 }
156 
158 {
159  if (!impl) {
160  throw DOMException(DOMException::NOT_FOUND_ERR);
161  }
162  return static_cast<ElementImpl *>(impl)->tagName();
163 }
164 
166 {
167  // ### getAttribute() and getAttributeNS() are supposed to return the empty string if the attribute
168  // does not exist. However, there are a number of places around khtml that expect a null string
169  // for nonexistent attributes. These need to be changed to use hasAttribute() instead.
170  if (!impl) {
171  throw DOMException(DOMException::NOT_FOUND_ERR);
172  }
173  if (!name.implementation()) {
174  throw DOMException(DOMException::NOT_FOUND_ERR);
175  }
176 
177  return static_cast<ElementImpl *>(impl)->getAttribute(name);
178 }
179 
180 void Element::setAttribute(const DOMString &name, const DOMString &value)
181 {
182  if (!impl) {
183  throw DOMException(DOMException::NOT_FOUND_ERR);
184  }
185  int exceptioncode = 0;
186  static_cast<ElementImpl *>(impl)->setAttribute(name, value, exceptioncode);
187  if (exceptioncode) {
188  throw DOMException(exceptioncode);
189  }
190 }
191 
193 {
194  if (!impl) {
195  throw DOMException(DOMException::NOT_FOUND_ERR);
196  }
197 
198  int exceptioncode = 0;
199  static_cast<ElementImpl *>(impl)->removeAttribute(name, exceptioncode);
200  // it's allowed to remove attributes that don't exist.
201  if (exceptioncode && exceptioncode != DOMException::NOT_FOUND_ERR) {
202  throw DOMException(exceptioncode);
203  }
204 }
205 
207 {
208  if (!impl) {
209  throw DOMException(DOMException::NOT_FOUND_ERR);
210  }
211  if (!name.implementation()) {
212  throw DOMException(DOMException::NOT_FOUND_ERR);
213  }
214 
215  return static_cast<ElementImpl *>(impl)->getAttributeNode(name);
216 }
217 
219 {
220  if (!impl || newAttr.isNull()) {
221  throw DOMException(DOMException::NOT_FOUND_ERR);
222  }
223  // WRONG_DOCUMENT_ERR and INUSE_ATTRIBUTE_ERR are already tested & thrown by setNamedItem
224 
225  int exceptioncode = 0;
226  Attr r = static_cast<ElementImpl *>(impl)->setAttributeNode(
227  static_cast<AttrImpl *>(newAttr.handle()), exceptioncode);
228  if (exceptioncode) {
229  throw DOMException(exceptioncode);
230  }
231  return r;
232 }
233 
235 {
236  int exceptioncode = 0;
237  if (!impl) {
238  throw DOMException(DOMException::NOT_FOUND_ERR);
239  }
240 
241  Attr ret = static_cast<ElementImpl *>(impl)->removeAttributeNode(
242  static_cast<AttrImpl *>(oldAttr.handle()), exceptioncode);
243  if (exceptioncode) {
244  throw DOMException(exceptioncode);
245  }
246 
247  return ret;
248 }
249 
251 {
252  if (!impl) {
253  return nullptr;
254  }
255  return static_cast<ElementImpl *>(impl)->getElementsByTagName(tagName);
256 }
257 
259  const DOMString &localName)
260 {
261  if (!impl) {
262  return nullptr;
263  }
264  return static_cast<ElementImpl *>(impl)->getElementsByTagNameNS(namespaceURI, localName);
265 }
266 
268 {
269  if (!impl) {
270  return nullptr;
271  }
272  return impl->getElementsByClassName(className);
273 }
274 
276  const DOMString &localName)
277 {
278  if (!impl) {
279  throw DOMException(DOMException::NOT_FOUND_ERR);
280  }
281  ElementImpl *e = static_cast<ElementImpl *>(impl);
282  int exceptioncode = 0;
283  DOMString ret = e->getAttributeNS(namespaceURI, localName, exceptioncode);
284  if (exceptioncode) {
285  throw DOMException(exceptioncode);
286  }
287  return ret;
288 }
289 
290 void Element::setAttributeNS(const DOMString &namespaceURI,
291  const DOMString &qualifiedName,
292  const DOMString &value)
293 {
294  if (!impl) {
295  throw DOMException(DOMException::NOT_FOUND_ERR);
296  }
297 
298  int exceptioncode = 0;
299  static_cast<ElementImpl *>(impl)->setAttributeNS(namespaceURI, qualifiedName, value, exceptioncode);
300  if (exceptioncode) {
301  throw DOMException(exceptioncode);
302  }
303 }
304 
305 void Element::removeAttributeNS(const DOMString &namespaceURI,
306  const DOMString &localName)
307 {
308  if (!impl) {
309  throw DOMException(DOMException::NOT_FOUND_ERR);
310  }
311 
312  int exceptioncode = 0;
313  static_cast<ElementImpl *>(impl)->removeAttributeNS(namespaceURI, localName, exceptioncode);
314  if (exceptioncode) {
315  throw DOMException(exceptioncode);
316  }
317 }
318 
320  const DOMString &localName)
321 {
322  if (!impl) {
323  throw DOMException(DOMException::NOT_FOUND_ERR);
324  }
325 
326  int exceptioncode = 0;
327  Attr r = static_cast<ElementImpl *>(impl)->getAttributeNodeNS(namespaceURI, localName, exceptioncode);
328  if (exceptioncode) {
329  throw DOMException(exceptioncode);
330  }
331  return r;
332 }
333 
335 {
336  if (!impl) {
337  throw DOMException(DOMException::NOT_FOUND_ERR);
338  }
339 
340  int exceptioncode = 0;
341  Attr r = static_cast<ElementImpl *>(impl)->setAttributeNodeNS(
342  static_cast<AttrImpl *>(newAttr.handle()), exceptioncode);
343  return r;
344 }
345 
347 {
348  if (!impl) {
349  return false; // ### throw ?
350  }
351  return static_cast<ElementImpl *>(impl)->hasAttribute(name);
352 }
353 
354 bool Element::hasAttributeNS(const DOMString &namespaceURI,
355  const DOMString &localName)
356 {
357  if (!impl) {
358  return false;
359  }
360  return static_cast<ElementImpl *>(impl)->hasAttributeNS(namespaceURI, localName);
361 }
362 
364 {
365  if (!impl) {
366  return false;
367  }
368  return ((ElementImpl *)impl)->isHTMLElement();
369 }
370 
372 {
373  if (!impl || !impl->isGenericFormElement()) {
374  return nullptr;
375  }
376  return static_cast<HTMLGenericFormElementImpl *>(impl)->form();
377  ElementImpl *f = static_cast<HTMLGenericFormElementImpl *>(impl)->form();
378 
379  if (f && f->implicitNode()) {
380  return nullptr;
381  }
382  return f;
383 }
384 
386 {
387  if (impl) {
388  return ((ElementImpl *)impl)->getInlineStyleDecls();
389  }
390  return nullptr;
391 }
392 
394 {
395  if (!impl) {
396  throw DOMException(DOMException::NOT_FOUND_ERR);
397  }
398  return static_cast<ElementImpl *>(impl)->firstElementChild();
399 }
400 
402 {
403  if (!impl) {
404  throw DOMException(DOMException::NOT_FOUND_ERR);
405  }
406  return static_cast<ElementImpl *>(impl)->lastElementChild();
407 }
408 
410 {
411  if (!impl) {
412  throw DOMException(DOMException::NOT_FOUND_ERR);
413  }
414  return static_cast<ElementImpl *>(impl)->previousElementSibling();
415 }
416 
418 {
419  if (!impl) {
420  throw DOMException(DOMException::NOT_FOUND_ERR);
421  }
422  return static_cast<ElementImpl *>(impl)->nextElementSibling();
423 }
424 
425 unsigned long Element::childElementCount() const
426 {
427  if (!impl) {
428  throw DOMException(DOMException::NOT_FOUND_ERR);
429  }
430  return static_cast<ElementImpl *>(impl)->childElementCount();
431 }
432 
434 {
435  int ec = 0;
436  if (!impl) {
437  throw DOMException(DOMException::NOT_FOUND_ERR);
438  }
439  Element res = impl->querySelector(query, ec).get();
440  if (ec) {
441  throw DOMException(ec);
442  }
443  return res;
444 }
445 
447 {
448  int ec = 0;
449  if (!impl) {
450  throw DOMException(DOMException::NOT_FOUND_ERR);
451  }
452  NodeList res = impl->querySelectorAll(query, ec).get();
453  if (ec) {
454  throw DOMException(ec);
455  }
456  return res;
457 }
458 
459 static inline bool isExtender(ushort c)
460 {
461  return c > 0x00B6 &&
462  (c == 0x00B7 || c == 0x02D0 || c == 0x02D1 || c == 0x0387 ||
463  c == 0x0640 || c == 0x0E46 || c == 0x0EC6 || c == 0x3005 ||
464  (c >= 0x3031 && c <= 0x3035) ||
465  (c >= 0x309D && c <= 0x309E) ||
466  (c >= 0x30FC && c <= 0x30FE)
467  );
468 }
469 
470 bool Element::khtmlValidAttrName(const DOMString &name)
471 {
472  // Check if name is valid
473  // https://www.w3.org/TR/2000/REC-xml-20001006#NT-Name
474  DOMStringImpl *_name = name.implementation();
475  QChar ch = _name->s[0];
476  if (!ch.isLetter() && ch != '_' && ch != ':') {
477  return false; // first char isn't valid
478  }
479  for (uint i = 0; i < _name->l; ++i) {
480  ch = _name->s[i];
481  if (!ch.isLetter() && !ch.isDigit() && ch != '.'
482  && ch != '-' && ch != '_' && ch != ':'
484  && !isExtender(ch.unicode())) {
485  return false;
486  }
487  }
488  return true;
489 }
490 
491 bool Element::khtmlValidPrefix(const DOMString &name)
492 {
493  // Null prefix is ok. If not null, reuse code from khtmlValidAttrName
494  return !name.implementation() || khtmlValidAttrName(name);
495 }
496 
497 bool Element::khtmlValidQualifiedName(const DOMString &name)
498 {
499  return khtmlValidAttrName(name);
500 }
501 
502 bool Element::khtmlMalformedQualifiedName(const DOMString &name)
503 {
504  // #### see XML Namespaces spec for possibly more
505 
506  // ### does this disctinction make sense?
507  if (name.isNull()) {
508  return true;
509  }
510  if (name.isEmpty()) {
511  return false;
512  }
513 
514  // a prefix is optional but both prefix as well as local part
515  // cannot be empty
516  int colonpos = name.find(':');
517  if (colonpos == 0 || colonpos == name.length() - 1) {
518  return true;
519  }
520 
521  return false;
522 }
523 
524 bool Element::khtmlMalformedPrefix(const DOMString &/*name*/)
525 {
526  // ####
527  return false;
528 }
NodeList getElementsByTagNameNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2 Returns a NodeList of all the descendant Elements with a given local name a...
DOMString tagName() const
The name of the element.
The Node interface is the primary datatype for the entire Document Object Model.
Definition: dom_node.h:278
CSSStyleDeclaration style()
Introduced in DOM Level 2 This method is from the CSSStyleDeclaration interface.
Mark_SpacingCombining
Element firstElementChild() const
Introduced in DOM level 3 This method is part of the ElementTraversal interface.
Attr getAttributeNodeNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2.
The CSSStyleDeclaration interface represents a single CSS declaration block .
Definition: css_value.h:59
DOMString value() const
On retrieval, the value of the attribute is returned as a string.
Definition: dom_element.cpp:96
bool isDigit() const const
Element querySelector(const DOMString &query) const
Introduced in Selectors Level 1.
Element nextElementSibling() const
Introduced in DOM level 3 This method is part of the ElementTraversal interface.
void setAttributeNS(const DOMString &namespaceURI, const DOMString &qualifiedName, const DOMString &value)
Introduced in DOM Level 2.
QChar::Category category() const const
Element previousElementSibling() const
Introduced in DOM level 3 This method is part of the ElementTraversal interface.
DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation is impos...
Definition: dom_exception.h:58
bool isLetter() const const
NodeList getElementsByTagName(const DOMString &name)
Returns a NodeList of all descendant elements with a given tag name, in the order in which they would...
Attr setAttributeNodeNS(const Attr &newAttr)
Introduced in DOM Level 2.
void removeAttribute(const DOMString &name)
Removes an attribute by name.
void setAttribute(const DOMString &name, const DOMString &value)
Adds a new attribute.
By far the vast majority of objects (apart from text) that authors encounter when traversing a docume...
Definition: dom_element.h:212
The Attr interface represents an attribute in an Element object.
Definition: dom_element.h:89
bool hasAttributeNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2.
bool isHTMLElement() const
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
NodeList getElementsByClassName(const DOMString &className)
Introduced in HTML 5.
NodeList querySelectorAll(const DOMString &query) const
Introduced in Selectors Level 1.
ushort unicode() const const
void setValue(const DOMString &)
see value
bool specified() const
If this attribute was explicitly given a value in the original document, this is true ; otherwise...
Definition: dom_element.cpp:80
The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented.
Definition: dom_node.h:976
This library provides a full-featured HTML parser and widget.
unsigned long childElementCount() const
Introduced in DOM level 3 This method is part of the ElementTraversal interface.
Element ownerElement() const
Introduced in DOM Level 2.
Definition: dom_element.cpp:88
void removeAttributeNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2.
DOMString getAttribute(const DOMString &name)
Retrieves an attribute value by name.
Element lastElementChild() const
Introduced in DOM level 3 This method is part of the ElementTraversal interface.
DOMString getAttributeNS(const DOMString &namespaceURI, const DOMString &localName)
Introduced in DOM Level 2.
Element form() const
KHTML extension to DOM This method returns the associated form element.
DOMStringImpl * implementation() const
Definition: dom_string.h:145
bool hasAttribute(const DOMString &name)
Returns true when an attribute with a given name is specified on this element or has a default value...
DOMString name() const
Returns the name of this attribute.
Definition: dom_element.cpp:72
Attr setAttributeNode(const Attr &newAttr)
Adds a new attribute.
Attr getAttributeNode(const DOMString &name)
Retrieves an Attr node by name.
NodeImpl * handle() const
Definition: dom_node.h:936
Attr removeAttributeNode(const Attr &oldAttr)
Removes the specified attribute.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 8 2020 22:44:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.