1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

SMJS: Fix error "forms.namedItem is not a function".

Commit 63752c854b303f5f58636a512a137bf3702e051b on 2004-12-27
seems to have broken this.
This commit is contained in:
Kalle Olavi Niemitalo 2007-06-10 13:11:53 +03:00 committed by Kalle Olavi Niemitalo
parent 174b1f3af5
commit c9b2fbbd46
2 changed files with 72 additions and 18 deletions

View File

@ -1178,6 +1178,29 @@ const JSPropertySpec forms_props[] = {
{ NULL }
};
/* Find the form whose name is @name, which should normally be a
* string (but might not be). If found, set *rval = the DOM
* object. If not found, leave *rval unchanged. */
static void
find_form_by_name(JSContext *ctx, JSObject *jsdoc,
struct document_view *doc_view,
jsval name, jsval *rval)
{
unsigned char *string = jsval_to_string(ctx, &name);
struct form *form;
if (!*string)
return;
foreach (form, doc_view->document->forms) {
if (form->name && !strcasecmp(string, form->name)) {
object_to_jsval(ctx, rval, get_form_object(ctx, jsdoc,
find_form_view(doc_view, form)));
break;
}
}
}
/* @forms_class.getProperty */
static JSBool
forms_get_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
@ -1206,7 +1229,13 @@ forms_get_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
document = doc_view->document;
if (JSVAL_IS_STRING(id)) {
forms_namedItem(ctx, obj, 1, &id, vp);
/* When SMJS evaluates forms.namedItem("foo"), it first
* calls forms_get_property with id = JSString "namedItem"
* and *vp = JSObject JSFunction forms_namedItem.
* If we don't find a form whose name is id,
* we must leave *vp unchanged here, to avoid
* "TypeError: forms.namedItem is not a function". */
find_form_by_name(ctx, parent_doc, doc_view, id, vp);
return JS_TRUE;
}
@ -1274,9 +1303,6 @@ forms_namedItem(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *r
JSObject *parent_win; /* instance of @window_class */
struct view_state *vs;
struct document_view *doc_view;
struct document *document;
struct form *form;
unsigned char *string;
if (!JS_InstanceOf(ctx, obj, (JSClass *) &forms_class, argv)) return JS_FALSE;
parent_doc = JS_GetParent(ctx, obj);
@ -1289,25 +1315,12 @@ forms_namedItem(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *r
vs = JS_GetInstancePrivate(ctx, parent_win,
(JSClass *) &window_class, NULL);
doc_view = vs->doc_view;
document = doc_view->document;
if (argc != 1)
return JS_TRUE;
undef_to_jsval(ctx, rval);
string = jsval_to_string(ctx, &argv[0]);
if (!*string)
return JS_TRUE;
foreach (form, document->forms) {
if (form->name && !strcasecmp(string, form->name)) {
object_to_jsval(ctx, rval, get_form_object(ctx, parent_doc,
find_form_view(doc_view, form)));
break;
}
}
find_form_by_name(ctx, parent_doc, doc_view, argv[0], rval);
return JS_TRUE;
}

View File

@ -0,0 +1,41 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>forms.namedItem variations</TITLE>
</HEAD>
<BODY>
<FORM name="f" action="#">
<P><BUTTON name="b" type="button">dummy</BUTTON></P>
</FORM>
<SCRIPT type="application/ecmascript">
// iceweasel 2.0+dfsg-1: [object HTMLFormElement]
// ELinks 0.11.3: [object form]
window.alert("window.document.forms.f=" + window.document.forms.f);
</SCRIPT>
<SCRIPT type="application/ecmascript">
// iceweasel 2.0+dfsg-1: [object HTMLFormElement]
// ELinks 0.11.3: [object form]
window.alert("window.document.forms[\"f\"]=" + window.document.forms["f"]);
</SCRIPT>
<SCRIPT type="application/ecmascript">
// iceweasel 2.0+dfsg-1: [object HTMLFormElement]
// ELinks 0.11.3: TypeError: window.document.forms.namedItem is not a function
window.alert("window.document.forms.namedItem(\"f\")=" + window.document.forms.namedItem("f"));
</SCRIPT>
<SCRIPT type="application/ecmascript">
// iceweasel 2.0+dfsg-1: undefined
// ELinks 0.11.3: null
window.alert("window.document.forms.notfound=" + window.document.forms.notfound);
</SCRIPT>
<SCRIPT type="application/ecmascript">
// iceweasel 2.0+dfsg-1: undefined
// ELinks 0.11.3: null
window.alert("window.document.forms[\"notfound\"]=" + window.document.forms["notfound"]);
</SCRIPT>
<SCRIPT type="application/ecmascript">
// iceweasel 2.0+dfsg-1: null
// ELinks 0.11.3: TypeError: window.document.forms.namedItem is not a function
window.alert("window.document.forms.namedItem(\"notfound\")=" + window.document.forms.namedItem("notfound"));
</SCRIPT>
</BODY>
</HTML>