KHtml

dom_element.h
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  * This file includes excerpts from the Document Object Model (DOM)
22  * Level 1 Specification (Recommendation)
23  * https://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.
27  *
28  */
29 #ifndef _DOM_ELEMENT_h_
30 #define _DOM_ELEMENT_h_
31 
32 #include <khtml_export.h>
33 #include <dom/dom_node.h>
34 #include <dom/css_value.h>
35 
36 namespace DOM
37 {
38 
39 class DOMString;
40 class AttrImpl;
41 class Element;
42 class ElementImpl;
43 class NamedAttrMapImpl;
44 class DocumentImpl;
45 
46 /**
47  * The \c Attr interface represents an attribute in an
48  * \c Element object. Typically the allowable values for
49  * the attribute are defined in a document type definition.
50  *
51  * \c Attr objects inherit the \c Node
52  * interface, but since they are not actually child nodes of the
53  * element they describe, the DOM does not consider them part of the
54  * document tree. Thus, the \c Node attributes
55  * \c parentNode , \c previousSibling , and
56  * \c nextSibling have a null value for \c Attr
57  * objects. The DOM takes the view that attributes are properties of
58  * elements rather than having a separate identity from the elements
59  * they are associated with; this should make it more efficient to
60  * implement such features as default attributes associated with all
61  * elements of a given type. Furthermore, \c Attr nodes
62  * may not be immediate children of a \c DocumentFragment
63  * . However, they can be associated with \c Element nodes
64  * contained within a \c DocumentFragment . In short,
65  * users and implementors of the DOM need to be aware that \c Attr
66  * nodes have some things in common with other objects
67  * inheriting the \c Node interface, but they also are
68  * quite distinct.
69  *
70  * The attribute's effective value is determined as follows: if this
71  * attribute has been explicitly assigned any value, that value is the
72  * attribute's effective value; otherwise, if there is a declaration
73  * for this attribute, and that declaration includes a default value,
74  * then that default value is the attribute's effective value;
75  * otherwise, the attribute does not exist on this element in the
76  * structure model until it has been explicitly added. Note that the
77  * \c nodeValue attribute on the \c Attr
78  * instance can also be used to retrieve the string version of the
79  * attribute's value(s).
80  *
81  * In XML, where the value of an attribute can contain entity
82  * references, the child nodes of the \c Attr node provide
83  * a representation in which entity references are not expanded. These
84  * child nodes may be either \c Text or
85  * \c EntityReference nodes. Because the attribute type may be
86  * unknown, there are no tokenized attribute values.
87  *
88  */
89 class KHTML_EXPORT Attr : public Node
90 {
91  friend class Element;
92  friend class Document;
93  friend class DocumentImpl;
94  friend class HTMLDocument;
95  friend class ElementImpl;
96  friend class NamedAttrMapImpl;
97  friend class AttrImpl;
98 
99 public:
100  Attr();
101  Attr(const Node &other) : Node()
102  {
103  (*this) = other;
104  }
105  Attr(const Attr &other);
106 
107  Attr &operator = (const Node &other);
108  Attr &operator = (const Attr &other);
109 
110  ~Attr();
111 
112  /**
113  * Returns the name of this attribute.
114  *
115  */
116  DOMString name() const;
117 
118  /**
119  * If this attribute was explicitly given a value in the original
120  * document, this is \c true ; otherwise, it is
121  * \c false . Note that the implementation is in charge of
122  * this attribute, not the user. If the user changes the value of
123  * the attribute (even if it ends up having the same value as the
124  * default value) then the \c specified flag is
125  * automatically flipped to \c true . To re-specify
126  * the attribute as the default value from the DTD, the user must
127  * delete the attribute. The implementation will then make a new
128  * attribute available with \c specified set to
129  * \c false and the default value (if one exists).
130  *
131  * In summary:
132  * \li If the attribute has an assigned
133  * value in the document then \c specified is
134  * \c true , and the value is the assigned value.
135  *
136  * \li If the attribute has no assigned value in the
137  * document and has a default value in the DTD, then
138  * \c specified is \c false , and the value is
139  * the default value in the DTD.
140  *
141  * \li If the attribute has no assigned value in the
142  * document and has a value of #IMPLIED in the DTD, then the
143  * attribute does not appear in the structure model of the
144  * document.
145  *
146  *
147  *
148  */
149  bool specified() const;
150 
151  /**
152  * On retrieval, the value of the attribute is returned as a
153  * string. Character and general entity references are replaced
154  * with their values.
155  *
156  * On setting, this creates a \c Text node with the
157  * unparsed contents of the string.
158  *
159  */
160  DOMString value() const;
161 
162  /**
163  * see value
164  */
165  void setValue(const DOMString &);
166 
167  /**
168  * Introduced in DOM Level 2
169  *
170  * The Element node this attribute is attached to or null if this attribute
171  * is not in use.
172  */
173  Element ownerElement() const;
174 
175 protected:
176 
177  Attr(AttrImpl *_impl);
178 };
179 
180 class NodeList;
181 class Attr;
182 class DOMString;
183 
184 /**
185  * By far the vast majority of objects (apart from text) that authors
186  * encounter when traversing a document are \c Element
187  * nodes. Assume the following XML document: &lt;elementExample
188  * id=&quot;demo&quot;&gt; &lt;subelement1/&gt;
189  * &lt;subelement2&gt;&lt;subsubelement/&gt;&lt;/subelement2&gt;
190  * &lt;/elementExample&gt;
191  *
192  * When represented using DOM, the top node is an \c Element
193  * node for &quot;elementExample&quot;, which contains two
194  * child \c Element nodes, one for &quot;subelement1&quot;
195  * and one for &quot;subelement2&quot;. &quot;subelement1&quot;
196  * contains no child nodes.
197  *
198  * Elements may have attributes associated with them; since the
199  * \c Element interface inherits from \c Node
200  * , the generic \c Node interface method
201  * \c getAttributes may be used to retrieve the set of all
202  * attributes for an element. There are methods on the \c Element
203  * interface to retrieve either an \c Attr object
204  * by name or an attribute value by name. In XML, where an attribute
205  * value may contain entity references, an \c Attr object
206  * should be retrieved to examine the possibly fairly complex sub-tree
207  * representing the attribute value. On the other hand, in HTML, where
208  * all attributes have simple string values, methods to directly
209  * access an attribute value can safely be used as a convenience.
210  *
211  */
212 class KHTML_EXPORT Element : public Node
213 {
214  friend class Document;
215  friend class DocumentFragment;
216  friend class HTMLDocument;
217 // friend class AttrImpl;
218  friend class Attr;
219 
220 public:
221  Element();
222  Element(const Node &other) : Node()
223  {
224  (*this) = other;
225  }
226  Element(const Element &other);
227 
228  Element &operator = (const Node &other);
229  Element &operator = (const Element &other);
230 
231  ~Element();
232 
233  /**
234  * The name of the element. For example, in: &lt;elementExample
235  * id=&quot;demo&quot;&gt; ... &lt;/elementExample&gt; ,
236  * \c tagName has the value \c &quot;elementExample&quot;
237  * . Note that this is case-preserving in XML, as are all
238  * of the operations of the DOM. The HTML DOM returns the
239  * \c tagName of an HTML element in the canonical uppercase
240  * form, regardless of the case in the source HTML document.
241  *
242  */
243  DOMString tagName() const;
244 
245  /**
246  * Retrieves an attribute value by name.
247  *
248  * @param name The name of the attribute to retrieve.
249  *
250  * @return The \c Attr value as a string, or the empty
251  * string if that attribute does not have a specified or default
252  * value.
253  *
254  */
255  DOMString getAttribute(const DOMString &name);
256 
257  /**
258  * Adds a new attribute. If an attribute with that name is already
259  * present in the element, its value is changed to be that of the
260  * value parameter. This value is a simple string, it is not
261  * parsed as it is being set. So any markup (such as syntax to be
262  * recognized as an entity reference) is treated as literal text,
263  * and needs to be appropriately escaped by the implementation
264  * when it is written out. In order to assign an attribute value
265  * that contains entity references, the user must create an
266  * \c Attr node plus any \c Text and
267  * \c EntityReference nodes, build the appropriate subtree,
268  * and use \c setAttributeNode to assign it as the
269  * value of an attribute.
270  *
271  * @param name The name of the attribute to create or alter.
272  *
273  * @param value Value to set in string form.
274  *
275  * @return
276  *
277  * @exception DOMException
278  * INVALID_CHARACTER_ERR: Raised if the specified name contains an
279  * invalid character.
280  *
281  * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
282  *
283  */
284  void setAttribute(const DOMString &name, const DOMString &value);
285 
286  /**
287  * Removes an attribute by name. If the removed attribute has a
288  * default value it is immediately replaced.
289  *
290  * @param name The name of the attribute to remove.
291  *
292  * @return
293  *
294  * @exception DOMException
295  * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
296  *
297  */
298  void removeAttribute(const DOMString &name);
299 
300  /**
301  * Retrieves an \c Attr node by name.
302  *
303  * @param name The name of the attribute to retrieve.
304  *
305  * @return The \c Attr node with the specified
306  * attribute name or \c null if there is no such
307  * attribute.
308  *
309  */
310  Attr getAttributeNode(const DOMString &name);
311 
312  /**
313  * Adds a new attribute. If an attribute with that name is already
314  * present in the element, it is replaced by the new one.
315  *
316  * @param newAttr The \c Attr node to add to the
317  * attribute list.
318  *
319  * @return If the \c newAttr attribute replaces an
320  * existing attribute with the same name, the previously existing
321  * \c Attr node is returned, otherwise \c null
322  * is returned.
323  *
324  * @exception DOMException
325  * WRONG_DOCUMENT_ERR: Raised if \c newAttr was
326  * created from a different document than the one that created the
327  * element.
328  *
329  * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
330  *
331  * INUSE_ATTRIBUTE_ERR: Raised if \c newAttr is
332  * already an attribute of another \c Element object.
333  * The DOM user must explicitly clone \c Attr nodes to
334  * re-use them in other elements.
335  *
336  */
337  Attr setAttributeNode(const Attr &newAttr);
338 
339  /**
340  * Removes the specified attribute.
341  *
342  * @param oldAttr The \c Attr node to remove from the
343  * attribute list. If the removed \c Attr has a
344  * default value it is immediately replaced.
345  *
346  * @return The \c Attr node that was removed.
347  *
348  * @exception DOMException
349  * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
350  *
351  * NOT_FOUND_ERR: Raised if \c oldAttr is not an
352  * attribute of the element.
353  *
354  */
355  Attr removeAttributeNode(const Attr &oldAttr);
356 
357  /**
358  * Returns a \c NodeList of all descendant elements
359  * with a given tag name, in the order in which they would be
360  * encountered in a preorder traversal of the \c Element
361  * tree.
362  *
363  * @param name The name of the tag to match on. The special value
364  * "*" matches all tags.
365  *
366  * @return A list of matching \c Element nodes.
367  *
368  */
369  NodeList getElementsByTagName(const DOMString &name);
370 
371  /**
372  * Introduced in DOM Level 2
373  * Returns a NodeList of all the descendant Elements with a given local
374  * name and namespace URI in the order in which they are encountered in a
375  * preorder traversal of this Element tree.
376  *
377  * @param namespaceURI The namespace URI of the elements to match on. The
378  * special value "*" matches all namespaces.
379  *
380  * @param localName The local name of the elements to match on. The special
381  * value "*" matches all local names.
382  *
383  * @return A new NodeList object containing all the matched Elements.
384  */
385  NodeList getElementsByTagNameNS(const DOMString &namespaceURI,
386  const DOMString &localName);
387 
388  /**
389  * Introduced in HTML 5.
390  * No Exceptions.
391  *
392  * Returns a \c NodeList of all the \c Element 's
393  * with a given class name in the order in which they
394  * would be encountered in a preorder traversal of the
395  * \c Document tree.
396  *
397  * @param tagname An unordered set of unique space-separated
398  * tokens representing classes.
399  *
400  * @return A new \c NodeList object containing all the
401  * matched \c Element s.
402  *
403  * @since 4.1
404  */
405  NodeList getElementsByClassName(const DOMString &className);
406 
407  /**
408  * Introduced in DOM Level 2.
409  *
410  * No Exceptions.
411  *
412  * Retrieves an attribute value by local name and namespace URI. HTML-only
413  * DOM implementations do not need to implement this method.
414  *
415  * @param namespaceURI The namespace URI of the attribute to retrieve.
416  *
417  * @param localName The local name of the attribute to retrieve.
418  *
419  * @return The Attr value as a string, or the empty string if that
420  * attribute does not have a specified or default value.
421  */
422  DOMString getAttributeNS(const DOMString &namespaceURI,
423  const DOMString &localName);
424 
425  /**
426  * Introduced in DOM Level 2
427  *
428  * Adds a new attribute. If an attribute with the same local name and
429  * namespace URI is already present on the element, its prefix is changed
430  * to be the prefix part of the qualifiedName, and its value is changed to
431  * be the value parameter. This value is a simple string; it is not parsed
432  * as it is being set. So any markup (such as syntax to be recognized as an
433  * entity reference) is treated as literal text, and needs to be
434  * appropriately escaped by the implementation when it is written out. In
435  * order to assign an attribute value that contains entity references, the
436  * user must create an Attr node plus any Text and EntityReference nodes,
437  * build the appropriate subtree, and use setAttributeNodeNS or
438  * setAttributeNode to assign it as the value of an attribute.
439  *
440  * HTML-only DOM implementations do not need to implement this method.
441  *
442  * @param namespaceURI The namespace URI of the attribute to create or
443  * alter.
444  *
445  * @param qualifiedName The qualified name of the attribute to create or
446  * alter.
447  *
448  * @param value The value to set in string form.
449  *
450  * @exception DOMException
451  * INVALID_CHARACTER_ERR: Raised if the specified qualified name contains
452  * an illegal character.
453  *
454  * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
455  *
456  * NAMESPACE_ERR: Raised if the qualifiedName is malformed, if the
457  * qualifiedName has a prefix and the namespaceURI is null, if the
458  * qualifiedName has a prefix that is "xml" and the namespaceURI is
459  * different from "http://www.w3.org/XML/1998/namespace", or if the
460  * qualifiedName is "xmlns" and the namespaceURI is different from
461  * "http://www.w3.org/2000/xmlns/".
462  */
463  void setAttributeNS(const DOMString &namespaceURI,
464  const DOMString &qualifiedName,
465  const DOMString &value);
466 
467  /**
468  * Introduced in DOM Level 2
469  *
470  * Removes an attribute by local name and namespace URI. If the removed
471  * attribute has a default value it is immediately replaced. The replacing
472  * attribute has the same namespace URI and local name, as well as the
473  * original prefix.
474  *
475  * HTML-only DOM implementations do not need to implement this method.
476  *
477  * @param namespaceURI The namespace URI of the attribute to remove.
478  *
479  * @param localName The local name of the attribute to remove.
480  *
481  * @exception DOMException
482  * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
483  */
484  void removeAttributeNS(const DOMString &namespaceURI,
485  const DOMString &localName);
486 
487  /**
488  * Introduced in DOM Level 2
489  *
490  * Retrieves an Attr node by local name and namespace URI. HTML-only DOM
491  * implementations do not need to implement this method.
492  *
493  * @param namespaceURI The namespace URI of the attribute to retrieve.
494  *
495  * @param localName The local name of the attribute to retrieve.
496  *
497  * @return The Attr node with the specified attribute local name and
498  * namespace URI or null if there is no such attribute.
499  */
500  Attr getAttributeNodeNS(const DOMString &namespaceURI,
501  const DOMString &localName);
502 
503  /**
504  * Introduced in DOM Level 2
505  *
506  * Adds a new attribute. If an attribute with that local name and that
507  * namespace URI is already present in the element, it is replaced by the
508  * new one.
509  *
510  * HTML-only DOM implementations do not need to implement this method.
511  *
512  * @param newAttr The Attr node to add to the attribute list.
513  *
514  * @return If the newAttr attribute replaces an existing attribute with the
515  * same local name and namespace URI, the replaced Attr node is returned,
516  * otherwise null is returned.
517  *
518  * @exception DOMException
519  * WRONG_DOCUMENT_ERR: Raised if newAttr was created from a different
520  * document than the one that created the element.
521  *
522  * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
523  *
524  * INUSE_ATTRIBUTE_ERR: Raised if newAttr is already an attribute of
525  * another Element object. The DOM user must explicitly clone Attr nodes to
526  * re-use them in other elements.
527  */
528  Attr setAttributeNodeNS(const Attr &newAttr);
529 
530  /**
531  * Returns true when an attribute with a given name is specified on this
532  * element or has a default value, false otherwise.
533  * Introduced in DOM Level 2.
534  *
535  * @param name The name of the attribute to look for.
536  *
537  * @return true if an attribute with the given name is specified on this
538  * element or has a default value, false otherwise.
539  */
540  bool hasAttribute(const DOMString &name);
541 
542  /**
543  * Introduced in DOM Level 2
544  *
545  * Returns true when an attribute with a given local name and namespace URI
546  * is specified on this element or has a default value, false otherwise.
547  * HTML-only DOM implementations do not need to implement this method.
548  *
549  * @param namespaceURI The namespace URI of the attribute to look for.
550  *
551  * @param localName The local name of the attribute to look for.
552  *
553  * @return true if an attribute with the given local name and namespace URI
554  * is specified or has a default value on this element, false otherwise.
555  */
556  bool hasAttributeNS(const DOMString &namespaceURI,
557  const DOMString &localName);
558 
559  /**
560  * Introduced in DOM Level 2
561  * This method is from the CSSStyleDeclaration interface
562  *
563  * The style attribute
564  */
565  CSSStyleDeclaration style();
566 
567  /**
568  * Introduced in DOM level 3
569  * This method is part of the ElementTraversal interface
570  *
571  * The first child node which is of nodeType ELEMENT_NODE.
572  *
573  */
574  Element firstElementChild() const;
575 
576  /**
577  * Introduced in DOM level 3
578  * This method is part of the ElementTraversal interface
579  *
580  * @return The last child node of that element which is of nodeType ELEMENT_NODE.
581  *
582  */
583  Element lastElementChild() const;
584 
585  /**
586  * Introduced in DOM level 3
587  * This method is part of the ElementTraversal interface
588  *
589  * @return The sibling node of that element which most immediately precedes that element in document order,
590  * and which is of nodeType ELEMENT_NODE
591  *
592  */
593  Element previousElementSibling() const;
594 
595  /**
596  * Introduced in DOM level 3
597  * This method is part of the ElementTraversal interface
598  *
599  * @return The sibling node of that element which most immediately follows that element in document order,
600  * and which is of nodeType ELEMENT_NODE
601  *
602  */
603  Element nextElementSibling() const;
604 
605  /**
606  * Introduced in DOM level 3
607  * This method is part of the ElementTraversal interface
608  *
609  * @return The current number of child nodes of that element which are of nodeType ELEMENT_NODE
610  *
611  */
612  unsigned long childElementCount() const;
613 
614  /**
615  * Introduced in Selectors Level 1.
616  *
617  * Returns the first (in document order) element in this element's subtree
618  * matching the given CSS selector @p query.
619  *
620  * @since 4.5
621  */
622  Element querySelector(const DOMString &query) const;
623 
624  /**
625  * Introduced in Selectors Level 1.
626  *
627  * Returns all (in document order) elements in this element's subtree
628  * matching the given CSS selector @p query. Note that the returned NodeList
629  * is static and not live, and will not be updated when the document
630  * changes
631  *
632  * @since 4.5
633  */
634  NodeList querySelectorAll(const DOMString &query) const;
635 
636  /**
637  * not part of the official DOM
638  *
639  * This method will always reflect the editability setting of this
640  * element as specified by a direct or indirect (that means, inherited)
641  * assignment to contentEditable or the respective CSS rule, even if
642  * design mode is active.
643  *
644  * @return whether this element is editable.
645  * @see setContentEditable
646  */
647  bool contentEditable() const;
648 
649  /**
650  * not part of the official DOM
651  *
652  * This element can be made editable by setting its contentEditable
653  * property to @p true. The setting will be inherited to its children
654  * as well.
655  *
656  * Setting or clearing contentEditable when design mode is active will
657  * take no effect. However, its status will still be propagated to all
658  * child elements.
659  *
660  * @param enabled @p true to make this element editable, @p false
661  * otherwise.
662  * @see DOM::Document::designMode
663  */
664  void setContentEditable(bool enabled);
665 
666  /**
667  * @internal
668  * not part of the DOM
669  */
670  bool isHTMLElement() const;
671 
672  /**
673  * KHTML extension to DOM
674  * This method returns the associated form element.
675  * returns null if this element is not a form-like element
676  * or if this elment is not in the scope of a form element.
677  */
678  Element form() const;
679 
680  static bool khtmlValidAttrName(const DOMString &name);
681  static bool khtmlValidPrefix(const DOMString &name);
682  static bool khtmlValidQualifiedName(const DOMString &name);
683 
684  static bool khtmlMalformedQualifiedName(const DOMString &name);
685  static bool khtmlMalformedPrefix(const DOMString &name);
686 protected:
687  Element(ElementImpl *_impl);
688 };
689 
690 } //namespace
691 #endif
The Node interface is the primary datatype for the entire Document Object Model.
Definition: dom_node.h:278
An HTMLDocument is the root of the HTML hierarchy and holds the entire content.
Definition: html_document.h:74
The CSSStyleDeclaration interface represents a single CSS declaration block .
Definition: css_value.h:59
DocumentFragment is a "lightweight" or "minimal" Document object.
Definition: dom_doc.h:1042
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
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
The Document interface represents the entire HTML or XML document.
Definition: dom_doc.h:246
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.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 9 2021 22:46:03 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.