From 9cc9db4e24e54499e3736d14563ee2721012d724 Mon Sep 17 00:00:00 2001 From: Date: Sat, 28 Jan 2006 11:17:22 +0100 Subject: [PATCH] Handling onsubmit --- src/document/forms.c | 1 + src/document/forms.h | 1 + src/document/html/parser/forms.c | 2 +- src/ecmascript/see/form.c | 2 +- src/ecmascript/spidermonkey/form.c | 2 +- src/viewer/text/form.c | 8 ++++-- src/viewer/text/form.h | 2 +- src/viewer/text/link.c | 44 ++++++++++++++++++++++++++++-- 8 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/document/forms.c b/src/document/forms.c index 1397a741..6eefac52 100644 --- a/src/document/forms.c +++ b/src/document/forms.c @@ -91,6 +91,7 @@ done_form(struct form *form) mem_free_if(form->action); mem_free_if(form->name); + mem_free_if(form->onsubmit); mem_free_if(form->target); foreach (fc, form->items) { diff --git a/src/document/forms.h b/src/document/forms.h index 8ebf4ed7..601e8e2e 100644 --- a/src/document/forms.h +++ b/src/document/forms.h @@ -32,6 +32,7 @@ struct form { unsigned char *action; unsigned char *name; + unsigned char *onsubmit; unsigned char *target; enum form_method method; diff --git a/src/document/html/parser/forms.c b/src/document/html/parser/forms.c index 5e464b38..6e63f96d 100644 --- a/src/document/html/parser/forms.c +++ b/src/document/html/parser/forms.c @@ -69,7 +69,7 @@ html_form(struct html_context *html_context, unsigned char *a, } mem_free(al); } - + form->onsubmit = get_attr_val(a, "onsubmit", html_context->options); al = get_attr_val(a, "name", html_context->options); if (al) form->name = al; diff --git a/src/ecmascript/see/form.c b/src/ecmascript/see/form.c index 6971752c..908ec4f8 100644 --- a/src/ecmascript/see/form.c +++ b/src/ecmascript/see/form.c @@ -933,7 +933,7 @@ js_form_submit(struct SEE_interpreter *interp, struct SEE_object *self, assert(form); checktime(interp); - submit_given_form(ses, doc_view, form); + submit_given_form(ses, doc_view, form, 0); SEE_SET_BOOLEAN(res, 0); } diff --git a/src/ecmascript/spidermonkey/form.c b/src/ecmascript/spidermonkey/form.c index feea2a0d..f089c64f 100644 --- a/src/ecmascript/spidermonkey/form.c +++ b/src/ecmascript/spidermonkey/form.c @@ -805,7 +805,7 @@ form_submit(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) struct form *form = find_form_by_form_view(doc_view->document, fv); assert(form); - submit_given_form(ses, doc_view, form); + submit_given_form(ses, doc_view, form, 0); boolean_to_jsval(ctx, rval, 0); diff --git a/src/viewer/text/form.c b/src/viewer/text/form.c index 48100c0e..7f4bf9e4 100644 --- a/src/viewer/text/form.c +++ b/src/viewer/text/form.c @@ -1102,7 +1102,8 @@ submit_form(struct session *ses, struct document_view *doc_view, int do_reload) } void -submit_given_form(struct session *ses, struct document_view *doc_view, struct form *form) +submit_given_form(struct session *ses, struct document_view *doc_view, + struct form *form, int do_reload) { /* Added support for submitting forms in hidden * links in 1.285, commented code can safely be removed once we have made sure the new @@ -1125,11 +1126,12 @@ submit_given_form(struct session *ses, struct document_view *doc_view, struct fo if (!list_empty(form->items)) { struct form_control *fc = (struct form_control *)form->items.next; struct uri *uri; + enum cache_mode mode = do_reload ? CACHE_MODE_FORCE_RELOAD : CACHE_MODE_NORMAL; if (!fc) return; uri = get_form_uri(ses, doc_view, fc); if (!uri) return; - goto_uri_frame(ses, uri, form->target, CACHE_MODE_NORMAL); + goto_uri_frame(ses, uri, form->target, mode); done_uri(uri); } } @@ -1140,7 +1142,7 @@ auto_submit_form(struct session *ses) struct document *document = ses->doc_view->document; if (!list_empty(document->forms)) - submit_given_form(ses, ses->doc_view, document->forms.next); + submit_given_form(ses, ses->doc_view, document->forms.next, 0); } diff --git a/src/viewer/text/form.h b/src/viewer/text/form.h index e71329ec..1bf47982 100644 --- a/src/viewer/text/form.h +++ b/src/viewer/text/form.h @@ -88,7 +88,7 @@ void draw_forms(struct terminal *term, struct document_view *doc_view); enum frame_event_status reset_form(struct session *ses, struct document_view *doc_view, int a); enum frame_event_status submit_form(struct session *ses, struct document_view *doc_view, int do_reload); -void submit_given_form(struct session *ses, struct document_view *doc_view, struct form *form); +void submit_given_form(struct session *ses, struct document_view *doc_view, struct form *form, int do_reload); void auto_submit_form(struct session *ses); void do_reset_form(struct document_view *doc_view, struct form *form); diff --git a/src/viewer/text/link.c b/src/viewer/text/link.c index 75d382a5..f28e81e5 100644 --- a/src/viewer/text/link.c +++ b/src/viewer/text/link.c @@ -854,6 +854,40 @@ get_link_uri(struct session *ses, struct document_view *doc_view, } } +static void +try_submit_given_form(struct session *ses, struct document_view *doc_view, + struct form *form, int do_reload) +{ +#ifdef CONFIG_ECMASCRIPT + if (form->onsubmit) { + struct string code; + + if (init_string(&code)) { + struct view_state *vs = doc_view->vs; + struct ecmascript_interpreter *interpreter; + int res = 1; + unsigned char *ret = form->onsubmit; + + if (vs->ecmascript_fragile) + ecmascript_reset_state(vs); + interpreter = vs->ecmascript; + assert(interpreter); +#ifdef CONFIG_ECMASCRIPT_SEE + /* SEE doesn't like return outside functions */ + while ((ret = strstr(ret, "return "))) { + while (*ret != ' ') *ret++ = ' '; + } +#endif + add_to_string(&code, form->onsubmit); + res = ecmascript_eval_boolback(interpreter, &code); + done_string(&code); + if (!res) return; + } + } +#endif + submit_given_form(ses, doc_view, form, do_reload); +} + struct link * goto_current_link(struct session *ses, struct document_view *doc_view, int do_reload) { @@ -866,9 +900,13 @@ goto_current_link(struct session *ses, struct document_view *doc_view, int do_re link = get_current_link(doc_view); if (!link) return NULL; - if (link_is_form(link)) - uri = get_form_uri(ses, doc_view, get_link_form_control(link)); - else + if (link_is_form(link)) { + struct form_control *fc = link->data.form_control; + struct form *form = fc->form; + + try_submit_given_form(ses, doc_view, form, do_reload); + return link; + } else uri = get_link_uri(ses, doc_view, link); if (!uri) return NULL;