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

KHTML

  • 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 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sun Apr 28 2013 14:30:06 by doxygen 1.8.1.1 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-4.10.2 API Reference

Skip menu "kdelibs-4.10.2 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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