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 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.
25 #ifndef _DOM_NodeImpl_h_
26 #define _DOM_NodeImpl_h_
28 #include "dom/dom_misc.h"
29 #include "dom/dom_string.h"
30 #include "dom/dom_node.h"
31 #include "misc/helper.h"
32 #include "misc/shared.h"
33 #include "dom_atomicstring.h"
36 template <class type> class QPtrList;
37 template <class type> class QPtrDict;
53 class CSSStyleDeclarationImpl;
57 class NamedNodeMapImpl;
62 class RegisteredEventListener;
64 // The namespace used for XHTML elements
65 #define XHTML_NAMESPACE "http://www.w3.org/1999/xhtml"
67 const Q_UINT16 noNamespace = 0;
68 const Q_UINT16 anyNamespace = 1;
69 const Q_UINT16 xhtmlNamespace = 2;
70 const Q_UINT16 anyLocalName = 0;
72 const Q_UINT32 namespaceMask = 0xFFFF0000U;
73 const Q_UINT32 localNameMask = 0x0000FFFFU;
75 inline Q_UINT16 namespacePart(Q_UINT32 i) { return i >> 16; }
76 inline Q_UINT16 localNamePart(Q_UINT32 i) { return i; }
77 inline Q_UINT32 makeId(Q_UINT16 n, Q_UINT16 l) { return (n << 16) | l; }
79 // Can't use makeId here because it results in an "initroutine".
80 const Q_UINT32 anyQName = anyNamespace << 16 | anyLocalName;
82 class DocumentPtr : public khtml::Shared<DocumentPtr>
85 DocumentImpl *document() const { return doc; }
87 DocumentPtr() { doc = 0; }
88 friend class DocumentImpl;
89 friend class DOMImplementationImpl;
94 // this class implements nodes, which can have a parent but no children:
95 class NodeImpl : public khtml::TreeShared<NodeImpl>
97 friend class DocumentImpl;
99 NodeImpl(DocumentPtr *doc);
102 // DOM methods & attributes for Node
103 virtual DOMString nodeName() const;
104 virtual DOMString nodeValue() const;
105 virtual void setNodeValue( const DOMString &_nodeValue, int &exceptioncode );
106 virtual unsigned short nodeType() const;
107 NodeImpl *parentNode() const { return m_parent; }
108 NodeImpl *previousSibling() const { return m_previous; }
109 NodeImpl *nextSibling() const { return m_next; }
110 virtual NodeListImpl *childNodes();
111 virtual NodeImpl *firstChild() const;
112 virtual NodeImpl *lastChild() const;
113 virtual NodeImpl *insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode );
114 virtual NodeImpl *replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode );
115 virtual NodeImpl *removeChild ( NodeImpl *oldChild, int &exceptioncode );
116 virtual NodeImpl *appendChild ( NodeImpl *newChild, int &exceptioncode );
117 virtual void remove(int &exceptioncode);
118 virtual bool hasChildNodes ( ) const;
119 virtual NodeImpl *cloneNode ( bool deep ) = 0;
120 virtual DOMString localName() const;
121 virtual DOMString prefix() const;
122 virtual void setPrefix(const DOMString &_prefix, int &exceptioncode );
125 // Other methods (not part of DOM)
126 virtual bool isElementNode() const { return false; }
127 virtual bool isHTMLElement() const { return false; }
128 virtual bool isAttributeNode() const { return false; }
129 virtual bool isTextNode() const { return false; }
130 virtual bool isDocumentNode() const { return false; }
131 virtual bool isXMLElementNode() const { return false; }
132 bool isBlockFlow() const;
134 // Used by <form> elements to indicate a malformed state of some kind, typically
135 // used to keep from applying the bottom margin of the form.
136 virtual bool isMalformed() { return false; }
137 virtual void setMalformed(bool malformed) {};
139 virtual bool containsOnlyWhitespace() const { return false; }
141 // helper functions not being part of the DOM
142 // Attention: they assume that the caller did the consistency checking!
143 void setPreviousSibling(NodeImpl *previous) { m_previous = previous; }
144 void setNextSibling(NodeImpl *next) { m_next = next; }
146 virtual void setFirstChild(NodeImpl *child);
147 virtual void setLastChild(NodeImpl *child);
149 bool isAtomicNode() const;
150 NodeImpl *previousNodeConsideringAtomicNodes() const;
151 NodeImpl *nextNodeConsideringAtomicNodes() const;
153 /** (Not part of the official DOM)
154 * Returns the next leaf node.
156 * Using this function delivers leaf nodes as if the whole DOM tree
157 * were a linear chain of its leaf nodes.
158 * @return next leaf node or 0 if there are no more.
160 NodeImpl *nextLeafNode() const;
162 /** (Not part of the official DOM)
163 * Returns the previous leaf node.
165 * Using this function delivers leaf nodes as if the whole DOM tree
166 * were a linear chain of its leaf nodes.
167 * @return previous leaf node or 0 if there are no more.
169 NodeImpl *previousLeafNode() const;
171 bool isEditableBlock() const;
172 ElementImpl *enclosingBlockFlowElement() const;
173 ElementImpl *enclosingInlineElement() const;
174 ElementImpl *rootEditableElement() const;
176 bool inSameRootEditableElement(NodeImpl *);
177 bool inSameContainingBlockFlowElement(NodeImpl *);
179 // used by the parser. Doesn't do as many error checkings as
180 // appendChild(), and returns the node into which will be parsed next.
181 virtual NodeImpl *addChild(NodeImpl *newChild);
184 // id() is used to easily and exactly identify a node. It
185 // is optimized for quick comparison and low memory consumption.
186 // its value depends on the owner document of the node and is
187 // categorized in the following way:
188 // 1..ID_LAST_TAG: the node inherits HTMLElementImpl and is
189 // part of the HTML namespace.
190 // The HTML namespace is either the global
191 // one (no namespace) or the XHTML namespace
192 // depending on the owner document's doctype
193 // ID_LAST_TAG+1..0xffff: non-HTML elements in the global namespace
194 // others non-HTML elements in a namespace.
195 // the upper 16 bit identify the namespace
196 // the lower 16 bit identify the local part of the
197 // qualified element name.
198 virtual Id id() const { return 0; };
200 Id identifier() const;
202 enum MouseEventType {
212 MouseEvent( int _button, MouseEventType _type,
213 const DOMString &_url = DOMString(), const DOMString& _target = DOMString(),
214 NodeImpl *_innerNode = 0)
216 button = _button; type = _type;
217 url = _url; target = _target;
218 innerNode = _innerNode;
223 DOMString url; // url under mouse or empty
228 // for LINK and STYLE
229 virtual void sheetLoaded() {}
231 bool hasID() const { return m_hasId; }
232 bool hasClass() const { return m_hasClass; }
233 bool hasStyle() const { return m_hasStyle; }
234 bool active() const { return m_active; }
235 bool focused() const { return m_focused; }
236 bool attached() const { return m_attached; }
237 bool changed() const { return m_changed; }
238 bool hasChangedChild() const { return m_hasChangedChild; }
239 bool hasAnchor() const { return m_hasAnchor; }
240 bool inDocument() const { return m_inDocument; }
241 bool styleElement() const { return m_styleElement; }
242 bool implicitNode() const { return m_implicit; }
243 void setHasID(bool b=true) { m_hasId = b; }
244 void setHasClass(bool b=true) { m_hasClass = b; }
245 void setHasStyle(bool b=true) { m_hasStyle = b; }
246 void setHasChangedChild( bool b = true ) { m_hasChangedChild = b; }
247 void setInDocument(bool b=true) { m_inDocument = b; }
248 virtual void setFocus(bool b=true) { m_focused = b; }
249 virtual void setActive(bool b=true) { m_active = b; }
250 void setChanged(bool b=true);
252 unsigned short tabIndex() const { return m_tabIndex; }
253 void setTabIndex(unsigned short _tabIndex) { m_tabIndex = _tabIndex; }
256 * whether this node can receive the keyboard focus.
258 virtual bool isFocusable() const;
259 virtual bool isKeyboardFocusable() const;
260 virtual bool isMouseFocusable() const;
262 virtual bool isInline() const;
263 QString startMarkup(const DOM::RangeImpl *range) const;
264 QString endMarkup(void) const;
265 virtual QString toHTML() const;
266 QString recursive_toHTML(bool onlyIncludeChildren=false, QPtrList<NodeImpl> *nodes=NULL) const;
267 static QString recursive_toString(const NodeImpl *startNode, bool onlyIncludeChildren, bool includeSiblings, QPtrList<NodeImpl> *nodes);
268 void recursive_completeURLs(QString baseURL);
270 virtual bool isContentEditable() const;
271 virtual QRect getRect() const;
273 enum StyleChange { NoChange, NoInherit, Inherit, Detach, Force };
274 virtual void recalcStyle( StyleChange = NoChange ) {}
275 StyleChange diff( khtml::RenderStyle *s1, khtml::RenderStyle *s2 ) const;
277 unsigned long nodeIndex() const;
278 // Returns the document that this node is associated with. This is guaranteed to always be non-null, as opposed to
279 // DOM's ownerDocument() which is null for Document nodes (and sometimes DocumentType nodes).
280 DocumentImpl* getDocument() const { return document->document(); }
282 void setDocument(DocumentPtr *doc);
284 void addEventListener(int id, EventListener *listener, const bool useCapture);
285 void removeEventListener(int id, EventListener *listener, bool useCapture);
286 void removeHTMLEventListener(int id);
287 void setHTMLEventListener(int id, EventListener *listener);
288 EventListener *getHTMLEventListener(int id);
290 bool dispatchEvent(EventImpl *evt, int &exceptioncode, bool tempEvent = false);
291 bool dispatchGenericEvent( EventImpl *evt, int &exceptioncode);
292 bool dispatchHTMLEvent(int _id, bool canBubbleArg, bool cancelableArg);
293 bool dispatchWindowEvent(int _id, bool canBubbleArg, bool cancelableArg);
294 bool dispatchMouseEvent(QMouseEvent *e, int overrideId = 0, int overrideDetail = 0);
295 bool dispatchUIEvent(int _id, int detail = 0);
296 bool dispatchSubtreeModifiedEvent();
297 bool dispatchKeyEvent(QKeyEvent *key);
299 void handleLocalEvents(EventImpl *evt, bool useCapture);
302 * Perform the default action for an event e.g. submitting a form
304 virtual void defaultEventHandler(EventImpl *evt);
307 * Used for disabled form elements; if true, prevents mouse events from being dispatched
308 * to event listeners, and prevents DOMActivate events from being sent at all.
310 virtual bool disabled() const;
312 virtual bool isReadOnly();
313 virtual bool childTypeAllowed( unsigned short /*type*/ ) { return false; }
314 virtual unsigned long childNodeCount() const;
315 virtual NodeImpl *childNode(unsigned long index);
318 * Does a pre-order traversal of the tree to find the node next node after this one. This uses the same order that
319 * the tags appear in the source file.
321 * @param stayWithin If not null, the traversal will stop once the specified node is reached. This can be used to
322 * restrict traversal to a particular sub-tree.
324 * @return The next node, in document order
326 * see @ref traversePreviousNode()
328 NodeImpl *traverseNextNode(NodeImpl *stayWithin = 0) const;
330 /* Like traverseNextNode, but skips children and starts with the next sibling. */
331 NodeImpl *traverseNextSibling(NodeImpl *stayWithin = 0) const;
334 * Does a reverse pre-order traversal to find the node that comes before the current one in document order
336 * see @ref traverseNextNode()
338 NodeImpl *traversePreviousNode() const;
340 /* Like traversePreviousNode, but visits nodes before their children. */
341 NodeImpl *traversePreviousNodePostOrder(NodeImpl *stayWithin = 0) const;
343 DocumentPtr *docPtr() const { return document; }
345 NodeImpl *previousEditable() const;
346 NodeImpl *nextEditable() const;
347 //bool isEditable() const;
349 khtml::RenderObject *renderer() const { return m_render; }
350 khtml::RenderObject *nextRenderer();
351 khtml::RenderObject *previousRenderer();
352 void setRenderer(khtml::RenderObject* renderer) { m_render = renderer; }
354 void checkSetPrefix(const DOMString &_prefix, int &exceptioncode);
355 void checkAddChild(NodeImpl *newChild, int &exceptioncode);
356 bool isAncestor(const NodeImpl *) const;
357 virtual bool childAllowed( NodeImpl *newChild );
359 virtual long maxOffset() const;
360 virtual long caretMinOffset() const;
361 virtual long caretMaxOffset() const;
362 virtual unsigned long caretMaxRenderedOffset() const;
365 virtual void dump(QTextStream *stream, QString ind = "") const;
368 // -----------------------------------------------------------------------------
369 // Integration with rendering tree
372 * Attaches this node to the rendering tree. This calculates the style to be applied to the node and creates an
373 * appropriate RenderObject which will be inserted into the tree (except when the style has display: none). This
374 * makes the node visible in the KHTMLView.
376 virtual void attach();
379 * Detaches the node from the rendering tree, making it invisible in the rendered view. This method will remove
380 * the node's rendering object from the rendering tree and delete it.
382 virtual void detach();
384 void createRendererIfNeeded();
385 virtual khtml::RenderStyle *styleForRenderer(khtml::RenderObject *parent);
386 virtual bool rendererIsNeeded(khtml::RenderStyle *);
387 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
389 // -----------------------------------------------------------------------------
390 // Methods for maintaining the state of the element between history navigation
393 * Indicates whether or not this type of node maintains it's state. If so, the state of the node will be stored when
394 * the user goes to a different page using the state() method, and restored using the restoreState() method if the
395 * user returns (e.g. using the back button). This is used to ensure that user-changeable elements such as form
396 * controls maintain their contents when the user returns to a previous page in the history.
398 virtual bool maintainsState();
401 * Returns the state of this node represented as a string. This string will be passed to restoreState() if the user
402 * returns to the page.
404 * @return State information about the node represented as a string
406 virtual QString state();
409 * Sets the state of the element based on strings previously returned by state(). This is used to initialize form
410 * controls with their old values when the user returns to the page in their history. The receiver
411 * should remove the string from the list that it uses for its restore.
413 * @param states The strings previously returned by nodes' state methods.
415 virtual void restoreState(QStringList &stateList);
417 // -----------------------------------------------------------------------------
418 // Notification of document stucture changes
421 * Notifies the node that it has been inserted into the document. This is called during document parsing, and also
422 * when a node is added through the DOM methods insertBefore(), appendChild() or replaceChild(). Note that this only
423 * happens when the node becomes part of the document tree, i.e. only when the document is actually an ancestor of
424 * the node. The call happens _after_ the node has been added to the tree.
426 * This is similar to the DOMNodeInsertedIntoDocument DOM event, but does not require the overhead of event
429 virtual void insertedIntoDocument();
432 * Notifies the node that it is no longer part of the document tree, i.e. when the document is no longer an ancestor
435 * This is similar to the DOMNodeRemovedFromDocument DOM event, but does not require the overhead of event
436 * dispatching, and is called _after_ the node is removed from the tree.
438 virtual void removedFromDocument();
441 * Notifies the node that it's list of children have changed (either by adding or removing child nodes), or a child
442 * node that is of the type CDATA_SECTION_NODE, TEXT_NODE or COMMENT_NODE has changed it's value.
444 virtual void childrenChanged();
446 virtual DOMString toString() const = 0;
449 virtual void formatForDebugger(char *buffer, unsigned length) const;
452 void registerNodeList(NodeListImpl *list);
453 void unregisterNodeList(NodeListImpl *list);
454 void notifyNodeListsSubtreeModified();
455 void notifyLocalNodeListsSubtreeModified();
458 DocumentPtr *document;
459 NodeImpl *m_previous;
462 khtml::RenderObject *m_render;
463 QPtrList<RegisteredEventListener> *m_regdListeners;
464 QPtrDict<NodeListImpl> *m_nodeLists;
466 unsigned short m_tabIndex : 15;
467 bool m_hasTabIndex : 1;
474 bool m_hasChangedChild : 1;
475 bool m_inDocument : 1;
477 bool m_hasAnchor : 1;
478 bool m_specified : 1; // used in AttrImpl. Accessor functions there
481 bool m_styleElement : 1; // contains stylesheet text
482 bool m_implicit : 1; // implicitely generated by the parser
487 // this is the full Node Implementation with parents and children.
488 class NodeBaseImpl : public NodeImpl
491 NodeBaseImpl(DocumentPtr *doc);
492 virtual ~NodeBaseImpl();
494 // DOM methods overridden from parent classes
495 virtual NodeImpl *firstChild() const;
496 virtual NodeImpl *lastChild() const;
497 virtual NodeImpl *insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode );
498 virtual NodeImpl *replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode );
499 virtual NodeImpl *removeChild ( NodeImpl *oldChild, int &exceptioncode );
500 virtual NodeImpl *appendChild ( NodeImpl *newChild, int &exceptioncode );
501 virtual bool hasChildNodes ( ) const;
503 // Other methods (not part of DOM)
504 void removeChildren();
505 void cloneChildNodes(NodeImpl *clone);
507 virtual void setFirstChild(NodeImpl *child);
508 virtual void setLastChild(NodeImpl *child);
509 virtual NodeImpl *addChild(NodeImpl *newChild);
510 virtual void attach();
511 virtual void detach();
513 virtual NodeListImpl *getElementsByTagNameNS ( DOMStringImpl* namespaceURI,
514 DOMStringImpl* localName );
516 virtual QRect getRect() const;
517 bool getUpperLeftCorner(int &xPos, int &yPos) const;
518 bool getLowerRightCorner(int &xPos, int &yPos) const;
520 virtual void setFocus(bool=true);
521 virtual void setActive(bool=true);
522 virtual unsigned long childNodeCount() const;
523 virtual NodeImpl *childNode(unsigned long index);
525 virtual void insertedIntoDocument();
526 virtual void removedFromDocument();
528 // check for being (grand-..)father:
529 bool checkNoOwner( NodeImpl *other, int &exceptioncode );
535 // helper functions for inserting children:
537 // ### this should vanish. do it in dom/ !
538 // check for same source document:
539 bool checkSameDocument( NodeImpl *newchild, int &exceptioncode );
540 // check for being child:
541 bool checkIsChild( NodeImpl *oldchild, int &exceptioncode );
544 // find out if a node is allowed to be our child
545 void dispatchChildInsertedEvents( NodeImpl *child, int &exceptioncode );
546 void dispatchChildRemovalEvents( NodeImpl *child, int &exceptioncode );
549 // --------------------------------------------------------------------------
553 class NodeListImpl : public khtml::Shared<NodeListImpl>
556 NodeListImpl( NodeImpl *_rootNode );
557 virtual ~NodeListImpl();
559 // DOM methods & attributes for NodeList
560 virtual unsigned long length() const = 0;
561 virtual NodeImpl *item ( unsigned long index ) const = 0;
563 // Other methods (not part of DOM)
565 void rootNodeSubtreeModified();
568 static NodeList createInstance(NodeListImpl *impl);
571 // helper functions for searching all ElementImpls in a tree
572 unsigned long recursiveLength(NodeImpl *start = 0) const;
573 NodeImpl *recursiveItem ( unsigned long &offset, NodeImpl *start = 0 ) const;
574 virtual bool nodeMatches( NodeImpl *testNode ) const = 0;
577 mutable int cachedLength;
578 mutable bool isCacheValid;
581 class ChildNodeListImpl : public NodeListImpl
584 ChildNodeListImpl( NodeImpl *n);
586 // DOM methods overridden from parent classes
588 virtual unsigned long length() const;
589 virtual NodeImpl *item ( unsigned long index ) const;
592 virtual bool nodeMatches( NodeImpl *testNode ) const;
597 * NodeList which lists all Nodes in a document with a given tag name
599 class TagNodeListImpl : public NodeListImpl
602 TagNodeListImpl( NodeImpl *n, NodeImpl::Id tagId, NodeImpl::Id tagIdMask );
604 // DOM methods overridden from parent classes
606 virtual unsigned long length() const;
607 virtual NodeImpl *item ( unsigned long index ) const;
609 // Other methods (not part of DOM)
612 virtual bool nodeMatches( NodeImpl *testNode ) const;
615 NodeImpl::Id m_idMask;
620 * NodeList which lists all Nodes in a Element with a given "name=" tag
622 class NameNodeListImpl : public NodeListImpl
625 NameNodeListImpl( NodeImpl *doc, const DOMString &t );
627 // DOM methods overridden from parent classes
629 virtual unsigned long length() const;
630 virtual NodeImpl *item ( unsigned long index ) const;
632 // Other methods (not part of DOM)
635 virtual bool nodeMatches( NodeImpl *testNode ) const;
641 // Generic NamedNodeMap interface
642 // Other classes implement this for more specific situations e.g. attributes
644 class NamedNodeMapImpl : public khtml::Shared<NamedNodeMapImpl>
648 virtual ~NamedNodeMapImpl();
650 // DOM methods & attributes for NamedNodeMap
651 virtual NodeImpl *getNamedItem ( NodeImpl::Id id ) const = 0;
652 virtual Node removeNamedItem ( NodeImpl::Id id, int &exceptioncode ) = 0;
653 virtual Node setNamedItem ( NodeImpl* arg, int &exceptioncode ) = 0;
655 virtual NodeImpl *item ( unsigned long index ) const = 0;
656 virtual unsigned long length( ) const = 0;
658 // Other methods (not part of DOM)
659 virtual NodeImpl::Id mapId(const DOMString& namespaceURI, const DOMString& localName, bool readonly) = 0;
660 virtual bool isReadOnly() { return false; }
663 static NamedNodeMap createInstance(NamedNodeMapImpl *impl);
670 // Generic read-only NamedNodeMap implementation
671 // You can add items using the internal function addItem()
672 class GenericRONamedNodeMapImpl : public NamedNodeMapImpl
675 GenericRONamedNodeMapImpl(DocumentPtr* doc);
676 virtual ~GenericRONamedNodeMapImpl();
678 // DOM methods & attributes for NamedNodeMap
680 virtual NodeImpl *getNamedItem ( const DOMString &name, int &exceptioncode ) const;
681 virtual Node setNamedItem ( const Node &arg, int &exceptioncode );
682 virtual Node removeNamedItem ( const DOMString &name, int &exceptioncode );
683 virtual NodeImpl *item ( unsigned long index ) const;
684 virtual unsigned long length( ) const;
685 virtual NodeImpl *getNamedItemNS( const DOMString &namespaceURI, const DOMString &localName,
686 int &exceptioncode ) const;
687 virtual NodeImpl *setNamedItemNS( NodeImpl *arg, int &exceptioncode );
688 virtual NodeImpl *removeNamedItemNS( const DOMString &namespaceURI, const DOMString &localName,
689 int &exceptioncode );
691 // Other methods (not part of DOM)
693 virtual bool isReadOnly() { return true; }
695 void addNode(NodeImpl *n);
699 QPtrList<NodeImpl> *m_contents;