implementation of ElementTraversal interface
[kdelibs.git] / khtml / dom / dom_element.h
blob4986c740559e31b1933a647a3df83d367fb90dd7
1 /*
2 * This file is part of the DOM implementation for KDE.
4 * Copyright 1999 Lars Knoll (knoll@kde.org)
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.
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.
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.
21 * This file includes excerpts from the Document Object Model (DOM)
22 * Level 1 Specification (Recommendation)
23 * http://www.w3.org/TR/REC-DOM-Level-1/
24 * Copyright © World Wide Web Consortium , (Massachusetts Institute of
25 * Technology , Institut National de Recherche en Informatique et en
26 * Automatique , Keio University ). All Rights Reserved.
29 #ifndef _DOM_ELEMENT_h_
30 #define _DOM_ELEMENT_h_
32 #include <khtml_export.h>
33 #include <dom/dom_node.h>
34 #include <dom/css_value.h>
36 namespace DOM {
38 class DOMString;
39 class AttrImpl;
40 class Element;
41 class ElementImpl;
42 class NamedAttrMapImpl;
43 class DocumentImpl;
45 /**
46 * The \c Attr interface represents an attribute in an
47 * \c Element object. Typically the allowable values for
48 * the attribute are defined in a document type definition.
50 * \c Attr objects inherit the \c Node
51 * interface, but since they are not actually child nodes of the
52 * element they describe, the DOM does not consider them part of the
53 * document tree. Thus, the \c Node attributes
54 * \c parentNode , \c previousSibling , and
55 * \c nextSibling have a null value for \c Attr
56 * objects. The DOM takes the view that attributes are properties of
57 * elements rather than having a separate identity from the elements
58 * they are associated with; this should make it more efficient to
59 * implement such features as default attributes associated with all
60 * elements of a given type. Furthermore, \c Attr nodes
61 * may not be immediate children of a \c DocumentFragment
62 * . However, they can be associated with \c Element nodes
63 * contained within a \c DocumentFragment . In short,
64 * users and implementors of the DOM need to be aware that \c Attr
65 * nodes have some things in common with other objects
66 * inheriting the \c Node interface, but they also are
67 * quite distinct.
69 * The attribute's effective value is determined as follows: if this
70 * attribute has been explicitly assigned any value, that value is the
71 * attribute's effective value; otherwise, if there is a declaration
72 * for this attribute, and that declaration includes a default value,
73 * then that default value is the attribute's effective value;
74 * otherwise, the attribute does not exist on this element in the
75 * structure model until it has been explicitly added. Note that the
76 * \c nodeValue attribute on the \c Attr
77 * instance can also be used to retrieve the string version of the
78 * attribute's value(s).
80 * In XML, where the value of an attribute can contain entity
81 * references, the child nodes of the \c Attr node provide
82 * a representation in which entity references are not expanded. These
83 * child nodes may be either \c Text or
84 * \c EntityReference nodes. Because the attribute type may be
85 * unknown, there are no tokenized attribute values.
88 class KHTML_EXPORT Attr : public Node
90 friend class Element;
91 friend class Document;
92 friend class DocumentImpl;
93 friend class HTMLDocument;
94 friend class ElementImpl;
95 friend class NamedAttrMapImpl;
96 friend class AttrImpl;
98 public:
99 Attr();
100 Attr(const Node &other) : Node()
101 {(*this)=other;}
102 Attr(const Attr &other);
104 Attr & operator = (const Node &other);
105 Attr & operator = (const Attr &other);
107 ~Attr();
110 * Returns the name of this attribute.
113 DOMString name() const;
116 * If this attribute was explicitly given a value in the original
117 * document, this is \c true ; otherwise, it is
118 * \c false . Note that the implementation is in charge of
119 * this attribute, not the user. If the user changes the value of
120 * the attribute (even if it ends up having the same value as the
121 * default value) then the \c specified flag is
122 * automatically flipped to \c true . To re-specify
123 * the attribute as the default value from the DTD, the user must
124 * delete the attribute. The implementation will then make a new
125 * attribute available with \c specified set to
126 * \c false and the default value (if one exists).
128 * In summary:
129 * \li If the attribute has an assigned
130 * value in the document then \c specified is
131 * \c true , and the value is the assigned value.
133 * \li If the attribute has no assigned value in the
134 * document and has a default value in the DTD, then
135 * \c specified is \c false , and the value is
136 * the default value in the DTD.
138 * \li If the attribute has no assigned value in the
139 * document and has a value of #IMPLIED in the DTD, then the
140 * attribute does not appear in the structure model of the
141 * document.
146 bool specified() const;
149 * On retrieval, the value of the attribute is returned as a
150 * string. Character and general entity references are replaced
151 * with their values.
153 * On setting, this creates a \c Text node with the
154 * unparsed contents of the string.
157 DOMString value() const;
160 * see value
162 void setValue( const DOMString & );
165 * Introduced in DOM Level 2
167 * The Element node this attribute is attached to or null if this attribute
168 * is not in use.
170 Element ownerElement() const;
172 protected:
174 Attr( AttrImpl *_impl );
177 class NodeList;
178 class Attr;
179 class DOMString;
182 * By far the vast majority of objects (apart from text) that authors
183 * encounter when traversing a document are \c Element
184 * nodes. Assume the following XML document: &lt;elementExample
185 * id=&quot;demo&quot;&gt; &lt;subelement1/&gt;
186 * &lt;subelement2&gt;&lt;subsubelement/&gt;&lt;/subelement2&gt;
187 * &lt;/elementExample&gt;
189 * When represented using DOM, the top node is an \c Element
190 * node for &quot;elementExample&quot;, which contains two
191 * child \c Element nodes, one for &quot;subelement1&quot;
192 * and one for &quot;subelement2&quot;. &quot;subelement1&quot;
193 * contains no child nodes.
195 * Elements may have attributes associated with them; since the
196 * \c Element interface inherits from \c Node
197 * , the generic \c Node interface method
198 * \c getAttributes may be used to retrieve the set of all
199 * attributes for an element. There are methods on the \c Element
200 * interface to retrieve either an \c Attr object
201 * by name or an attribute value by name. In XML, where an attribute
202 * value may contain entity references, an \c Attr object
203 * should be retrieved to examine the possibly fairly complex sub-tree
204 * representing the attribute value. On the other hand, in HTML, where
205 * all attributes have simple string values, methods to directly
206 * access an attribute value can safely be used as a convenience.
209 class KHTML_EXPORT Element : public Node
211 friend class Document;
212 friend class HTMLDocument;
213 // friend class AttrImpl;
214 friend class Attr;
216 public:
217 Element();
218 Element(const Node &other) : Node()
219 {(*this)=other;}
220 Element(const Element &other);
222 Element & operator = (const Node &other);
223 Element & operator = (const Element &other);
225 ~Element();
228 * The name of the element. For example, in: &lt;elementExample
229 * id=&quot;demo&quot;&gt; ... &lt;/elementExample&gt; ,
230 * \c tagName has the value \c &quot;elementExample&quot;
231 * . Note that this is case-preserving in XML, as are all
232 * of the operations of the DOM. The HTML DOM returns the
233 * \c tagName of an HTML element in the canonical uppercase
234 * form, regardless of the case in the source HTML document.
237 DOMString tagName() const;
240 * Retrieves an attribute value by name.
242 * @param name The name of the attribute to retrieve.
244 * @return The \c Attr value as a string, or the empty
245 * string if that attribute does not have a specified or default
246 * value.
249 DOMString getAttribute ( const DOMString &name );
252 * Adds a new attribute. If an attribute with that name is already
253 * present in the element, its value is changed to be that of the
254 * value parameter. This value is a simple string, it is not
255 * parsed as it is being set. So any markup (such as syntax to be
256 * recognized as an entity reference) is treated as literal text,
257 * and needs to be appropriately escaped by the implementation
258 * when it is written out. In order to assign an attribute value
259 * that contains entity references, the user must create an
260 * \c Attr node plus any \c Text and
261 * \c EntityReference nodes, build the appropriate subtree,
262 * and use \c setAttributeNode to assign it as the
263 * value of an attribute.
265 * @param name The name of the attribute to create or alter.
267 * @param value Value to set in string form.
269 * @return
271 * @exception DOMException
272 * INVALID_CHARACTER_ERR: Raised if the specified name contains an
273 * invalid character.
275 * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
278 void setAttribute ( const DOMString &name, const DOMString &value );
281 * Removes an attribute by name. If the removed attribute has a
282 * default value it is immediately replaced.
284 * @param name The name of the attribute to remove.
286 * @return
288 * @exception DOMException
289 * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
292 void removeAttribute ( const DOMString &name );
295 * Retrieves an \c Attr node by name.
297 * @param name The name of the attribute to retrieve.
299 * @return The \c Attr node with the specified
300 * attribute name or \c null if there is no such
301 * attribute.
304 Attr getAttributeNode ( const DOMString &name );
307 * Adds a new attribute. If an attribute with that name is already
308 * present in the element, it is replaced by the new one.
310 * @param newAttr The \c Attr node to add to the
311 * attribute list.
313 * @return If the \c newAttr attribute replaces an
314 * existing attribute with the same name, the previously existing
315 * \c Attr node is returned, otherwise \c null
316 * is returned.
318 * @exception DOMException
319 * WRONG_DOCUMENT_ERR: Raised if \c newAttr was
320 * created from a different document than the one that created the
321 * element.
323 * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
325 * INUSE_ATTRIBUTE_ERR: Raised if \c newAttr is
326 * already an attribute of another \c Element object.
327 * The DOM user must explicitly clone \c Attr nodes to
328 * re-use them in other elements.
331 Attr setAttributeNode ( const Attr &newAttr );
334 * Removes the specified attribute.
336 * @param oldAttr The \c Attr node to remove from the
337 * attribute list. If the removed \c Attr has a
338 * default value it is immediately replaced.
340 * @return The \c Attr node that was removed.
342 * @exception DOMException
343 * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
345 * NOT_FOUND_ERR: Raised if \c oldAttr is not an
346 * attribute of the element.
349 Attr removeAttributeNode ( const Attr &oldAttr );
352 * Returns a \c NodeList of all descendant elements
353 * with a given tag name, in the order in which they would be
354 * encountered in a preorder traversal of the \c Element
355 * tree.
357 * @param name The name of the tag to match on. The special value
358 * "*" matches all tags.
360 * @return A list of matching \c Element nodes.
363 NodeList getElementsByTagName ( const DOMString &name );
366 * Introduced in DOM Level 2
367 * Returns a NodeList of all the descendant Elements with a given local
368 * name and namespace URI in the order in which they are encountered in a
369 * preorder traversal of this Element tree.
371 * @param namespaceURI The namespace URI of the elements to match on. The
372 * special value "*" matches all namespaces.
374 * @param localName The local name of the elements to match on. The special
375 * value "*" matches all local names.
377 * @return A new NodeList object containing all the matched Elements.
379 NodeList getElementsByTagNameNS ( const DOMString &namespaceURI,
380 const DOMString &localName );
383 * Introduced in HTML 5.
384 * No Exceptions.
386 * Returns a \c NodeList of all the \c Element 's
387 * with a given class name in the order in which they
388 * would be encountered in a preorder traversal of the
389 * \c Document tree.
391 * @param tagname An unordered set of unique space-separated
392 * tokens representing classes.
394 * @return A new \c NodeList object containing all the
395 * matched \c Element s.
397 * @since 4.1
399 NodeList getElementsByClassName ( const DOMString &className );
402 * Introduced in DOM Level 2.
404 * No Exceptions.
406 * Retrieves an attribute value by local name and namespace URI. HTML-only
407 * DOM implementations do not need to implement this method.
409 * @param namespaceURI The namespace URI of the attribute to retrieve.
411 * @param localName The local name of the attribute to retrieve.
413 * @return The Attr value as a string, or the empty string if that
414 * attribute does not have a specified or default value.
416 DOMString getAttributeNS ( const DOMString &namespaceURI,
417 const DOMString &localName );
420 * Introduced in DOM Level 2
422 * Adds a new attribute. If an attribute with the same local name and
423 * namespace URI is already present on the element, its prefix is changed
424 * to be the prefix part of the qualifiedName, and its value is changed to
425 * be the value parameter. This value is a simple string; it is not parsed
426 * as it is being set. So any markup (such as syntax to be recognized as an
427 * entity reference) is treated as literal text, and needs to be
428 * appropriately escaped by the implementation when it is written out. In
429 * order to assign an attribute value that contains entity references, the
430 * user must create an Attr node plus any Text and EntityReference nodes,
431 * build the appropriate subtree, and use setAttributeNodeNS or
432 * setAttributeNode to assign it as the value of an attribute.
434 * HTML-only DOM implementations do not need to implement this method.
436 * @param namespaceURI The namespace URI of the attribute to create or
437 * alter.
439 * @param qualifiedName The qualified name of the attribute to create or
440 * alter.
442 * @param value The value to set in string form.
444 * @exception DOMException
445 * INVALID_CHARACTER_ERR: Raised if the specified qualified name contains
446 * an illegal character.
448 * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
450 * NAMESPACE_ERR: Raised if the qualifiedName is malformed, if the
451 * qualifiedName has a prefix and the namespaceURI is null, if the
452 * qualifiedName has a prefix that is "xml" and the namespaceURI is
453 * different from "http://www.w3.org/XML/1998/namespace", or if the
454 * qualifiedName is "xmlns" and the namespaceURI is different from
455 * "http://www.w3.org/2000/xmlns/".
457 void setAttributeNS ( const DOMString &namespaceURI,
458 const DOMString &qualifiedName,
459 const DOMString &value );
462 * Introduced in DOM Level 2
464 * Removes an attribute by local name and namespace URI. If the removed
465 * attribute has a default value it is immediately replaced. The replacing
466 * attribute has the same namespace URI and local name, as well as the
467 * original prefix.
469 * HTML-only DOM implementations do not need to implement this method.
471 * @param namespaceURI The namespace URI of the attribute to remove.
473 * @param localName The local name of the attribute to remove.
475 * @exception DOMException
476 * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
478 void removeAttributeNS ( const DOMString &namespaceURI,
479 const DOMString &localName );
482 * Introduced in DOM Level 2
484 * Retrieves an Attr node by local name and namespace URI. HTML-only DOM
485 * implementations do not need to implement this method.
487 * @param namespaceURI The namespace URI of the attribute to retrieve.
489 * @param localName The local name of the attribute to retrieve.
491 * @return The Attr node with the specified attribute local name and
492 * namespace URI or null if there is no such attribute.
494 Attr getAttributeNodeNS ( const DOMString &namespaceURI,
495 const DOMString &localName );
498 * Introduced in DOM Level 2
500 * Adds a new attribute. If an attribute with that local name and that
501 * namespace URI is already present in the element, it is replaced by the
502 * new one.
504 * HTML-only DOM implementations do not need to implement this method.
506 * @param newAttr The Attr node to add to the attribute list.
508 * @return If the newAttr attribute replaces an existing attribute with the
509 * same local name and namespace URI, the replaced Attr node is returned,
510 * otherwise null is returned.
512 * @exception DOMException
513 * WRONG_DOCUMENT_ERR: Raised if newAttr was created from a different
514 * document than the one that created the element.
516 * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
518 * INUSE_ATTRIBUTE_ERR: Raised if newAttr is already an attribute of
519 * another Element object. The DOM user must explicitly clone Attr nodes to
520 * re-use them in other elements.
522 Attr setAttributeNodeNS ( const Attr &newAttr );
525 * Returns true when an attribute with a given name is specified on this
526 * element or has a default value, false otherwise.
527 * Introduced in DOM Level 2.
529 * @param name The name of the attribute to look for.
531 * @return true if an attribute with the given name is specified on this
532 * element or has a default value, false otherwise.
534 bool hasAttribute( const DOMString& name );
537 * Introduced in DOM Level 2
539 * Returns true when an attribute with a given local name and namespace URI
540 * is specified on this element or has a default value, false otherwise.
541 * HTML-only DOM implementations do not need to implement this method.
543 * @param namespaceURI The namespace URI of the attribute to look for.
545 * @param localName The local name of the attribute to look for.
547 * @return true if an attribute with the given local name and namespace URI
548 * is specified or has a default value on this element, false otherwise.
550 bool hasAttributeNS ( const DOMString &namespaceURI,
551 const DOMString &localName );
554 * Introduced in DOM Level 2
555 * This method is from the CSSStyleDeclaration interface
557 * The style attribute
559 CSSStyleDeclaration style ( );
562 * Introduced in DOM level 3
563 * This method is part of the ElementTraversal interface
565 * The first child node which is of nodeType ELEMENT_NODE.
568 Element firstElementChild ( ) const;
571 * Introduced in DOM level 3
572 * This method is part of the ElementTraversal interface
574 * @return The last child node of that element which is of nodeType ELEMENT_NODE.
577 Element lastElementChild ( ) const;
580 * Introduced in DOM level 3
581 * This method is part of the ElementTraversal interface
583 * @return The sibling node of that element which most immediately precedes that element in document order,
584 * and which is of nodeType ELEMENT_NODE
587 Element previousElementSibling ( ) const;
590 * Introduced in DOM level 3
591 * This method is part of the ElementTraversal interface
593 * @return The sibling node of that element which most immediately follows that element in document order,
594 * and which is of nodeType ELEMENT_NODE
597 Element nextElementSibling ( ) const;
600 * Introduced in DOM level 3
601 * This method is part of the ElementTraversal interface
603 * @return The current number of child nodes of that element which are of nodeType ELEMENT_NODE
606 unsigned long childElementCount ( ) const;
609 * not part of the official DOM
611 * This method will always reflect the editability setting of this
612 * element as specified by a direct or indirect (that means, inherited)
613 * assignment to contentEditable or the respective CSS rule, even if
614 * design mode is active.
616 * @return whether this element is editable.
617 * @see setContentEditable
619 bool contentEditable() const;
622 * not part of the official DOM
624 * This element can be made editable by setting its contentEditable
625 * property to @p true. The setting will be inherited to its children
626 * as well.
628 * Setting or clearing contentEditable when design mode is active will
629 * take no effect. However, its status will still be propagated to all
630 * child elements.
632 * @param enabled @p true to make this element editable, @p false
633 * otherwise.
634 * @see DOM::Document::designMode
636 void setContentEditable(bool enabled);
639 * @internal
640 * not part of the DOM
642 bool isHTMLElement() const;
645 * KHTML extension to DOM
646 * This method returns the associated form element.
647 * returns null if this element is not a form-like element
648 * or if this elment is not in the scope of a form element.
650 Element form() const;
652 static bool khtmlValidAttrName(const DOMString &name);
653 static bool khtmlValidPrefix(const DOMString &name);
654 static bool khtmlValidQualifiedName(const DOMString &name);
656 static bool khtmlMalformedQualifiedName(const DOMString &name);
657 static bool khtmlMalformedPrefix(const DOMString &name);
658 protected:
659 Element(ElementImpl *_impl);
662 } //namespace
663 #endif