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
This commit is contained in:
jst%netscape.com
2001-07-11 05:27:19 +00:00
parent 41ff1bf3e6
commit 34c093eed8
2 changed files with 80 additions and 44 deletions

View File

@@ -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<nsIFormControl> 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<nsISupports> 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<nsISupports> native;
wrapper->GetNative(getter_AddRefs(native));
nsCOMPtr<nsIDOMHTMLSelectElement> select(do_QueryInterface(native));
NS_ENSURE_TRUE(select, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIDOMHTMLCollection> options;
select->GetOptions(getter_AddRefs(options));
nsCOMPtr<nsIDOMNSHTMLOptionCollection> oc(do_QueryInterface(options));
NS_ENSURE_TRUE(oc, NS_ERROR_UNEXPECTED);
return SetOption(cx, vp, n, oc);
}
nsCOMPtr<nsISupports> native;
wrapper->GetNative(getter_AddRefs(native));
nsCOMPtr<nsIDOMHTMLSelectElement> select(do_QueryInterface(native));
NS_ENSURE_TRUE(select, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIDOMHTMLCollection> options;
select->GetOptions(getter_AddRefs(options));
nsCOMPtr<nsIDOMNSHTMLOptionCollection> 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<nsISupports> 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<nsISupports> 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;
}

View File

@@ -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;