khtml Library API Documentation

dom_nodeimpl.h

00001 /*
00002  * This file is part of the DOM implementation for KDE.
00003  *
00004  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
00005  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
00006  *           (C) 2001 Dirk Mueller (mueller@kde.org)
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Library General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Library General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Library General Public License
00019  * along with this library; see the file COPYING.LIB.  If not, write to
00020  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021  * Boston, MA 02111-1307, USA.
00022  *
00023  * $Id: dom_nodeimpl.h,v 1.137.2.4 2003/06/30 10:24:54 faure Exp $
00024  */
00025 #ifndef _DOM_NodeImpl_h_
00026 #define _DOM_NodeImpl_h_
00027 
00028 #include "dom/dom_misc.h"
00029 #include "dom/dom_string.h"
00030 #include "dom/dom_node.h"
00031 #include "misc/helper.h"
00032 #include "misc/shared.h"
00033 
00034 // The namespace used for XHTML elements
00035 #define XHTML_NAMESPACE "http://www.w3.org/1999/xhtml"
00036 
00037 class QPainter;
00038 template <class type> class QPtrList;
00039 class KHTMLView;
00040 class QRect;
00041 class QMouseEvent;
00042 class QKeyEvent;
00043 class QTextStream;
00044 
00045 namespace khtml {
00046     class RenderStyle;
00047     class RenderObject;
00048 }
00049 
00050 namespace DOM {
00051 
00052 class NodeListImpl;
00053 class NamedNodeMapImpl;
00054 class DocumentImpl;
00055 class CSSStyleDeclarationImpl;
00056 class RegisteredEventListener;
00057 class EventImpl;
00058 
00059 class DocumentPtr : public khtml::Shared<DocumentPtr>
00060 {
00061 public:
00062     DocumentImpl *document() const { return doc; }
00063 private:
00064     DocumentPtr() { doc = 0; }
00065     friend class DocumentImpl;
00066     friend class DOMImplementationImpl;
00067 
00068     DocumentImpl *doc;
00069 };
00070 
00071 // this class implements nodes, which can have a parent but no children:
00072 #define NodeImpl_IdNSMask    0xffff0000
00073 #define NodeImpl_IdLocalMask 0x0000ffff
00074 
00075 class NodeImpl : public khtml::TreeShared<NodeImpl>
00076 {
00077     friend class DocumentImpl;
00078 public:
00079     NodeImpl(DocumentPtr *doc);
00080     virtual ~NodeImpl();
00081 
00082     // DOM methods & attributes for Node
00083     virtual DOMString nodeName() const;
00084     virtual DOMString nodeValue() const;
00085     virtual void setNodeValue( const DOMString &_nodeValue, int &exceptioncode );
00086     virtual unsigned short nodeType() const;
00087     NodeImpl *parentNode() const { return m_parent; }
00088     NodeImpl *previousSibling() const { return m_previous; }
00089     NodeImpl *nextSibling() const { return m_next; }
00090     virtual NodeListImpl *childNodes();
00091     virtual NodeImpl *firstChild() const;
00092     virtual NodeImpl *lastChild() const;
00093     virtual NodeImpl *insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode );
00094     virtual NodeImpl *replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode );
00095     virtual NodeImpl *removeChild ( NodeImpl *oldChild, int &exceptioncode );
00096     virtual NodeImpl *appendChild ( NodeImpl *newChild, int &exceptioncode );
00097     virtual bool hasChildNodes (  ) const;
00098     virtual NodeImpl *cloneNode ( bool deep ) = 0;
00099     virtual DOMString localName() const;
00100     virtual DOMString prefix() const;
00101     virtual void setPrefix(const DOMString &_prefix, int &exceptioncode );
00102     void normalize ();
00103 
00104     // Other methods (not part of DOM)
00105     virtual bool isElementNode() const { return false; }
00106     virtual bool isHTMLElement() const { return false; }
00107     virtual bool isAttributeNode() const { return false; }
00108     virtual bool isTextNode() const { return false; }
00109     virtual bool isDocumentNode() const { return false; }
00110     virtual bool isXMLElementNode() const { return false; }
00111     virtual bool isGenericFormElement() const { return false; }
00112 
00113     // helper functions not being part of the DOM
00114     // Attention: they assume that the caller did the consistency checking!
00115     void setPreviousSibling(NodeImpl *previous) { m_previous = previous; }
00116     void setNextSibling(NodeImpl *next) { m_next = next; }
00117 
00118     virtual void setFirstChild(NodeImpl *child);
00119     virtual void setLastChild(NodeImpl *child);
00120 
00121     // used by the parser. Doesn't do as many error checkings as
00122     // appendChild(), and returns the node into which will be parsed next.
00123     virtual NodeImpl *addChild(NodeImpl *newChild);
00124 
00125     typedef Q_UINT32 Id;
00126     // id() is used to easily and exactly identify a node. It
00127     // is optimized for quick comparison and low memory consumption.
00128     // its value depends on the owner document of the node and is
00129     // categorized in the following way:
00130     // 1..ID_LAST_TAG: the node inherits HTMLElementImpl and is
00131     //                 part of the HTML namespace.
00132     //                 The HTML namespace is either the global
00133     //                 one (no namespace) or the XHTML namespace
00134     //                 depending on the owner document's doctype
00135     // ID_LAST_TAG+1..0xffff: non-HTML elements in the global namespace
00136     // others       non-HTML elements in a namespace.
00137     //                 the upper 16 bit identify the namespace
00138     //                 the lower 16 bit identify the local part of the
00139     //                 qualified element name.
00140     virtual Id id() const { return 0; };
00141 
00142     enum MouseEventType {
00143         MousePress,
00144         MouseRelease,
00145         MouseClick,
00146         MouseDblClick,
00147         MouseMove
00148     };
00149 
00150     struct MouseEvent
00151     {
00152         MouseEvent( int _button, MouseEventType _type,
00153                     const DOMString &_url = DOMString(), const DOMString& _target = DOMString(),
00154                     NodeImpl *_innerNode = 0)
00155             {
00156                 button = _button; type = _type;
00157                 url = _url; target = _target;
00158                 innerNode = _innerNode;
00159             }
00160 
00161         int button;
00162         MouseEventType type;
00163         DOMString url; // url under mouse or empty
00164         DOMString target;
00165         Node innerNode;
00166     };
00167 
00168     // for LINK and STYLE
00169     virtual void sheetLoaded() {}
00170 
00171     bool hasID() const      { return m_hasId; }
00172     bool hasStyle() const   { return m_hasStyle; }
00173     bool pressed() const    { return m_pressed; }
00174     bool active() const     { return m_active; }
00175     bool focused() const { return m_focused; }
00176     bool attached() const   { return m_attached; }
00177     bool changed() const    { return m_changed; }
00178     bool hasChangedChild() const { return m_hasChangedChild; }
00179     bool hasAnchor() const { return m_hasAnchor; }
00180     bool inDocument() const { return m_inDocument; }
00181     bool styleElement() const { return m_styleElement; }
00182     bool implicitNode() const { return m_implicit; }
00183     void setHasID(bool b=true) { m_hasId = b; }
00184     void setHasStyle(bool b=true) { m_hasStyle = b; }
00185     void setPressed(bool b=true) { m_pressed = b; }
00186     void setHasChangedChild( bool b = true ) { m_hasChangedChild = b; }
00187     void setInDocument(bool b=true) { m_inDocument = b; }
00188     virtual void setFocus(bool b=true) { m_focused = b; }
00189     virtual void setActive(bool b=true) { m_active = b; }
00190     virtual void setChanged(bool b=true);
00191 
00192     unsigned short tabIndex() const { return m_tabIndex; }
00193     void setTabIndex(unsigned short _tabIndex) { m_tabIndex = _tabIndex; }
00194 
00198     virtual bool isSelectable() const { return false; };
00199 
00200     bool isInline() const;
00201     virtual QString toHTML() const;
00202     QString recursive_toHTML(bool start = false) const;
00203 
00204     virtual void getCursor(int offset, int &_x, int &_y, int &height);
00205     virtual QRect getRect() const;
00206 
00207     enum StyleChange { NoChange, NoInherit, Inherit, Force };
00208     virtual void recalcStyle( StyleChange = NoChange ) {}
00209     StyleChange diff( khtml::RenderStyle *s1, khtml::RenderStyle *s2 ) const;
00210 
00211     unsigned long nodeIndex() const;
00212     // Returns the document that this node is associated with. This is guaranteed to always be non-null, as opposed to
00213     // DOM's ownerDocument() which is null for Document nodes (and sometimes DocumentType nodes).
00214     DocumentImpl* getDocument() const { return document->document(); }
00215 
00216     void addEventListener(int id, EventListener *listener, const bool useCapture);
00217     void removeEventListener(int id, EventListener *listener, bool useCapture);
00218     void removeHTMLEventListener(int id);
00219     void setHTMLEventListener(int id, EventListener *listener);
00220     EventListener *getHTMLEventListener(int id);
00221 
00222     bool dispatchEvent(EventImpl *evt, int &exceptioncode, bool tempEvent = false);
00223     bool dispatchGenericEvent( EventImpl *evt, int &exceptioncode);
00224     bool dispatchHTMLEvent(int _id, bool canBubbleArg, bool cancelableArg);
00225     bool dispatchWindowEvent(int _id, bool canBubbleArg, bool cancelableArg);
00226     bool dispatchMouseEvent(QMouseEvent *e, int overrideId = 0, int overrideDetail = 0);
00227     bool dispatchUIEvent(int _id, int detail = 0);
00228     bool dispatchSubtreeModifiedEvent();
00229     bool dispatchKeyEvent(QKeyEvent *key);
00230 
00231     void handleLocalEvents(EventImpl *evt, bool useCapture);
00232 
00236     virtual void defaultEventHandler(EventImpl *evt);
00237 
00238     virtual bool isReadOnly();
00239     virtual bool childTypeAllowed( unsigned short /*type*/ ) { return false; }
00240     virtual unsigned long childNodeCount();
00241     virtual NodeImpl *childNode(unsigned long index);
00242 
00254     NodeImpl *traverseNextNode(NodeImpl *stayWithin = 0) const;
00255 
00261     NodeImpl *traversePreviousNode() const;
00262 
00263     DocumentPtr *docPtr() const { return document; }
00264 
00265     khtml::RenderObject *renderer() const { return m_render; }
00266     khtml::RenderObject *nextRenderer();
00267 
00268     void checkSetPrefix(const DOMString &_prefix, int &exceptioncode);
00269     void checkAddChild(NodeImpl *newChild, int &exceptioncode);
00270     bool isAncestor( NodeImpl *other );
00271     virtual bool childAllowed( NodeImpl *newChild );
00272 #ifndef NDEBUG
00273     virtual void dump(QTextStream *stream, QString ind = "") const;
00274 #endif
00275 
00276     // -----------------------------------------------------------------------------
00277     // Integration with rendering tree
00278 
00284     virtual void attach();
00285 
00290     virtual void detach();
00291 
00292     // -----------------------------------------------------------------------------
00293     // Methods for maintaining the state of the element between history navigation
00294 
00301     virtual bool maintainsState();
00302 
00309     virtual QString state();
00310 
00317     virtual void restoreState(const QString &state);
00318 
00319     // -----------------------------------------------------------------------------
00320     // Notification of document stucture changes
00321 
00331     virtual void insertedIntoDocument();
00332 
00340     virtual void removedFromDocument();
00341 
00346     virtual void childrenChanged();
00347 
00348 private: // members
00349     DocumentPtr *document;
00350     NodeImpl *m_previous;
00351     NodeImpl *m_next;
00352 protected:
00353     khtml::RenderObject *m_render;
00354     QPtrList<RegisteredEventListener> *m_regdListeners;
00355 
00356     unsigned short m_tabIndex : 15;
00357     bool m_hasTabIndex  : 1;
00358 
00359     bool m_hasId : 1;
00360     bool m_hasStyle : 1;
00361     bool m_pressed : 1;
00362     bool m_attached : 1;
00363     bool m_changed : 1;
00364     bool m_hasChangedChild : 1;
00365     bool m_inDocument : 1;
00366 
00367     bool m_hasAnchor : 1;
00368     bool m_specified : 1; // used in AttrImpl. Accessor functions there
00369     bool m_focused : 1;
00370     bool m_active : 1;
00371     bool m_styleElement : 1; // contains stylesheet text
00372     bool m_implicit : 1; // implicitely generated by the parser
00373 
00374     // 2 bits unused
00375 };
00376 
00377 // this is the full Node Implementation with parents and children.
00378 class NodeBaseImpl : public NodeImpl
00379 {
00380 public:
00381     NodeBaseImpl(DocumentPtr *doc)
00382         : NodeImpl(doc), _first(0), _last(0) {}
00383     virtual ~NodeBaseImpl();
00384 
00385     // DOM methods overridden from  parent classes
00386     virtual NodeImpl *firstChild() const;
00387     virtual NodeImpl *lastChild() const;
00388     virtual NodeImpl *insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode );
00389     virtual NodeImpl *replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode );
00390     virtual NodeImpl *removeChild ( NodeImpl *oldChild, int &exceptioncode );
00391     virtual NodeImpl *appendChild ( NodeImpl *newChild, int &exceptioncode );
00392     virtual bool hasChildNodes (  ) const;
00393 
00394     // Other methods (not part of DOM)
00395     void removeChildren();
00396     void cloneChildNodes(NodeImpl *clone);
00397 
00398     virtual void setFirstChild(NodeImpl *child);
00399     virtual void setLastChild(NodeImpl *child);
00400     virtual NodeImpl *addChild(NodeImpl *newChild);
00401     virtual void attach();
00402     virtual void detach();
00403 
00404     virtual NodeListImpl *getElementsByTagNameNS ( DOMStringImpl* namespaceURI,
00405                                                    DOMStringImpl* localName );
00406 
00407     virtual QRect getRect() const;
00408     bool getUpperLeftCorner(int &xPos, int &yPos) const;
00409     bool getLowerRightCorner(int &xPos, int &yPos) const;
00410 
00411     virtual void setFocus(bool=true);
00412     virtual void setActive(bool=true);
00413     virtual unsigned long childNodeCount();
00414     virtual NodeImpl *childNode(unsigned long index);
00415 
00416 protected:
00417     NodeImpl *_first;
00418     NodeImpl *_last;
00419 
00420     // helper functions for inserting children:
00421 
00422     // ### this should vanish. do it in dom/ !
00423     // check for same source document:
00424     bool checkSameDocument( NodeImpl *newchild, int &exceptioncode );
00425     // check for being child:
00426     bool checkIsChild( NodeImpl *oldchild, int &exceptioncode );
00427     // ###
00428 
00429     // find out if a node is allowed to be our child
00430     void dispatchChildInsertedEvents( NodeImpl *child, int &exceptioncode );
00431     void dispatchChildRemovalEvents( NodeImpl *child, int &exceptioncode );
00432 };
00433 
00434 // --------------------------------------------------------------------------
00435 class Node;
00436 class NodeImpl;
00437 
00438 class NodeListImpl : public khtml::Shared<NodeListImpl>
00439 {
00440 public:
00441     virtual ~NodeListImpl() {}
00442 
00443     // DOM methods & attributes for NodeList
00444     virtual unsigned long length() const;
00445     virtual NodeImpl *item ( unsigned long index ) const;
00446 
00447     // Other methods (not part of DOM)
00448 
00449 protected:
00450     // helper functions for searching all ElementImpls in a tree
00451     unsigned long recursiveLength(NodeImpl *start) const;
00452     NodeImpl *recursiveItem ( NodeImpl *start, unsigned long &offset ) const;
00453     virtual bool nodeMatches( NodeImpl *testNode ) const = 0;
00454 };
00455 
00456 class ChildNodeListImpl : public NodeListImpl
00457 {
00458 public:
00459     ChildNodeListImpl( NodeImpl *n);
00460     virtual ~ChildNodeListImpl();
00461 
00462     // DOM methods overridden from  parent classes
00463 
00464     virtual unsigned long length() const;
00465     virtual NodeImpl *item ( unsigned long index ) const;
00466 
00467 protected:
00468     virtual bool nodeMatches( NodeImpl *testNode ) const;
00469 
00470     NodeImpl *refNode;
00471 };
00472 
00473 
00477 class TagNodeListImpl : public NodeListImpl
00478 {
00479 public:
00480     TagNodeListImpl( NodeImpl *n, NodeImpl::Id tagId, NodeImpl::Id tagIdMask );
00481     virtual ~TagNodeListImpl();
00482 
00483     // DOM methods overridden from  parent classes
00484 
00485     virtual unsigned long length() const;
00486     virtual NodeImpl *item ( unsigned long index ) const;
00487 
00488     // Other methods (not part of DOM)
00489 
00490 protected:
00491     virtual bool nodeMatches( NodeImpl *testNode ) const;
00492 
00493     NodeImpl *refNode;
00494     NodeImpl::Id m_id;
00495     NodeImpl::Id m_idMask;
00496 };
00497 
00498 
00502 class NameNodeListImpl : public NodeListImpl
00503 {
00504 public:
00505     NameNodeListImpl( NodeImpl *doc, const DOMString &t );
00506     virtual ~NameNodeListImpl();
00507 
00508     // DOM methods overridden from  parent classes
00509 
00510     virtual unsigned long length() const;
00511     virtual NodeImpl *item ( unsigned long index ) const;
00512 
00513     // Other methods (not part of DOM)
00514 
00515 protected:
00516     virtual bool nodeMatches( NodeImpl *testNode ) const;
00517 
00518     NodeImpl *refNode;
00519     DOMString nodeName;
00520 };
00521 
00522 
00527 class NamedTagNodeListImpl : public TagNodeListImpl
00528 {
00529 public:
00530     NamedTagNodeListImpl( NodeImpl *n, NodeImpl::Id tagId, const DOMString& name,
00531                           NodeImpl::Id tagIdMask = NodeImpl_IdNSMask | NodeImpl_IdLocalMask );
00532 protected:
00533     virtual bool nodeMatches( NodeImpl *testNode ) const;
00534     DOMString nodeName;
00535 };
00536 
00537 // Generic NamedNodeMap interface
00538 // Other classes implement this for more specific situations e.g. attributes
00539 // of an element
00540 class NamedNodeMapImpl : public khtml::Shared<NamedNodeMapImpl>
00541 {
00542 public:
00543     NamedNodeMapImpl();
00544     virtual ~NamedNodeMapImpl();
00545 
00546     // DOM methods & attributes for NamedNodeMap
00547     virtual NodeImpl *getNamedItem ( NodeImpl::Id id ) const = 0;
00548     virtual Node removeNamedItem ( NodeImpl::Id id, int &exceptioncode ) = 0;
00549     virtual Node setNamedItem ( NodeImpl* arg, int &exceptioncode ) = 0;
00550 
00551     virtual NodeImpl *item ( unsigned long index ) const = 0;
00552     virtual unsigned long length(  ) const = 0;
00553 
00554     // Other methods (not part of DOM)
00555     virtual NodeImpl::Id mapId(const DOMString& namespaceURI,  const DOMString& localName,  bool readonly) = 0;
00556     virtual bool isReadOnly() { return false; }
00557 };
00558 
00559 // ### fixme
00560 #if 0
00561 // Generic read-only NamedNodeMap implementation
00562 // You can add items using the internal function addItem()
00563 class GenericRONamedNodeMapImpl : public NamedNodeMapImpl
00564 {
00565 public:
00566     GenericRONamedNodeMapImpl(DocumentPtr* doc);
00567     virtual ~GenericRONamedNodeMapImpl();
00568 
00569     // DOM methods & attributes for NamedNodeMap
00570 
00571     virtual NodeImpl *getNamedItem ( const DOMString &name, int &exceptioncode ) const;
00572     virtual Node setNamedItem ( const Node &arg, int &exceptioncode );
00573     virtual Node removeNamedItem ( const DOMString &name, int &exceptioncode );
00574     virtual NodeImpl *item ( unsigned long index ) const;
00575     virtual unsigned long length(  ) const;
00576     virtual NodeImpl *getNamedItemNS( const DOMString &namespaceURI, const DOMString &localName,
00577                                       int &exceptioncode ) const;
00578     virtual NodeImpl *setNamedItemNS( NodeImpl *arg, int &exceptioncode );
00579     virtual NodeImpl *removeNamedItemNS( const DOMString &namespaceURI, const DOMString &localName,
00580                                          int &exceptioncode );
00581 
00582     // Other methods (not part of DOM)
00583 
00584     virtual bool isReadOnly() { return true; }
00585 
00586     void addNode(NodeImpl *n);
00587 
00588 protected:
00589     DocumentImpl* m_doc;
00590     QPtrList<NodeImpl> *m_contents;
00591 };
00592 #endif
00593 
00594 } //namespace
00595 #endif
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:33:47 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001