1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-08-20 20:54:47 -04:00

DOM, ecmascript: Added some helper functions for handling forms.

Defined the item function of HTMLCollection.
This commit is contained in:
Witold Filipczyk 2007-06-12 13:01:54 +02:00 committed by Witold Filipczyk
parent 829a953eb2
commit 09db7aa8a0
12 changed files with 158 additions and 31 deletions

View File

@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLButtonElement_props[];
struct BUTTON_struct {
struct HTMLElement_struct html;
struct dom_node *form;
struct dom_node *form; /* Must be first! */
unsigned char *access_key;
unsigned char *name;
unsigned char *type;

View File

@ -6,28 +6,32 @@
#include "document/dom/ecmascript/spidermonkey.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLCollection.h"
static JSBool
HTMLCollection_getProperty(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
{
if (!JSVAL_IS_INT(id))
return JS_TRUE;
switch (JSVAL_TO_INT(id)) {
case JSP_HTML_COLLECTION_LENGTH:
/* Write me! */
break;
default:
break;
}
return JS_TRUE;
}
#include "dom/node.h"
static JSBool
HTMLCollection_item(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
/* Write me! */
struct dom_node_list *list;
int ind;
if (!obj || (!JS_InstanceOf(ctx, obj, (JSClass *)&HTMLCollection_class, NULL)))
return JS_FALSE;
list = JS_GetPrivate(ctx, obj);
if (!list)
return JS_FALSE;
if (!JS_ValueToInt32(ctx, argv[0], &ind))
return JS_FALSE;
if (ind >= list->size)
undef_to_jsval(ctx, rval);
else {
struct dom_node *node = list->entries[ind];
object_to_jsval(ctx, rval, node->ecmascript_obj);
}
return JS_TRUE;
}
@ -35,10 +39,48 @@ static JSBool
HTMLCollection_namedItem(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
struct dom_node_list *list;
if (!obj || (!JS_InstanceOf(ctx, obj, (JSClass *)&HTMLCollection_class, NULL)))
return JS_FALSE;
list = JS_GetPrivate(ctx, obj);
if (!list)
return JS_FALSE;
/* Write me! */
return JS_TRUE;
}
static JSBool
HTMLCollection_getProperty(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
{
struct dom_node_list *list;
if (!obj || (!JS_InstanceOf(ctx, obj, (JSClass *)&HTMLCollection_class, NULL)))
return JS_FALSE;
list = JS_GetPrivate(ctx, obj);
if (!list)
return JS_FALSE;
if (JSVAL_IS_STRING(id))
return HTMLCollection_namedItem(ctx, obj, 1, &id, vp);
if (JSVAL_IS_INT(id)) {
switch (JSVAL_TO_INT(id)) {
case JSP_HTML_COLLECTION_LENGTH:
int_to_jsval(ctx, vp, list->size);
break;
default:
return HTMLCollection_item(ctx, obj, 1, &id, vp);
break;
}
}
return JS_TRUE;
}
const JSPropertySpec HTMLCollection_props[] = {
{ "length", JSP_HTML_COLLECTION_LENGTH, JSPROP_ENUMERATE | JSPROP_READONLY },
{ NULL }

View File

@ -9,7 +9,7 @@ extern const JSPropertySpec HTMLFieldSetElement_props[];
struct FIELDSET_struct {
struct HTMLElement_struct html;
struct dom_node *form;
struct dom_node *form; /* Must be first! */
};
void make_FIELDSET_object(JSContext *ctx, struct dom_node *node);

View File

@ -6,8 +6,11 @@
#include "document/dom/ecmascript/spidermonkey.h"
#include "document/dom/ecmascript/spidermonkey/Node.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLCollection.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLFormElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLInputElement.h"
#include "dom/node.h"
#include "dom/sgml/html/html.h"
static JSBool
HTMLFormElement_getProperty(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
@ -32,8 +35,24 @@ HTMLFormElement_getProperty(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
switch (JSVAL_TO_INT(id)) {
case JSP_HTML_FORM_ELEMENT_ELEMENTS:
string_to_jsval(ctx, vp, html->elements);
/* Write me! */
if (!html->elements)
undef_to_jsval(ctx, vp);
else {
if (html->elements->ctx == ctx)
object_to_jsval(ctx, vp, html->elements->ecmascript_obj);
else {
JSObject *new_obj;
html->elements->ctx = ctx;
new_obj = JS_NewObject(ctx,
(JSClass *)&HTMLCollection_class, NULL, NULL);
if (new_obj) {
JS_SetPrivate(ctx, new_obj, html->elements);
}
html->elements->ecmascript_obj = new_obj;
object_to_jsval(ctx, vp, new_obj);
}
}
break;
case JSP_HTML_FORM_ELEMENT_LENGTH:
int_to_jsval(ctx, vp, html->length);
@ -154,19 +173,81 @@ void
make_FORM_object(JSContext *ctx, struct dom_node *node)
{
struct html_objects *o = JS_GetContextPrivate(ctx);
struct FORM_struct *form;
node->data.element.html_data = mem_calloc(1, sizeof(struct FORM_struct));
if (node->data.element.html_data) {
form = node->data.element.html_data;
if (form) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLFormElement_class, o->HTMLElement_object, NULL);
}
}
static void
del_from_elements(struct dom_node_list *list, struct dom_node *node)
{
struct INPUT_struct *html = node->data.element.html_data;
if (html) {
html->form = NULL;
}
del_from_dom_node_list(list, node);
}
void
register_form_element(struct dom_node *form, struct dom_node *node)
{
struct INPUT_struct *html = node->data.element.html_data;
struct FORM_struct *data = form->data.element.html_data;
if (html) {
html->form = form;
data->elements = add_to_dom_node_list(&data->elements, node, -1);
}
}
void
unregister_form_element(struct dom_node *form, struct dom_node *node)
{
struct FORM_struct *data = form->data.element.html_data;
struct dom_node_list *list = data->elements;
del_from_elements(list, node);
}
struct dom_node *
find_parent_form(struct dom_node *node)
{
if (!node)
return NULL;
while (1) {
node = node->parent;
if (!node)
break;
if (node->type == DOM_NODE_ELEMENT && node->data.element.type == HTML_ELEMENT_FORM)
break;
}
return node;
}
static void
done_elements(struct dom_node_list *list)
{
while (list->size)
del_from_elements(list, list->entries[0]);
if (list->ecmascript_obj) {
JS_SetPrivate(list->ctx, list->ecmascript_obj, NULL);
}
mem_free(list);
}
void
done_FORM_object(void *data)
{
struct FORM_struct *d = data;
/* elements */
if (d->elements)
done_elements(d->elements);
mem_free_if(d->name);
mem_free_if(d->accept_charset);
mem_free_if(d->action);

View File

@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLFormElement_props[];
struct FORM_struct {
struct HTMLElement_struct html;
unsigned char *elements; /* FIXME: proper type */
struct dom_node_list *elements;
unsigned char *name;
unsigned char *accept_charset;
unsigned char *action;
@ -22,4 +22,8 @@ struct FORM_struct {
void make_FORM_object(JSContext *ctx, struct dom_node *node);
void done_FORM_object(void *data);
void register_form_element(struct dom_node *form, struct dom_node *node);
void unregister_form_element(struct dom_node *form, struct dom_node *node);
struct dom_node *find_parent_form(struct dom_node *node);
#endif

View File

@ -10,8 +10,8 @@ extern const JSPropertySpec HTMLInputElement_props[];
struct INPUT_struct {
struct HTMLElement_struct html;
struct dom_node *form; /* Must be first! */
unsigned char *default_value;
struct dom_node *form;
unsigned char *accept;
unsigned char *access_key;
unsigned char *align;

View File

@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLIsIndexElement_props[];
struct ISINDEX_struct {
struct HTMLElement_struct html;
struct dom_node *form;
struct dom_node *form; /* Must be first! */
unsigned char *prompt;
};

View File

@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLLabelElement_props[];
struct LABEL_struct {
struct HTMLElement_struct html;
struct dom_node *form;
struct dom_node *form; /* Must be first! */
unsigned char *access_key;
unsigned char *html_for;
};

View File

@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLLegendElement_props[];
struct LEGEND_struct {
struct HTMLElement_struct html;
struct dom_node *form;
struct dom_node *form; /* Must be first! */
unsigned char *access_key;
unsigned char *align;
};

View File

@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLObjectElement_props[];
struct OBJECT_struct {
struct HTMLElement_struct html;
struct dom_node *form;
struct dom_node *form; /* Must be first! */
unsigned char *code;
unsigned char *align;
unsigned char *archive;

View File

@ -10,9 +10,9 @@ extern const JSPropertySpec HTMLSelectElement_props[];
struct SELECT_struct {
struct HTMLElement_struct html;
struct dom_node *form; /* Must be first! */
unsigned char *type;
unsigned char *value;
struct dom_node *form;
unsigned char *options; /* FIXME: proper type */
unsigned char *name;
int selected_index;

View File

@ -10,8 +10,8 @@ extern const JSPropertySpec HTMLTextAreaElement_props[];
struct TEXTAREA_struct {
struct HTMLElement_struct html;
struct dom_node *form; /* Must be first! */
unsigned char *default_value;
struct dom_node *form;
unsigned char *access_key;
unsigned char *name;
unsigned char *type;