From 34c093eed8cc1bd556b5bc75328ba8199882fb7f Mon Sep 17 00:00:00 2001 From: "jst%netscape.com" Date: Wed, 11 Jul 2001 05:27:19 +0000 Subject: [PATCH] Fixing PDT+ bug 87961, problems with accessing items in DOM arrays. r/sr=vidur@netscape.com, brendan@netscape.com git-svn-id: svn://10.0.0.236/branches/MOZILLA_0_9_2_BRANCH@99056 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/dom/src/base/nsDOMClassInfo.cpp | 118 +++++++++++++++--------- mozilla/dom/src/base/nsDOMClassInfo.h | 6 ++ 2 files changed, 80 insertions(+), 44 deletions(-) diff --git a/mozilla/dom/src/base/nsDOMClassInfo.cpp b/mozilla/dom/src/base/nsDOMClassInfo.cpp index fd40cec733d..ce083288c7c 100644 --- a/mozilla/dom/src/base/nsDOMClassInfo.cpp +++ b/mozilla/dom/src/base/nsDOMClassInfo.cpp @@ -31,6 +31,7 @@ // JavaScript includes #include "jsapi.h" +#include "jsnum.h" // General helper includes #include "nsIContent.h" @@ -692,6 +693,33 @@ nsDOMClassInfo::Init() return NS_OK; } +// static +PRInt32 +nsDOMClassInfo::GetArrayIndexFromId(JSContext *cx, jsval id, PRBool *aIsNumber) +{ + jsdouble array_index; + + if (aIsNumber) { + *aIsNumber = PR_FALSE; + } + + if (!::JS_ValueToNumber(cx, id, &array_index)) { + return -1; + } + + jsint i; + + if (!JSDOUBLE_IS_INT(array_index, i)) { + return -1; + } + + if (aIsNumber) { + *aIsNumber = PR_TRUE; + } + + return i; +} + NS_IMETHODIMP nsDOMClassInfo::GetInterfaces(PRUint32 *aCount, nsIID ***aArray) { @@ -2131,10 +2159,10 @@ NS_IMETHODIMP nsArraySH::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, jsval *vp, PRBool *_retval) { - int32 n = -1; + PRBool is_number = PR_FALSE; + int32 n = GetArrayIndexFromId(cx, id, &is_number); - if ((JSVAL_IS_NUMBER(id) || JSVAL_IS_STRING(id)) && - ::JS_ValueToECMAInt32(cx, id, &n)) { + if (is_number) { if (n < 0) { return NS_ERROR_DOM_INDEX_SIZE_ERR; } @@ -2523,9 +2551,9 @@ nsHTMLFormElementSH::GetProperty(nsIXPConnectWrappedNative *wrapper, return NS_OK; // Don't fall through } - int32 n = -1; + PRInt32 n = GetArrayIndexFromId(cx, id); - if (JSVAL_IS_NUMBER(id) && ::JS_ValueToECMAInt32(cx, id, &n) && n >= 0) { + if (n >= 0) { nsCOMPtr control; form->GetElementAt(n, getter_AddRefs(control)); @@ -2546,10 +2574,9 @@ nsHTMLSelectElementSH::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, jsval *vp, PRBool *_retval) { - int32 n = -1; + PRInt32 n = GetArrayIndexFromId(cx, id); - if ((JSVAL_IS_NUMBER(id) || JSVAL_IS_STRING(id)) && - ::JS_ValueToECMAInt32(cx, id, &n) && n >= 0) { + if (n >= 0) { nsCOMPtr native; wrapper->GetNative(getter_AddRefs(native)); @@ -2615,26 +2642,25 @@ nsHTMLSelectElementSH::SetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, jsval *vp, PRBool *_retval) { - int32 n = -1; + int32 n = GetArrayIndexFromId(cx, id); - if (!(JSVAL_IS_NUMBER(id) || JSVAL_IS_STRING(id)) || - !::JS_ValueToECMAInt32(cx, id, &n) || n < 0) { - return NS_OK; + if (n >= 0) { + nsCOMPtr native; + wrapper->GetNative(getter_AddRefs(native)); + + nsCOMPtr select(do_QueryInterface(native)); + NS_ENSURE_TRUE(select, NS_ERROR_UNEXPECTED); + + nsCOMPtr options; + select->GetOptions(getter_AddRefs(options)); + + nsCOMPtr oc(do_QueryInterface(options)); + NS_ENSURE_TRUE(oc, NS_ERROR_UNEXPECTED); + + return SetOption(cx, vp, n, oc); } - nsCOMPtr native; - wrapper->GetNative(getter_AddRefs(native)); - - nsCOMPtr select(do_QueryInterface(native)); - NS_ENSURE_TRUE(select, NS_ERROR_UNEXPECTED); - - nsCOMPtr options; - select->GetOptions(getter_AddRefs(options)); - - nsCOMPtr oc(do_QueryInterface(options)); - NS_ENSURE_TRUE(oc, NS_ERROR_UNEXPECTED); - - return SetOption(cx, vp, n, oc); + return nsElementSH::SetProperty(wrapper, cx, obj, id, vp, _retval); } @@ -3047,10 +3073,9 @@ nsHTMLOptionCollectionSH::SetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, jsval *vp, PRBool *_retval) { - int32 n = -1; + int32 n = GetArrayIndexFromId(cx, id); - if (!(JSVAL_IS_NUMBER(id) || JSVAL_IS_STRING(id)) || - !::JS_ValueToECMAInt32(cx, id, &n) || n < 0) { + if (n < 0) { return NS_OK; } @@ -3173,25 +3198,30 @@ nsStringArraySH::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, jsval *vp, PRBool *_retval) { - if (JSVAL_IS_NUMBER(id)) { - nsCOMPtr native; - wrapper->GetNative(getter_AddRefs(native)); + PRBool is_number = PR_FALSE; + PRInt32 n = GetArrayIndexFromId(cx, id, &is_number); - nsAutoString val; - - nsresult rv = GetStringAt(native, JSVAL_TO_INT(id), val); - NS_ENSURE_SUCCESS(rv, rv); - - // XXX: Null strings? - - JSString *str = - ::JS_NewUCStringCopyN(cx, NS_REINTERPRET_CAST(const jschar *, val.get()), - val.Length()); - NS_ENSURE_TRUE(str, NS_ERROR_OUT_OF_MEMORY); - - *vp = STRING_TO_JSVAL(str); + if (!is_number) { + return NS_OK; } + nsCOMPtr native; + wrapper->GetNative(getter_AddRefs(native)); + + nsAutoString val; + + nsresult rv = GetStringAt(native, JSVAL_TO_INT(id), val); + NS_ENSURE_SUCCESS(rv, rv); + + // XXX: Null strings? + + JSString *str = + ::JS_NewUCStringCopyN(cx, NS_REINTERPRET_CAST(const jschar *, val.get()), + val.Length()); + NS_ENSURE_TRUE(str, NS_ERROR_OUT_OF_MEMORY); + + *vp = STRING_TO_JSVAL(str); + return NS_OK; } diff --git a/mozilla/dom/src/base/nsDOMClassInfo.h b/mozilla/dom/src/base/nsDOMClassInfo.h index 8319b2a828e..fd06e68858e 100644 --- a/mozilla/dom/src/base/nsDOMClassInfo.h +++ b/mozilla/dom/src/base/nsDOMClassInfo.h @@ -76,6 +76,12 @@ public: protected: static nsresult Init(); + // Checks if id is a number and returns the number, if aIsNumber is + // non-null it's set to true if the id is a number and false if it's + // not a number. If id is not a number this method returns -1 + static PRInt32 GetArrayIndexFromId(JSContext *cx, jsval id, + PRBool *aIsNumber = nsnull); + nsDOMClassInfoID mID; static nsIXPConnect *sXPConnect;