mirror of
https://github.com/rkd77/elinks.git
synced 2024-09-17 01:16:16 -04:00
DOM, ecmascript: Added some helper functions for handling forms.
Defined the item function of HTMLCollection.
This commit is contained in:
parent
829a953eb2
commit
09db7aa8a0
@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLButtonElement_props[];
|
|||||||
|
|
||||||
struct BUTTON_struct {
|
struct BUTTON_struct {
|
||||||
struct HTMLElement_struct html;
|
struct HTMLElement_struct html;
|
||||||
struct dom_node *form;
|
struct dom_node *form; /* Must be first! */
|
||||||
unsigned char *access_key;
|
unsigned char *access_key;
|
||||||
unsigned char *name;
|
unsigned char *name;
|
||||||
unsigned char *type;
|
unsigned char *type;
|
||||||
|
@ -6,28 +6,32 @@
|
|||||||
|
|
||||||
#include "document/dom/ecmascript/spidermonkey.h"
|
#include "document/dom/ecmascript/spidermonkey.h"
|
||||||
#include "document/dom/ecmascript/spidermonkey/html/HTMLCollection.h"
|
#include "document/dom/ecmascript/spidermonkey/html/HTMLCollection.h"
|
||||||
|
#include "dom/node.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static JSBool
|
static JSBool
|
||||||
HTMLCollection_item(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv,
|
HTMLCollection_item(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv,
|
||||||
jsval *rval)
|
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;
|
return JS_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,10 +39,48 @@ static JSBool
|
|||||||
HTMLCollection_namedItem(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv,
|
HTMLCollection_namedItem(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv,
|
||||||
jsval *rval)
|
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! */
|
/* Write me! */
|
||||||
return JS_TRUE;
|
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[] = {
|
const JSPropertySpec HTMLCollection_props[] = {
|
||||||
{ "length", JSP_HTML_COLLECTION_LENGTH, JSPROP_ENUMERATE | JSPROP_READONLY },
|
{ "length", JSP_HTML_COLLECTION_LENGTH, JSPROP_ENUMERATE | JSPROP_READONLY },
|
||||||
{ NULL }
|
{ NULL }
|
||||||
|
@ -9,7 +9,7 @@ extern const JSPropertySpec HTMLFieldSetElement_props[];
|
|||||||
|
|
||||||
struct FIELDSET_struct {
|
struct FIELDSET_struct {
|
||||||
struct HTMLElement_struct html;
|
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);
|
void make_FIELDSET_object(JSContext *ctx, struct dom_node *node);
|
||||||
|
@ -6,8 +6,11 @@
|
|||||||
|
|
||||||
#include "document/dom/ecmascript/spidermonkey.h"
|
#include "document/dom/ecmascript/spidermonkey.h"
|
||||||
#include "document/dom/ecmascript/spidermonkey/Node.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/HTMLFormElement.h"
|
||||||
|
#include "document/dom/ecmascript/spidermonkey/html/HTMLInputElement.h"
|
||||||
#include "dom/node.h"
|
#include "dom/node.h"
|
||||||
|
#include "dom/sgml/html/html.h"
|
||||||
|
|
||||||
static JSBool
|
static JSBool
|
||||||
HTMLFormElement_getProperty(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
|
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)) {
|
switch (JSVAL_TO_INT(id)) {
|
||||||
case JSP_HTML_FORM_ELEMENT_ELEMENTS:
|
case JSP_HTML_FORM_ELEMENT_ELEMENTS:
|
||||||
string_to_jsval(ctx, vp, html->elements);
|
if (!html->elements)
|
||||||
/* Write me! */
|
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;
|
break;
|
||||||
case JSP_HTML_FORM_ELEMENT_LENGTH:
|
case JSP_HTML_FORM_ELEMENT_LENGTH:
|
||||||
int_to_jsval(ctx, vp, html->length);
|
int_to_jsval(ctx, vp, html->length);
|
||||||
@ -154,19 +173,81 @@ void
|
|||||||
make_FORM_object(JSContext *ctx, struct dom_node *node)
|
make_FORM_object(JSContext *ctx, struct dom_node *node)
|
||||||
{
|
{
|
||||||
struct html_objects *o = JS_GetContextPrivate(ctx);
|
struct html_objects *o = JS_GetContextPrivate(ctx);
|
||||||
|
struct FORM_struct *form;
|
||||||
|
|
||||||
node->data.element.html_data = mem_calloc(1, sizeof(struct FORM_struct));
|
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);
|
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
|
void
|
||||||
done_FORM_object(void *data)
|
done_FORM_object(void *data)
|
||||||
{
|
{
|
||||||
struct FORM_struct *d = data;
|
struct FORM_struct *d = data;
|
||||||
|
|
||||||
/* elements */
|
if (d->elements)
|
||||||
|
done_elements(d->elements);
|
||||||
mem_free_if(d->name);
|
mem_free_if(d->name);
|
||||||
mem_free_if(d->accept_charset);
|
mem_free_if(d->accept_charset);
|
||||||
mem_free_if(d->action);
|
mem_free_if(d->action);
|
||||||
|
@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLFormElement_props[];
|
|||||||
|
|
||||||
struct FORM_struct {
|
struct FORM_struct {
|
||||||
struct HTMLElement_struct html;
|
struct HTMLElement_struct html;
|
||||||
unsigned char *elements; /* FIXME: proper type */
|
struct dom_node_list *elements;
|
||||||
unsigned char *name;
|
unsigned char *name;
|
||||||
unsigned char *accept_charset;
|
unsigned char *accept_charset;
|
||||||
unsigned char *action;
|
unsigned char *action;
|
||||||
@ -22,4 +22,8 @@ struct FORM_struct {
|
|||||||
|
|
||||||
void make_FORM_object(JSContext *ctx, struct dom_node *node);
|
void make_FORM_object(JSContext *ctx, struct dom_node *node);
|
||||||
void done_FORM_object(void *data);
|
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
|
#endif
|
||||||
|
@ -10,8 +10,8 @@ extern const JSPropertySpec HTMLInputElement_props[];
|
|||||||
|
|
||||||
struct INPUT_struct {
|
struct INPUT_struct {
|
||||||
struct HTMLElement_struct html;
|
struct HTMLElement_struct html;
|
||||||
|
struct dom_node *form; /* Must be first! */
|
||||||
unsigned char *default_value;
|
unsigned char *default_value;
|
||||||
struct dom_node *form;
|
|
||||||
unsigned char *accept;
|
unsigned char *accept;
|
||||||
unsigned char *access_key;
|
unsigned char *access_key;
|
||||||
unsigned char *align;
|
unsigned char *align;
|
||||||
|
@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLIsIndexElement_props[];
|
|||||||
|
|
||||||
struct ISINDEX_struct {
|
struct ISINDEX_struct {
|
||||||
struct HTMLElement_struct html;
|
struct HTMLElement_struct html;
|
||||||
struct dom_node *form;
|
struct dom_node *form; /* Must be first! */
|
||||||
unsigned char *prompt;
|
unsigned char *prompt;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLLabelElement_props[];
|
|||||||
|
|
||||||
struct LABEL_struct {
|
struct LABEL_struct {
|
||||||
struct HTMLElement_struct html;
|
struct HTMLElement_struct html;
|
||||||
struct dom_node *form;
|
struct dom_node *form; /* Must be first! */
|
||||||
unsigned char *access_key;
|
unsigned char *access_key;
|
||||||
unsigned char *html_for;
|
unsigned char *html_for;
|
||||||
};
|
};
|
||||||
|
@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLLegendElement_props[];
|
|||||||
|
|
||||||
struct LEGEND_struct {
|
struct LEGEND_struct {
|
||||||
struct HTMLElement_struct html;
|
struct HTMLElement_struct html;
|
||||||
struct dom_node *form;
|
struct dom_node *form; /* Must be first! */
|
||||||
unsigned char *access_key;
|
unsigned char *access_key;
|
||||||
unsigned char *align;
|
unsigned char *align;
|
||||||
};
|
};
|
||||||
|
@ -10,7 +10,7 @@ extern const JSPropertySpec HTMLObjectElement_props[];
|
|||||||
|
|
||||||
struct OBJECT_struct {
|
struct OBJECT_struct {
|
||||||
struct HTMLElement_struct html;
|
struct HTMLElement_struct html;
|
||||||
struct dom_node *form;
|
struct dom_node *form; /* Must be first! */
|
||||||
unsigned char *code;
|
unsigned char *code;
|
||||||
unsigned char *align;
|
unsigned char *align;
|
||||||
unsigned char *archive;
|
unsigned char *archive;
|
||||||
|
@ -10,9 +10,9 @@ extern const JSPropertySpec HTMLSelectElement_props[];
|
|||||||
|
|
||||||
struct SELECT_struct {
|
struct SELECT_struct {
|
||||||
struct HTMLElement_struct html;
|
struct HTMLElement_struct html;
|
||||||
|
struct dom_node *form; /* Must be first! */
|
||||||
unsigned char *type;
|
unsigned char *type;
|
||||||
unsigned char *value;
|
unsigned char *value;
|
||||||
struct dom_node *form;
|
|
||||||
unsigned char *options; /* FIXME: proper type */
|
unsigned char *options; /* FIXME: proper type */
|
||||||
unsigned char *name;
|
unsigned char *name;
|
||||||
int selected_index;
|
int selected_index;
|
||||||
|
@ -10,8 +10,8 @@ extern const JSPropertySpec HTMLTextAreaElement_props[];
|
|||||||
|
|
||||||
struct TEXTAREA_struct {
|
struct TEXTAREA_struct {
|
||||||
struct HTMLElement_struct html;
|
struct HTMLElement_struct html;
|
||||||
|
struct dom_node *form; /* Must be first! */
|
||||||
unsigned char *default_value;
|
unsigned char *default_value;
|
||||||
struct dom_node *form;
|
|
||||||
unsigned char *access_key;
|
unsigned char *access_key;
|
||||||
unsigned char *name;
|
unsigned char *name;
|
||||||
unsigned char *type;
|
unsigned char *type;
|
||||||
|
Loading…
Reference in New Issue
Block a user