kjs_dom.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 2000 Harri Porten (porten@kde.org)
00005  *  Copyright (C) 2003 Apple Computer, Inc.
00006  *
00007  *  This library is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU Library General Public
00009  *  License as published by the Free Software Foundation; either
00010  *  version 2 of the License, or (at your option) any later version.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Library General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Library General Public
00018  *  License along with this library; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #include <khtmlview.h>
00023 #include "xml/dom2_eventsimpl.h"
00024 #include "rendering/render_canvas.h"
00025 #include "rendering/render_layer.h"
00026 #include "xml/dom_nodeimpl.h"
00027 #include "xml/dom_docimpl.h"
00028 #include "misc/htmltags.h" // ID_*
00029 #include "misc/htmlattrs.h" // ATTR_*
00030 #include "html/html_baseimpl.h"
00031 #include <kdebug.h>
00032 #include <khtml_part.h>
00033 
00034 #include "kjs_dom.h"
00035 #include "kjs_html.h"
00036 #include "kjs_css.h"
00037 #include "kjs_range.h"
00038 #include "kjs_traversal.h"
00039 #include "kjs_events.h"
00040 #include "kjs_views.h"
00041 #include "kjs_window.h"
00042 #include "dom/dom_exception.h"
00043 #include "kjs_dom.lut.h"
00044 #include "khtmlpart_p.h"
00045 
00046 using namespace KJS;
00047 
00048 // -------------------------------------------------------------------------
00049 /* Source for DOMNodeProtoTable.
00050 @begin DOMNodeProtoTable 13
00051   insertBefore  DOMNode::InsertBefore   DontDelete|Function 2
00052   replaceChild  DOMNode::ReplaceChild   DontDelete|Function 2
00053   removeChild   DOMNode::RemoveChild    DontDelete|Function 1
00054   appendChild   DOMNode::AppendChild    DontDelete|Function 1
00055   hasAttributes DOMNode::HasAttributes  DontDelete|Function 0
00056   hasChildNodes DOMNode::HasChildNodes  DontDelete|Function 0
00057   cloneNode DOMNode::CloneNode  DontDelete|Function 1
00058 # DOM2
00059   normalize DOMNode::Normalize  DontDelete|Function 0
00060   isSupported   DOMNode::IsSupported    DontDelete|Function 2
00061 # from the EventTarget interface
00062   addEventListener  DOMNode::AddEventListener   DontDelete|Function 3
00063   removeEventListener   DOMNode::RemoveEventListener    DontDelete|Function 3
00064   dispatchEvent     DOMNode::DispatchEvent  DontDelete|Function 1
00065 # IE extensions
00066   contains  DOMNode::Contains       DontDelete|Function 1
00067   insertAdjacentHTML    DOMNode::InsertAdjacentHTML DontDelete|Function 2
00068 # "DOM level 0" (from Gecko DOM reference; also in WinIE)
00069   item          DOMNode::Item           DontDelete|Function 1
00070 @end
00071 */
00072 DEFINE_PROTOTYPE("DOMNode",DOMNodeProto)
00073 IMPLEMENT_PROTOFUNC_DOM(DOMNodeProtoFunc)
00074 IMPLEMENT_PROTOTYPE(DOMNodeProto,DOMNodeProtoFunc)
00075 
00076 const ClassInfo DOMNode::info = { "Node", 0, &DOMNodeTable, 0 };
00077 
00078 DOMNode::DOMNode(ExecState *exec, const DOM::Node& n)
00079   : DOMObject(DOMNodeProto::self(exec)), node(n)
00080 {
00081 }
00082 
00083 DOMNode::DOMNode(const Object& proto, const DOM::Node& n)
00084   : DOMObject(proto), node(n)
00085 {
00086 }
00087 
00088 DOMNode::~DOMNode()
00089 {
00090   ScriptInterpreter::forgetDOMObject(node.handle());
00091 }
00092 
00093 bool DOMNode::toBoolean(ExecState *) const
00094 {
00095     return !node.isNull();
00096 }
00097 
00098 /* Source for DOMNodeTable.
00099 @begin DOMNodeTable 53
00100   nodeName  DOMNode::NodeName   DontDelete|ReadOnly
00101   nodeValue DOMNode::NodeValue  DontDelete
00102   nodeType  DOMNode::NodeType   DontDelete|ReadOnly
00103   parentNode    DOMNode::ParentNode DontDelete|ReadOnly
00104   parentElement DOMNode::ParentElement  DontDelete|ReadOnly
00105   childNodes    DOMNode::ChildNodes DontDelete|ReadOnly
00106   firstChild    DOMNode::FirstChild DontDelete|ReadOnly
00107   lastChild DOMNode::LastChild  DontDelete|ReadOnly
00108   previousSibling  DOMNode::PreviousSibling DontDelete|ReadOnly
00109   nextSibling   DOMNode::NextSibling    DontDelete|ReadOnly
00110   attributes    DOMNode::Attributes DontDelete|ReadOnly
00111   namespaceURI  DOMNode::NamespaceURI   DontDelete|ReadOnly
00112 # DOM2
00113   prefix    DOMNode::Prefix     DontDelete
00114   localName DOMNode::LocalName  DontDelete|ReadOnly
00115   ownerDocument DOMNode::OwnerDocument  DontDelete|ReadOnly
00116 # Event handlers
00117 # IE also has: onactivate, onbefore*, oncontextmenu, oncontrolselect, oncut,
00118 # ondeactivate, ondrag*, ondrop, onfocusin, onfocusout, onhelp, onmousewheel,
00119 # onmove*, onpaste, onpropertychange, onreadystatechange, onresizeend/start,
00120 # onselectionchange, onstop
00121   onabort   DOMNode::OnAbort        DontDelete
00122   onblur    DOMNode::OnBlur         DontDelete
00123   onchange  DOMNode::OnChange       DontDelete
00124   onclick   DOMNode::OnClick        DontDelete
00125   ondblclick    DOMNode::OnDblClick     DontDelete
00126   ondragdrop    DOMNode::OnDragDrop     DontDelete
00127   onerror   DOMNode::OnError        DontDelete
00128   onfocus   DOMNode::OnFocus            DontDelete
00129   onkeydown DOMNode::OnKeyDown      DontDelete
00130   onkeypress    DOMNode::OnKeyPress     DontDelete
00131   onkeyup   DOMNode::OnKeyUp        DontDelete
00132   onload    DOMNode::OnLoad         DontDelete
00133   onmousedown   DOMNode::OnMouseDown        DontDelete
00134   onmousemove   DOMNode::OnMouseMove        DontDelete
00135   onmouseout    DOMNode::OnMouseOut     DontDelete
00136   onmouseover   DOMNode::OnMouseOver        DontDelete
00137   onmouseup DOMNode::OnMouseUp      DontDelete
00138   onmove    DOMNode::OnMove         DontDelete
00139   onreset   DOMNode::OnReset        DontDelete
00140   onresize  DOMNode::OnResize       DontDelete
00141   onselect  DOMNode::OnSelect       DontDelete
00142   onsubmit  DOMNode::OnSubmit       DontDelete
00143   onunload  DOMNode::OnUnload       DontDelete
00144 # IE extensions
00145   offsetLeft    DOMNode::OffsetLeft     DontDelete|ReadOnly
00146   offsetTop DOMNode::OffsetTop      DontDelete|ReadOnly
00147   offsetWidth   DOMNode::OffsetWidth        DontDelete|ReadOnly
00148   offsetHeight  DOMNode::OffsetHeight       DontDelete|ReadOnly
00149   offsetParent  DOMNode::OffsetParent       DontDelete|ReadOnly
00150   clientWidth   DOMNode::ClientWidth        DontDelete|ReadOnly
00151   clientHeight  DOMNode::ClientHeight       DontDelete|ReadOnly
00152   scrollLeft    DOMNode::ScrollLeft     DontDelete
00153   scrollTop DOMNode::ScrollTop      DontDelete
00154   scrollWidth   DOMNode::ScrollWidth            DontDelete|ReadOnly
00155   scrollHeight  DOMNode::ScrollHeight           DontDelete|ReadOnly
00156   sourceIndex   DOMNode::SourceIndex        DontDelete|ReadOnly
00157 @end
00158 */
00159 Value DOMNode::tryGet(ExecState *exec, const Identifier &propertyName) const
00160 {
00161 #ifdef KJS_VERBOSE
00162   kdDebug(6070) << "DOMNode::tryGet " << propertyName.qstring() << endl;
00163 #endif
00164   return DOMObjectLookupGetValue<DOMNode, DOMObject>(exec, propertyName, &DOMNodeTable, this);
00165 }
00166 
00167 Value DOMNode::getValueProperty(ExecState *exec, int token) const
00168 {
00169   switch (token) {
00170   case NodeName:
00171     return String(node.nodeName());
00172   case NodeValue:
00173     return getString(node.nodeValue()); // initially null, per domts/level1/core/hc_documentcreateelement.html
00174   case NodeType:
00175     return Number((unsigned int)node.nodeType());
00176   case ParentNode:
00177     return getDOMNode(exec,node.parentNode());
00178   case ParentElement: // IE only apparently
00179     return getDOMNode(exec,node.parentNode());
00180   case ChildNodes:
00181     return getDOMNodeList(exec,node.childNodes());
00182   case FirstChild:
00183     return getDOMNode(exec,node.firstChild());
00184   case LastChild:
00185     return getDOMNode(exec,node.lastChild());
00186   case PreviousSibling:
00187     return getDOMNode(exec,node.previousSibling());
00188   case NextSibling:
00189     return getDOMNode(exec,node.nextSibling());
00190   case Attributes:
00191     return getDOMNamedNodeMap(exec,node.attributes());
00192   case NamespaceURI:
00193     return getString(node.namespaceURI()); // Moz returns null if not set (dom/namespaces.html)
00194   case Prefix:
00195     return getString(node.prefix());  // Moz returns null if not set (dom/namespaces.html)
00196   case LocalName:
00197     return getString(node.localName());  // Moz returns null if not set (dom/namespaces.html)
00198   case OwnerDocument:
00199     return getDOMNode(exec,node.ownerDocument());
00200   case OnAbort:
00201     return getListener(DOM::EventImpl::ABORT_EVENT);
00202   case OnBlur:
00203     return getListener(DOM::EventImpl::BLUR_EVENT);
00204   case OnChange:
00205     return getListener(DOM::EventImpl::CHANGE_EVENT);
00206   case OnClick:
00207     return getListener(DOM::EventImpl::KHTML_ECMA_CLICK_EVENT);
00208   case OnDblClick:
00209     return getListener(DOM::EventImpl::KHTML_ECMA_DBLCLICK_EVENT);
00210   case OnDragDrop:
00211     return getListener(DOM::EventImpl::KHTML_DRAGDROP_EVENT);
00212   case OnError:
00213     return getListener(DOM::EventImpl::KHTML_ERROR_EVENT);
00214   case OnFocus:
00215     return getListener(DOM::EventImpl::FOCUS_EVENT);
00216   case OnKeyDown:
00217     return getListener(DOM::EventImpl::KEYDOWN_EVENT);
00218   case OnKeyPress:
00219     return getListener(DOM::EventImpl::KEYPRESS_EVENT);
00220   case OnKeyUp:
00221     return getListener(DOM::EventImpl::KEYUP_EVENT);
00222   case OnLoad:
00223     return getListener(DOM::EventImpl::LOAD_EVENT);
00224   case OnMouseDown:
00225     return getListener(DOM::EventImpl::MOUSEDOWN_EVENT);
00226   case OnMouseMove:
00227     return getListener(DOM::EventImpl::MOUSEMOVE_EVENT);
00228   case OnMouseOut:
00229     return getListener(DOM::EventImpl::MOUSEOUT_EVENT);
00230   case OnMouseOver:
00231     return getListener(DOM::EventImpl::MOUSEOVER_EVENT);
00232   case OnMouseUp:
00233     return getListener(DOM::EventImpl::MOUSEUP_EVENT);
00234   case OnMove:
00235     return getListener(DOM::EventImpl::KHTML_MOVE_EVENT);
00236   case OnReset:
00237     return getListener(DOM::EventImpl::RESET_EVENT);
00238   case OnResize:
00239     return getListener(DOM::EventImpl::RESIZE_EVENT);
00240   case OnSelect:
00241     return getListener(DOM::EventImpl::SELECT_EVENT);
00242   case OnSubmit:
00243     return getListener(DOM::EventImpl::SUBMIT_EVENT);
00244   case OnUnload:
00245     return getListener(DOM::EventImpl::UNLOAD_EVENT);
00246   case SourceIndex: {
00247     // Retrieves the ordinal position of the object, in source order, as the object
00248     // appears in the document's all collection
00249     // i.e. document.all[n.sourceIndex] == n
00250     DOM::Document doc = node.ownerDocument();
00251     if (doc.isHTMLDocument()) {
00252       DOM::HTMLCollection all = static_cast<DOM::HTMLDocument>(doc).all();
00253       unsigned long i = 0;
00254       DOM::Node n = all.firstItem();
00255       for ( ; !n.isNull() && n != node; n = all.nextItem() )
00256         ++i;
00257       Q_ASSERT( !n.isNull() ); // node not in document.all !?
00258       return Number(i);
00259     }
00260   }
00261   default:
00262     // no DOM standard, found in IE only
00263 
00264     // Make sure our layout is up to date before we allow a query on these attributes.
00265     DOM::DocumentImpl* docimpl = node.handle()->getDocument();
00266     if (docimpl) {
00267       docimpl->updateLayout();
00268     }
00269 
00270     khtml::RenderObject *rend = node.handle()->renderer();
00271 
00272     switch (token) {
00273     case OffsetLeft:
00274       return rend ? static_cast<Value>( Number( rend->offsetLeft() ) ) : Undefined();
00275     case OffsetTop:
00276       return rend ? static_cast<Value>(  Number( rend->offsetTop() ) ) : Undefined();
00277     case OffsetWidth:
00278       return rend ? static_cast<Value>(  Number( rend->offsetWidth() ) ) : Undefined();
00279     case OffsetHeight:
00280       return rend ? static_cast<Value>(  Number( rend->offsetHeight() ) ) : Undefined();
00281     case OffsetParent:
00282     {
00283       khtml::RenderObject* par = rend ? rend->offsetParent() : 0;
00284       return getDOMNode( exec, par ? par->element() : 0 );
00285     }
00286     case ClientWidth:
00287       return rend ? static_cast<Value>( Number( rend->clientWidth() ) ) : Undefined();
00288     case ClientHeight:
00289       return rend ? static_cast<Value>( Number( rend->clientHeight() ) ) : Undefined();
00290     case ScrollWidth:
00291       return rend ? static_cast<Value>( Number(rend->scrollWidth()) ) : Undefined();
00292     case ScrollHeight:
00293       return rend ? static_cast<Value>( Number(rend->scrollHeight()) ) : Undefined();
00294     case ScrollLeft:
00295       if (rend && rend->layer()) {
00296           if (rend->isRoot() && !rend->style()->hidesOverflow())
00297               return Number( node.ownerDocument().view() ? node.ownerDocument().view()->contentsX() : 0);
00298           return Number( rend->layer()->scrollXOffset() );
00299       }
00300       return Number( 0 );
00301     case ScrollTop:
00302       if (rend && rend->layer()) {
00303           if (rend->isRoot() && !rend->style()->hidesOverflow())
00304               return Number( node.ownerDocument().view() ? node.ownerDocument().view()->contentsY() : 0);
00305           return Number( rend->layer()->scrollYOffset() );
00306       }
00307       return Number( 0 );
00308     default:
00309       kdDebug(6070) << "WARNING: Unhandled token in DOMNode::getValueProperty : " << token << endl;
00310       break;
00311     }
00312   }
00313   return Undefined();
00314 }
00315 
00316 
00317 void DOMNode::tryPut(ExecState *exec, const Identifier& propertyName, const Value& value, int attr)
00318 {
00319 #ifdef KJS_VERBOSE
00320   kdDebug(6070) << "DOMNode::tryPut " << propertyName.qstring() << endl;
00321 #endif
00322   DOMObjectLookupPut<DOMNode,DOMObject>(exec, propertyName, value, attr,
00323                                         &DOMNodeTable, this );
00324 }
00325 
00326 void DOMNode::putValueProperty(ExecState *exec, int token, const Value& value, int /*attr*/)
00327 {
00328   switch (token) {
00329   case NodeValue:
00330     node.setNodeValue(value.toString(exec).string());
00331     break;
00332   case Prefix:
00333     node.setPrefix(value.toString(exec).string());
00334     break;
00335   case OnAbort:
00336     setListener(exec,DOM::EventImpl::ABORT_EVENT,value);
00337     break;
00338   case OnBlur:
00339     setListener(exec,DOM::EventImpl::BLUR_EVENT,value);
00340     break;
00341   case OnChange:
00342     setListener(exec,DOM::EventImpl::CHANGE_EVENT,value);
00343     break;
00344   case OnClick:
00345     setListener(exec,DOM::EventImpl::KHTML_ECMA_CLICK_EVENT,value);
00346     break;
00347   case OnDblClick:
00348     setListener(exec,DOM::EventImpl::KHTML_ECMA_DBLCLICK_EVENT,value);
00349     break;
00350   case OnDragDrop:
00351     setListener(exec,DOM::EventImpl::KHTML_DRAGDROP_EVENT,value);
00352     break;
00353   case OnError:
00354     setListener(exec,DOM::EventImpl::KHTML_ERROR_EVENT,value);
00355     break;
00356   case OnFocus:
00357     setListener(exec,DOM::EventImpl::FOCUS_EVENT,value);
00358     break;
00359   case OnKeyDown:
00360     setListener(exec,DOM::EventImpl::KEYDOWN_EVENT,value);
00361     break;
00362   case OnKeyPress:
00363     setListener(exec,DOM::EventImpl::KEYPRESS_EVENT,value);
00364     break;
00365   case OnKeyUp:
00366     setListener(exec,DOM::EventImpl::KEYUP_EVENT,value);
00367     break;
00368   case OnLoad:
00369     setListener(exec,DOM::EventImpl::LOAD_EVENT,value);
00370     break;
00371   case OnMouseDown:
00372     setListener(exec,DOM::EventImpl::MOUSEDOWN_EVENT,value);
00373     break;
00374   case OnMouseMove:
00375     setListener(exec,DOM::EventImpl::MOUSEMOVE_EVENT,value);
00376     break;
00377   case OnMouseOut:
00378     setListener(exec,DOM::EventImpl::MOUSEOUT_EVENT,value);
00379     break;
00380   case OnMouseOver:
00381     setListener(exec,DOM::EventImpl::MOUSEOVER_EVENT,value);
00382     break;
00383   case OnMouseUp:
00384     setListener(exec,DOM::EventImpl::MOUSEUP_EVENT,value);
00385     break;
00386   case OnMove:
00387     setListener(exec,DOM::EventImpl::KHTML_MOVE_EVENT,value);
00388     break;
00389   case OnReset:
00390     setListener(exec,DOM::EventImpl::RESET_EVENT,value);
00391     break;
00392   case OnResize:
00393     setListener(exec,DOM::EventImpl::RESIZE_EVENT,value);
00394     break;
00395   case OnSelect:
00396     setListener(exec,DOM::EventImpl::SELECT_EVENT,value);
00397     break;
00398   case OnSubmit:
00399     setListener(exec,DOM::EventImpl::SUBMIT_EVENT,value);
00400     break;
00401   case OnUnload:
00402     setListener(exec,DOM::EventImpl::UNLOAD_EVENT,value);
00403     break;
00404   default:
00405     // Make sure our layout is up to date 
00406     DOM::DocumentImpl* docimpl = node.handle()->getDocument();
00407     if (docimpl)
00408       docimpl->updateLayout();
00409 
00410     khtml::RenderObject *rend = node.handle() ? node.handle()->renderer() : 0L;
00411 
00412     switch (token) {
00413       case ScrollLeft:
00414         if (rend && rend->layer()) {
00415           if (rend->style()->hidesOverflow())
00416             rend->layer()->scrollToXOffset(value.toInt32(exec));
00417           else if (rend->isRoot()) {
00418             QScrollView* sview = node.ownerDocument().view();
00419             if (sview)
00420               sview->setContentsPos(value.toInt32(exec), sview->contentsY());
00421           }
00422         }
00423         break;
00424       case ScrollTop:
00425         if (rend && rend->layer()) {
00426           if (rend->style()->hidesOverflow())
00427             rend->layer()->scrollToYOffset(value.toInt32(exec));
00428           else if (rend->isRoot()) {
00429             QScrollView* sview = node.ownerDocument().view();
00430             if (sview)
00431               sview->setContentsPos(sview->contentsX(), value.toInt32(exec));
00432           }
00433         }
00434         break;
00435       default:
00436       kdDebug(6070) << "WARNING: DOMNode::putValueProperty unhandled token " << token << endl;
00437     }
00438   }
00439 }
00440 
00441 Value DOMNode::toPrimitive(ExecState *exec, Type /*preferred*/) const
00442 {
00443   if (node.isNull())
00444     return Null();
00445 
00446   return String(toString(exec));
00447 }
00448 
00449 UString DOMNode::toString(ExecState *) const
00450 {
00451   if (node.isNull())
00452     return "null";
00453   UString s;
00454 
00455   DOM::Element e = node;
00456   if ( !e.isNull() ) {
00457     s = e.nodeName().string();
00458   } else
00459     s = className(); // fallback
00460 
00461   return "[object " + s + "]";
00462 }
00463 
00464 void DOMNode::setListener(ExecState *exec, int eventId, const Value& func) const
00465 {
00466   node.handle()->setHTMLEventListener(eventId,Window::retrieveActive(exec)->getJSEventListener(func,true));
00467 }
00468 
00469 Value DOMNode::getListener(int eventId) const
00470 {
00471   DOM::EventListener *listener = node.handle()->getHTMLEventListener(eventId);
00472   JSEventListener *jsListener = static_cast<JSEventListener*>(listener);
00473   if ( jsListener && jsListener->listenerObjImp() )
00474     return jsListener->listenerObj();
00475   else
00476     return Null();
00477 }
00478 
00479 void DOMNode::pushEventHandlerScope(ExecState *, ScopeChain &) const
00480 {
00481 }
00482 
00483 Value DOMNodeProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00484 {
00485   KJS_CHECK_THIS( DOMNode, thisObj );
00486   DOM::Node node = static_cast<DOMNode *>( thisObj.imp() )->toNode();
00487   switch (id) {
00488     case DOMNode::HasAttributes:
00489       return Boolean(node.hasAttributes());
00490     case DOMNode::HasChildNodes:
00491       return Boolean(node.hasChildNodes());
00492     case DOMNode::CloneNode:
00493       return getDOMNode(exec,node.cloneNode(args[0].toBoolean(exec)));
00494     case DOMNode::Normalize:
00495       node.normalize();
00496       return Undefined();
00497     case DOMNode::IsSupported:
00498       return Boolean(node.isSupported(args[0].toString(exec).string(),args[1].toString(exec).string()));
00499     case DOMNode::AddEventListener: {
00500         JSEventListener *listener = Window::retrieveActive(exec)->getJSEventListener(args[1]);
00501         node.addEventListener(args[0].toString(exec).string(),listener,args[2].toBoolean(exec));
00502         return Undefined();
00503     }
00504     case DOMNode::RemoveEventListener: {
00505         JSEventListener *listener = Window::retrieveActive(exec)->getJSEventListener(args[1]);
00506         node.removeEventListener(args[0].toString(exec).string(),listener,args[2].toBoolean(exec));
00507         return Undefined();
00508     }
00509     case DOMNode::DispatchEvent:
00510       return Boolean(node.dispatchEvent(toEvent(args[0])));
00511     case DOMNode::AppendChild:
00512       return getDOMNode(exec,node.appendChild(toNode(args[0])));
00513     case DOMNode::RemoveChild:
00514       return getDOMNode(exec,node.removeChild(toNode(args[0])));
00515     case DOMNode::InsertBefore:
00516       return getDOMNode(exec,node.insertBefore(toNode(args[0]), toNode(args[1])));
00517     case DOMNode::ReplaceChild:
00518       return getDOMNode(exec,node.replaceChild(toNode(args[0]), toNode(args[1])));
00519     case DOMNode::Contains:
00520     {
00521     DOM::Node other = toNode(args[0]);
00522     if (!other.isNull() && node.nodeType()==DOM::Node::ELEMENT_NODE)
00523     {
00524         DOM::NodeBaseImpl *impl = static_cast<DOM::NodeBaseImpl *>(node.handle());
00525         bool retval = other.handle()->isAncestor(impl);
00526         return Boolean(retval);
00527     }
00528         return Undefined();
00529     }
00530     case DOMNode::InsertAdjacentHTML:
00531     {
00532       // see http://www.faqts.com/knowledge_base/view.phtml/aid/5756
00533       // and http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/insertAdjacentHTML.asp
00534       Range range = node.ownerDocument().createRange();
00535 
00536       range.setStartBefore(node);
00537 
00538       DocumentFragment docFrag = range.createContextualFragment(args[1].toString(exec).string());
00539 
00540       DOMString where = args[0].toString(exec).string();
00541 
00542       if (where == "beforeBegin" || where == "BeforeBegin")
00543         node.parentNode().insertBefore(docFrag, node);
00544       else if (where == "afterBegin" || where == "AfterBegin")
00545         node.insertBefore(docFrag, node.firstChild());
00546       else if (where == "beforeEnd" || where == "BeforeEnd")
00547         return getDOMNode(exec, node.appendChild(docFrag));
00548       else if (where == "afterEnd" || where == "AfterEnd")
00549         if (!node.nextSibling().isNull())
00550       node.parentNode().insertBefore(docFrag, node.nextSibling());
00551     else
00552       node.parentNode().appendChild(docFrag);
00553 
00554       return Undefined();
00555     }
00556     case DOMNode::Item:
00557       return getDOMNode(exec, node.childNodes().item(static_cast<unsigned long>(args[0].toNumber(exec))));
00558   }
00559 
00560   return Undefined();
00561 }
00562 
00563 // -------------------------------------------------------------------------
00564 
00565 /*
00566 @begin DOMNodeListProtoTable 2
00567   item      DOMNodeList::Item       DontDelete|Function 1
00568 # IE extension (IE treats DOMNodeList like an HTMLCollection)
00569   namedItem DOMNodeList::NamedItem      DontDelete|Function 1
00570 @end
00571 */
00572 DEFINE_PROTOTYPE("DOMNodeList", DOMNodeListProto)
00573 IMPLEMENT_PROTOFUNC_DOM(DOMNodeListProtoFunc)
00574 IMPLEMENT_PROTOTYPE(DOMNodeListProto,DOMNodeListProtoFunc)
00575 
00576 const ClassInfo DOMNodeList::info = { "NodeList", 0, 0, 0 };
00577 
00578 DOMNodeList::DOMNodeList(ExecState *exec, const DOM::NodeList& l)
00579  : DOMObject(DOMNodeListProto::self(exec)), list(l) { }
00580 
00581 DOMNodeList::~DOMNodeList()
00582 {
00583   ScriptInterpreter::forgetDOMObject(list.handle());
00584 }
00585 
00586 // We have to implement hasProperty since we don't use a hashtable for 'length'
00587 // ## this breaks "for (..in..)" though.
00588 bool DOMNodeList::hasProperty(ExecState *exec, const Identifier &p) const
00589 {
00590   if (p == lengthPropertyName)
00591     return true;
00592   // ## missing: accept p if array index or item id...
00593   return ObjectImp::hasProperty(exec, p);
00594 }
00595 
00596 Value DOMNodeList::tryGet(ExecState *exec, const Identifier &p) const
00597 {
00598 #ifdef KJS_VERBOSE
00599   kdDebug(6070) << "DOMNodeList::tryGet " << p.ascii() << endl;
00600 #endif
00601   if (p == lengthPropertyName)
00602     return Number(list.length());
00603 
00604   // Look in the prototype (for functions) before assuming it's an item's name
00605   Object proto = Object::dynamicCast(prototype());
00606   if (proto.isValid() && proto.hasProperty(exec,p))
00607     return proto.get(exec,p);
00608 
00609   Value result;
00610 
00611   // array index ?
00612   bool ok;
00613   long unsigned int idx = p.toULong(&ok);
00614   if (ok)
00615     result = getDOMNode(exec,list.item(idx));
00616   else {
00617     // Find by ID
00618     DOM::HTMLElement e;
00619     unsigned long l = list.length();
00620     bool found = false;
00621 
00622     for ( unsigned long i = 0; i < l; i++ )
00623       if ( ( e = list.item( i ) ).id() == p.string() ) {
00624         result = getDOMNode(exec, list.item( i ) );
00625         found = true;
00626         break;
00627       }
00628 
00629     if ( !found )
00630       result = ObjectImp::get(exec, p);
00631   }
00632 
00633   return result;
00634 }
00635 
00636 // Need to support both get and call, so that list[0] and list(0) work.
00637 Value DOMNodeList::call(ExecState *exec, Object &thisObj, const List &args)
00638 {
00639   // This code duplication is necessary, DOMNodeList isn't a DOMFunction
00640   Value val;
00641   try {
00642     val = tryCall(exec, thisObj, args);
00643   }
00644   // pity there's no way to distinguish between these in JS code
00645   catch (...) {
00646     Object err = Error::create(exec, GeneralError, "Exception from DOMNodeList");
00647     exec->setException(err);
00648   }
00649   return val;
00650 }
00651 
00652 Value DOMNodeList::tryCall(ExecState *exec, Object &, const List &args)
00653 {
00654   // Do not use thisObj here. See HTMLCollection.
00655   UString s = args[0].toString(exec);
00656 
00657   // index-based lookup?
00658   bool ok;
00659   unsigned int u = s.toULong(&ok);
00660   if (ok)
00661     return getDOMNode(exec,list.item(u));
00662 
00663   // try lookup by name
00664   // ### NodeList::namedItem() would be cool to have
00665   // ### do we need to support the same two arg overload as in HTMLCollection?
00666   Value result = tryGet(exec, Identifier(s));
00667 
00668   if (result.isValid())
00669     return result;
00670 
00671   return Undefined();
00672 }
00673 
00674 // Not a prototype class currently, but should probably be converted to one
00675 Value DOMNodeListProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00676 {
00677   KJS_CHECK_THIS( KJS::DOMNodeList, thisObj );
00678   DOM::NodeList list = static_cast<DOMNodeList *>(thisObj.imp())->nodeList();
00679   switch (id) {
00680   case KJS::DOMNodeList::Item:
00681     return getDOMNode(exec, list.item(args[0].toInt32(exec)));
00682   case KJS::DOMNodeList::NamedItem:
00683   {
00684     // Not a real namedItem implementation like the one HTMLCollection has.
00685     // This is only an IE extension...
00686     DOM::HTMLElement e;
00687     unsigned long len = list.length();
00688     DOM::DOMString s = args[0].toString(exec).string();
00689 
00690     for ( unsigned long i = 0; i < len; i++ )
00691     {
00692       e = list.item( i );
00693       if ( !e.isNull() && (
00694              e.id() == s || static_cast<ElementImpl *>(e.handle())->getAttribute(ATTR_NAME) == s )
00695       )
00696       {
00697         return getDOMNode(exec, e );
00698       }
00699     }
00700     return Null(); // see HTMLCollection::NamedItem implementation
00701   }
00702   default:
00703     return Undefined();
00704   }
00705 }
00706 
00707 // -------------------------------------------------------------------------
00708 
00709 const ClassInfo DOMAttr::info = { "Attr", &DOMNode::info, &DOMAttrTable, 0 };
00710 
00711 /* Source for DOMAttrTable.
00712 @begin DOMAttrTable 5
00713   name      DOMAttr::Name       DontDelete|ReadOnly
00714   specified DOMAttr::Specified  DontDelete|ReadOnly
00715   value     DOMAttr::ValueProperty  DontDelete
00716   ownerElement  DOMAttr::OwnerElement   DontDelete|ReadOnly
00717 @end
00718 */
00719 Value DOMAttr::tryGet(ExecState *exec, const Identifier &propertyName) const
00720 {
00721 #ifdef KJS_VERBOSE
00722   kdDebug(6070) << "DOMAttr::tryGet " << propertyName.qstring() << endl;
00723 #endif
00724   return DOMObjectLookupGetValue<DOMAttr,DOMNode>(exec, propertyName,
00725                                                   &DOMAttrTable, this );
00726 }
00727 
00728 Value DOMAttr::getValueProperty(ExecState *exec, int token) const
00729 {
00730   switch (token) {
00731   case Name:
00732     return String(static_cast<DOM::Attr>(node).name());
00733   case Specified:
00734     return Boolean(static_cast<DOM::Attr>(node).specified());
00735   case ValueProperty:
00736     return String(static_cast<DOM::Attr>(node).value());
00737   case OwnerElement: // DOM2
00738     return getDOMNode(exec,static_cast<DOM::Attr>(node).ownerElement());
00739   }
00740   return Value(); // not reached
00741 }
00742 
00743 void DOMAttr::tryPut(ExecState *exec, const Identifier &propertyName, const Value& value, int attr)
00744 {
00745 #ifdef KJS_VERBOSE
00746   kdDebug(6070) << "DOMAttr::tryPut " << propertyName.qstring() << endl;
00747 #endif
00748   DOMObjectLookupPut<DOMAttr,DOMNode>(exec, propertyName, value, attr,
00749                                       &DOMAttrTable, this );
00750 }
00751 
00752 void DOMAttr::putValueProperty(ExecState *exec, int token, const Value& value, int /*attr*/)
00753 {
00754   switch (token) {
00755   case ValueProperty:
00756     static_cast<DOM::Attr>(node).setValue(value.toString(exec).string());
00757     return;
00758   default:
00759     kdDebug(6070) << "WARNING: DOMAttr::putValueProperty unhandled token " << token << endl;
00760   }
00761 }
00762 
00763 // -------------------------------------------------------------------------
00764 
00765 /* Source for DOMDocumentProtoTable.
00766 @begin DOMDocumentProtoTable 23
00767   createElement   DOMDocument::CreateElement                   DontDelete|Function 1
00768   createDocumentFragment DOMDocument::CreateDocumentFragment   DontDelete|Function 1
00769   createTextNode  DOMDocument::CreateTextNode                  DontDelete|Function 1
00770   createComment   DOMDocument::CreateComment                   DontDelete|Function 1
00771   createCDATASection DOMDocument::CreateCDATASection           DontDelete|Function 1
00772   createProcessingInstruction DOMDocument::CreateProcessingInstruction DontDelete|Function 1
00773   createAttribute DOMDocument::CreateAttribute                 DontDelete|Function 1
00774   createEntityReference DOMDocument::CreateEntityReference     DontDelete|Function 1
00775   getElementsByTagName  DOMDocument::GetElementsByTagName      DontDelete|Function 1
00776   importNode           DOMDocument::ImportNode                 DontDelete|Function 2
00777   createElementNS      DOMDocument::CreateElementNS            DontDelete|Function 2
00778   createAttributeNS    DOMDocument::CreateAttributeNS          DontDelete|Function 2
00779   getElementsByTagNameNS  DOMDocument::GetElementsByTagNameNS  DontDelete|Function 2
00780   getElementById     DOMDocument::GetElementById               DontDelete|Function 1
00781   createRange        DOMDocument::CreateRange                  DontDelete|Function 0
00782   createNodeIterator DOMDocument::CreateNodeIterator           DontDelete|Function 3
00783   createTreeWalker   DOMDocument::CreateTreeWalker             DontDelete|Function 4
00784   createEvent        DOMDocument::CreateEvent                  DontDelete|Function 1
00785   getOverrideStyle   DOMDocument::GetOverrideStyle             DontDelete|Function 2
00786   abort              DOMDocument::Abort                        DontDelete|Function 0
00787   load               DOMDocument::Load                         DontDelete|Function 1
00788   loadXML            DOMDocument::LoadXML                      DontDelete|Function 2
00789 @end
00790 */
00791 DEFINE_PROTOTYPE("DOMDocument", DOMDocumentProto)
00792 IMPLEMENT_PROTOFUNC_DOM(DOMDocumentProtoFunc)
00793 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMDocumentProto, DOMDocumentProtoFunc, DOMNodeProto)
00794 
00795 const ClassInfo DOMDocument::info = { "Document", &DOMNode::info, &DOMDocumentTable, 0 };
00796 
00797 /* Source for DOMDocumentTable.
00798 @begin DOMDocumentTable 4
00799   doctype         DOMDocument::DocType                         DontDelete|ReadOnly
00800   implementation  DOMDocument::Implementation                  DontDelete|ReadOnly
00801   documentElement DOMDocument::DocumentElement                 DontDelete|ReadOnly
00802   styleSheets     DOMDocument::StyleSheets                     DontDelete|ReadOnly
00803   preferredStylesheetSet  DOMDocument::PreferredStylesheetSet  DontDelete|ReadOnly
00804   selectedStylesheetSet  DOMDocument::SelectedStylesheetSet    DontDelete
00805   readyState      DOMDocument::ReadyState                      DontDelete|ReadOnly
00806   defaultView     DOMDocument::DefaultView                     DontDelete|ReadOnly
00807   async           DOMDocument::Async                           DontDelete
00808 @end
00809 */
00810 
00811 DOMDocument::DOMDocument(ExecState *exec, const DOM::Document& d)
00812   : DOMNode(DOMDocumentProto::self(exec), d) { }
00813 
00814 DOMDocument::DOMDocument(const Object& proto, const DOM::Document& d)
00815   : DOMNode(proto, d) { }
00816 
00817 DOMDocument::~DOMDocument()
00818 {
00819   ScriptInterpreter::forgetDOMObject(node.handle());
00820 }
00821 
00822 Value DOMDocument::tryGet(ExecState *exec, const Identifier &propertyName) const
00823 {
00824 #ifdef KJS_VERBOSE
00825   kdDebug(6070) << "DOMDocument::tryGet " << propertyName.qstring() << endl;
00826 #endif
00827   return DOMObjectLookupGetValue<DOMDocument, DOMNode>(
00828     exec, propertyName, &DOMDocumentTable, this);
00829 }
00830 
00831 Value DOMDocument::getValueProperty(ExecState *exec, int token) const
00832 {
00833   DOM::Document doc = static_cast<DOM::Document>(node);
00834 
00835   switch(token) {
00836   case DocType:
00837     return getDOMNode(exec,doc.doctype());
00838   case Implementation:
00839     return getDOMDOMImplementation(exec,doc.implementation());
00840   case DocumentElement:
00841     return getDOMNode(exec,doc.documentElement());
00842   case StyleSheets:
00843     //kdDebug() << "DOMDocument::StyleSheets, returning " << doc.styleSheets().length() << " stylesheets" << endl;
00844     return getDOMStyleSheetList(exec, doc.styleSheets(), doc);
00845   case DOMDocument::DefaultView: // DOM2
00846     {
00847     KHTMLView *view = node.handle()->getDocument()->view();
00848     if (view)
00849         return Window::retrieve(view->part());
00850     return getDOMAbstractView(exec, doc.defaultView());
00851     }
00852   case PreferredStylesheetSet:
00853     return String(doc.preferredStylesheetSet());
00854   case SelectedStylesheetSet:
00855     return String(doc.selectedStylesheetSet());
00856   case ReadyState:
00857     {
00858     DOM::DocumentImpl* docimpl = node.handle()->getDocument();
00859     if ( docimpl && docimpl->view() )
00860     {
00861       KHTMLPart* part = docimpl->view()->part();
00862       if ( part ) {
00863         if (part->d->m_bComplete) return String("complete");
00864         if (docimpl->parsing()) return String("loading");
00865         return String("loaded");
00866         // What does the interactive value mean ?
00867         // Missing support for "uninitialized"
00868       }
00869     }
00870     return Undefined();
00871     }
00872   case Async:
00873     return Boolean(doc.async());
00874   default:
00875     kdDebug(6070) << "WARNING: DOMDocument::getValueProperty unhandled token " << token << endl;
00876     return Value();
00877   }
00878 }
00879 
00880 void DOMDocument::tryPut(ExecState *exec, const Identifier& propertyName, const Value& value, int attr)
00881 {
00882 #ifdef KJS_VERBOSE
00883   kdDebug(6070) << "DOMDocument::tryPut " << propertyName.qstring() << endl;
00884 #endif
00885   DOMObjectLookupPut<DOMDocument,DOMNode>(exec, propertyName, value, attr, &DOMDocumentTable, this );
00886 }
00887 
00888 void DOMDocument::putValueProperty(ExecState *exec, int token, const Value& value, int /*attr*/)
00889 {
00890   DOM::Document doc = static_cast<DOM::Document>(node);
00891   switch (token) {
00892     case SelectedStylesheetSet: {
00893       doc.setSelectedStylesheetSet(value.toString(exec).string());
00894       break;
00895     }
00896     case Async: {
00897       doc.setAsync(value.toBoolean(exec));
00898       break;
00899     }
00900   }
00901 }
00902 
00903 Value DOMDocumentProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00904 {
00905   KJS_CHECK_THIS( KJS::DOMDocument, thisObj );
00906   DOM::Node node = static_cast<DOMNode *>( thisObj.imp() )->toNode();
00907   DOM::Document doc = static_cast<DOM::Document>(node);
00908   String str = args[0].toString(exec);
00909   DOM::DOMString s = str.value().string();
00910 
00911   switch(id) {
00912   case DOMDocument::CreateElement:
00913     return getDOMNode(exec,doc.createElement(s));
00914   case DOMDocument::CreateDocumentFragment:
00915     return getDOMNode(exec,doc.createDocumentFragment());
00916   case DOMDocument::CreateTextNode:
00917     return getDOMNode(exec,doc.createTextNode(s));
00918   case DOMDocument::CreateComment:
00919     return getDOMNode(exec,doc.createComment(s));
00920   case DOMDocument::CreateCDATASection:
00921     return getDOMNode(exec,doc.createCDATASection(s));  /* TODO: okay ? */
00922   case DOMDocument::CreateProcessingInstruction:
00923     return getDOMNode(exec,doc.createProcessingInstruction(args[0].toString(exec).string(),
00924                                                                  args[1].toString(exec).string()));
00925   case DOMDocument::CreateAttribute:
00926     return getDOMNode(exec,doc.createAttribute(s));
00927   case DOMDocument::CreateEntityReference:
00928     return getDOMNode(exec,doc.createEntityReference(args[0].toString(exec).string()));
00929   case DOMDocument::GetElementsByTagName:
00930     return getDOMNodeList(exec,doc.getElementsByTagName(s));
00931   case DOMDocument::ImportNode: // DOM2
00932     return getDOMNode(exec,doc.importNode(toNode(args[0]), args[1].toBoolean(exec)));
00933   case DOMDocument::CreateElementNS: // DOM2
00934     return getDOMNode(exec,doc.createElementNS(args[0].toString(exec).string(), args[1].toString(exec).string()));
00935   case DOMDocument::CreateAttributeNS: // DOM2
00936     return getDOMNode(exec,doc.createAttributeNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
00937   case DOMDocument::GetElementsByTagNameNS: // DOM2
00938     return getDOMNodeList(exec,doc.getElementsByTagNameNS(args[0].toString(exec).string(),
00939                                                           args[1].toString(exec).string()));
00940   case DOMDocument::GetElementById:
00941 #ifdef KJS_VERBOSE
00942   kdDebug(6070) << "DOMDocument::GetElementById looking for " << args[0].toString(exec).string() << endl;
00943 #endif
00944     return getDOMNode(exec,doc.getElementById(args[0].toString(exec).string()));
00945   case DOMDocument::CreateRange:
00946     return getDOMRange(exec,doc.createRange());
00947   case DOMDocument::CreateNodeIterator:
00948     if (args[2].isA(NullType)) {
00949         DOM::NodeFilter filter;
00950         return getDOMNodeIterator(exec,
00951                                   doc.createNodeIterator(toNode(args[0]),
00952                                                          (long unsigned int)(args[1].toNumber(exec)),
00953                                                          filter,args[3].toBoolean(exec)));
00954     }
00955     else {
00956       Object obj = Object::dynamicCast(args[2]);
00957       if (obj.isValid())
00958       {
00959         DOM::CustomNodeFilter *customFilter = new JSNodeFilter(obj);
00960         DOM::NodeFilter filter = DOM::NodeFilter::createCustom(customFilter);
00961         return getDOMNodeIterator(exec,
00962           doc.createNodeIterator(
00963             toNode(args[0]),(long unsigned int)(args[1].toNumber(exec)),
00964             filter,args[3].toBoolean(exec)));
00965       }// else?
00966     }
00967   case DOMDocument::CreateTreeWalker:
00968     return getDOMTreeWalker(exec,doc.createTreeWalker(toNode(args[0]),(long unsigned int)(args[1].toNumber(exec)),
00969              toNodeFilter(args[2]),args[3].toBoolean(exec)));
00970   case DOMDocument::CreateEvent:
00971     return getDOMEvent(exec,doc.createEvent(s));
00972   case DOMDocument::GetOverrideStyle: {
00973     DOM::Node arg0 = toNode(args[0]);
00974     if (arg0.nodeType() != DOM::Node::ELEMENT_NODE)
00975       return Undefined(); // throw exception?
00976     else
00977       return getDOMCSSStyleDeclaration(exec,doc.getOverrideStyle(static_cast<DOM::Element>(arg0),args[1].toString(exec).string()));
00978   }
00979   case DOMDocument::Abort:
00980     doc.abort();
00981     break;
00982   case DOMDocument::Load: {
00983     Window* active = Window::retrieveActive(exec);
00984     // Complete the URL using the "active part" (running interpreter). We do this for the security
00985     // check and to make sure we load exactly the same url as we have verified to be safe
00986     KHTMLPart *khtmlpart = ::qt_cast<KHTMLPart *>(active->part());
00987     if (khtmlpart) {
00988       // Security: only allow documents to be loaded from the same host
00989       QString dstUrl = khtmlpart->htmlDocument().completeURL(s).string();
00990       KParts::ReadOnlyPart *part = static_cast<KJS::ScriptInterpreter*>(exec->interpreter())->part();
00991       if (part->url().host() == KURL(dstUrl).host()) {
00992     kdDebug(6070) << "JavaScript: access granted for document.load() of " << dstUrl << endl;
00993     doc.load(dstUrl);
00994       }
00995       else {
00996     kdDebug(6070) << "JavaScript: access denied for document.load() of " << dstUrl << endl;
00997       }
00998     }
00999     break;
01000   }
01001   case DOMDocument::LoadXML:
01002     doc.loadXML(s);
01003     break;
01004   default:
01005     break;
01006   }
01007 
01008   return Undefined();
01009 }
01010 
01011 // -------------------------------------------------------------------------
01012 
01013 /* Source for DOMElementProtoTable.
01014 @begin DOMElementProtoTable 17
01015   getAttribute      DOMElement::GetAttribute    DontDelete|Function 1
01016   setAttribute      DOMElement::SetAttribute    DontDelete|Function 2
01017   removeAttribute   DOMElement::RemoveAttribute DontDelete|Function 1
01018   getAttributeNode  DOMElement::GetAttributeNode    DontDelete|Function 1
01019   setAttributeNode  DOMElement::SetAttributeNode    DontDelete|Function 2
01020   removeAttributeNode   DOMElement::RemoveAttributeNode DontDelete|Function 1
01021   getElementsByTagName  DOMElement::GetElementsByTagName    DontDelete|Function 1
01022   hasAttribute      DOMElement::HasAttribute    DontDelete|Function 1
01023   getAttributeNS    DOMElement::GetAttributeNS  DontDelete|Function 2
01024   setAttributeNS    DOMElement::SetAttributeNS  DontDelete|Function 3
01025   removeAttributeNS DOMElement::RemoveAttributeNS   DontDelete|Function 2
01026   getAttributeNodeNS    DOMElement::GetAttributeNodeNS  DontDelete|Function 2
01027   setAttributeNodeNS    DOMElement::SetAttributeNodeNS  DontDelete|Function 1
01028   getElementsByTagNameNS DOMElement::GetElementsByTagNameNS DontDelete|Function 2
01029   hasAttributeNS    DOMElement::HasAttributeNS  DontDelete|Function 2
01030 @end
01031 */
01032 DEFINE_PROTOTYPE("DOMElement",DOMElementProto)
01033 IMPLEMENT_PROTOFUNC_DOM(DOMElementProtoFunc)
01034 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMElementProto,DOMElementProtoFunc,DOMNodeProto)
01035 
01036 const ClassInfo DOMElement::info = { "Element", &DOMNode::info, &DOMElementTable, 0 };
01037 /* Source for DOMElementTable.
01038 @begin DOMElementTable 3
01039   tagName   DOMElement::TagName                         DontDelete|ReadOnly
01040   style     DOMElement::Style                           DontDelete|ReadOnly
01041 @end
01042 */
01043 DOMElement::DOMElement(ExecState *exec, const DOM::Element& e)
01044   : DOMNode(DOMElementProto::self(exec), e) { }
01045 
01046 DOMElement::DOMElement(const Object& proto, const DOM::Element& e)
01047   : DOMNode(proto, e) { }
01048 
01049 Value DOMElement::tryGet(ExecState *exec, const Identifier &propertyName) const
01050 {
01051 #ifdef KJS_VERBOSE
01052   kdDebug(6070) << "DOMElement::tryGet " << propertyName.qstring() << endl;
01053 #endif
01054   DOM::Element element = static_cast<DOM::Element>(node);
01055 
01056   const HashEntry* entry = Lookup::findEntry(&DOMElementTable, propertyName);
01057   if (entry)
01058   {
01059     switch( entry->value ) {
01060     case TagName:
01061       return String(element.tagName());
01062     case Style:
01063       return getDOMCSSStyleDeclaration(exec,element.style());
01064     default:
01065       kdDebug(6070) << "WARNING: Unhandled token in DOMElement::tryGet : " << entry->value << endl;
01066       break;
01067     }
01068   }
01069   // We have to check in DOMNode before giving access to attributes, otherwise
01070   // onload="..." would make onload return the string (attribute value) instead of
01071   // the listener object (function).
01072   if (DOMNode::hasProperty(exec, propertyName))
01073     return DOMNode::tryGet(exec, propertyName);
01074 
01075   DOM::DOMString attr = element.getAttribute( propertyName.string() );
01076   // Give access to attributes
01077   if ( !attr.isNull() )
01078     return String( attr );
01079 
01080   return Undefined();
01081 }
01082 
01083 Value DOMElementProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
01084 {
01085   KJS_CHECK_THIS( KJS::DOMNode, thisObj ); // node should be enough here, given the cast
01086   DOM::Node node = static_cast<DOMNode *>( thisObj.imp() )->toNode();
01087   DOM::Element element = static_cast<DOM::Element>(node);
01088 
01089   switch(id) {
01090     case DOMElement::GetAttribute:
01094       return getString(element.getAttribute(args[0].toString(exec).string()));
01095     case DOMElement::SetAttribute:
01096       element.setAttribute(args[0].toString(exec).string(),args[1].toString(exec).string());
01097       return Undefined();
01098     case DOMElement::RemoveAttribute:
01099       element.removeAttribute(args[0].toString(exec).string());
01100       return Undefined();
01101     case DOMElement::GetAttributeNode:
01102       return getDOMNode(exec,element.getAttributeNode(args[0].toString(exec).string()));
01103     case DOMElement::SetAttributeNode:
01104       return getDOMNode(exec,element.setAttributeNode((new DOMNode(exec,KJS::toNode(args[0])))->toNode()));
01105     case DOMElement::RemoveAttributeNode:
01106       return getDOMNode(exec,element.removeAttributeNode((new DOMNode(exec,KJS::toNode(args[0])))->toNode()));
01107     case DOMElement::GetElementsByTagName:
01108       return getDOMNodeList(exec,element.getElementsByTagName(args[0].toString(exec).string()));
01109     case DOMElement::HasAttribute: // DOM2
01110       return Boolean(element.hasAttribute(args[0].toString(exec).string()));
01111     case DOMElement::GetAttributeNS: // DOM2
01112       return String(element.getAttributeNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01113     case DOMElement::SetAttributeNS: // DOM2
01114       element.setAttributeNS(args[0].toString(exec).string(),args[1].toString(exec).string(),args[2].toString(exec).string());
01115       return Undefined();
01116     case DOMElement::RemoveAttributeNS: // DOM2
01117       element.removeAttributeNS(args[0].toString(exec).string(),args[1].toString(exec).string());
01118       return Undefined();
01119     case DOMElement::GetAttributeNodeNS: // DOM2
01120       return getDOMNode(exec,element.getAttributeNodeNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01121     case DOMElement::SetAttributeNodeNS: // DOM2
01122       return getDOMNode(exec,element.setAttributeNodeNS((new DOMNode(exec,KJS::toNode(args[0])))->toNode()));
01123     case DOMElement::GetElementsByTagNameNS: // DOM2
01124       return getDOMNodeList(exec,element.getElementsByTagNameNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01125     case DOMElement::HasAttributeNS: // DOM2
01126       return Boolean(element.hasAttributeNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01127   default:
01128     return Undefined();
01129   }
01130 }
01131 
01132 // -------------------------------------------------------------------------
01133 
01134 /* Source for DOMDOMImplementationProtoTable.
01135 @begin DOMDOMImplementationProtoTable 5
01136   hasFeature        DOMDOMImplementation::HasFeature        DontDelete|Function 2
01137   createCSSStyleSheet   DOMDOMImplementation::CreateCSSStyleSheet   DontDelete|Function 2
01138 # DOM2
01139   createDocumentType    DOMDOMImplementation::CreateDocumentType    DontDelete|Function 3
01140   createDocument    DOMDOMImplementation::CreateDocument        DontDelete|Function 3
01141   createHTMLDocument    DOMDOMImplementation::CreateHTMLDocument        DontDelete|Function 1
01142 @end
01143 */
01144 DEFINE_PROTOTYPE("DOMImplementation",DOMDOMImplementationProto)
01145 IMPLEMENT_PROTOFUNC_DOM(DOMDOMImplementationProtoFunc)
01146 IMPLEMENT_PROTOTYPE(DOMDOMImplementationProto,DOMDOMImplementationProtoFunc)
01147 
01148 const ClassInfo DOMDOMImplementation::info = { "DOMImplementation", 0, 0, 0 };
01149 
01150 DOMDOMImplementation::DOMDOMImplementation(ExecState *exec, const DOM::DOMImplementation& i)
01151   : DOMObject(DOMDOMImplementationProto::self(exec)), implementation(i) { }
01152 
01153 DOMDOMImplementation::~DOMDOMImplementation()
01154 {
01155   ScriptInterpreter::forgetDOMObject(implementation.handle());
01156 }
01157 
01158 Value DOMDOMImplementationProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
01159 {
01160   KJS_CHECK_THIS( KJS::DOMDOMImplementation, thisObj );
01161   DOM::DOMImplementation implementation = static_cast<DOMDOMImplementation *>( thisObj.imp() )->toImplementation();
01162 
01163   switch(id) {
01164   case DOMDOMImplementation::HasFeature:
01165     return Boolean(implementation.hasFeature(args[0].toString(exec).string(),args[1].toString(exec).string()));
01166   case DOMDOMImplementation::CreateDocumentType: // DOM2
01167     return getDOMNode(exec,implementation.createDocumentType(args[0].toString(exec).string(),args[1].toString(exec).string(),args[2].toString(exec).string()));
01168   case DOMDOMImplementation::CreateDocument: { // DOM2
01169     // Initially set the URL to document of the creator... this is so that it resides in the same
01170     // host/domain for security checks. The URL will be updated if Document.load() is called.
01171     KHTMLPart *part = ::qt_cast<KHTMLPart*>(static_cast<KJS::ScriptInterpreter*>(exec->interpreter())->part());
01172     if (part) {
01173       Document doc = implementation.createDocument(args[0].toString(exec).string(),args[1].toString(exec).string(),toNode(args[2]));
01174       KURL url = static_cast<DocumentImpl*>(part->document().handle())->URL();
01175       static_cast<DocumentImpl*>(doc.handle())->setURL(url.url());
01176       return getDOMNode(exec,doc);
01177     }
01178     break;
01179   }
01180   case DOMDOMImplementation::CreateCSSStyleSheet: // DOM2
01181     return getDOMStyleSheet(exec,implementation.createCSSStyleSheet(args[0].toString(exec).string(),args[1].toString(exec).string()));
01182   case DOMDOMImplementation::CreateHTMLDocument: // DOM2-HTML
01183     return getDOMNode(exec, implementation.createHTMLDocument(args[0].toString(exec).string()));
01184   default:
01185     break;
01186   }
01187   return Undefined();
01188 }
01189 
01190 // -------------------------------------------------------------------------
01191 
01192 const ClassInfo DOMDocumentType::info = { "DocumentType", &DOMNode::info, &DOMDocumentTypeTable, 0 };
01193 
01194 /* Source for DOMDocumentTypeTable.
01195 @begin DOMDocumentTypeTable 6
01196   name          DOMDocumentType::Name       DontDelete|ReadOnly
01197   entities      DOMDocumentType::Entities   DontDelete|ReadOnly
01198   notations     DOMDocumentType::Notations  DontDelete|ReadOnly
01199 # DOM2
01200   publicId      DOMDocumentType::PublicId   DontDelete|ReadOnly
01201   systemId      DOMDocumentType::SystemId   DontDelete|ReadOnly
01202   internalSubset    DOMDocumentType::InternalSubset DontDelete|ReadOnly
01203 @end
01204 */
01205 DOMDocumentType::DOMDocumentType(ExecState *exec, const DOM::DocumentType& dt)
01206   : DOMNode( /*### no proto yet*/exec, dt ) { }
01207 
01208 Value DOMDocumentType::tryGet(ExecState *exec, const Identifier &propertyName) const
01209 {
01210 #ifdef KJS_VERBOSE
01211   kdDebug(6070) << "DOMDocumentType::tryGet " << propertyName.qstring() << endl;
01212 #endif
01213   return DOMObjectLookupGetValue<DOMDocumentType, DOMNode>(exec, propertyName, &DOMDocumentTypeTable, this);
01214 }
01215 
01216 Value DOMDocumentType::getValueProperty(ExecState *exec, int token) const
01217 {
01218   DOM::DocumentType type = static_cast<DOM::DocumentType>(node);
01219   switch (token) {
01220   case Name:
01221     return String(type.name());
01222   case Entities:
01223     return getDOMNamedNodeMap(exec,type.entities());
01224   case Notations:
01225     return getDOMNamedNodeMap(exec,type.notations());
01226   case PublicId: // DOM2
01227     return String(type.publicId());
01228   case SystemId: // DOM2
01229     return String(type.systemId());
01230   case InternalSubset: // DOM2
01231     return getString(type.internalSubset()); // can be null, see domts/level2/core/internalSubset01.html
01232   default:
01233     kdDebug(6070) << "WARNING: DOMDocumentType::getValueProperty unhandled token " << token << endl;
01234     return Value();
01235   }
01236 }
01237 
01238 // -------------------------------------------------------------------------
01239 
01240 /* Source for DOMNamedNodeMapProtoTable.
01241 @begin DOMNamedNodeMapProtoTable 7
01242   getNamedItem      DOMNamedNodeMap::GetNamedItem       DontDelete|Function 1
01243   setNamedItem      DOMNamedNodeMap::SetNamedItem       DontDelete|Function 1
01244   removeNamedItem   DOMNamedNodeMap::RemoveNamedItem    DontDelete|Function 1
01245   item          DOMNamedNodeMap::Item           DontDelete|Function 1
01246 # DOM2
01247   getNamedItemNS    DOMNamedNodeMap::GetNamedItemNS     DontDelete|Function 2
01248   setNamedItemNS    DOMNamedNodeMap::SetNamedItemNS     DontDelete|Function 1
01249   removeNamedItemNS DOMNamedNodeMap::RemoveNamedItemNS  DontDelete|Function 2
01250 @end
01251 @begin DOMNamedNodeMapTable 7
01252   length        DOMNamedNodeMap::Length         DontDelete|Function 1
01253 @end
01254 */
01255 DEFINE_PROTOTYPE("NamedNodeMap", DOMNamedNodeMapProto)
01256 IMPLEMENT_PROTOFUNC_DOM(DOMNamedNodeMapProtoFunc)
01257 IMPLEMENT_PROTOTYPE(DOMNamedNodeMapProto,DOMNamedNodeMapProtoFunc)
01258 
01259 const ClassInfo DOMNamedNodeMap::info = { "NamedNodeMap", 0, &DOMNamedNodeMapTable, 0 };
01260 
01261 DOMNamedNodeMap::DOMNamedNodeMap(ExecState *exec, const DOM::NamedNodeMap& m)
01262   : DOMObject(DOMNamedNodeMapProto::self(exec)), map(m) { }
01263 
01264 DOMNamedNodeMap::~DOMNamedNodeMap()
01265 {
01266   ScriptInterpreter::forgetDOMObject(map.handle());
01267 }
01268 
01269 bool DOMNamedNodeMap::hasProperty(ExecState *exec, const Identifier &p) const
01270 {
01271   // ## missing? array index
01272   return DOMObject::hasProperty(exec, p);
01273 }
01274 
01275 Value DOMNamedNodeMap::tryGet(ExecState* exec, const Identifier &p) const
01276 {
01277   if (p == lengthPropertyName)
01278     return Number(map.length());
01279 
01280   // array index ?
01281   bool ok;
01282   long unsigned int idx = p.toULong(&ok);
01283   if (ok)
01284     return getDOMNode(exec,map.item(idx));
01285 
01286   // Anything else (including functions, defined in the prototype)
01287   return DOMObject::tryGet(exec, p);
01288 }
01289 
01290 Value DOMNamedNodeMapProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
01291 {
01292   KJS_CHECK_THIS( KJS::DOMNamedNodeMap, thisObj );
01293   DOM::NamedNodeMap map = static_cast<DOMNamedNodeMap *>(thisObj.imp())->toMap();
01294 
01295   switch(id) {
01296     case DOMNamedNodeMap::GetNamedItem:
01297       return getDOMNode(exec, map.getNamedItem(args[0].toString(exec).string()));
01298     case DOMNamedNodeMap::SetNamedItem:
01299       return getDOMNode(exec, map.setNamedItem((new DOMNode(exec,KJS::toNode(args[0])))->toNode()));
01300     case DOMNamedNodeMap::RemoveNamedItem:
01301       return getDOMNode(exec, map.removeNamedItem(args[0].toString(exec).string()));
01302     case DOMNamedNodeMap::Item:
01303       return getDOMNode(exec, map.item(args[0].toInt32(exec)));
01304     case DOMNamedNodeMap::GetNamedItemNS: // DOM2
01305       return getDOMNode(exec, map.getNamedItemNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01306     case DOMNamedNodeMap::SetNamedItemNS: // DOM2
01307       return getDOMNode(exec, map.setNamedItemNS(toNode(args[0])));
01308     case DOMNamedNodeMap::RemoveNamedItemNS: // DOM2
01309       return getDOMNode(exec, map.removeNamedItemNS(args[0].toString(exec).string(),args[1].toString(exec).string()));
01310     default:
01311       break;
01312   }
01313 
01314   return Undefined();
01315 }
01316 
01317 // -------------------------------------------------------------------------
01318 
01319 const ClassInfo DOMProcessingInstruction::info = { "ProcessingInstruction", &DOMNode::info, &DOMProcessingInstructionTable, 0 };
01320 
01321 /* Source for DOMProcessingInstructionTable.
01322 @begin DOMProcessingInstructionTable 3
01323   target    DOMProcessingInstruction::Target    DontDelete|ReadOnly
01324   data      DOMProcessingInstruction::Data      DontDelete
01325   sheet     DOMProcessingInstruction::Sheet     DontDelete|ReadOnly
01326 @end
01327 */
01328 Value DOMProcessingInstruction::tryGet(ExecState *exec, const Identifier &propertyName) const
01329 {
01330   return DOMObjectLookupGetValue<DOMProcessingInstruction, DOMNode>(exec, propertyName, &DOMProcessingInstructionTable, this);
01331 }
01332 
01333 Value DOMProcessingInstruction::getValueProperty(ExecState *exec, int token) const
01334 {
01335   switch (token) {
01336   case Target:
01337     return String(static_cast<DOM::ProcessingInstruction>(node).target());
01338   case Data:
01339     return String(static_cast<DOM::ProcessingInstruction>(node).data());
01340   case Sheet:
01341     return getDOMStyleSheet(exec,static_cast<DOM::ProcessingInstruction>(node).sheet());
01342   default:
01343     kdDebug(6070) << "WARNING: DOMProcessingInstruction::getValueProperty unhandled token " << token << endl;
01344     return Value();
01345   }
01346 }
01347 
01348 void DOMProcessingInstruction::tryPut(ExecState *exec, const Identifier &propertyName, const Value& value, int attr)
01349 {
01350   // Not worth using the hashtable for this one ;)
01351   if (propertyName == "data")
01352     static_cast<DOM::ProcessingInstruction>(node).setData(value.toString(exec).string());
01353   else
01354     DOMNode::tryPut(exec, propertyName,value,attr);
01355 }
01356 
01357 // -------------------------------------------------------------------------
01358 
01359 const ClassInfo DOMNotation::info = { "Notation", &DOMNode::info, &DOMNotationTable, 0 };
01360 
01361 /* Source for DOMNotationTable.
01362 @begin DOMNotationTable 2
01363   publicId      DOMNotation::PublicId   DontDelete|ReadOnly
01364   systemId      DOMNotation::SystemId   DontDelete|ReadOnly
01365 @end
01366 */
01367 Value DOMNotation::tryGet(ExecState *exec, const Identifier &propertyName) const
01368 {
01369   return DOMObjectLookupGetValue<DOMNotation, DOMNode>(exec, propertyName, &DOMNotationTable, this);
01370 }
01371 
01372 Value DOMNotation::getValueProperty(ExecState *, int token) const
01373 {
01374   switch (token) {
01375   case PublicId:
01376     return String(static_cast<DOM::Notation>(node).publicId());
01377   case SystemId:
01378     return String(static_cast<DOM::Notation>(node).systemId());
01379   default:
01380     kdDebug(6070) << "WARNING: DOMNotation::getValueProperty unhandled token " << token << endl;
01381     return Value();
01382   }
01383 }
01384 
01385 // -------------------------------------------------------------------------
01386 
01387 const ClassInfo DOMEntity::info = { "Entity", &DOMNode::info, 0, 0 };
01388 
01389 /* Source for DOMEntityTable.
01390 @begin DOMEntityTable 2
01391   publicId      DOMEntity::PublicId     DontDelete|ReadOnly
01392   systemId      DOMEntity::SystemId     DontDelete|ReadOnly
01393   notationName      DOMEntity::NotationName DontDelete|ReadOnly
01394 @end
01395 */
01396 Value DOMEntity::tryGet(ExecState *exec, const Identifier &propertyName) const
01397 {
01398   return DOMObjectLookupGetValue<DOMEntity, DOMNode>(exec, propertyName, &DOMEntityTable, this);
01399 }
01400 
01401 Value DOMEntity::getValueProperty(ExecState *, int token) const
01402 {
01403   switch (token) {
01404   case PublicId:
01405     return String(static_cast<DOM::Entity>(node).publicId());
01406   case SystemId:
01407     return String(static_cast<DOM::Entity>(node).systemId());
01408   case NotationName:
01409     return String(static_cast<DOM::Entity>(node).notationName());
01410   default:
01411     kdDebug(6070) << "WARNING: DOMEntity::getValueProperty unhandled token " << token << endl;
01412     return Value();
01413   }
01414 }
01415 
01416 // -------------------------------------------------------------------------
01417 
01418 bool KJS::checkNodeSecurity(ExecState *exec, const DOM::Node& n)
01419 {
01420   // Check to see if the currently executing interpreter is allowed to access the specified node
01421   if (n.isNull())
01422     return true;
01423   KHTMLView *view = n.handle()->getDocument()->view();
01424   Window* win = view && view->part() ? Window::retrieveWindow(view->part()) : 0L;
01425   if ( !win || !win->isSafeScript(exec) )
01426     return false;
01427   return true;
01428 }
01429 
01430 Value KJS::getDOMNode(ExecState *exec, const DOM::Node& n)
01431 {
01432   DOMObject *ret = 0;
01433   if (n.isNull())
01434     return Null();
01435   ScriptInterpreter* interp = static_cast<ScriptInterpreter *>(exec->interpreter());
01436   if ((ret = interp->getDOMObject(n.handle())))
01437     return Value(ret);
01438 
01439   switch (n.nodeType()) {
01440     case DOM::Node::ELEMENT_NODE:
01441       if (static_cast<DOM::Element>(n).isHTMLElement())
01442         ret = new HTMLElement(exec, static_cast<DOM::HTMLElement>(n));
01443       else
01444         ret = new DOMElement(exec, static_cast<DOM::Element>(n));
01445       break;
01446     case DOM::Node::ATTRIBUTE_NODE:
01447       ret = new DOMAttr(exec, static_cast<DOM::Attr>(n));
01448       break;
01449     case DOM::Node::TEXT_NODE:
01450     case DOM::Node::CDATA_SECTION_NODE:
01451       ret = new DOMText(exec, static_cast<DOM::Text>(n));
01452       break;
01453     case DOM::Node::ENTITY_REFERENCE_NODE:
01454       ret = new DOMNode(exec, n);
01455       break;
01456     case DOM::Node::ENTITY_NODE:
01457       ret = new DOMEntity(exec, static_cast<DOM::Entity>(n));
01458       break;
01459     case DOM::Node::PROCESSING_INSTRUCTION_NODE:
01460       ret = new DOMProcessingInstruction(exec, static_cast<DOM::ProcessingInstruction>(n));
01461       break;
01462     case DOM::Node::COMMENT_NODE:
01463       ret = new DOMCharacterData(exec, static_cast<DOM::CharacterData>(n));
01464       break;
01465     case DOM::Node::DOCUMENT_NODE:
01466       if (static_cast<DOM::Document>(n).isHTMLDocument())
01467         ret = new HTMLDocument(exec, static_cast<DOM::HTMLDocument>(n));
01468       else
01469         ret = new DOMDocument(exec, static_cast<DOM::Document>(n));
01470       break;
01471     case DOM::Node::DOCUMENT_TYPE_NODE:
01472       ret = new DOMDocumentType(exec, static_cast<DOM::DocumentType>(n));
01473       break;
01474     case DOM::Node::DOCUMENT_FRAGMENT_NODE:
01475       ret = new DOMNode(exec, n);
01476       break;
01477     case DOM::Node::NOTATION_NODE:
01478       ret = new DOMNotation(exec, static_cast<DOM::Notation>(n));
01479       break;
01480     default:
01481       ret = new DOMNode(exec, n);
01482   }
01483   interp->putDOMObject(n.handle(),ret);
01484 
01485   return Value(ret);
01486 }
01487 
01488 Value KJS::getDOMNamedNodeMap(ExecState *exec, const DOM::NamedNodeMap& m)
01489 {
01490   return Value(cacheDOMObject<DOM::NamedNodeMap, KJS::DOMNamedNodeMap>(exec, m));
01491 }
01492 
01493 Value KJS::getDOMNodeList(ExecState *exec, const DOM::NodeList& l)
01494 {
01495   return Value(cacheDOMObject<DOM::NodeList, KJS::DOMNodeList>(exec, l));
01496 }
01497 
01498 Value KJS::getDOMDOMImplementation(ExecState *exec, const DOM::DOMImplementation& i)
01499 {
01500   return Value(cacheDOMObject<DOM::DOMImplementation, KJS::DOMDOMImplementation>(exec, i));
01501 }
01502 
01503 // -------------------------------------------------------------------------
01504 
01505 const ClassInfo NodeConstructor::info = { "NodeConstructor", 0, &NodeConstructorTable, 0 };
01506 /* Source for NodeConstructorTable.
01507 @begin NodeConstructorTable 11
01508   ELEMENT_NODE      DOM::Node::ELEMENT_NODE     DontDelete|ReadOnly
01509   ATTRIBUTE_NODE    DOM::Node::ATTRIBUTE_NODE       DontDelete|ReadOnly
01510   TEXT_NODE     DOM::Node::TEXT_NODE        DontDelete|ReadOnly
01511   CDATA_SECTION_NODE    DOM::Node::CDATA_SECTION_NODE   DontDelete|ReadOnly
01512   ENTITY_REFERENCE_NODE DOM::Node::ENTITY_REFERENCE_NODE    DontDelete|ReadOnly
01513   ENTITY_NODE       DOM::Node::ENTITY_NODE      DontDelete|ReadOnly
01514   PROCESSING_INSTRUCTION_NODE DOM::Node::PROCESSING_INSTRUCTION_NODE DontDelete|ReadOnly
01515   COMMENT_NODE      DOM::Node::COMMENT_NODE     DontDelete|ReadOnly
01516   DOCUMENT_NODE     DOM::Node::DOCUMENT_NODE        DontDelete|ReadOnly
01517   DOCUMENT_TYPE_NODE    DOM::Node::DOCUMENT_TYPE_NODE   DontDelete|ReadOnly
01518   DOCUMENT_FRAGMENT_NODE DOM::Node::DOCUMENT_FRAGMENT_NODE  DontDelete|ReadOnly
01519   NOTATION_NODE     DOM::Node::NOTATION_NODE        DontDelete|ReadOnly
01520 @end
01521 */
01522 
01523 NodeConstructor::NodeConstructor(ExecState *exec)
01524   : DOMObject(exec->interpreter()->builtinObjectPrototype())
01525 {
01526 }
01527 
01528 Value NodeConstructor::tryGet(ExecState *exec, const Identifier &propertyName) const
01529 {
01530   return DOMObjectLookupGetValue<NodeConstructor, DOMObject>(exec, propertyName, &NodeConstructorTable, this);
01531 }
01532 
01533 Value NodeConstructor::getValueProperty(ExecState *, int token) const
01534 {
01535   // We use the token as the value to return directly
01536   return Number((unsigned int)token);
01537 #if 0
01538   switch (token) {
01539   case ELEMENT_NODE:
01540     return Number((unsigned int)DOM::Node::ELEMENT_NODE);
01541   case ATTRIBUTE_NODE:
01542     return Number((unsigned int)DOM::Node::ATTRIBUTE_NODE);
01543   case TEXT_NODE:
01544     return Number((unsigned int)DOM::Node::TEXT_NODE);
01545   case CDATA_SECTION_NODE:
01546     return Number((unsigned int)DOM::Node::CDATA_SECTION_NODE);
01547   case ENTITY_REFERENCE_NODE:
01548     return Number((unsigned int)DOM::Node::ENTITY_REFERENCE_NODE);
01549   case ENTITY_NODE:
01550     return Number((unsigned int)DOM::Node::ENTITY_NODE);
01551   case PROCESSING_INSTRUCTION_NODE:
01552     return Number((unsigned int)DOM::Node::PROCESSING_INSTRUCTION_NODE);
01553   case COMMENT_NODE:
01554     return Number((unsigned int)DOM::Node::COMMENT_NODE);
01555   case DOCUMENT_NODE:
01556     return Number((unsigned int)DOM::Node::DOCUMENT_NODE);
01557   case DOCUMENT_TYPE_NODE:
01558     return Number((unsigned int)DOM::Node::DOCUMENT_TYPE_NODE);
01559   case DOCUMENT_FRAGMENT_NODE:
01560     return Number((unsigned int)DOM::Node::DOCUMENT_FRAGMENT_NODE);
01561   case NOTATION_NODE:
01562     return Number((unsigned int)DOM::Node::NOTATION_NODE);
01563   default:
01564     kdDebug(6070) << "WARNING: NodeConstructor::getValueProperty unhandled token " << token << endl;
01565     return Value();
01566   }
01567 #endif
01568 }
01569 
01570 Object KJS::getNodeConstructor(ExecState *exec)
01571 {
01572   return Object(cacheGlobalObject<NodeConstructor>(exec, "[[node.constructor]]"));
01573 }
01574 
01575 // -------------------------------------------------------------------------
01576 
01577 const ClassInfo DOMExceptionConstructor::info = { "DOMExceptionConstructor", 0, 0, 0 };
01578 
01579 /* Source for DOMExceptionConstructorTable.
01580 @begin DOMExceptionConstructorTable 15
01581   INDEX_SIZE_ERR        DOM::DOMException::INDEX_SIZE_ERR       DontDelete|ReadOnly
01582   DOMSTRING_SIZE_ERR        DOM::DOMException::DOMSTRING_SIZE_ERR   DontDelete|ReadOnly
01583   HIERARCHY_REQUEST_ERR     DOM::DOMException::HIERARCHY_REQUEST_ERR    DontDelete|ReadOnly
01584   WRONG_DOCUMENT_ERR        DOM::DOMException::WRONG_DOCUMENT_ERR   DontDelete|ReadOnly
01585   INVALID_CHARACTER_ERR     DOM::DOMException::INVALID_CHARACTER_ERR    DontDelete|ReadOnly
01586   NO_DATA_ALLOWED_ERR       DOM::DOMException::NO_DATA_ALLOWED_ERR  DontDelete|ReadOnly
01587   NO_MODIFICATION_ALLOWED_ERR   DOM::DOMException::NO_MODIFICATION_ALLOWED_ERR  DontDelete|ReadOnly
01588   NOT_FOUND_ERR         DOM::DOMException::NOT_FOUND_ERR        DontDelete|ReadOnly
01589   NOT_SUPPORTED_ERR     DOM::DOMException::NOT_SUPPORTED_ERR    DontDelete|ReadOnly
01590   INUSE_ATTRIBUTE_ERR       DOM::DOMException::INUSE_ATTRIBUTE_ERR  DontDelete|ReadOnly
01591   INVALID_STATE_ERR     DOM::DOMException::INVALID_STATE_ERR    DontDelete|ReadOnly
01592   SYNTAX_ERR            DOM::DOMException::SYNTAX_ERR       DontDelete|ReadOnly
01593   INVALID_MODIFICATION_ERR  DOM::DOMException::INVALID_MODIFICATION_ERR DontDelete|ReadOnly
01594   NAMESPACE_ERR         DOM::DOMException::NAMESPACE_ERR        DontDelete|ReadOnly
01595   INVALID_ACCESS_ERR        DOM::DOMException::INVALID_ACCESS_ERR   DontDelete|ReadOnly
01596 @end
01597 */
01598 
01599 DOMExceptionConstructor::DOMExceptionConstructor(ExecState* exec)
01600   : DOMObject(exec->interpreter()->builtinObjectPrototype())
01601 {
01602 }
01603 
01604 Value DOMExceptionConstructor::tryGet(ExecState *exec, const Identifier &propertyName) const
01605 {
01606   return DOMObjectLookupGetValue<DOMExceptionConstructor, DOMObject>(exec, propertyName, &DOMExceptionConstructorTable, this);
01607 }
01608 
01609 Value DOMExceptionConstructor::getValueProperty(ExecState *, int token) const
01610 {
01611   // We use the token as the value to return directly
01612   return Number((unsigned int)token);
01613 #if 0
01614   switch (token) {
01615   case INDEX_SIZE_ERR:
01616     return Number((unsigned int)DOM::DOMException::INDEX_SIZE_ERR);
01617   case DOMSTRING_SIZE_ERR:
01618     return Number((unsigned int)DOM::DOMException::DOMSTRING_SIZE_ERR);
01619   case HIERARCHY_REQUEST_ERR:
01620     return Number((unsigned int)DOM::DOMException::HIERARCHY_REQUEST_ERR);
01621   case WRONG_DOCUMENT_ERR:
01622     return Number((unsigned int)DOM::DOMException::WRONG_DOCUMENT_ERR);
01623   case INVALID_CHARACTER_ERR:
01624     return Number((unsigned int)DOM::DOMException::INVALID_CHARACTER_ERR);
01625   case NO_DATA_ALLOWED_ERR:
01626     return Number((unsigned int)DOM::DOMException::NO_DATA_ALLOWED_ERR);
01627   case NO_MODIFICATION_ALLOWED_ERR:
01628     return Number((unsigned int)DOM::DOMException::NO_MODIFICATION_ALLOWED_ERR);
01629   case NOT_FOUND_ERR:
01630     return Number((unsigned int)DOM::DOMException::NOT_FOUND_ERR);
01631   case NOT_SUPPORTED_ERR:
01632     return Number((unsigned int)DOM::DOMException::NOT_SUPPORTED_ERR);
01633   case INUSE_ATTRIBUTE_ERR:
01634     return Number((unsigned int)DOM::DOMException::INUSE_ATTRIBUTE_ERR);
01635   case INVALID_STATE_ERR:
01636     return Number((unsigned int)DOM::DOMException::INVALID_STATE_ERR);
01637   case SYNTAX_ERR:
01638     return Number((unsigned int)DOM::DOMException::SYNTAX_ERR);
01639   case INVALID_MODIFICATION_ERR:
01640     return Number((unsigned int)DOM::DOMException::INVALID_MODIFICATION_ERR);
01641   case NAMESPACE_ERR:
01642     return Number((unsigned int)DOM::DOMException::NAMESPACE_ERR);
01643   case INVALID_ACCESS_ERR:
01644     return Number((unsigned int)DOM::DOMException::INVALID_ACCESS_ERR);
01645   default:
01646     kdDebug(6070) << "WARNING: DOMExceptionConstructor::getValueProperty unhandled token " << token << endl;
01647     return Value();
01648   }
01649 #endif
01650 }
01651 
01652 Object KJS::getDOMExceptionConstructor(ExecState *exec)
01653 {
01654   return cacheGlobalObject<DOMExceptionConstructor>(exec, "[[DOMException.constructor]]");
01655 }
01656 
01657 // -------------------------------------------------------------------------
01658 
01659 /* Source for DOMNamedNodesCollection.
01660 @begin DOMNamedNodesCollectionTable 1
01661   length        KJS::DOMNamedNodesCollection::Length        DontDelete|ReadOnly
01662 @end
01663 */
01664 const ClassInfo KJS::DOMNamedNodesCollection::info = { "DOMNamedNodesCollection", 0, &DOMNamedNodesCollectionTable, 0 };
01665 
01666 // Such a collection is usually very short-lived, it only exists
01667 // for constructs like document.forms.<name>[1],
01668 // so it shouldn't be a problem that it's storing all the nodes (with the same name). (David)
01669 DOMNamedNodesCollection::DOMNamedNodesCollection(ExecState *exec, const QValueList<DOM::Node>& nodes )
01670   : DOMObject(exec->interpreter()->builtinObjectPrototype()),
01671   m_nodes(nodes)
01672 {
01673   // Maybe we should ref (and deref in the dtor) the nodes, though ?
01674 }
01675 
01676 Value DOMNamedNodesCollection::tryGet(ExecState *exec, const Identifier &propertyName) const
01677 {
01678   kdDebug(6070) << k_funcinfo << propertyName.ascii() << endl;
01679   if (propertyName == lengthPropertyName)
01680     return Number(m_nodes.count());
01681   // index?
01682   bool ok;
01683   unsigned int u = propertyName.toULong(&ok);
01684   if (ok && u < m_nodes.count()) {
01685     DOM::Node node = m_nodes[u];
01686     return getDOMNode(exec,node);
01687   }
01688   return DOMObject::tryGet(exec,propertyName);
01689 }
01690 
01691 // -------------------------------------------------------------------------
01692 
01693 const ClassInfo DOMCharacterData::info = { "CharacterImp",
01694                       &DOMNode::info, &DOMCharacterDataTable, 0 };
01695 /*
01696 @begin DOMCharacterDataTable 2
01697   data      DOMCharacterData::Data      DontDelete
01698   length    DOMCharacterData::Length    DontDelete|ReadOnly
01699 @end
01700 @begin DOMCharacterDataProtoTable 7
01701   substringData DOMCharacterData::SubstringData DontDelete|Function 2
01702   appendData    DOMCharacterData::AppendData    DontDelete|Function 1
01703   insertData    DOMCharacterData::InsertData    DontDelete|Function 2
01704   deleteData    DOMCharacterData::DeleteData    DontDelete|Function 2
01705   replaceData   DOMCharacterData::ReplaceData   DontDelete|Function 2
01706 @end
01707 */
01708 DEFINE_PROTOTYPE("DOMCharacterData",DOMCharacterDataProto)
01709 IMPLEMENT_PROTOFUNC_DOM(DOMCharacterDataProtoFunc)
01710 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMCharacterDataProto,DOMCharacterDataProtoFunc, DOMNodeProto)
01711 
01712 DOMCharacterData::DOMCharacterData(ExecState *exec, const DOM::CharacterData& d)
01713  : DOMNode(DOMCharacterDataProto::self(exec), d) {}
01714 
01715 DOMCharacterData::DOMCharacterData(const Object& proto, const DOM::CharacterData& d)
01716  : DOMNode(proto, d) {}
01717 
01718 Value DOMCharacterData::tryGet(ExecState *exec, const Identifier &p) const
01719 {
01720 #ifdef KJS_VERBOSE
01721   kdDebug(6070)<<"DOMCharacterData::tryGet "<<p.string().string()<<endl;
01722 #endif
01723   return DOMObjectLookupGetValue<DOMCharacterData,DOMNode>(exec,p,&DOMCharacterDataTable,this);
01724 }
01725 
01726 Value DOMCharacterData::getValueProperty(ExecState *, int token) const
01727 {
01728   DOM::CharacterData data = static_cast<DOM::CharacterData>(node);
01729   switch (token) {
01730   case Data:
01731     return String(data.data());
01732   case Length:
01733     return Number(data.length());
01734  default:
01735    kdDebug(6070) << "WARNING: Unhandled token in DOMCharacterData::getValueProperty : " << token << endl;
01736    return Value();
01737   }
01738 }
01739 
01740 void DOMCharacterData::tryPut(ExecState *exec, const Identifier &propertyName, const Value& value, int attr)
01741 {
01742   if (propertyName == "data")
01743     static_cast<DOM::CharacterData>(node).setData(value.toString(exec).string());
01744   else
01745     DOMNode::tryPut(exec, propertyName,value,attr);
01746 }
01747 
01748 Value DOMCharacterDataProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
01749 {
01750   KJS_CHECK_THIS( KJS::DOMCharacterData, thisObj );
01751   DOM::CharacterData data = static_cast<DOMCharacterData *>(thisObj.imp())->toData();
01752   switch(id) {
01753     case DOMCharacterData::SubstringData:
01754       return String(data.substringData(args[0].toInteger(exec),args[1].toInteger(exec)));
01755     case DOMCharacterData::AppendData:
01756       data.appendData(args[0].toString(exec).string());
01757       return Undefined();
01758       break;
01759     case DOMCharacterData::InsertData:
01760       data.insertData(args[0].toInteger(exec),args[1].toString(exec).string());
01761       return  Undefined();
01762       break;
01763     case DOMCharacterData::DeleteData:
01764       data.deleteData(args[0].toInteger(exec),args[1].toInteger(exec));
01765       return  Undefined();
01766       break;
01767     case DOMCharacterData::ReplaceData:
01768       data.replaceData(args[0].toInteger(exec),args[1].toInteger(exec),args[2].toString(exec).string());
01769       return Undefined();
01770     default:
01771       break;
01772   }
01773   return Undefined();
01774 }
01775 
01776 // -------------------------------------------------------------------------
01777 
01778 const ClassInfo DOMText::info = { "Text",
01779                  &DOMCharacterData::info, 0, 0 };
01780 /*
01781 @begin DOMTextProtoTable 1
01782   splitText DOMText::SplitText  DontDelete|Function 1
01783 @end
01784 */
01785 DEFINE_PROTOTYPE("DOMText",DOMTextProto)
01786 IMPLEMENT_PROTOFUNC_DOM(DOMTextProtoFunc)
01787 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMTextProto,DOMTextProtoFunc,DOMCharacterDataProto)
01788 
01789 DOMText::DOMText(ExecState *exec, const DOM::Text& t)
01790   : DOMCharacterData(DOMTextProto::self(exec), t) { }
01791 
01792 Value DOMText::tryGet(ExecState *exec, const Identifier &p) const
01793 {
01794   if (p.isEmpty())
01795     return Undefined(); // ### TODO
01796   else
01797     return DOMCharacterData::tryGet(exec, p);
01798 }
01799 
01800 Value DOMTextProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
01801 {
01802   KJS_CHECK_THIS( KJS::DOMText, thisObj );
01803   DOM::Text text = static_cast<DOMText *>(thisObj.imp())->toText();
01804   switch(id) {
01805     case DOMText::SplitText:
01806       return getDOMNode(exec,text.splitText(args[0].toInteger(exec)));
01807     default:
01808       break;
01809   }
01810   return Undefined();
01811 }
KDE Home | KDE Accessibility Home | Description of Access Keys