• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KHTML

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

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal