File indexing completed on 2024-12-01 12:33:30
0001 /* 0002 * This file is part of the KDE libraries 0003 * Copyright (C) 2000 Harri Porten (porten@kde.org) 0004 * Copyright (C) 2003 Apple Computer, Inc. 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Library General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2 of the License, or (at your option) any later version. 0010 * 0011 * This library is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Library General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Library General Public 0017 * License along with this library; if not, write to the Free Software 0018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0019 */ 0020 0021 #ifndef _KJS_DOM_H_ 0022 #define _KJS_DOM_H_ 0023 0024 #include "xml/dom_nodeimpl.h" 0025 #include "xml/dom_docimpl.h" 0026 #include "xml/dom_elementimpl.h" 0027 #include "xml/dom_xmlimpl.h" 0028 0029 #include "ecma/kjs_binding.h" 0030 0031 namespace KJS 0032 { 0033 0034 class DOMNode : public DOMObject 0035 { 0036 public: 0037 // Build a DOMNode 0038 DOMNode(ExecState *exec, DOM::NodeImpl *n); 0039 DOMNode(JSObject *proto, DOM::NodeImpl *n); 0040 ~DOMNode(); 0041 bool toBoolean(ExecState *) const override; 0042 using KJS::JSObject::getOwnPropertySlot; 0043 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0044 JSValue *getValueProperty(ExecState *exec, int token) const; 0045 0046 using KJS::JSObject::put; 0047 void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None) override; 0048 void putValueProperty(ExecState *exec, int token, JSValue *value, int attr); 0049 const ClassInfo *classInfo() const override 0050 { 0051 return &info; 0052 } 0053 static const ClassInfo info; 0054 0055 JSValue *toPrimitive(ExecState *exec, JSType preferred = UndefinedType) const override; 0056 UString toString(ExecState *exec) const override; 0057 void setListener(ExecState *exec, int eventId, JSValue *func) const; 0058 JSValue *getListener(int eventId) const; 0059 virtual void pushEventHandlerScope(ExecState *exec, ScopeChain &scope) const; 0060 0061 enum { NodeName, NodeValue, NodeType, ParentNode, ParentElement, 0062 ChildNodes, FirstChild, LastChild, PreviousSibling, NextSibling, 0063 Attributes, NamespaceURI, Prefix, LocalName, OwnerDocument, InsertBefore, 0064 ReplaceChild, RemoveChild, AppendChild, HasAttributes, HasChildNodes, 0065 CloneNode, Normalize, IsSupported, AddEventListener, RemoveEventListener, 0066 DispatchEvent, Contains, InsertAdjacentHTML, 0067 OnAbort, OnBlur, OnChange, OnClick, OnDblClick, OnDragDrop, OnError, 0068 OnFocus, OnKeyDown, OnKeyPress, OnKeyUp, OnLoad, OnMouseDown, 0069 OnMouseMove, OnMouseOut, OnMouseOver, OnMouseUp, OnMove, OnReset, 0070 OnResize, OnScroll, OnSelect, OnSubmit, OnUnload, 0071 OffsetLeft, OffsetTop, OffsetWidth, OffsetHeight, OffsetParent, 0072 ClientLeft, ClientTop, ClientWidth, ClientHeight, ScrollLeft, ScrollTop, 0073 ScrollWidth, ScrollHeight, SourceIndex, TextContent, CompareDocumentPosition 0074 }; 0075 0076 //### toNode? virtual 0077 DOM::NodeImpl *impl() const 0078 { 0079 return m_impl.get(); 0080 } 0081 protected: 0082 SharedPtr<DOM::NodeImpl> m_impl; 0083 }; 0084 0085 DEFINE_CONSTANT_TABLE(DOMNodeConstants) 0086 KJS_DEFINE_PROTOTYPE(DOMNodeProto) 0087 DEFINE_PSEUDO_CONSTRUCTOR(NodeConstructor) 0088 0089 class DOMNodeList : public DOMObject 0090 { 0091 public: 0092 DOMNodeList(ExecState *, DOM::NodeListImpl *l); 0093 ~DOMNodeList(); 0094 0095 JSValue *indexGetter(ExecState *exec, unsigned index); 0096 using KJS::JSObject::getOwnPropertySlot; 0097 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0098 JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) override; 0099 bool implementsCall() const override 0100 { 0101 return true; 0102 } 0103 bool isFunctionType() const override 0104 { 0105 return false; 0106 } 0107 void getOwnPropertyNames(ExecState *, PropertyNameArray &, PropertyMap::PropertyMode mode) override; 0108 0109 // no put - all read-only 0110 const ClassInfo *classInfo() const override 0111 { 0112 return &info; 0113 } 0114 bool toBoolean(ExecState *) const override 0115 { 0116 return true; 0117 } 0118 static const ClassInfo info; 0119 0120 enum { Length, Item, NamedItem }; 0121 0122 DOM::NodeListImpl *impl() const 0123 { 0124 return m_impl.get(); 0125 } 0126 0127 DOM::NodeImpl *getByName(const Identifier &name); 0128 private: 0129 SharedPtr<DOM::NodeListImpl> m_impl; 0130 0131 static JSValue *nameGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &); 0132 static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &); 0133 }; 0134 0135 DEFINE_PSEUDO_CONSTRUCTOR(NodeListPseudoCtor) 0136 0137 class DOMDocument : public DOMNode 0138 { 0139 public: 0140 // Build a DOMDocument 0141 DOMDocument(ExecState *exec, DOM::DocumentImpl *d); 0142 DOMDocument(JSObject *proto, DOM::DocumentImpl *d); 0143 0144 using KJS::JSObject::getOwnPropertySlot; 0145 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0146 JSValue *getValueProperty(ExecState *exec, int token) const; 0147 0148 using KJS::JSObject::put; 0149 void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None) override; 0150 void putValueProperty(ExecState *exec, int token, JSValue *value, int attr); 0151 const ClassInfo *classInfo() const override 0152 { 0153 return &info; 0154 } 0155 static const ClassInfo info; 0156 enum { DocType, Implementation, DocumentElement, CharacterSet, 0157 // Functions 0158 CreateElement, CreateDocumentFragment, CreateTextNode, CreateComment, 0159 CreateCDATASection, CreateProcessingInstruction, CreateAttribute, 0160 CreateEntityReference, GetElementsByTagName, ImportNode, CreateElementNS, 0161 CreateAttributeNS, GetElementsByTagNameNS, GetElementById, 0162 CreateRange, CreateNodeIterator, CreateTreeWalker, DefaultView, 0163 CreateEvent, StyleSheets, GetOverrideStyle, Abort, Load, LoadXML, 0164 PreferredStylesheetSet, SelectedStylesheetSet, ReadyState, Async, 0165 GetElementsByClassName, Title, ExecCommand, QueryCommandEnabled, 0166 QueryCommandIndeterm, QueryCommandState, QueryCommandSupported, 0167 QueryCommandValue, QuerySelector, QuerySelectorAll, 0168 CreateExpression, CreateNSResolver, Evaluate 0169 }; 0170 DOM::DocumentImpl *impl() 0171 { 0172 return static_cast<DOM::DocumentImpl *>(m_impl.get()); 0173 } 0174 }; 0175 0176 KJS_DEFINE_PROTOTYPE(DOMDocumentProto) 0177 0178 DEFINE_PSEUDO_CONSTRUCTOR(DocumentPseudoCtor) 0179 0180 class DOMDocumentFragment : public DOMNode 0181 { 0182 public: 0183 DOMDocumentFragment(ExecState *exec, DOM::DocumentFragmentImpl *i); 0184 const ClassInfo *classInfo() const override 0185 { 0186 return &info; 0187 } 0188 static const ClassInfo info; 0189 0190 enum { QuerySelector, QuerySelectorAll }; 0191 }; 0192 DEFINE_PSEUDO_CONSTRUCTOR(DocumentFragmentPseudoCtor) 0193 0194 class DOMAttr : public DOMNode 0195 { 0196 public: 0197 DOMAttr(ExecState *exec, DOM::AttrImpl *a) : DOMNode(exec, a) { } 0198 using KJS::JSObject::getOwnPropertySlot; 0199 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0200 JSValue *getValueProperty(ExecState *exec, int token) const; 0201 0202 using KJS::JSObject::put; 0203 void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None) override; 0204 void putValueProperty(ExecState *exec, int token, JSValue *value, int attr); 0205 const ClassInfo *classInfo() const override 0206 { 0207 return &info; 0208 } 0209 static const ClassInfo info; 0210 enum { Name, Specified, ValueProperty, OwnerElement }; 0211 }; 0212 0213 class DOMElement : public DOMNode 0214 { 0215 public: 0216 // Build a DOMElement 0217 DOMElement(ExecState *exec, DOM::ElementImpl *e); 0218 DOMElement(JSObject *proto, DOM::ElementImpl *e); 0219 using KJS::JSObject::getOwnPropertySlot; 0220 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0221 JSValue *getValueProperty(ExecState *exec, int token) const; 0222 0223 // no put - all read-only 0224 const ClassInfo *classInfo() const override 0225 { 0226 return &info; 0227 } 0228 static const ClassInfo info; 0229 enum { TagName, Style, FirstElementChild, LastElementChild, 0230 PreviousElementSibling, NextElementSibling, ChildElementCount, 0231 GetAttribute, SetAttribute, RemoveAttribute, GetAttributeNode, 0232 SetAttributeNode, RemoveAttributeNode, GetElementsByTagName, 0233 GetAttributeNS, SetAttributeNS, RemoveAttributeNS, GetAttributeNodeNS, 0234 SetAttributeNodeNS, GetElementsByTagNameNS, HasAttribute, HasAttributeNS, 0235 GetElementsByClassName, Blur, Focus, QuerySelector, QuerySelectorAll, 0236 GetClientRects, GetBoundingClientRect 0237 }; 0238 private: 0239 #if 0 0240 static JSValue *attributeGetter(ExecState *exec, JSObject *, const Identifier &, const PropertySlot &slot); 0241 #endif 0242 }; 0243 0244 DOM::AttrImpl *toAttr(JSValue *); // returns 0 if passed-in value is not a DOMAtt object 0245 DOM::ElementImpl *toElement(JSValue *); // returns 0 if passed-in value is not a DOMElement object 0246 0247 KJS_DEFINE_PROTOTYPE(DOMElementProto) 0248 DEFINE_PSEUDO_CONSTRUCTOR(ElementPseudoCtor) 0249 0250 class DOMDOMImplementation : public DOMObject 0251 { 0252 public: 0253 // Build a DOMDOMImplementation 0254 DOMDOMImplementation(ExecState *, DOM::DOMImplementationImpl *i); 0255 ~DOMDOMImplementation(); 0256 // no put - all functions 0257 const ClassInfo *classInfo() const override 0258 { 0259 return &info; 0260 } 0261 bool toBoolean(ExecState *) const override 0262 { 0263 return true; 0264 } 0265 static const ClassInfo info; 0266 enum { HasFeature, CreateDocumentType, CreateDocument, CreateCSSStyleSheet, CreateHTMLDocument }; 0267 0268 DOM::DOMImplementationImpl *impl() const 0269 { 0270 return m_impl.get(); 0271 } 0272 private: 0273 SharedPtr<DOM::DOMImplementationImpl> m_impl; 0274 }; 0275 0276 class DOMDocumentType : public DOMNode 0277 { 0278 public: 0279 // Build a DOMDocumentType 0280 DOMDocumentType(ExecState *exec, DOM::DocumentTypeImpl *dt); 0281 0282 using KJS::JSObject::getOwnPropertySlot; 0283 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0284 JSValue *getValueProperty(ExecState *exec, int token) const; 0285 // no put - all read-only 0286 const ClassInfo *classInfo() const override 0287 { 0288 return &info; 0289 } 0290 static const ClassInfo info; 0291 enum { Name, Entities, Notations, PublicId, SystemId, InternalSubset }; 0292 }; 0293 0294 class DOMNamedNodeMap : public DOMObject 0295 { 0296 public: 0297 DOMNamedNodeMap(ExecState *, DOM::NamedNodeMapImpl *m); 0298 ~DOMNamedNodeMap(); 0299 0300 using KJS::JSObject::getOwnPropertySlot; 0301 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0302 JSValue *getValueProperty(ExecState *exec, int token) const; 0303 // no put - all read-only 0304 const ClassInfo *classInfo() const override 0305 { 0306 return &info; 0307 } 0308 bool toBoolean(ExecState *) const override 0309 { 0310 return true; 0311 } 0312 static const ClassInfo info; 0313 enum { GetNamedItem, SetNamedItem, RemoveNamedItem, Item, Length, 0314 GetNamedItemNS, SetNamedItemNS, RemoveNamedItemNS 0315 }; 0316 0317 DOM::NamedNodeMapImpl *impl() const 0318 { 0319 return m_impl.get(); 0320 } 0321 0322 JSValue *indexGetter(ExecState *exec, unsigned index); 0323 private: 0324 static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &slot); 0325 SharedPtr<DOM::NamedNodeMapImpl> m_impl; 0326 }; 0327 0328 class DOMProcessingInstruction : public DOMNode 0329 { 0330 public: 0331 DOMProcessingInstruction(ExecState *exec, DOM::ProcessingInstructionImpl *pi) : DOMNode(exec, pi) { } 0332 0333 using KJS::JSObject::getOwnPropertySlot; 0334 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0335 JSValue *getValueProperty(ExecState *exec, int token) const; 0336 0337 using KJS::JSObject::put; 0338 void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None) override; 0339 const ClassInfo *classInfo() const override 0340 { 0341 return &info; 0342 } 0343 static const ClassInfo info; 0344 enum { Target, Data, Sheet }; 0345 }; 0346 0347 class DOMNotation : public DOMNode 0348 { 0349 public: 0350 DOMNotation(ExecState *exec, DOM::NotationImpl *n) : DOMNode(exec, n) { } 0351 0352 using KJS::JSObject::getOwnPropertySlot; 0353 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0354 JSValue *getValueProperty(ExecState *exec, int token) const; 0355 // no put - all read-only 0356 const ClassInfo *classInfo() const override 0357 { 0358 return &info; 0359 } 0360 static const ClassInfo info; 0361 enum { PublicId, SystemId }; 0362 }; 0363 0364 class DOMEntity : public DOMNode 0365 { 0366 public: 0367 DOMEntity(ExecState *exec, DOM::EntityImpl *e) : DOMNode(exec, e) { } 0368 using KJS::JSObject::getOwnPropertySlot; 0369 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0370 JSValue *getValueProperty(ExecState *exec, int token) const; 0371 // no put - all read-only 0372 const ClassInfo *classInfo() const override 0373 { 0374 return &info; 0375 } 0376 static const ClassInfo info; 0377 enum { PublicId, SystemId, NotationName }; 0378 }; 0379 0380 DEFINE_PSEUDO_CONSTRUCTOR(DOMExceptionPseudoCtor) 0381 0382 class JSDOMException : public DOMObject 0383 { 0384 public: 0385 JSDOMException(ExecState *exec); 0386 const ClassInfo *classInfo() const override 0387 { 0388 return &info; 0389 } 0390 static const ClassInfo info; 0391 }; 0392 0393 bool checkNodeSecurity(ExecState *exec, const DOM::NodeImpl *n); 0394 JSValue *getEventTarget(ExecState *exec, DOM::EventTargetImpl *t); 0395 JSValue *getDOMNode(ExecState *exec, DOM::NodeImpl *n); 0396 JSValue *getDOMNamedNodeMap(ExecState *exec, DOM::NamedNodeMapImpl *m); 0397 JSValue *getDOMNodeList(ExecState *exec, DOM::NodeListImpl *l); 0398 JSValue *getDOMDOMImplementation(ExecState *exec, DOM::DOMImplementationImpl *i); 0399 JSObject *getDOMExceptionConstructor(ExecState *exec); 0400 0401 // Internal class, used for the collection return by e.g. document.forms.myinput 0402 // when multiple nodes have the same name. 0403 class DOMNamedNodesCollection : public DOMObject 0404 { 0405 public: 0406 DOMNamedNodesCollection(ExecState *exec, const QList<SharedPtr<DOM::NodeImpl> > &nodes); 0407 using KJS::JSObject::getOwnPropertySlot; 0408 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0409 const ClassInfo *classInfo() const override 0410 { 0411 return &info; 0412 } 0413 static const ClassInfo info; 0414 const QList<SharedPtr<DOM::NodeImpl> > nodes() const 0415 { 0416 return m_nodes; 0417 } 0418 enum { Length }; 0419 0420 JSValue *indexGetter(ExecState *exec, unsigned index); 0421 private: 0422 static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &slot); 0423 QList<SharedPtr<DOM::NodeImpl> > m_nodes; 0424 }; 0425 0426 class DOMCharacterData : public DOMNode 0427 { 0428 public: 0429 // Build a DOMCharacterData 0430 DOMCharacterData(ExecState *exec, DOM::CharacterDataImpl *d); 0431 using KJS::JSObject::getOwnPropertySlot; 0432 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0433 JSValue *getValueProperty(ExecState *, int token) const; 0434 using KJS::JSObject::put; 0435 void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None) override; 0436 const ClassInfo *classInfo() const override 0437 { 0438 return &info; 0439 } 0440 static const ClassInfo info; 0441 DOM::CharacterDataImpl *impl() const 0442 { 0443 return static_cast<DOM::CharacterDataImpl *>(m_impl.get()); 0444 } 0445 enum { Data, Length, 0446 SubstringData, AppendData, InsertData, DeleteData, ReplaceData 0447 }; 0448 }; 0449 0450 class DOMText : public DOMCharacterData 0451 { 0452 public: 0453 DOMText(ExecState *exec, DOM::TextImpl *t); 0454 using KJS::JSObject::getOwnPropertySlot; 0455 bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override; 0456 JSValue *getValueProperty(ExecState *exec, int token) const; 0457 const ClassInfo *classInfo() const override 0458 { 0459 return &info; 0460 } 0461 static const ClassInfo info; 0462 DOM::TextImpl *impl() const 0463 { 0464 return static_cast<DOM::TextImpl *>(m_impl.get()); 0465 } 0466 enum { SplitText, WholeText, ReplaceWholeText }; 0467 }; 0468 0469 class DOMComment : public DOMCharacterData 0470 { 0471 public: 0472 DOMComment(ExecState *exec, DOM::CommentImpl *t); 0473 const ClassInfo *classInfo() const override 0474 { 0475 return &info; 0476 } 0477 static const ClassInfo info; 0478 DOM::CommentImpl *impl() const 0479 { 0480 return static_cast<DOM::CommentImpl *>(m_impl.get()); 0481 } 0482 }; 0483 0484 } // namespace 0485 0486 #endif