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 Peter Kelly (pmk@post.com)
7 * (C) 2001 Dirk Mueller (mueller@kde.org)
8 * Copyright (C) 2003 Apple Computer, Inc.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public License
21 * along with this library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
26 #ifndef _DOM_ELEMENTImpl_h_
27 #define _DOM_ELEMENTImpl_h_
29 #include "dom_nodeimpl.h"
30 #include "dom/dom_element.h"
31 #include "xml/dom_stringimpl.h"
32 #include "misc/shared.h"
36 #define id id_AVOID_KEYWORD
41 class CSSStyleSelector;
48 class NamedAttrMapImpl;
49 class AtomicStringList;
51 // this has no counterpart in DOM, purely internal
52 // representation of the nodevalue of an Attr.
53 // the actual Attr (AttrImpl) with its value as textchild
54 // is only allocated on demand by the DOM bindings.
55 // Any use of AttrImpl inside khtml should be avoided.
56 class AttributeImpl : public khtml::Shared<AttributeImpl>
58 friend class NamedAttrMapImpl;
59 friend class ElementImpl;
60 friend class AttrImpl;
63 // null value is forbidden !
64 AttributeImpl(NodeImpl::Id id, const AtomicString& value)
65 : m_id(id), _value(value), _impl(0)
67 virtual ~AttributeImpl() {};
69 const AtomicString& value() const { return _value; }
70 const AtomicString& prefix() const { return _prefix; }
71 NodeImpl::Id id() const { return m_id; }
72 AttrImpl* attrImpl() const { return _impl; }
74 bool isNull() const { return _value.isNull(); }
75 bool isEmpty() const { return _value.isEmpty(); }
77 virtual AttributeImpl* clone(bool preserveDecl=true) const;
80 void setValue(const AtomicString& value) {
83 void setPrefix(const AtomicString& prefix) {
86 void allocateImpl(ElementImpl* e);
95 // Attr can have Text and EntityReference children
96 // therefore it has to be a fullblown Node. The plan
97 // is to dynamically allocate a textchild and store the
98 // resulting nodevalue in the AttributeImpl upon
99 // destruction. however, this is not yet implemented.
100 class AttrImpl : public NodeBaseImpl
102 friend class ElementImpl;
103 friend class NamedAttrMapImpl;
106 AttrImpl(ElementImpl* element, DocumentPtr* docPtr, AttributeImpl* a);
110 AttrImpl(const AttrImpl &other);
111 AttrImpl &operator = (const AttrImpl &other);
114 // DOM methods & attributes for Attr
115 bool specified() const { return m_specified; }
116 ElementImpl* ownerElement() const { return m_element; }
117 AttributeImpl* attrImpl() const { return m_attribute; }
119 //DOMString value() const;
120 void setValue( const DOMString &v, int &exceptioncode );
122 // DOM methods overridden from parent classes
123 virtual DOMString nodeName() const;
124 virtual unsigned short nodeType() const;
125 virtual DOMString prefix() const;
126 virtual void setPrefix(const DOMString &_prefix, int &exceptioncode );
128 virtual DOMString nodeValue() const;
129 virtual void setNodeValue( const DOMString &, int &exceptioncode );
130 virtual NodeImpl *cloneNode ( bool deep );
132 // Other methods (not part of DOM)
133 virtual bool isAttributeNode() const { return true; }
134 virtual bool childAllowed( NodeImpl *newChild );
135 virtual bool childTypeAllowed( unsigned short type );
137 virtual DOMString toString() const;
140 static Attr createInstance(AttrImpl *impl);
144 ElementImpl* m_element;
145 AttributeImpl* m_attribute;
149 class ElementImpl : public NodeBaseImpl
151 friend class DocumentImpl;
152 friend class NamedAttrMapImpl;
153 friend class AttrImpl;
154 friend class NodeImpl;
155 friend class khtml::CSSStyleSelector;
157 ElementImpl(DocumentPtr *doc);
160 // Used to quickly determine whether or not an element has a given CSS class.
161 virtual const AtomicStringList* getClassList() const;
162 const AtomicString& getIDAttribute() const;
163 const AtomicString& getAttribute( NodeImpl::Id id ) const;
164 const AtomicString& getAttribute(const DOMString& localName) const { return getAttributeNS(QString::null, localName); }
165 const AtomicString& getAttributeNS(const DOMString &namespaceURI,
166 const DOMString &localName) const;
167 void setAttribute( NodeImpl::Id id, DOMStringImpl* value, int &exceptioncode );
168 void removeAttribute( NodeImpl::Id id, int &exceptioncode );
169 bool hasAttributes() const;
171 DOMString prefix() const { return m_prefix; }
172 void setPrefix(const DOMString &_prefix, int &exceptioncode );
174 // DOM methods overridden from parent classes
175 virtual DOMString tagName() const;
176 virtual unsigned short nodeType() const;
177 virtual NodeImpl *cloneNode ( bool deep );
178 virtual DOMString nodeName() const;
179 virtual bool isElementNode() const { return true; }
181 // convenience methods which ignore exceptions
182 void setAttribute (NodeImpl::Id id, const DOMString &value);
184 NamedAttrMapImpl* attributes(bool readonly = false) const;
186 // This method is called whenever an attribute is added, changed or removed.
187 virtual void attributeChanged(AttributeImpl* attr, bool preserveDecls = false) {}
189 // not part of the DOM
190 void setAttributeMap ( NamedAttrMapImpl* list );
192 // State of the element.
193 virtual QString state() { return QString::null; }
195 virtual void attach();
196 virtual void detach();
197 virtual khtml::RenderStyle *styleForRenderer(khtml::RenderObject *parent);
198 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
199 virtual void recalcStyle( StyleChange = NoChange );
201 virtual void mouseEventHandler( MouseEvent */*ev*/, bool /*inside*/ ) {};
202 virtual bool childAllowed( NodeImpl *newChild );
203 virtual bool childTypeAllowed( unsigned short type );
205 virtual AttributeImpl* createAttribute(NodeImpl::Id id, DOMStringImpl* value);
207 void dispatchAttrRemovalEvent(AttributeImpl *attr);
208 void dispatchAttrAdditionEvent(AttributeImpl *attr);
210 virtual void accessKeyAction() {};
212 virtual DOMString toString() const;
214 virtual bool isURLAttribute(AttributeImpl *attr) const;
217 virtual void dump(QTextStream *stream, QString ind = "") const;
221 static Element createInstance(ElementImpl *impl);
225 virtual void formatForDebugger(char *buffer, unsigned length) const;
229 virtual void createAttributeMap() const;
230 DOMString openTagStartToString() const;
233 void updateId(const AtomicString& oldId, const AtomicString& newId);
235 virtual void updateStyleAttributeIfNeeded() const {};
237 protected: // member variables
238 mutable NamedAttrMapImpl *namedAttrMap;
239 DOMStringImpl *m_prefix;
243 class XMLElementImpl : public ElementImpl
247 XMLElementImpl(DocumentPtr *doc, DOMStringImpl *_tagName);
248 XMLElementImpl(DocumentPtr *doc, DOMStringImpl *_qualifiedName, DOMStringImpl *_namespaceURI);
251 // DOM methods overridden from parent classes
253 virtual DOMString localName() const;
254 virtual NodeImpl *cloneNode ( bool deep );
256 // Other methods (not part of DOM)
257 virtual bool isXMLElementNode() const { return true; }
258 virtual Id id() const { return m_id; }
264 // the map of attributes of an element
265 class NamedAttrMapImpl : public NamedNodeMapImpl
267 friend class ElementImpl;
269 NamedAttrMapImpl(ElementImpl *e);
270 virtual ~NamedAttrMapImpl();
271 NamedAttrMapImpl(const NamedAttrMapImpl&);
272 NamedAttrMapImpl &operator =(const NamedAttrMapImpl &other);
274 // DOM methods & attributes for NamedNodeMap
275 virtual AttrImpl *getNamedItem ( NodeImpl::Id id ) const;
276 virtual Node removeNamedItem ( NodeImpl::Id id, int &exceptioncode );
277 virtual Node setNamedItem ( NodeImpl* arg, int &exceptioncode );
279 virtual AttrImpl *item ( unsigned long index ) const;
280 unsigned long length() const { return len; }
282 // Other methods (not part of DOM)
283 virtual NodeImpl::Id mapId(const DOMString& namespaceURI, const DOMString& localName, bool readonly);
284 AttributeImpl* attributeItem(unsigned long index) const { return attrs ? attrs[index] : 0; }
285 AttributeImpl* getAttributeItem(NodeImpl::Id id) const;
286 virtual bool isReadOnly() { return element ? element->isReadOnly() : false; }
288 // used during parsing: only inserts if not already there
289 // no error checking!
290 void insertAttribute(AttributeImpl* newAttribute) {
291 if (!getAttributeItem(newAttribute->id()))
292 addAttribute(newAttribute);
294 newAttribute->deref();
297 virtual bool isHTMLAttributeMap() const;
299 const AtomicString& id() const { return m_id; }
300 void setID(const AtomicString& _id) { m_id = _id; }
303 // this method is internal, does no error checking at all
304 void addAttribute(AttributeImpl* newAttribute);
305 // this method is internal, does no error checking at all
306 void removeAttribute(NodeImpl::Id id);
307 virtual void clearAttributes();
308 void detachFromElement();
311 ElementImpl *element;
312 AttributeImpl **attrs;