📄 mgjs_collection.cpp
字号:
#include "mgjs_main.h"#include <../misc/htmlattrs.h> #include <html_formimpl.h>#include <htmltags.h>#define DEBUG_BY_XHTANG 0static JSBool GetHTMLCollectionProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp);static JSBool SetHTMLCollectionProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp);static JSBool myjsHTMLCollection_Item(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);static JSBool myjsHTMLCollection_NamedItem(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);enum HTMLCollection_slots {MDHTMLCOLLECTION_LENGTH};static JSBool GetHTMLCollectionProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){ myJSCollectionPrivate *mCollectionPrivate=(myJSCollectionPrivate *)JS_GetPrivate(cx,obj); long int len; HTMLElementImpl *nImpl; nImpl=(HTMLElementImpl*)mCollectionPrivate->CurNode; if(!nImpl)return JS_FALSE;#if 0//DEBUG_BY_XHTANG QString qs; char s[256],t[64]; qs=(nImpl->nodeName()).string(); strcpy(s,(const char*)qs);strcat(s," "); fprintf(stderr,"Collection.cpp: nodeName is %s.\n",s);#endif len=HTMLCollectionImpl_length(nImpl,mCollectionPrivate->Type); if (JSVAL_IS_INT(id)) { switch (JSVAL_TO_INT(id)) { case MDHTMLCOLLECTION_LENGTH: *vp=INT_TO_JSVAL(len); break; } } return JS_TRUE;}static JSBool SetHTMLCollectionProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp){ if (JSVAL_IS_INT(id)) { switch (JSVAL_TO_INT(id)) { } } return JS_TRUE;}static JSBool myjsHTMLCollection_Item(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ myJSCollectionPrivate *mCollectionPrivate=(myJSCollectionPrivate *)JS_GetPrivate(cx,obj);// JSObject *eobj; HTMLElementImpl *nImpl; long int index;// HTMLCollectionImpl *hCollectionImpl; if(argc<1)return JS_FALSE; index = JSVAL_TO_INT(argv[0]); if(index<0)return JS_FALSE; nImpl=(HTMLElementImpl*)mCollectionPrivate->CurNode; if(!nImpl)return JS_FALSE;#if 0//DEBUG_BY_XHTANG JSObject *elementObj; DOMString ds; char s[1024],cs[256],t[64]; NodeListImpl *lImpl; NodeImpl* e;int len,i; QString qs,qs1; if(!nImpl){ sprintf(s,"nImpl:%d is Null.",index); *rval=STRING_TO_JSVAL (JS_NewStringCopyZ (cx,s)); return JS_TRUE;} qs=(nImpl->nodeName()).string(); lImpl=nImpl->childNodes(); len=lImpl?lImpl->length():-1; sprintf(s,"\n======NodeName:%s len:%d The child is:\n",(const char*)qs,len); HTMLElementImpl *child = (HTMLElementImpl *)(nImpl->firstChild()); i=0; while(child != 0) { sprintf(t,"%d:",++i);strcat(s,t); qs1=(child->nodeName()).string(); strcat(s,(const char*)qs1);strcat(s," "); child = (HTMLElementImpl *)(child->nextSibling());} len=HTMLCollectionImpl_length(nImpl,DOC_ALL); sprintf(cs,"DOC_All Len:%d",len);strcat(s,cs); len=HTMLCollectionImpl_length(nImpl,FORM_ELEMENTS); sprintf(cs,"FORM_ELEMENTS Len:%d",len);strcat(s,cs); len=HTMLCollectionImpl_length(nImpl,SELECT_OPTIONS); sprintf(t,"SELECT_OPTIONS:%d",len);strcat(s,t); fprintf(stderr,"%s",s); fprintf(stderr,"Collection Type:%d\n",mCollectionPrivate->Type); #endif NodeImpl* CurNode; CurNode=HTMLCollectionImpl_item(nImpl,index,mCollectionPrivate->Type); if(!CurNode)return JS_FALSE;#if 0//DEBUG_BY_XHTANG ElementImpl *eImpl=(ElementImpl*)(CurNode); qs=(eImpl->nodeName()).string(); strcpy(s,(const char*)qs); fprintf(stderr,"Collection.cpp CurNodeName:%s eImpl.id:%d\n",s,eImpl->id());#endif return HTML_NewObjectByElementCommon(cx,obj,JS_GetPrototype(cx,obj),0,(ElementImpl*)CurNode,rval); }static JSBool myjsHTMLCollection_NamedItem(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ myJSCollectionPrivate *mCollectionPrivate=(myJSCollectionPrivate *)JS_GetPrivate(cx,obj); HTMLElementImpl *nImpl; NodeImpl* CurNode; int type; DOMString elementName; JSString *jss; if(argc<1)return JS_FALSE; if (!(jss = JS_ValueToString (cx, argv[0])))return JS_FALSE; elementName = (const char*)JS_GetStringBytes (jss); nImpl=(HTMLElementImpl*)mCollectionPrivate->CurNode; if(!nImpl)return JS_FALSE; type=mCollectionPrivate->Type; CurNode=HTMLCollectionImpl_namedItem(nImpl,elementName,type); if(!CurNode)return JS_FALSE; return HTML_NewObjectByElementCommon(cx,obj,JS_GetPrototype(cx,obj),0,(ElementImpl*)CurNode,rval);}static JSPropertySpec HTMLCollectionProperties[] ={ {"length", MDHTMLCOLLECTION_LENGTH, JSPROP_ENUMERATE | JSPROP_READONLY}, {0}};static JSClass HTMLCollectionClass = { "HTMLCollection", JSCLASS_HAS_PRIVATE, JS_PropertyStub, JS_PropertyStub, GetHTMLCollectionProperty, SetHTMLCollectionProperty, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub };static JSFunctionSpec HTMLCollectionMethods[] = { {"item", myjsHTMLCollection_Item, 1}, {"namedItem", myjsHTMLCollection_NamedItem, 1}, {0}};JSObject* InitHTMLCollectionClass(JSContext *cx,JSObject *globalObj,JSObject *proto){ JSObject *Obj; Obj = JS_DefineObject (cx,globalObj, "HtmlCollection", &HTMLCollectionClass,proto,JSPROP_ENUMERATE); JS_DefineProperties (cx,Obj,HTMLCollectionProperties); JS_DefineFunctions (cx,Obj,HTMLCollectionMethods); return Obj;} JSObject* NewScriptHTMLCollectionElement(JSContext *cx,JSObject *proto,JSObject *parent,ElementImpl *eImpl,int Type){#if DEBUG_BY_XHTANG fprintf(stderr,"proto:%p parent:%p eImpl:%p Type:%d\n",proto,parent,eImpl,Type); #endif JSObject *obj; JSObject *global = JS_GetGlobalObject(cx); jsval vp; if ((JS_TRUE == JS_LookupProperty(cx, global, "HtmlCollection", &vp) && JSVAL_IS_OBJECT(vp))) obj=JS_NewObject(cx,&HTMLCollectionClass,JSVAL_TO_OBJECT(vp),NULL); else obj=InitHTMLCollectionClass(cx,JS_GetParent(cx,proto),proto); myJSCollectionPrivate *mCollectionPrivate=new myJSCollectionPrivate;// myJSCollectionPrivate *protoCollectionPrivate=(myJSCollectionPrivate *)JS_GetPrivate(cx,proto); mCollectionPrivate->CurNode=(ElementImpl*)eImpl; mCollectionPrivate->Type=Type; JS_SetPrivate(cx,obj,(void*)mCollectionPrivate); return obj;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -