khtml Library API Documentation

dom_elementimpl.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 Peter Kelly (pmk@post.com)
00007  *           (C) 2001 Dirk Mueller (mueller@kde.org)
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Library General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Library General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Library General Public License
00020  * along with this library; see the file COPYING.LIB.  If not, write to
00021  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00022  * Boston, MA 02111-1307, USA.
00023  *
00024  * $Id: dom_elementimpl.h,v 1.101 2002/05/23 12:09:37 wildfox Exp $
00025  */
00026 #ifndef _DOM_ELEMENTImpl_h_
00027 #define _DOM_ELEMENTImpl_h_
00028 
00029 #include "dom_nodeimpl.h"
00030 #include "dom/dom_element.h"
00031 #include "xml/dom_stringimpl.h"
00032 #include "misc/shared.h"
00033 
00034 namespace khtml {
00035     class CSSStyleSelector;
00036 }
00037 
00038 namespace DOM {
00039 
00040 class ElementImpl;
00041 class DocumentImpl;
00042 class NamedAttrMapImpl;
00043 
00044 // this has no counterpart in DOM, purely internal
00045 // representation of the nodevalue of an Attr.
00046 // the actual Attr (AttrImpl) with its value as textchild
00047 // is only allocated on demand by the DOM bindings.
00048 // Any use of AttrImpl inside khtml should be avoided.
00049 class AttributeImpl : public khtml::Shared<AttributeImpl>
00050 {
00051     friend class NamedAttrMapImpl;
00052     friend class ElementImpl;
00053     friend class AttrImpl;
00054 
00055 public:
00056     // null value is forbidden !
00057     AttributeImpl(NodeImpl::Id id, DOMStringImpl* value)
00058         : m_id(id), _prefix(0), _value(value), _impl(0)
00059         { _value->ref(); };
00060     ~AttributeImpl() {
00061         if (_prefix) _prefix->deref();
00062         if (_value) _value->deref();
00063         // assert : _impl == 0
00064     }
00065 
00066     DOMString value() const { return _value; }
00067     DOMStringImpl* val() const { return _value; }
00068     DOMStringImpl* prefix() const { return _prefix; }
00069     NodeImpl::Id id() const { return m_id; }
00070     AttrImpl* attrImpl() const { return _impl; }
00071 
00072 private:
00073     // null pointers can never happen here
00074     void setValue(DOMStringImpl* value) {
00075         _value->deref();
00076         _value = value;
00077         _value->ref();
00078     }
00079     void setPrefix(DOMStringImpl* prefix) {
00080         if (_prefix) _prefix->deref();
00081         _prefix = prefix;
00082         if (_prefix) _prefix->ref();
00083     }
00084     void allocateImpl(ElementImpl* e);
00085 
00086 protected:
00087     NodeImpl::Id m_id;
00088     DOMStringImpl *_prefix;
00089     DOMStringImpl *_value;
00090     AttrImpl* _impl;
00091 };
00092 
00093 // Attr can have Text and EntityReference children
00094 // therefore it has to be a fullblown Node. The plan
00095 // is to dynamically allocate a textchild and store the
00096 // resulting nodevalue in the AttributeImpl upon
00097 // destruction. however, this is not yet implemented.
00098 class AttrImpl : public NodeBaseImpl
00099 {
00100     friend class ElementImpl;
00101     friend class NamedAttrMapImpl;
00102 
00103 public:
00104     AttrImpl(ElementImpl* element, DocumentPtr* docPtr, AttributeImpl* a);
00105     ~AttrImpl();
00106 
00107 private:
00108     AttrImpl(const AttrImpl &other);
00109     AttrImpl &operator = (const AttrImpl &other);
00110 public:
00111 
00112     // DOM methods & attributes for Attr
00113     bool specified() const { return m_specified; }
00114     ElementImpl* ownerElement() const { return m_element; }
00115     AttributeImpl* attrImpl() const { return m_attribute; }
00116 
00117     //DOMString value() const;
00118     void setValue( const DOMString &v, int &exceptioncode );
00119 
00120     // DOM methods overridden from  parent classes
00121     virtual DOMString nodeName() const;
00122     virtual unsigned short nodeType() const;
00123     virtual DOMString prefix() const;
00124     virtual void setPrefix(const DOMString &_prefix, int &exceptioncode );
00125 
00126     virtual DOMString nodeValue() const;
00127     virtual void setNodeValue( const DOMString &, int &exceptioncode );
00128     virtual NodeImpl *cloneNode ( bool deep );
00129 
00130     // Other methods (not part of DOM)
00131     virtual bool isAttributeNode() const { return true; }
00132     virtual bool childAllowed( NodeImpl *newChild );
00133     virtual bool childTypeAllowed( unsigned short type );
00134 
00135 protected:
00136     ElementImpl* m_element;
00137     AttributeImpl* m_attribute;
00138 };
00139 
00140 
00141 class ElementImpl : public NodeBaseImpl
00142 {
00143     friend class DocumentImpl;
00144     friend class NamedAttrMapImpl;
00145     friend class AttrImpl;
00146     friend class NodeImpl;
00147     friend class khtml::CSSStyleSelector;
00148 public:
00149     ElementImpl(DocumentPtr *doc);
00150     ~ElementImpl();
00151 
00152     DOMString getAttribute( NodeImpl::Id id ) const;
00153     void setAttribute( NodeImpl::Id id, DOMStringImpl* value, int &exceptioncode );
00154     void removeAttribute( NodeImpl::Id id, int &exceptioncode );
00155 
00156     DOMString prefix() const { return m_prefix; }
00157     void setPrefix(const DOMString &_prefix, int &exceptioncode );
00158 
00159     // DOM methods overridden from  parent classes
00160     virtual DOMString tagName() const;
00161     virtual unsigned short nodeType() const;
00162     virtual NodeImpl *cloneNode ( bool deep );
00163     virtual DOMString nodeName() const;
00164     virtual bool isElementNode() const { return true; }
00165 
00166     // convenience methods which ignore exceptions
00167     void setAttribute (NodeImpl::Id id, const DOMString &value);
00168 
00169     NamedAttrMapImpl* attributes(bool readonly = false) const
00170     {
00171         if (!readonly && !namedAttrMap) createAttributeMap();
00172         return namedAttrMap;
00173     }
00174 
00175     //This is always called, whenever an attribute changed
00176     virtual void parseAttribute(AttributeImpl *) {}
00177 
00178     // not part of the DOM
00179     void setAttributeMap ( NamedAttrMapImpl* list );
00180 
00181     // State of the element.
00182     virtual QString state() { return QString::null; }
00183 
00184     virtual void attach();
00185     virtual void recalcStyle( StyleChange = NoChange );
00186 
00187     virtual void mouseEventHandler( MouseEvent */*ev*/, bool /*inside*/ ) {};
00188     virtual bool isSelectable() const;
00189     virtual bool childAllowed( NodeImpl *newChild );
00190     virtual bool childTypeAllowed( unsigned short type );
00191 
00192     DOM::CSSStyleDeclarationImpl *styleRules() {
00193       if (!m_styleDecls) createDecl();
00194       return m_styleDecls;
00195     }
00196 
00197     void dispatchAttrRemovalEvent(AttributeImpl *attr);
00198     void dispatchAttrAdditionEvent(AttributeImpl *attr);
00199 
00200 #ifndef NDEBUG
00201     virtual void dump(QTextStream *stream, QString ind = "") const;
00202 #endif
00203 
00204 protected:
00205     void createAttributeMap() const;
00206     void createDecl();
00207 
00208 private:
00209     // map of default attributes. derived element classes are responsible
00210     // for setting this according to the corresponding element description
00211     // in the DTD
00212     virtual NamedAttrMapImpl* defaultMap() const;
00213 
00214 protected: // member variables
00215     mutable NamedAttrMapImpl *namedAttrMap;
00216 
00217     DOM::CSSStyleDeclarationImpl *m_styleDecls;
00218     DOMStringImpl *m_prefix;
00219 };
00220 
00221 
00222 class XMLElementImpl : public ElementImpl
00223 {
00224 
00225 public:
00226     XMLElementImpl(DocumentPtr *doc, DOMStringImpl *_tagName);
00227     XMLElementImpl(DocumentPtr *doc, DOMStringImpl *_qualifiedName, DOMStringImpl *_namespaceURI);
00228     ~XMLElementImpl();
00229 
00230     // DOM methods overridden from  parent classes
00231 
00232     virtual DOMString localName() const;
00233     virtual NodeImpl *cloneNode ( bool deep );
00234 
00235     // Other methods (not part of DOM)
00236     virtual bool isXMLElementNode() const { return true; }
00237     virtual Id id() const { return m_id; }
00238 
00239 protected:
00240     Id m_id;
00241 };
00242 
00243 // the map of attributes of an element
00244 class NamedAttrMapImpl : public NamedNodeMapImpl
00245 {
00246     friend class ElementImpl;
00247 public:
00248     NamedAttrMapImpl(ElementImpl *e);
00249     virtual ~NamedAttrMapImpl();
00250     NamedAttrMapImpl(const NamedAttrMapImpl&);
00251     NamedAttrMapImpl &operator =(const NamedAttrMapImpl &other);
00252 
00253     // DOM methods & attributes for NamedNodeMap
00254     virtual AttrImpl *getNamedItem ( NodeImpl::Id id ) const;
00255     virtual Node removeNamedItem ( NodeImpl::Id id, int &exceptioncode );
00256     virtual Node setNamedItem ( NodeImpl* arg, int &exceptioncode );
00257 
00258     virtual AttrImpl *item ( unsigned long index ) const;
00259     virtual unsigned long length(  ) const;
00260 
00261     // Other methods (not part of DOM)
00262     virtual NodeImpl::Id mapId(const DOMString& namespaceURI,  const DOMString& localName,  bool readonly);
00263     AttributeImpl* attributeItem(unsigned long index) const { return attrs ? attrs[index] : 0; }
00264     AttributeImpl* getAttributeItem(NodeImpl::Id id) const;
00265     virtual bool isReadOnly() { return element ? element->isReadOnly() : false; }
00266 
00267     // used during parsing: only inserts if not already there
00268     // no error checking!
00269     void insertAttribute(AttributeImpl* newAttribute) {
00270         if (!getAttributeItem(newAttribute->id()))
00271             addAttribute(newAttribute);
00272         else
00273             newAttribute->deref();
00274     }
00275 
00276 private:
00277     // this method is internal, does no error checking at all
00278     void addAttribute(AttributeImpl* newAttribute);
00279     // this method is internal, does no error checking at all
00280     void removeAttribute(NodeImpl::Id id);
00281     void clearAttributes();
00282     void detachFromElement();
00283 
00284 protected:
00285     ElementImpl *element;
00286     AttributeImpl **attrs;
00287     uint len;
00288 };
00289 
00290 }; //namespace
00291 
00292 #endif
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:22:35 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001