1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00

DOM forms: register and unregister functions for FORM elements.

Elements like INPUT, TEXTAREA or SELECT look for the parent FORM
and register themselves to it. When those elements are freed they
unregister themselves.
This commit is contained in:
Witold Filipczyk 2007-06-12 15:07:43 +02:00 committed by Witold Filipczyk
parent c6274851b2
commit 4df27a6700
12 changed files with 65 additions and 36 deletions

View File

@ -7,6 +7,7 @@
#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/HTMLButtonElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLButtonElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLFormElement.h"
#include "dom/node.h" #include "dom/node.h"
static JSBool static JSBool
@ -130,6 +131,7 @@ make_BUTTON_object(JSContext *ctx, struct dom_node *node)
node->data.element.html_data = mem_calloc(1, sizeof(struct BUTTON_struct)); node->data.element.html_data = mem_calloc(1, sizeof(struct BUTTON_struct));
if (node->data.element.html_data) { if (node->data.element.html_data) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLButtonElement_class, o->HTMLElement_object, NULL); node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLButtonElement_class, o->HTMLElement_object, NULL);
register_form_element(node);
} }
} }
@ -138,7 +140,7 @@ done_BUTTON_object(struct dom_node *node)
{ {
struct BUTTON_struct *d = node->data.element.html_data; struct BUTTON_struct *d = node->data.element.html_data;
/* form musn't be freed */ unregister_form_element(d->form, node);
mem_free_if(d->access_key); mem_free_if(d->access_key);
mem_free_if(d->name); mem_free_if(d->name);
mem_free_if(d->type); mem_free_if(d->type);

View File

@ -8,6 +8,7 @@
#include "document/dom/ecmascript/spidermonkey/Node.h" #include "document/dom/ecmascript/spidermonkey/Node.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLFieldSetElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLFieldSetElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLFormElement.h"
#include "dom/node.h" #include "dom/node.h"
static JSBool static JSBool
@ -67,10 +68,14 @@ make_FIELDSET_object(JSContext *ctx, struct dom_node *node)
node->data.element.html_data = mem_calloc(1, sizeof(struct FIELDSET_struct)); node->data.element.html_data = mem_calloc(1, sizeof(struct FIELDSET_struct));
if (node->data.element.html_data) { if (node->data.element.html_data) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLFieldSetElement_class, o->HTMLElement_object, NULL); node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLFieldSetElement_class, o->HTMLElement_object, NULL);
register_form_element(node);
} }
} }
void void
done_FIELDSET_object(struct dom_node *node) done_FIELDSET_object(struct dom_node *node)
{ {
struct FIELDSET_struct *d = node->data.element.html_data;
unregister_form_element(d->form, node);
} }

View File

