khtml Library API Documentation

kjs_traversal.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Library General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Library General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Library General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 #include "kjs_traversal.h"
00022 #include "kjs_traversal.lut.h"
00023 #include "kjs_proxy.h"
00024 #include <dom/dom_node.h>
00025 #include <xml/dom_nodeimpl.h>
00026 #include <xml/dom_docimpl.h>
00027 #include <khtmlview.h>
00028 #include <kdebug.h>
00029 
00030 using namespace KJS;
00031 
00032 // -------------------------------------------------------------------------
00033 
00034 const ClassInfo DOMNodeIterator::info = { "NodeIterator", 0, &DOMNodeIteratorTable, 0 };
00035 /*
00036 @begin DOMNodeIteratorTable 5
00037   root              DOMNodeIterator::Root           DontDelete|ReadOnly
00038   whatToShow            DOMNodeIterator::WhatToShow     DontDelete|ReadOnly
00039   filter            DOMNodeIterator::Filter         DontDelete|ReadOnly
00040   expandEntityReferences    DOMNodeIterator::ExpandEntityReferences DontDelete|ReadOnly
00041 @end
00042 @begin DOMNodeIteratorProtoTable 3
00043   nextNode  DOMNodeIterator::NextNode   DontDelete|Function 0
00044   previousNode  DOMNodeIterator::PreviousNode   DontDelete|Function 0
00045   detach    DOMNodeIterator::Detach     DontDelete|Function 0
00046 @end
00047 */
00048 DEFINE_PROTOTYPE("DOMNodeIterator",DOMNodeIteratorProto)
00049 IMPLEMENT_PROTOFUNC_DOM(DOMNodeIteratorProtoFunc)
00050 IMPLEMENT_PROTOTYPE(DOMNodeIteratorProto,DOMNodeIteratorProtoFunc)
00051 
00052 DOMNodeIterator::DOMNodeIterator(ExecState *exec, DOM::NodeIterator ni)
00053   : DOMObject(DOMNodeIteratorProto::self(exec)), nodeIterator(ni) {}
00054 
00055 DOMNodeIterator::~DOMNodeIterator()
00056 {
00057   ScriptInterpreter::forgetDOMObject(nodeIterator.handle());
00058 }
00059 
00060 Value DOMNodeIterator::tryGet(ExecState *exec, const UString &p) const
00061 {
00062   return DOMObjectLookupGetValue<DOMNodeIterator,DOMObject>(exec,p,&DOMNodeIteratorTable,this);
00063 }
00064 
00065 Value DOMNodeIterator::getValueProperty(ExecState *exec, int token) const
00066 {
00067   DOM::NodeIterator ni(nodeIterator);
00068   switch (token) {
00069   case Root:
00070     return getDOMNode(exec,ni.root());
00071   case WhatToShow:
00072     return Number(ni.whatToShow());
00073   case Filter:
00074     return getDOMNodeFilter(exec,ni.filter());
00075   case ExpandEntityReferences:
00076     return Boolean(ni.expandEntityReferences());
00077  default:
00078    kdWarning() << "Unhandled token in DOMNodeIterator::getValueProperty : " << token << endl;
00079    return Value();
00080   }
00081 }
00082 
00083 Value DOMNodeIteratorProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &)
00084 {
00085   KJS_CHECK_THIS( KJS::DOMNodeIterator, thisObj );
00086   DOM::NodeIterator nodeIterator = static_cast<DOMNodeIterator *>(thisObj.imp())->toNodeIterator();
00087   switch (id) {
00088   case DOMNodeIterator::PreviousNode:
00089     return getDOMNode(exec,nodeIterator.previousNode());
00090   case DOMNodeIterator::NextNode:
00091     return getDOMNode(exec,nodeIterator.nextNode());
00092   case DOMNodeIterator::Detach:
00093     nodeIterator.detach();
00094     return Undefined();
00095   }
00096   return Undefined();
00097 }
00098 
00099 Value KJS::getDOMNodeIterator(ExecState *exec, DOM::NodeIterator ni)
00100 {
00101   return cacheDOMObject<DOM::NodeIterator, DOMNodeIterator>(exec, ni);
00102 }
00103 
00104 
00105 // -------------------------------------------------------------------------
00106 
00107 const ClassInfo NodeFilterConstructor::info = { "NodeFilterConstructor", 0, &NodeFilterConstructorTable, 0 };
00108 /*
00109 @begin NodeFilterConstructorTable 17
00110   FILTER_ACCEPT     DOM::NodeFilter::FILTER_ACCEPT  DontDelete|ReadOnly
00111   FILTER_REJECT     DOM::NodeFilter::FILTER_REJECT  DontDelete|ReadOnly
00112   FILTER_SKIP       DOM::NodeFilter::FILTER_SKIP    DontDelete|ReadOnly
00113   SHOW_ALL      DOM::NodeFilter::SHOW_ALL   DontDelete|ReadOnly
00114   SHOW_ELEMENT      DOM::NodeFilter::SHOW_ELEMENT   DontDelete|ReadOnly
00115   SHOW_ATTRIBUTE    DOM::NodeFilter::SHOW_ATTRIBUTE DontDelete|ReadOnly
00116   SHOW_TEXT     DOM::NodeFilter::SHOW_TEXT  DontDelete|ReadOnly
00117   SHOW_CDATA_SECTION    DOM::NodeFilter::SHOW_CDATA_SECTION DontDelete|ReadOnly
00118   SHOW_ENTITY_REFERENCE DOM::NodeFilter::SHOW_ENTITY_REFERENCE  DontDelete|ReadOnly
00119   SHOW_ENTITY       DOM::NodeFilter::SHOW_ENTITY    DontDelete|ReadOnly
00120   SHOW_PROCESSING_INSTRUCTION   DOM::NodeFilter::SHOW_PROCESSING_INSTRUCTION    DontDelete|ReadOnly
00121   SHOW_COMMENT      DOM::NodeFilter::SHOW_COMMENT   DontDelete|ReadOnly
00122   SHOW_DOCUMENT     DOM::NodeFilter::SHOW_DOCUMENT  DontDelete|ReadOnly
00123   SHOW_DOCUMENT_TYPE    DOM::NodeFilter::SHOW_DOCUMENT_TYPE DontDelete|ReadOnly
00124   SHOW_DOCUMENT_FRAGMENT    DOM::NodeFilter::SHOW_DOCUMENT_FRAGMENT DontDelete|ReadOnly
00125   SHOW_NOTATION     DOM::NodeFilter::SHOW_NOTATION  DontDelete|ReadOnly
00126 @end
00127 */
00128 
00129 NodeFilterConstructor::NodeFilterConstructor(ExecState* exec)
00130   : DOMObject(exec->interpreter()->builtinObjectPrototype())
00131 {
00132 }
00133 
00134 Value NodeFilterConstructor::tryGet(ExecState *exec, const UString &p) const
00135 {
00136   return DOMObjectLookupGetValue<NodeFilterConstructor,DOMObject>(exec,p,&NodeFilterConstructorTable,this);
00137 }
00138 
00139 Value NodeFilterConstructor::getValueProperty(ExecState *, int token) const
00140 {
00141   // We use the token as the value to return directly
00142   return Number(token);
00143 }
00144 
00145 Value KJS::getNodeFilterConstructor(ExecState *exec)
00146 {
00147   return cacheGlobalObject<NodeFilterConstructor>(exec, "[[nodeFilter.constructor]]");
00148 }
00149 
00150 // -------------------------------------------------------------------------
00151 
00152 const ClassInfo DOMNodeFilter::info = { "NodeFilter", 0, 0, 0 };
00153 /*
00154 @begin DOMNodeFilterProtoTable 1
00155   acceptNode    DOMNodeFilter::AcceptNode   DontDelete|Function 0
00156 @end
00157 */
00158 DEFINE_PROTOTYPE("DOMNodeFilter",DOMNodeFilterProto)
00159 IMPLEMENT_PROTOFUNC_DOM(DOMNodeFilterProtoFunc)
00160 IMPLEMENT_PROTOTYPE(DOMNodeFilterProto,DOMNodeFilterProtoFunc)
00161 
00162 DOMNodeFilter::DOMNodeFilter(ExecState *exec, DOM::NodeFilter nf)
00163   : DOMObject(DOMNodeFilterProto::self(exec)), nodeFilter(nf) {}
00164 
00165 DOMNodeFilter::~DOMNodeFilter()
00166 {
00167   ScriptInterpreter::forgetDOMObject(nodeFilter.handle());
00168 }
00169 
00170 Value DOMNodeFilterProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00171 {
00172   KJS_CHECK_THIS( KJS::DOMNodeFilter, thisObj );
00173   DOM::NodeFilter nodeFilter = static_cast<DOMNodeFilter *>(thisObj.imp())->toNodeFilter();
00174   switch (id) {
00175     case DOMNodeFilter::AcceptNode:
00176       return Number(nodeFilter.acceptNode(toNode(args[0])));
00177   }
00178   return Undefined();
00179 }
00180 
00181 Value KJS::getDOMNodeFilter(ExecState *exec, DOM::NodeFilter nf)
00182 {
00183   return cacheDOMObject<DOM::NodeFilter, DOMNodeFilter>(exec, nf);
00184 }
00185 
00186 // -------------------------------------------------------------------------
00187 
00188 const ClassInfo DOMTreeWalker::info = { "TreeWalker", 0, &DOMTreeWalkerTable, 0 };
00189 /*
00190 @begin DOMTreeWalkerTable 5
00191   root          DOMTreeWalker::Root     DontDelete|ReadOnly
00192   whatToShow        DOMTreeWalker::WhatToShow   DontDelete|ReadOnly
00193   filter        DOMTreeWalker::Filter       DontDelete|ReadOnly
00194   expandEntityReferences DOMTreeWalker::ExpandEntityReferences  DontDelete|ReadOnly
00195   currentNode       DOMTreeWalker::CurrentNode  DontDelete
00196 @end
00197 @begin DOMTreeWalkerProtoTable 7
00198   parentNode    DOMTreeWalker::ParentNode   DontDelete|Function 0
00199   firstChild    DOMTreeWalker::FirstChild   DontDelete|Function 0
00200   lastChild DOMTreeWalker::LastChild    DontDelete|Function 0
00201   previousSibling DOMTreeWalker::PreviousSibling    DontDelete|Function 0
00202   nextSibling   DOMTreeWalker::NextSibling  DontDelete|Function 0
00203   previousNode  DOMTreeWalker::PreviousNode DontDelete|Function 0
00204   nextNode  DOMTreeWalker::NextNode     DontDelete|Function 0
00205 @end
00206 */
00207 DEFINE_PROTOTYPE("DOMTreeWalker",DOMTreeWalkerProto)
00208 IMPLEMENT_PROTOFUNC_DOM(DOMTreeWalkerProtoFunc)
00209 IMPLEMENT_PROTOTYPE(DOMTreeWalkerProto,DOMTreeWalkerProtoFunc)
00210 
00211 DOMTreeWalker::DOMTreeWalker(ExecState *exec, DOM::TreeWalker tw)
00212   : DOMObject(DOMTreeWalkerProto::self(exec)), treeWalker(tw) {}
00213 
00214 DOMTreeWalker::~DOMTreeWalker()
00215 {
00216   ScriptInterpreter::forgetDOMObject(treeWalker.handle());
00217 }
00218 
00219 Value DOMTreeWalker::tryGet(ExecState *exec, const UString &p) const
00220 {
00221   return DOMObjectLookupGetValue<DOMTreeWalker,DOMObject>(exec,p,&DOMTreeWalkerTable,this);
00222 }
00223 
00224 Value DOMTreeWalker::getValueProperty(ExecState *exec, int token) const
00225 {
00226   DOM::TreeWalker tw(treeWalker);
00227   switch (token) {
00228   case Root:
00229     return getDOMNode(exec,tw.root());
00230   case WhatToShow:
00231     return Number(tw.whatToShow());
00232   case Filter:
00233     return getDOMNodeFilter(exec,tw.filter());
00234   case ExpandEntityReferences:
00235     return Boolean(tw.expandEntityReferences());
00236   case CurrentNode:
00237     return getDOMNode(exec,tw.currentNode());
00238   default:
00239     kdWarning() << "Unhandled token in DOMTreeWalker::getValueProperty : " << token << endl;
00240     return Value();
00241   }
00242 }
00243 
00244 void DOMTreeWalker::tryPut(ExecState *exec, const UString &propertyName,
00245                            const Value& value, int attr)
00246 {
00247   if (propertyName == "currentNode") {
00248     treeWalker.setCurrentNode(toNode(value));
00249   }
00250   else
00251     ObjectImp::put(exec, propertyName, value, attr);
00252 }
00253 
00254 Value DOMTreeWalkerProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &)
00255 {
00256   KJS_CHECK_THIS( KJS::DOMTreeWalker, thisObj );
00257   DOM::TreeWalker treeWalker = static_cast<DOMTreeWalker *>(thisObj.imp())->toTreeWalker();
00258   switch (id) {
00259     case DOMTreeWalker::ParentNode:
00260       return getDOMNode(exec,treeWalker.parentNode());
00261     case DOMTreeWalker::FirstChild:
00262       return getDOMNode(exec,treeWalker.firstChild());
00263     case DOMTreeWalker::LastChild:
00264       return getDOMNode(exec,treeWalker.lastChild());
00265     case DOMTreeWalker::PreviousSibling:
00266       return getDOMNode(exec,treeWalker.previousSibling());
00267     case DOMTreeWalker::NextSibling:
00268       return getDOMNode(exec,treeWalker.nextSibling());
00269     case DOMTreeWalker::PreviousNode:
00270       return getDOMNode(exec,treeWalker.previousSibling());
00271     case DOMTreeWalker::NextNode:
00272       return getDOMNode(exec,treeWalker.nextNode());
00273   }
00274   return Undefined();
00275 }
00276 
00277 Value KJS::getDOMTreeWalker(ExecState *exec, DOM::TreeWalker tw)
00278 {
00279   return cacheDOMObject<DOM::TreeWalker, DOMTreeWalker>(exec, tw);
00280 }
00281 
00282 DOM::NodeFilter KJS::toNodeFilter(const Value& val)
00283 {
00284   Object obj = Object::dynamicCast(val);
00285   if (obj.isNull() || !obj.inherits(&DOMNodeFilter::info))
00286     return DOM::NodeFilter();
00287 
00288   const DOMNodeFilter *dobj = static_cast<const DOMNodeFilter*>(obj.imp());
00289   return dobj->toNodeFilter();
00290 }
00291 
00292 // -------------------------------------------------------------------------
00293 
00294 JSNodeFilter::JSNodeFilter(Object & _filter) : DOM::CustomNodeFilter(), filter( _filter )
00295 {
00296 }
00297 
00298 JSNodeFilter::~JSNodeFilter()
00299 {
00300 }
00301 
00302 short JSNodeFilter::acceptNode(const DOM::Node &n)
00303 {
00304   KHTMLPart *part = static_cast<DOM::DocumentImpl *>( n.handle()->docPtr()->document() )->view()->part();
00305   KJSProxy *proxy = KJSProxy::proxy( part );
00306   if (proxy) {
00307     ExecState *exec = proxy->interpreter()->globalExec();
00308     Object acceptNodeFunc = Object::dynamicCast( filter.get(exec, "acceptNode") );
00309     if (acceptNodeFunc.implementsCall()) {
00310       List args;
00311       args.append(getDOMNode(exec,n));
00312       Value result = acceptNodeFunc.call(exec,filter,args);
00313       return result.toInteger(exec);
00314     }
00315   }
00316 
00317   return DOM::NodeFilter::FILTER_REJECT;
00318 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:16:38 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001