2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 1999 Antti Koivisto (koivisto@kde.org)
6 * (C) 2001 Dirk Mueller (mueller@kde.org)
7 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 #ifndef DOM_NodeImpl_h_
27 #define DOM_NodeImpl_h_
30 #include "PlatformString.h"
31 #include "DeprecatedString.h"
32 #include <wtf/Assertions.h>
33 #include <wtf/HashSet.h>
34 #include <wtf/PassRefPtr.h>
48 class PlatformKeyboardEvent;
49 class PlatformMouseEvent;
50 class PlatformWheelEvent;
52 class RegisteredEventListener;
58 typedef int ExceptionCode;
60 // this class implements nodes, which can have a parent but no children:
61 class Node : public TreeShared<Node> {
62 friend class Document;
68 CDATA_SECTION_NODE = 4,
69 ENTITY_REFERENCE_NODE = 5,
71 PROCESSING_INSTRUCTION_NODE = 7,
74 DOCUMENT_TYPE_NODE = 10,
75 DOCUMENT_FRAGMENT_NODE = 11,
77 XPATH_NAMESPACE_NODE = 13
83 // DOM methods & attributes for Node
85 virtual bool hasTagName(const QualifiedName&) const { return false; }
86 virtual String nodeName() const = 0;
87 virtual String nodeValue() const;
88 virtual void setNodeValue(const String&, ExceptionCode&);
89 virtual NodeType nodeType() const = 0;
90 Node* parentNode() const { return parent(); }
91 Node* previousSibling() const { return m_previous; }
92 Node* nextSibling() const { return m_next; }
93 virtual PassRefPtr<NodeList> childNodes();
94 virtual Node* firstChild() const;
95 virtual Node* lastChild() const;
96 virtual bool hasAttributes() const;
97 virtual NamedAttrMap* attributes() const;
99 // These should all actually return a node, but this is only important for language bindings,
100 // which will already know and hold a ref on the right node to return. Returning bool allows
101 // these methods to be more efficient since they don't need to return a ref
102 virtual bool insertBefore(PassRefPtr<Node> newChild, Node* refChild, ExceptionCode&);
103 virtual bool replaceChild(PassRefPtr<Node> newChild, Node* oldChild, ExceptionCode&);
104 virtual bool removeChild(Node* child, ExceptionCode&);
105 virtual bool appendChild(PassRefPtr<Node> newChild, ExceptionCode&);
107 virtual void remove(ExceptionCode&);
108 virtual bool hasChildNodes() const;
109 virtual PassRefPtr<Node> cloneNode(bool deep) = 0;
110 virtual const AtomicString& localName() const;
111 virtual const AtomicString& namespaceURI() const;
112 virtual const AtomicString& prefix() const;
113 virtual void setPrefix(const AtomicString&, ExceptionCode&);
115 static bool isSupported(const String& feature, const String& version);
117 bool isSameNode(Node* other) const { return this == other; }
118 bool isEqualNode(Node*) const;
119 bool isDefaultNamespace(const String& namespaceURI) const;
120 String lookupPrefix(const String& namespaceURI) const;
121 String lookupNamespaceURI(const String& prefix) const;
122 String lookupNamespacePrefix(const String& namespaceURI, const Element* originalElement) const;
124 String textContent(bool convertBRsToNewlines = false) const;
125 void setTextContent(const String&, ExceptionCode&);
127 Node* lastDescendant() const;
128 Node* firstDescendant() const;
130 // Other methods (not part of DOM)
132 virtual bool isElementNode() const { return false; }
133 virtual bool isHTMLElement() const { return false; }
135 virtual bool isSVGElement() const { return false; }
137 virtual bool isStyledElement() const { return false; }
138 virtual bool isAttributeNode() const { return false; }
139 virtual bool isTextNode() const { return false; }
140 virtual bool isCommentNode() const { return false; }
141 virtual bool isDocumentNode() const { return false; }
142 virtual bool isEventTargetNode() const { return false; }
143 virtual bool isShadowNode() const { return false; }
144 virtual Node* shadowParentNode() { return 0; }
145 Node* shadowAncestorNode();
146 bool isBlockFlow() const;
147 bool isBlockFlowOrBlockTable() const;
149 // Used by <form> elements to indicate a malformed state of some kind, typically
150 // used to keep from applying the bottom margin of the form.
151 virtual bool isMalformed() { return false; }
152 virtual void setMalformed(bool malformed) { }
154 // These low-level calls give the caller responsibility for maintaining the integrity of the tree.
155 void setPreviousSibling(Node* previous) { m_previous = previous; }
156 void setNextSibling(Node* next) { m_next = next; }
158 // FIXME: These two functions belong in editing -- "atomic node" is an editing concept.
159 Node* previousNodeConsideringAtomicNodes() const;
160 Node* nextNodeConsideringAtomicNodes() const;
162 /** (Not part of the official DOM)
163 * Returns the next leaf node.
165 * Using this function delivers leaf nodes as if the whole DOM tree were a linear chain of its leaf nodes.
166 * @return next leaf node or 0 if there are no more.
168 Node* nextLeafNode() const;
170 /** (Not part of the official DOM)
171 * Returns the previous leaf node.
173 * Using this function delivers leaf nodes as if the whole DOM tree were a linear chain of its leaf nodes.
174 * @return previous leaf node or 0 if there are no more.
176 Node* previousLeafNode() const;
178 bool isEditableBlock() const;
179 Element* enclosingBlockFlowElement() const;
180 Element* enclosingBlockFlowOrTableElement() const;
181 Element* enclosingInlineElement() const;
182 Element* rootEditableElement() const;
184 bool inSameContainingBlockFlowElement(Node*);
186 // Used by the parser. Checks against the DTD, unlike DOM operations like appendChild().
187 // Also does not dispatch DOM mutation events.
188 // Returns the appropriate container node for future insertions as you parse, or 0 for failure.
189 virtual ContainerNode* addChild(PassRefPtr<Node>);
191 // Called by the parser when this element's close tag is reached,
192 // signalling that all child tags have been parsed and added.
193 // This is needed for <applet> and <object> elements, which can't lay themselves out
194 // until they know all of their nested <param>s. [Radar 3603191, 4040848].
195 // Also used for script elements and some SVG elements for similar purposes,
196 // but making parsing a special case in this respect should be avoided if possible.
197 virtual void closeRenderer() { }
199 // Called by the frame right before dispatching an unloadEvent. [Radar 4532113]
200 // This is needed for HTMLInputElements to tell the frame that it is done editing
201 // (sends textFieldDidEndEditing notification)
202 virtual void aboutToUnload() { }
204 // For <link> and <style> elements.
205 virtual void sheetLoaded() { }
207 bool hasID() const { return m_hasId; }
208 bool hasClass() const { return m_hasClass; }
209 bool hasStyle() const { return m_hasStyle; }
210 bool active() const { return m_active; }
211 bool inActiveChain() const { return m_inActiveChain; }
212 bool hovered() const { return m_hovered; }
213 bool focused() const { return m_focused; }
214 bool attached() const { return m_attached; }
215 void setAttached(bool b = true) { m_attached = b; }
216 bool changed() const { return m_changed; }
217 bool hasChangedChild() const { return m_hasChangedChild; }
218 bool isLink() const { return m_isLink; }
219 bool styleElement() const { return m_styleElement; }
220 bool implicitNode() const { return m_implicit; }
221 void setHasID(bool b = true) { m_hasId = b; }
222 void setHasClass(bool b = true) { m_hasClass = b; }
223 void setHasStyle(bool b = true) { m_hasStyle = b; }
224 void setHasChangedChild( bool b = true ) { m_hasChangedChild = b; }
225 void setInDocument(bool b = true) { m_inDocument = b; }
226 void setInActiveChain(bool b = true) { m_inActiveChain = b; }
227 void setChanged(bool b = true);
229 virtual void setFocus(bool b = true) { m_focused = b; }
230 virtual void setActive(bool b = true, bool pause=false) { m_active = b; }
231 virtual void setHovered(bool b = true) { m_hovered = b; }
233 unsigned short tabIndex() const { return m_tabIndex; }
234 void setTabIndex(unsigned short i) { m_tabIndex = i; }
237 * Whether this node can receive the keyboard focus.
239 virtual bool supportsFocus() const { return isFocusable(); }
240 virtual bool isFocusable() const;
241 virtual bool isKeyboardFocusable(KeyboardEvent*) const;
242 virtual bool isMouseFocusable() const;
244 virtual bool isControl() const { return false; } // Eventually the notion of what is a control will be extensible.
245 virtual bool isEnabled() const { return true; }
246 virtual bool isChecked() const { return false; }
247 virtual bool isIndeterminate() const { return false; }
248 virtual bool isReadOnlyControl() const { return false; }
250 virtual bool isContentEditable() const;
251 virtual bool isContentRichlyEditable() const;
252 virtual IntRect getRect() const;
254 enum StyleChange { NoChange, NoInherit, Inherit, Detach, Force };
255 virtual void recalcStyle(StyleChange = NoChange) { }
256 StyleChange diff(RenderStyle*, RenderStyle*) const;
258 unsigned nodeIndex() const;
260 // Returns the DOM ownerDocument attribute. This method never returns NULL, except in the case
261 // of (1) a Document node or (2) a DocumentType node that is not used with any Document yet.
262 virtual Document* ownerDocument() const;
264 // Returns the document associated with this node. This method never returns NULL, except in the case
265 // of a DocumentType node that is not used with any Document yet. A Document node returns itself.
266 Document* document() const
269 ASSERT(m_document || nodeType() == DOCUMENT_TYPE_NODE && !inDocument());
270 return m_document.get();
272 void setDocument(Document*);
274 // Returns true if this node is associated with a document and is in its associated document's
275 // node tree, false otherwise.
276 bool inDocument() const
278 ASSERT(m_document || !m_inDocument);
282 virtual bool isReadOnlyNode();
283 virtual bool childTypeAllowed(NodeType) { return false; }
284 virtual unsigned childNodeCount() const;
285 virtual Node* childNode(unsigned index) const;
288 * Does a pre-order traversal of the tree to find the node next node after this one. This uses the same order that
289 * the tags appear in the source file.
291 * @param stayWithin If not null, the traversal will stop once the specified node is reached. This can be used to
292 * restrict traversal to a particular sub-tree.
294 * @return The next node, in document order
296 * see @ref traversePreviousNode()
298 Node* traverseNextNode(const Node* stayWithin = 0) const;
300 /* Like traverseNextNode, but skips children and starts with the next sibling. */
301 Node* traverseNextSibling(const Node* stayWithin = 0) const;
304 * Does a reverse pre-order traversal to find the node that comes before the current one in document order
306 * see @ref traverseNextNode()
308 Node* traversePreviousNode(const Node * stayWithin = 0) const;
310 /* Like traversePreviousNode, but visits nodes before their children. */
311 Node* traversePreviousNodePostOrder(const Node *stayWithin = 0) const;
314 * Finds previous or next editable leaf node.
316 Node* previousEditable() const;
317 Node* nextEditable() const;
318 Node* nextEditable(int offset) const;
320 RenderObject* renderer() const { return m_renderer; }
321 RenderObject* nextRenderer();
322 RenderObject* previousRenderer();
323 void setRenderer(RenderObject* renderer) { m_renderer = renderer; }
325 void checkSetPrefix(const AtomicString& prefix, ExceptionCode&);
326 bool isDescendantOf(const Node*) const;
328 // These two methods are mutually exclusive. The former is used to do strict error-checking
329 // when adding children via the public DOM API (e.g., appendChild()). The latter is called only when parsing,
330 // to sanity-check against the DTD for error recovery.
331 void checkAddChild(Node* newChild, ExceptionCode&); // Error-checking when adding via the DOM API
332 virtual bool childAllowed(Node* newChild); // Error-checking during parsing that checks the DTD
334 // Used to determine whether range offsets use characters or node indices.
335 virtual bool offsetInCharacters() const;
337 // FIXME: These next 7 functions are mostly editing-specific and should be moved out.
338 virtual int maxOffset() const;
339 virtual int caretMinOffset() const;
340 virtual int caretMaxOffset() const;
341 virtual unsigned caretMaxRenderedOffset() const;
342 virtual int previousOffset(int current) const;
343 virtual int nextOffset(int current) const;
346 virtual void dump(TextStream*, DeprecatedString indent = "") const;
349 // -----------------------------------------------------------------------------
350 // Integration with rendering tree
353 * Attaches this node to the rendering tree. This calculates the style to be applied to the node and creates an
354 * appropriate RenderObject which will be inserted into the tree (except when the style has display: none). This
355 * makes the node visible in the FrameView.
357 virtual void attach();
360 * Detaches the node from the rendering tree, making it invisible in the rendered view. This method will remove
361 * the node's rendering object from the rendering tree and delete it.
363 virtual void detach();
365 virtual void willRemove();
366 void createRendererIfNeeded();
367 virtual RenderStyle* styleForRenderer(RenderObject* parent);
368 virtual bool rendererIsNeeded(RenderStyle*);
370 virtual bool childShouldCreateRenderer(Node*) const { return true; }
372 virtual RenderObject* createRenderer(RenderArena*, RenderStyle*);
374 // Wrapper for nodes that don't have a renderer, but still cache the style (like HTMLOptionElement).
375 virtual RenderStyle* renderStyle() const;
376 virtual void setRenderStyle(RenderStyle*);
378 // -----------------------------------------------------------------------------
379 // Notification of document structure changes
382 * Notifies the node that it has been inserted into the document. This is called during document parsing, and also
383 * when a node is added through the DOM methods insertBefore(), appendChild() or replaceChild(). Note that this only
384 * happens when the node becomes part of the document tree, i.e. only when the document is actually an ancestor of
385 * the node. The call happens _after_ the node has been added to the tree.
387 * This is similar to the DOMNodeInsertedIntoDocument DOM event, but does not require the overhead of event
390 virtual void insertedIntoDocument();
393 * Notifies the node that it is no longer part of the document tree, i.e. when the document is no longer an ancestor
396 * This is similar to the DOMNodeRemovedFromDocument DOM event, but does not require the overhead of event
397 * dispatching, and is called _after_ the node is removed from the tree.
399 virtual void removedFromDocument();
401 // These functions are called whenever you are connected or disconnected from a tree. That tree may be the main
402 // document tree, or it could be another disconnected tree. Override these functions to do any work that depends
403 // on connectedness to some ancestor (e.g., an ancestor <form> for example).
404 virtual void insertedIntoTree(bool deep) { }
405 virtual void removedFromTree(bool deep) { }
408 * Notifies the node that it's list of children have changed (either by adding or removing child nodes), or a child
409 * node that is of the type CDATA_SECTION_NODE, TEXT_NODE or COMMENT_NODE has changed its value.
411 virtual void childrenChanged();
413 virtual String toString() const = 0;
416 virtual void formatForDebugger(char* buffer, unsigned length) const;
418 void showNode(const char* prefix = "") const;
419 void showTreeForThis() const;
420 void showTreeAndMark(const Node* markedNode1, const char* markedLabel1, const Node* markedNode2 = 0, const char* markedLabel2 = 0) const;
423 void registerNodeList(NodeList*);
424 void unregisterNodeList(NodeList*);
425 void notifyNodeListsChildrenChanged();
426 void notifyLocalNodeListsChildrenChanged();
427 void notifyNodeListsAttributeChanged();
428 void notifyLocalNodeListsAttributeChanged();
430 PassRefPtr<NodeList> getElementsByTagName(const String&);
431 PassRefPtr<NodeList> getElementsByTagNameNS(const String& namespaceURI, const String& localName);
434 DocPtr<Document> m_document;
437 RenderObject* m_renderer;
440 typedef HashSet<NodeList*> NodeListSet;
441 NodeListSet* m_nodeLists;
443 unsigned short m_tabIndex : 15;
444 bool m_hasTabIndex : 1;
451 bool m_hasChangedChild : 1;
452 bool m_inDocument : 1;
455 bool m_specified : 1; // used in Attr; accessor functions there
459 bool m_inActiveChain : 1;
460 bool m_styleElement : 1; // contains stylesheet text
461 bool m_implicit : 1; // implicitly generated by the parser
466 Element* ancestorElement() const;
472 // Outside the WebCore namespace for ease of invocation from gdb.
473 void showTree(const WebCore::Node*);