@ -193,33 +193,9 @@ del_from_elements(struct dom_node_list *list, struct dom_node *node)
del_from_dom_node_list(list, node); del_from_dom_node_list(list, node);
} }
void static struct dom_node *
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) find_parent_form(struct dom_node *node)
{ {
if (!node)
return NULL;
while (1) { while (1) {
node = node->parent; node = node->parent;
if (!node) if (!node)
@ -230,6 +206,37 @@ find_parent_form(struct dom_node *node)
return node; return node;
} }
void
register_form_element(struct dom_node *node)
{
struct dom_node *form;
if (!node)
return;
form = find_parent_form(node);
if (form) {
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)
{
if (form) {
struct FORM_struct *data = form->data.element.html_data;
struct dom_node_list *list = data->elements;
del_from_elements(list, node);
}
}
static void static void
done_elements(struct dom_node_list *list) done_elements(struct dom_node_list *list)
{ {

View File

@ -22,8 +22,7 @@ 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(struct dom_node *node); void done_FORM_object(struct dom_node *node);
void register_form_element(struct dom_node *form, struct dom_node *node); void register_form_element(struct dom_node *node);
void unregister_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

View File

@ -6,6 +6,7 @@
#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/HTMLFormElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLInputElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLInputElement.h"
#include "dom/node.h" #include "dom/node.h"
@ -244,6 +245,7 @@ make_INPUT_object(JSContext *ctx, struct dom_node *node)
node->data.element.html_data = mem_calloc(1, sizeof(struct INPUT_struct)); node->data.element.html_data = mem_calloc(1, sizeof(struct INPUT_struct));
if (node->data.element.html_data) { if (node->data.element.html_data) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLInputElement_class, o->HTMLElement_object, NULL); node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLInputElement_class, o->HTMLElement_object, NULL);
register_form_element(node);
} }
} }
@ -252,8 +254,8 @@ done_INPUT_object(struct dom_node *node)
{ {
struct INPUT_struct *d = node->data.element.html_data; struct INPUT_struct *d = node->data.element.html_data;
unregister_form_element(d->form, node);
mem_free_if(d->default_value); mem_free_if(d->default_value);
/* form ? */
mem_free_if(d->accept); mem_free_if(d->accept);
mem_free_if(d->access_key); mem_free_if(d->access_key);
mem_free_if(d->align); mem_free_if(d->align);

View File

@ -6,6 +6,7 @@
#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/HTMLFormElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLIsIndexElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLIsIndexElement.h"
#include "dom/node.h" #include "dom/node.h"
@ -99,6 +100,7 @@ make_ISINDEX_object(JSContext *ctx, struct dom_node *node)
node->data.element.html_data = mem_calloc(1, sizeof(struct ISINDEX_struct)); node->data.element.html_data = mem_calloc(1, sizeof(struct ISINDEX_struct));
if (node->data.element.html_data) { if (node->data.element.html_data) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLIsIndexElement_class, o->HTMLElement_object, NULL); node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLIsIndexElement_class, o->HTMLElement_object, NULL);
register_form_element(node);
} }
} }
@ -107,6 +109,6 @@ done_ISINDEX_object(struct dom_node *node)
{ {
struct ISINDEX_struct *d = node->data.element.html_data; struct ISINDEX_struct *d = node->data.element.html_data;
/* form ? */ unregister_form_element(d->form, node);
mem_free_if(d->prompt); mem_free_if(d->prompt);
} }

View File

@ -6,6 +6,7 @@
#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/HTMLFormElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLLabelElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLLabelElement.h"
#include "dom/node.h" #include "dom/node.h"
@ -106,6 +107,7 @@ make_LABEL_object(JSContext *ctx, struct dom_node *node)
node->data.element.html_data = mem_calloc(1, sizeof(struct LABEL_struct)); node->data.element.html_data = mem_calloc(1, sizeof(struct LABEL_struct));
if (node->data.element.html_data) { if (node->data.element.html_data) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLLabelElement_class, o->HTMLElement_object, NULL); node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLLabelElement_class, o->HTMLElement_object, NULL);
register_form_element(node);
} }
} }
@ -114,7 +116,7 @@ done_LABEL_object(struct dom_node *node)
{ {
struct LABEL_struct *d = node->data.element.html_data; struct LABEL_struct *d = node->data.element.html_data;
/* form ? */ unregister_form_element(d->form, node);
mem_free_if(d->access_key); mem_free_if(d->access_key);
mem_free_if(d->html_for); mem_free_if(d->html_for);
} }

View File

@ -6,6 +6,7 @@
#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/HTMLFormElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLLegendElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLLegendElement.h"
#include "dom/node.h" #include "dom/node.h"
@ -106,6 +107,7 @@ make_LEGEND_object(JSContext *ctx, struct dom_node *node)
node->data.element.html_data = mem_calloc(1, sizeof(struct LEGEND_struct)); node->data.element.html_data = mem_calloc(1, sizeof(struct LEGEND_struct));
if (node->data.element.html_data) { if (node->data.element.html_data) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLLegendElement_class, o->HTMLElement_object, NULL); node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLLegendElement_class, o->HTMLElement_object, NULL);
register_form_element(node);
} }
} }
@ -114,7 +116,7 @@ done_LEGEND_object(struct dom_node *node)
{ {
struct LEGEND_struct *d = node->data.element.html_data; struct LEGEND_struct *d = node->data.element.html_data;
/* form ? */ unregister_form_element(d->form, node);
mem_free_if(d->access_key); mem_free_if(d->access_key);
mem_free_if(d->align); mem_free_if(d->align);
} }

View File

