From 4f8479fdf735bb527e4015e64970f4d3022cdfb0 Mon Sep 17 00:00:00 2001 From: "jkeiser%netscape.com" Date: Wed, 13 Mar 2002 02:56:40 +0000 Subject: [PATCH] pete.zha@sun.com: add form elements into for x in form enum (bug 122726), r=jkeiser, sr=jst, a=asa --- dom/src/base/nsDOMClassInfo.cpp | 73 ++++++++++++++++++++++++++++++++- dom/src/base/nsDOMClassInfo.h | 5 +++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/dom/src/base/nsDOMClassInfo.cpp b/dom/src/base/nsDOMClassInfo.cpp index 67072760a44..3e6f129f1bb 100644 --- a/dom/src/base/nsDOMClassInfo.cpp +++ b/dom/src/base/nsDOMClassInfo.cpp @@ -495,7 +495,8 @@ static nsDOMClassInfoData sClassInfoData[] = { ELEMENT_SCRIPTABLE_FLAGS) NS_DEFINE_CLASSINFO_DATA(HTMLFormElement, nsHTMLFormElementSH, ELEMENT_SCRIPTABLE_FLAGS | - nsIXPCScriptable::WANT_GETPROPERTY) + nsIXPCScriptable::WANT_GETPROPERTY | + nsIXPCScriptable::WANT_NEWENUMERATE) NS_DEFINE_CLASSINFO_DATA(HTMLFrameElement, nsHTMLElementSH, ELEMENT_SCRIPTABLE_FLAGS) NS_DEFINE_CLASSINFO_DATA(HTMLFrameSetElement, nsHTMLElementSH, @@ -4478,6 +4479,76 @@ nsHTMLDocumentSH::GetProperty(nsIXPConnectWrappedNative *wrapper, return NS_OK; } +NS_IMETHODIMP +nsHTMLFormElementSH::NewEnumerate(nsIXPConnectWrappedNative *wrapper, + JSContext *cx, JSObject *obj, + PRUint32 enum_op, jsval *statep, + jsid *idp, PRBool *_retval) +{ + switch (enum_op) + { + case JSENUMERATE_INIT: + { + nsCOMPtr native; + wrapper->GetNative(getter_AddRefs(native)); + NS_ABORT_IF_FALSE(native, "No native!"); + + nsCOMPtr form(do_QueryInterface(native)); + + if (!form) { + *statep = JSVAL_NULL; + return NS_ERROR_UNEXPECTED; + } + PRUint32 count = 0; + form->GetElementCount(&count); + *statep = INT_TO_JSVAL(0); + if (idp) + *idp = INT_TO_JSVAL(count); + return NS_OK; + } + case JSENUMERATE_NEXT: + { + nsCOMPtr native; + wrapper->GetNative(getter_AddRefs(native)); + NS_ABORT_IF_FALSE(native, "No native!"); + + nsCOMPtr form(do_QueryInterface(native)); + NS_ENSURE_TRUE(form, NS_ERROR_FAILURE); + + PRInt32 index = (PRInt32) JSVAL_TO_INT(*statep); + PRUint32 count = 0; + form->GetElementCount(&count); + if ((PRUint32)index < count) { + nsCOMPtr controlNode; + form->GetElementAt(index, getter_AddRefs(controlNode)); + NS_ENSURE_TRUE(controlNode, NS_ERROR_FAILURE); + + nsCOMPtr domElement = do_QueryInterface(controlNode); + NS_ENSURE_TRUE(domElement, NS_ERROR_FAILURE); + + nsAutoString attr; + domElement->GetAttribute(NS_LITERAL_STRING("name"), attr); + if (attr.IsEmpty()) { + // If name is not there, use index instead + attr.AppendInt(index); + } + JSString *jsname = JS_NewUCStringCopyN(cx, NS_REINTERPRET_CAST(const jschar *, attr.get()), attr.Length()); + NS_ENSURE_TRUE(jsname, NS_ERROR_OUT_OF_MEMORY); + + JS_ValueToId(cx, STRING_TO_JSVAL(jsname), idp); + + *statep = INT_TO_JSVAL(++index); + } else { + *statep = JSVAL_NULL; + } + break; + } + case JSENUMERATE_DESTROY: + *statep = JSVAL_NULL; + break; + } + return NS_OK; +} // HTMLElement helper diff --git a/dom/src/base/nsDOMClassInfo.h b/dom/src/base/nsDOMClassInfo.h index 392d1522323..43c97f54e22 100644 --- a/dom/src/base/nsDOMClassInfo.h +++ b/dom/src/base/nsDOMClassInfo.h @@ -627,6 +627,11 @@ public: JSObject *obj, jsval id, jsval *vp, PRBool *_retval); + NS_IMETHOD NewEnumerate(nsIXPConnectWrappedNative *wrapper, + JSContext *cx, JSObject *obj, + PRUint32 enum_op, jsval *statep, + jsid *idp, PRBool *_retval); + static nsIClassInfo *doCreate(nsDOMClassInfoData* aData) { return new nsHTMLFormElementSH(aData);