@ -6,6 +6,7 @@
#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/HTMLFormElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLObjectElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLObjectElement.h"
#include "dom/node.h" #include "dom/node.h"
@ -212,6 +213,7 @@ make_OBJECT_object(JSContext *ctx, struct dom_node *node)
node->data.element.html_data = mem_calloc(1, sizeof(struct OBJECT_struct)); node->data.element.html_data = mem_calloc(1, sizeof(struct OBJECT_struct));
if (node->data.element.html_data) { if (node->data.element.html_data) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLObjectElement_class, o->HTMLElement_object, NULL); node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLObjectElement_class, o->HTMLElement_object, NULL);
register_form_element(node);
} }
} }
@ -220,7 +222,7 @@ done_OBJECT_object(struct dom_node *node)
{ {
struct OBJECT_struct *d = node->data.element.html_data; struct OBJECT_struct *d = node->data.element.html_data;
/* form ? */ unregister_form_element(d->form, node);
mem_free_if(d->code); mem_free_if(d->code);
mem_free_if(d->align); mem_free_if(d->align);
mem_free_if(d->archive); mem_free_if(d->archive);

View File

@ -6,6 +6,7 @@
#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/HTMLFormElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLOptionElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLOptionElement.h"
#include "dom/node.h" #include "dom/node.h"
@ -135,6 +136,7 @@ make_OPTION_object(JSContext *ctx, struct dom_node *node)
node->data.element.html_data = mem_calloc(1, sizeof(struct OPTION_struct)); node->data.element.html_data = mem_calloc(1, sizeof(struct OPTION_struct));
if (node->data.element.html_data) { if (node->data.element.html_data) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLOptionElement_class, o->HTMLElement_object, NULL); node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLOptionElement_class, o->HTMLElement_object, NULL);
register_form_element(node);
} }
} }
@ -143,7 +145,7 @@ done_OPTION_object(struct dom_node *node)
{ {
struct OPTION_struct *d = node->data.element.html_data; struct OPTION_struct *d = node->data.element.html_data;
/* form ? */ unregister_form_element(d->form, node);
mem_free_if(d->text); mem_free_if(d->text);
mem_free_if(d->label); mem_free_if(d->label);
mem_free_if(d->value); mem_free_if(d->value);

View File

@ -6,6 +6,7 @@
#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/HTMLFormElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLSelectElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLSelectElement.h"
#include "dom/node.h" #include "dom/node.h"
@ -189,6 +190,7 @@ make_SELECT_object(JSContext *ctx, struct dom_node *node)
node->data.element.html_data = mem_calloc(1, sizeof(struct SELECT_struct)); node->data.element.html_data = mem_calloc(1, sizeof(struct SELECT_struct));
if (node->data.element.html_data) { if (node->data.element.html_data) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLSelectElement_class, o->HTMLElement_object, NULL); node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLSelectElement_class, o->HTMLElement_object, NULL);
register_form_element(node);
} }
} }
@ -197,8 +199,8 @@ done_SELECT_object(struct dom_node *node)
{ {
struct SELECT_struct *d = node->data.element.html_data; struct SELECT_struct *d = node->data.element.html_data;
unregister_form_element(d->form, node);
mem_free_if(d->type); mem_free_if(d->type);
mem_free_if(d->value); mem_free_if(d->value);
/* form, options ? */
mem_free_if(d->name); mem_free_if(d->name);
} }

View File

@ -6,6 +6,7 @@
#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/HTMLFormElement.h"
#include "document/dom/ecmascript/spidermonkey/html/HTMLTextAreaElement.h" #include "document/dom/ecmascript/spidermonkey/html/HTMLTextAreaElement.h"
#include "dom/node.h" #include "dom/node.h"
@ -183,6 +184,7 @@ make_TEXTAREA_object(JSContext *ctx, struct dom_node *node)
node->data.element.html_data = mem_calloc(1, sizeof(struct TEXTAREA_struct)); node->data.element.html_data = mem_calloc(1, sizeof(struct TEXTAREA_struct));
if (node->data.element.html_data) { if (node->data.element.html_data) {
node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLTextAreaElement_class, o->HTMLElement_object, NULL); node->ecmascript_obj = JS_NewObject(ctx, (JSClass *)&HTMLTextAreaElement_class, o->HTMLElement_object, NULL);
register_form_element(node);
} }
} }
@ -191,8 +193,8 @@ done_TEXTAREA_object(struct dom_node *node)
{ {
struct TEXTAREA_struct *d = node->data.element.html_data; struct TEXTAREA_struct *d = node->data.element.html_data;
unregister_form_element(d->form, node);
mem_free_if(d->default_value); mem_free_if(d->default_value);
/* What to do with d->form? */
mem_free_if(d->access_key); mem_free_if(d->access_key);
mem_free_if(d->name); mem_free_if(d->name);
mem_free_if(d->type); mem_free_if(d->type);