2003-10-25 04:35:49 +04:00
|
|
|
/*
|
2004-08-08 23:13:40 +04:00
|
|
|
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
|
2003-10-25 04:35:49 +04:00
|
|
|
* Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
|
2010-06-04 13:35:08 +04:00
|
|
|
* Copyright 2004 John Tytgat <joty@netsurf-browser.org>
|
2009-02-20 14:39:25 +03:00
|
|
|
* Copyright 2005-9 John-Mark Bell <jmb@netsurf-browser.org>
|
2009-08-14 14:37:33 +04:00
|
|
|
* Copyright 2009 Paul Blokus <paul_pl@users.sourceforge.net>
|
2010-06-04 13:35:08 +04:00
|
|
|
* Copyright 2010 Michael Drake <tlsa@netsurf-browser.org>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-10-25 04:35:49 +04:00
|
|
|
*/
|
|
|
|
|
2017-01-20 00:15:08 +03:00
|
|
|
/**
|
|
|
|
* \file
|
2003-10-25 04:35:49 +04:00
|
|
|
* Form handling functions (implementation).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2007-03-12 02:48:29 +03:00
|
|
|
#include <limits.h>
|
2005-05-11 06:03:15 +04:00
|
|
|
#include <stdbool.h>
|
2004-03-21 23:07:14 +03:00
|
|
|
#include <stdio.h>
|
2003-10-25 04:35:49 +04:00
|
|
|
#include <string.h>
|
2012-03-25 00:55:22 +04:00
|
|
|
#include <dom/dom.h>
|
2014-01-30 01:35:53 +04:00
|
|
|
|
2014-10-13 14:56:31 +04:00
|
|
|
#include "utils/corestrings.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/messages.h"
|
|
|
|
#include "utils/talloc.h"
|
|
|
|
#include "utils/url.h"
|
|
|
|
#include "utils/utf8.h"
|
2016-09-13 00:43:39 +03:00
|
|
|
#include "utils/ascii.h"
|
2017-01-13 13:01:25 +03:00
|
|
|
#include "netsurf/browser_window.h"
|
|
|
|
#include "netsurf/mouse.h"
|
|
|
|
#include "netsurf/plotters.h"
|
|
|
|
#include "netsurf/misc.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "content/fetch.h"
|
2010-06-04 13:35:08 +04:00
|
|
|
#include "content/hlcache.h"
|
2016-06-06 16:47:27 +03:00
|
|
|
#include "css/utils.h"
|
2009-08-14 14:37:33 +04:00
|
|
|
#include "desktop/knockout.h"
|
2011-05-10 02:49:17 +04:00
|
|
|
#include "desktop/scrollbar.h"
|
2013-02-07 02:39:45 +04:00
|
|
|
#include "desktop/textarea.h"
|
2016-04-26 14:50:16 +03:00
|
|
|
#include "desktop/gui_internal.h"
|
2014-10-13 14:56:31 +04:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
#include "html/box.h"
|
|
|
|
#include "html/font.h"
|
|
|
|
#include "html/form_internal.h"
|
|
|
|
#include "html/html.h"
|
|
|
|
#include "html/html_internal.h"
|
|
|
|
#include "html/layout.h"
|
2003-10-25 04:35:49 +04:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
#define MAX_SELECT_HEIGHT 210
|
|
|
|
#define SELECT_LINE_SPACING 0.2
|
|
|
|
#define SELECT_BORDER_WIDTH 1
|
|
|
|
#define SELECT_SELECTED_COLOUR 0xDB9370
|
|
|
|
|
|
|
|
struct form_select_menu {
|
|
|
|
int line_height;
|
|
|
|
int width, height;
|
2011-05-10 02:49:17 +04:00
|
|
|
struct scrollbar *scrollbar;
|
2009-08-14 14:37:33 +04:00
|
|
|
int f_size;
|
|
|
|
bool scroll_capture;
|
|
|
|
select_menu_redraw_callback callback;
|
|
|
|
void *client_data;
|
2011-06-28 15:15:39 +04:00
|
|
|
struct content *c;
|
2009-08-14 14:37:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static plot_style_t plot_style_fill_selected = {
|
|
|
|
.fill_type = PLOT_OP_TYPE_SOLID,
|
|
|
|
.fill_colour = SELECT_SELECTED_COLOUR,
|
|
|
|
};
|
|
|
|
|
|
|
|
static plot_font_style_t plot_fstyle_entry = {
|
|
|
|
.family = PLOT_FONT_FAMILY_SANS_SERIF,
|
|
|
|
.weight = 400,
|
|
|
|
.flags = FONTF_NONE,
|
|
|
|
.background = 0xffffff,
|
|
|
|
.foreground = 0x000000,
|
|
|
|
};
|
2003-10-25 04:35:49 +04:00
|
|
|
|
2005-06-26 05:55:20 +04:00
|
|
|
static char *form_acceptable_charset(struct form *form);
|
2014-01-05 19:50:23 +04:00
|
|
|
static char *form_encode_item(const char *item, uint32_t len, const char *charset,
|
2007-02-26 03:32:07 +03:00
|
|
|
const char *fallback);
|
2009-08-14 14:37:33 +04:00
|
|
|
static void form_select_menu_clicked(struct form_control *control,
|
|
|
|
int x, int y);
|
|
|
|
static void form_select_menu_scroll_callback(void *client_data,
|
2011-05-10 02:49:17 +04:00
|
|
|
struct scrollbar_msg_data *scrollbar_data);
|
2003-10-26 01:51:15 +04:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
/* exported interface documented in html/form_internal.h */
|
|
|
|
struct form *form_new(void *node, const char *action, const char *target,
|
|
|
|
form_method method, const char *charset,
|
2009-02-20 14:39:25 +03:00
|
|
|
const char *doc_charset)
|
2004-12-13 00:51:01 +03:00
|
|
|
{
|
|
|
|
struct form *form;
|
|
|
|
|
2009-02-20 14:39:25 +03:00
|
|
|
form = calloc(1, sizeof *form);
|
2004-12-13 00:51:01 +03:00
|
|
|
if (!form)
|
2009-02-20 14:39:25 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
form->action = strdup(action != NULL ? action : "");
|
|
|
|
if (form->action == NULL) {
|
|
|
|
free(form);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
form->target = target != NULL ? strdup(target) : NULL;
|
|
|
|
if (target != NULL && form->target == NULL) {
|
|
|
|
free(form->action);
|
|
|
|
free(form);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
form->method = method;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
|
|
|
form->accept_charsets = charset != NULL ? strdup(charset) : NULL;
|
|
|
|
if (charset != NULL && form->accept_charsets == NULL) {
|
|
|
|
free(form->target);
|
|
|
|
free(form->action);
|
|
|
|
free(form);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-20 15:50:34 +03:00
|
|
|
form->document_charset = doc_charset != NULL ? strdup(doc_charset)
|
|
|
|
: NULL;
|
|
|
|
if (doc_charset && form->document_charset == NULL) {
|
2009-02-20 14:39:25 +03:00
|
|
|
free(form->accept_charsets);
|
|
|
|
free(form->target);
|
|
|
|
free(form->action);
|
|
|
|
free(form);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
form->node = node;
|
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
return form;
|
|
|
|
}
|
|
|
|
|
2014-02-08 21:11:55 +04:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
/* exported interface documented in html/form_internal.h */
|
2009-02-20 14:39:25 +03:00
|
|
|
void form_free(struct form *form)
|
|
|
|
{
|
|
|
|
struct form_control *c, *d;
|
|
|
|
|
|
|
|
for (c = form->controls; c != NULL; c = d) {
|
|
|
|
d = c->next;
|
|
|
|
|
|
|
|
form_free_control(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(form->action);
|
|
|
|
free(form->target);
|
|
|
|
free(form->accept_charsets);
|
|
|
|
free(form->document_charset);
|
|
|
|
|
|
|
|
free(form);
|
|
|
|
}
|
2004-12-13 00:51:01 +03:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
/* exported interface documented in html/form_internal.h */
|
2009-02-20 14:39:25 +03:00
|
|
|
struct form_control *form_new_control(void *node, form_control_type type)
|
2004-05-21 14:25:42 +04:00
|
|
|
{
|
|
|
|
struct form_control *control;
|
|
|
|
|
2009-02-20 14:39:25 +03:00
|
|
|
control = calloc(1, sizeof *control);
|
|
|
|
if (control == NULL)
|
2004-07-20 00:40:11 +04:00
|
|
|
return NULL;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
|
|
|
control->node = node;
|
2004-05-21 14:25:42 +04:00
|
|
|
control->type = type;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
return control;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
/**
|
|
|
|
* Add a control to the list of controls in a form.
|
2005-06-26 05:55:20 +04:00
|
|
|
*
|
|
|
|
* \param form The form to add the control to
|
|
|
|
* \param control The control to add
|
2003-10-25 04:35:49 +04:00
|
|
|
*/
|
|
|
|
void form_add_control(struct form *form, struct form_control *control)
|
|
|
|
{
|
2013-02-20 03:51:16 +04:00
|
|
|
if (form == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
control->form = form;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-07-20 00:40:11 +04:00
|
|
|
if (form->controls != NULL) {
|
2003-10-25 04:35:49 +04:00
|
|
|
assert(form->last_control);
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
form->last_control->next = control;
|
2004-04-09 03:46:41 +04:00
|
|
|
control->prev = form->last_control;
|
2004-07-20 00:40:11 +04:00
|
|
|
control->next = NULL;
|
2003-10-25 04:35:49 +04:00
|
|
|
form->last_control = control;
|
|
|
|
} else {
|
|
|
|
form->controls = form->last_control = control;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
/**
|
|
|
|
* Free a struct form_control.
|
|
|
|
*
|
|
|
|
* \param control structure to free
|
|
|
|
*/
|
|
|
|
void form_free_control(struct form_control *control)
|
|
|
|
{
|
2015-04-22 23:11:21 +03:00
|
|
|
struct form_control *c;
|
2015-03-27 18:27:33 +03:00
|
|
|
assert(control != NULL);
|
|
|
|
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO, "Control:%p name:%p value:%p initial:%p",
|
|
|
|
control, control->name, control->value, control->initial_value);
|
2004-05-21 14:25:42 +04:00
|
|
|
free(control->name);
|
|
|
|
free(control->value);
|
|
|
|
free(control->initial_value);
|
2019-08-05 00:07:42 +03:00
|
|
|
if (control->last_synced_value != NULL) {
|
|
|
|
free(control->last_synced_value);
|
|
|
|
}
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
if (control->type == GADGET_SELECT) {
|
|
|
|
struct form_option *option, *next;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
for (option = control->data.select.items; option;
|
|
|
|
option = next) {
|
|
|
|
next = option->next;
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"select option:%p text:%p value:%p", option,
|
|
|
|
option->text, option->value);
|
2004-05-21 14:25:42 +04:00
|
|
|
free(option->text);
|
|
|
|
free(option->value);
|
|
|
|
free(option);
|
|
|
|
}
|
2015-03-27 18:27:33 +03:00
|
|
|
if (control->data.select.menu != NULL) {
|
2009-08-14 14:37:33 +04:00
|
|
|
form_free_select_menu(control);
|
2015-03-27 18:27:33 +03:00
|
|
|
}
|
2004-05-21 14:25:42 +04:00
|
|
|
}
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2013-02-07 02:39:45 +04:00
|
|
|
if (control->type == GADGET_TEXTAREA ||
|
|
|
|
control->type == GADGET_TEXTBOX ||
|
|
|
|
control->type == GADGET_PASSWORD) {
|
|
|
|
|
2015-03-27 18:27:33 +03:00
|
|
|
if (control->data.text.initial != NULL) {
|
2013-02-07 02:39:45 +04:00
|
|
|
dom_string_unref(control->data.text.initial);
|
2015-03-27 18:27:33 +03:00
|
|
|
}
|
2013-02-07 02:39:45 +04:00
|
|
|
|
2015-03-27 18:27:33 +03:00
|
|
|
if (control->data.text.ta != NULL) {
|
2013-02-07 02:39:45 +04:00
|
|
|
textarea_destroy(control->data.text.ta);
|
2015-03-27 18:27:33 +03:00
|
|
|
}
|
2013-02-07 02:39:45 +04:00
|
|
|
}
|
|
|
|
|
2015-04-22 23:11:21 +03:00
|
|
|
/* unlink the control from the form */
|
2015-06-03 22:24:32 +03:00
|
|
|
if (control->form != NULL) {
|
|
|
|
for (c = control->form->controls; c != NULL; c = c->next) {
|
|
|
|
if (c->next == control) {
|
|
|
|
c->next = control->next;
|
|
|
|
if (control->form->last_control == control)
|
|
|
|
control->form->last_control = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c == control) {
|
|
|
|
/* can only happen if control was first control */
|
|
|
|
control->form->controls = control->next;
|
|
|
|
if (control->form->last_control == control)
|
|
|
|
control->form->controls =
|
|
|
|
control->form->last_control = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2015-04-22 23:11:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 00:07:42 +03:00
|
|
|
if (control->node_value != NULL) {
|
|
|
|
dom_string_unref(control->node_value);
|
|
|
|
}
|
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
free(control);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
/**
|
|
|
|
* Add an option to a form select control.
|
|
|
|
*
|
|
|
|
* \param control form control of type GADGET_SELECT
|
|
|
|
* \param value value of option, used directly (not copied)
|
|
|
|
* \param text text for option, used directly (not copied)
|
|
|
|
* \param selected this option is selected
|
2014-01-04 21:06:10 +04:00
|
|
|
* \param node the DOM node this option is associated with
|
2004-12-13 00:51:01 +03:00
|
|
|
* \return true on success, false on memory exhaustion
|
|
|
|
*/
|
|
|
|
bool form_add_option(struct form_control *control, char *value, char *text,
|
2014-01-04 21:06:10 +04:00
|
|
|
bool selected, void *node)
|
2004-12-13 00:51:01 +03:00
|
|
|
{
|
|
|
|
struct form_option *option;
|
|
|
|
|
|
|
|
assert(control);
|
|
|
|
assert(control->type == GADGET_SELECT);
|
|
|
|
|
2009-02-20 14:39:25 +03:00
|
|
|
option = calloc(1, sizeof *option);
|
2004-12-13 00:51:01 +03:00
|
|
|
if (!option)
|
|
|
|
return false;
|
2009-02-20 14:39:25 +03:00
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
option->value = value;
|
|
|
|
option->text = text;
|
|
|
|
|
|
|
|
/* add to linked list */
|
|
|
|
if (control->data.select.items == 0)
|
|
|
|
control->data.select.items = option;
|
|
|
|
else
|
|
|
|
control->data.select.last_item->next = option;
|
|
|
|
control->data.select.last_item = option;
|
|
|
|
|
|
|
|
/* set selected */
|
|
|
|
if (selected && (control->data.select.num_selected == 0 ||
|
|
|
|
control->data.select.multiple)) {
|
|
|
|
option->selected = option->initial_selected = true;
|
|
|
|
control->data.select.num_selected++;
|
|
|
|
control->data.select.current = option;
|
|
|
|
}
|
|
|
|
|
|
|
|
control->data.select.num_items++;
|
|
|
|
|
2014-01-04 21:06:10 +04:00
|
|
|
option->node = node;
|
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/** string allocation size for numeric values in multipart data */
|
|
|
|
#define FETCH_DATA_INT_VALUE_SIZE 20
|
2004-12-13 00:51:01 +03:00
|
|
|
|
2018-09-26 19:14:25 +03:00
|
|
|
/**
|
2018-09-29 18:31:09 +03:00
|
|
|
* append split key name and integer value to a multipart data list
|
2018-09-26 19:14:25 +03:00
|
|
|
*
|
2018-09-29 18:31:09 +03:00
|
|
|
* \param name key name
|
|
|
|
* \param ksfx key name suffix
|
|
|
|
* \param value The value to encode
|
|
|
|
* \param fetch_data_next_ptr The multipart data list to append to.
|
2018-09-26 19:14:25 +03:00
|
|
|
*/
|
|
|
|
static nserror
|
2018-09-29 18:31:09 +03:00
|
|
|
fetch_data_list_add_sname(const char *name,
|
|
|
|
const char *ksfx,
|
|
|
|
int value,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
2014-01-05 19:50:23 +04:00
|
|
|
{
|
2018-09-29 18:31:09 +03:00
|
|
|
struct fetch_multipart_data *fetch_data;
|
|
|
|
int keysize;
|
2014-01-05 19:50:23 +04:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
fetch_data = calloc(1, sizeof(*fetch_data));
|
|
|
|
if (fetch_data == NULL) {
|
|
|
|
NSLOG(netsurf, INFO, "failed allocation for fetch data");
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* key name */
|
|
|
|
keysize = snprintf(fetch_data->name, 0, "%s%s", name, ksfx);
|
|
|
|
fetch_data->name = malloc(keysize + 1); /* allow for null */
|
|
|
|
if (fetch_data->name == NULL) {
|
|
|
|
free(fetch_data);
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"keyname allocation failure for %s%s", name, ksfx);
|
2018-09-26 19:14:25 +03:00
|
|
|
return NSERROR_NOMEM;
|
2014-01-05 19:50:23 +04:00
|
|
|
}
|
2018-09-29 18:31:09 +03:00
|
|
|
snprintf(fetch_data->name, keysize + 1, "%s%s", name, ksfx);
|
|
|
|
|
|
|
|
/* value */
|
|
|
|
fetch_data->value = malloc(FETCH_DATA_INT_VALUE_SIZE);
|
|
|
|
if (fetch_data->value == NULL) {
|
|
|
|
free(fetch_data->name);
|
|
|
|
free(fetch_data);
|
|
|
|
NSLOG(netsurf, INFO, "value allocation failure");
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
snprintf(fetch_data->value, FETCH_DATA_INT_VALUE_SIZE, "%d", value);
|
2014-01-05 19:50:23 +04:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* link into list */
|
|
|
|
**fetch_data_next_ptr = fetch_data;
|
|
|
|
*fetch_data_next_ptr = &fetch_data->next;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/**
|
|
|
|
* append DOM string name/value pair to a multipart data list
|
|
|
|
*
|
|
|
|
* \param name key name
|
|
|
|
* \param value the value to associate with the key
|
|
|
|
* \param rawfile the raw file value to associate with the key.
|
|
|
|
* \param form_charset The form character set
|
|
|
|
* \param docu_charset The document character set for fallback
|
|
|
|
* \param fetch_data_next_ptr The multipart data list being constructed.
|
|
|
|
* \return NSERROR_OK on success or appropriate error code.
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
fetch_data_list_add(dom_string *name,
|
|
|
|
dom_string *value,
|
|
|
|
const char *rawfile,
|
|
|
|
const char *form_charset,
|
|
|
|
const char *docu_charset,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
struct fetch_multipart_data *fetch_data;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
assert(name != NULL);
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
fetch_data = calloc(1, sizeof(*fetch_data));
|
|
|
|
if (fetch_data == NULL) {
|
|
|
|
NSLOG(netsurf, INFO, "failed allocation for fetch data");
|
|
|
|
return NSERROR_NOMEM;
|
2014-01-05 19:50:23 +04:00
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
fetch_data->name = form_encode_item(dom_string_data(name),
|
|
|
|
dom_string_byte_length(name),
|
|
|
|
form_charset,
|
|
|
|
docu_charset);
|
|
|
|
if (fetch_data->name == NULL) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not encode name for fetch data");
|
|
|
|
free(fetch_data);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value == NULL) {
|
|
|
|
fetch_data->value = strdup("");
|
|
|
|
} else {
|
|
|
|
fetch_data->value = form_encode_item(dom_string_data(value),
|
|
|
|
dom_string_byte_length(value),
|
|
|
|
form_charset,
|
|
|
|
docu_charset);
|
|
|
|
}
|
|
|
|
if (fetch_data->value == NULL) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not encode value for fetch data");
|
|
|
|
free(fetch_data->name);
|
|
|
|
free(fetch_data);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* deal with raw file name */
|
|
|
|
if (rawfile != NULL) {
|
|
|
|
fetch_data->file = true;
|
|
|
|
fetch_data->rawfile = strdup(rawfile);
|
|
|
|
if (fetch_data->rawfile == NULL) {
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO,
|
2018-09-29 18:31:09 +03:00
|
|
|
"Could not encode rawfile value for fetch data");
|
|
|
|
free(fetch_data->value);
|
|
|
|
free(fetch_data->name);
|
|
|
|
free(fetch_data);
|
|
|
|
return NSERROR_NOMEM;
|
2014-01-05 19:50:23 +04:00
|
|
|
}
|
2018-09-29 18:31:09 +03:00
|
|
|
}
|
2014-11-15 21:23:44 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* link into list */
|
|
|
|
**fetch_data_next_ptr = fetch_data;
|
|
|
|
*fetch_data_next_ptr = &fetch_data->next;
|
2014-11-15 21:23:44 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/**
|
|
|
|
* process form HTMLTextAreaElement into multipart data.
|
|
|
|
*
|
|
|
|
* \param text_area_element The form select DOM element to convert.
|
|
|
|
* \param form_charset The form character set
|
|
|
|
* \param doc_charset The document character set for fallback
|
|
|
|
* \param fetch_data_next_ptr The multipart data list being constructed.
|
|
|
|
* \return NSERROR_OK on success or appropriate error code.
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data_textarea(dom_html_text_area_element *text_area_element,
|
|
|
|
const char *form_charset,
|
|
|
|
const char *doc_charset,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
bool element_disabled;
|
|
|
|
dom_string *inputname;
|
|
|
|
dom_string *inputvalue;
|
|
|
|
nserror res;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* check if element is disabled */
|
|
|
|
exp = dom_html_text_area_element_get_disabled(text_area_element,
|
|
|
|
&element_disabled);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get text area disabled property. exp %d", exp);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
if (element_disabled) {
|
|
|
|
/* allow enumeration to continue after disabled element */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* obtain name property */
|
|
|
|
exp = dom_html_text_area_element_get_name(text_area_element,
|
|
|
|
&inputname);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get text area name property. exp %d", exp);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
2015-03-10 04:15:20 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
if (inputname == NULL) {
|
|
|
|
/* allow enumeration to continue after element with no name */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2014-11-15 21:23:44 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* obtain text area value */
|
|
|
|
exp = dom_html_text_area_element_get_value(text_area_element,
|
|
|
|
&inputvalue);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get text area content. exp %d", exp);
|
|
|
|
dom_string_unref(inputname);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* add key/value pair to fetch data list */
|
|
|
|
res = fetch_data_list_add(inputname,
|
|
|
|
inputvalue,
|
|
|
|
NULL,
|
|
|
|
form_charset,
|
|
|
|
doc_charset,
|
|
|
|
fetch_data_next_ptr);
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
dom_string_unref(inputvalue);
|
|
|
|
dom_string_unref(inputname);
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data_select_option(dom_html_option_element *option_element,
|
|
|
|
dom_string *keyname,
|
|
|
|
const char *form_charset,
|
|
|
|
const char *docu_charset,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
nserror res;
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
dom_string *value;
|
|
|
|
bool selected;
|
|
|
|
|
|
|
|
exp = dom_html_option_element_get_selected(option_element, &selected);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get option selected property");
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!selected) {
|
|
|
|
/* unselected options do not add fetch data entries */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = dom_html_option_element_get_value(option_element, &value);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get option value");
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add key/value pair to fetch data list */
|
|
|
|
res = fetch_data_list_add(keyname,
|
|
|
|
value,
|
|
|
|
NULL,
|
|
|
|
form_charset,
|
|
|
|
docu_charset,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
dom_string_unref(value);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* process form HTMLSelectElement into multipart data.
|
|
|
|
*
|
|
|
|
* \param select_element The form select DOM element to convert.
|
|
|
|
* \param form_charset The form character set
|
|
|
|
* \param doc_charset The document character set for fallback
|
|
|
|
* \param fetch_data_next_ptr The multipart data list being constructed.
|
|
|
|
* \return NSERROR_OK on success or appropriate error code.
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data_select(dom_html_select_element *select_element,
|
|
|
|
const char *form_charset,
|
|
|
|
const char *doc_charset,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
nserror res = NSERROR_OK;
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
bool element_disabled;
|
|
|
|
dom_string *inputname;
|
|
|
|
dom_html_options_collection *options = NULL;
|
|
|
|
uint32_t options_count;
|
|
|
|
uint32_t option_index;
|
|
|
|
dom_node *option_element = NULL;
|
|
|
|
|
|
|
|
/* check if element is disabled */
|
|
|
|
exp = dom_html_select_element_get_disabled(select_element,
|
|
|
|
&element_disabled);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get select disabled property. exp %d", exp);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element_disabled) {
|
|
|
|
/* allow enumeration to continue after disabled element */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* obtain name property */
|
|
|
|
exp = dom_html_select_element_get_name(select_element, &inputname);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get select name property. exp %d", exp);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputname == NULL) {
|
|
|
|
/* allow enumeration to continue after element with no name */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get options collection */
|
|
|
|
exp = dom_html_select_element_get_options(select_element, &options);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get select options collection");
|
|
|
|
dom_string_unref(inputname);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get options collection length */
|
|
|
|
exp = dom_html_options_collection_get_length(options, &options_count);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get select options collection length");
|
|
|
|
dom_html_options_collection_unref(options);
|
|
|
|
dom_string_unref(inputname);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate over options collection */
|
|
|
|
for (option_index = 0; option_index < options_count; ++option_index) {
|
|
|
|
exp = dom_html_options_collection_item(options,
|
|
|
|
option_index,
|
|
|
|
&option_element);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO,
|
2018-09-29 18:31:09 +03:00
|
|
|
"Could not get options item %d", option_index);
|
|
|
|
res = NSERROR_DOM;
|
|
|
|
} else {
|
|
|
|
res = form_dom_to_data_select_option(
|
|
|
|
(dom_html_option_element *)option_element,
|
|
|
|
inputname,
|
|
|
|
form_charset,
|
|
|
|
doc_charset,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
dom_node_unref(option_element);
|
2014-01-05 19:50:23 +04:00
|
|
|
}
|
2018-09-29 18:31:09 +03:00
|
|
|
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
break;
|
2014-01-05 19:50:23 +04:00
|
|
|
}
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
dom_html_options_collection_unref(options);
|
|
|
|
dom_string_unref(inputname);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data_input_submit(dom_html_input_element *input_element,
|
|
|
|
dom_string *inputname,
|
|
|
|
const char *charset,
|
|
|
|
const char *document_charset,
|
|
|
|
dom_html_element **submit_button,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
dom_string *inputvalue;
|
|
|
|
nserror res;
|
2014-11-15 21:23:44 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
if (*submit_button == NULL) {
|
|
|
|
/* caller specified no button so use this one */
|
|
|
|
*submit_button = (dom_html_element *)input_element;
|
|
|
|
} else if (*submit_button != (dom_html_element *)input_element) {
|
|
|
|
return NSERROR_OK;
|
2014-01-05 19:50:23 +04:00
|
|
|
}
|
2014-11-15 21:23:44 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* matched button used to submit form */
|
|
|
|
exp = dom_html_input_element_get_value(input_element, &inputvalue);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get submit button value");
|
|
|
|
return NSERROR_DOM;
|
2014-01-05 19:50:23 +04:00
|
|
|
}
|
2014-11-15 21:23:44 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* add key/value pair to fetch data list */
|
|
|
|
res = fetch_data_list_add(inputname,
|
|
|
|
inputvalue,
|
|
|
|
NULL,
|
|
|
|
charset,
|
|
|
|
document_charset,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
dom_string_unref(inputvalue);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data_input_image(dom_html_input_element *input_element,
|
|
|
|
dom_string *inputname,
|
|
|
|
const char *charset,
|
|
|
|
const char *document_charset,
|
|
|
|
dom_html_element **submit_button,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
nserror res;
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
struct image_input_coords *coords;
|
|
|
|
char *basename;
|
|
|
|
|
|
|
|
/* Only use an image input if it was the thing which activated us */
|
|
|
|
if (*submit_button != (dom_html_element *)input_element) {
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = dom_node_get_user_data((dom_node *)input_element,
|
|
|
|
corestring_dom___ns_key_image_coords_node_data,
|
|
|
|
&coords);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get image XY data");
|
|
|
|
return NSERROR_DOM;
|
2014-11-15 21:23:44 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
if (coords == NULL) {
|
|
|
|
NSLOG(netsurf, INFO, "No XY data on the image input");
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* encode input name once */
|
|
|
|
basename = form_encode_item(dom_string_data(inputname),
|
|
|
|
dom_string_byte_length(inputname),
|
|
|
|
charset,
|
|
|
|
document_charset);
|
|
|
|
if (basename == NULL) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not encode basename");
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = fetch_data_list_add_sname(basename, ".x",
|
|
|
|
coords->x,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
if (res == NSERROR_OK) {
|
|
|
|
res = fetch_data_list_add_sname(basename, ".y",
|
|
|
|
coords->y,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(basename);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data_input_checkbox(dom_html_input_element *input_element,
|
|
|
|
dom_string *inputname,
|
|
|
|
const char *charset,
|
|
|
|
const char *document_charset,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
nserror res;
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
bool checked;
|
|
|
|
dom_string *inputvalue;
|
|
|
|
|
|
|
|
exp = dom_html_input_element_get_checked(input_element, &checked);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get input element checked");
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!checked) {
|
|
|
|
/* unchecked items do not generate a data entry */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = dom_html_input_element_get_value(input_element, &inputvalue);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get input element value");
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ensure a default value */
|
|
|
|
if (inputvalue == NULL) {
|
|
|
|
inputvalue = dom_string_ref(corestring_dom_on);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add key/value pair to fetch data list */
|
|
|
|
res = fetch_data_list_add(inputname,
|
|
|
|
inputvalue,
|
|
|
|
NULL,
|
|
|
|
charset,
|
|
|
|
document_charset,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
dom_string_unref(inputvalue);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data_input_file(dom_html_input_element *input_element,
|
|
|
|
dom_string *inputname,
|
|
|
|
const char *charset,
|
|
|
|
const char *document_charset,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
nserror res;
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
dom_string *inputvalue;
|
|
|
|
const char *rawfile = NULL;
|
|
|
|
|
|
|
|
exp = dom_html_input_element_get_value(input_element, &inputvalue);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get file value");
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = dom_node_get_user_data((dom_node *)input_element,
|
|
|
|
corestring_dom___ns_key_file_name_node_data,
|
|
|
|
&rawfile);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get file rawname");
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rawfile == NULL) {
|
|
|
|
rawfile = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add key/value pair to fetch data list */
|
|
|
|
res = fetch_data_list_add(inputname,
|
|
|
|
inputvalue,
|
|
|
|
rawfile,
|
|
|
|
charset,
|
|
|
|
document_charset,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
dom_string_unref(inputvalue);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data_input_text(dom_html_input_element *input_element,
|
|
|
|
dom_string *inputname,
|
|
|
|
const char *charset,
|
|
|
|
const char *document_charset,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
nserror res;
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
dom_string *inputvalue;
|
|
|
|
|
|
|
|
exp = dom_html_input_element_get_value(input_element, &inputvalue);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get input value");
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add key/value pair to fetch data list */
|
|
|
|
res = fetch_data_list_add(inputname,
|
|
|
|
inputvalue,
|
|
|
|
NULL,
|
|
|
|
charset,
|
|
|
|
document_charset,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
dom_string_unref(inputvalue);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* process form input element into multipart data.
|
|
|
|
*
|
|
|
|
* \param input_element The form input DOM element to convert.
|
|
|
|
* \param charset The form character set
|
|
|
|
* \param document_charset The document character set for fallback
|
|
|
|
* \param submit_button The DOM element of the button submitting the form
|
|
|
|
* \param had_submit A boolean value indicating if the submit button
|
|
|
|
* has already been processed in the form element enumeration.
|
|
|
|
* \param fetch_data_next_ptr The multipart data list being constructed.
|
|
|
|
* \return NSERROR_OK on success or appropriate error code.
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data_input(dom_html_input_element *input_element,
|
|
|
|
const char *charset,
|
|
|
|
const char *document_charset,
|
|
|
|
dom_html_element **submit_button,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
bool element_disabled;
|
|
|
|
dom_string *inputname;
|
|
|
|
dom_string *inputtype;
|
|
|
|
nserror res;
|
|
|
|
|
|
|
|
/* check if element is disabled */
|
|
|
|
exp = dom_html_input_element_get_disabled(input_element,
|
|
|
|
&element_disabled);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get input disabled property. exp %d", exp);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element_disabled) {
|
|
|
|
/* disabled element requires no more processing */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* obtain name property */
|
|
|
|
exp = dom_html_input_element_get_name(input_element, &inputname);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get input name property. exp %d", exp);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputname == NULL) {
|
|
|
|
/* element with no name is not converted */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get input type */
|
|
|
|
exp = dom_html_input_element_get_type(input_element, &inputtype);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get input element type");
|
2014-01-05 19:50:23 +04:00
|
|
|
dom_string_unref(inputname);
|
2018-09-29 18:31:09 +03:00
|
|
|
return NSERROR_DOM;
|
2014-11-15 21:23:44 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* process according to input element type */
|
|
|
|
if (dom_string_caseless_isequal(inputtype, corestring_dom_submit)) {
|
|
|
|
|
|
|
|
res = form_dom_to_data_input_submit(input_element,
|
|
|
|
inputname,
|
|
|
|
charset,
|
|
|
|
document_charset,
|
|
|
|
submit_button,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
} else if (dom_string_caseless_isequal(inputtype,
|
|
|
|
corestring_dom_image)) {
|
|
|
|
|
|
|
|
res = form_dom_to_data_input_image(input_element,
|
|
|
|
inputname,
|
|
|
|
charset,
|
|
|
|
document_charset,
|
|
|
|
submit_button,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
} else if (dom_string_caseless_isequal(inputtype,
|
|
|
|
corestring_dom_radio) ||
|
|
|
|
dom_string_caseless_isequal(inputtype,
|
|
|
|
corestring_dom_checkbox)) {
|
|
|
|
|
|
|
|
res = form_dom_to_data_input_checkbox(input_element,
|
|
|
|
inputname,
|
|
|
|
charset,
|
|
|
|
document_charset,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
} else if (dom_string_caseless_isequal(inputtype,
|
|
|
|
corestring_dom_file)) {
|
|
|
|
|
|
|
|
res = form_dom_to_data_input_file(input_element,
|
|
|
|
inputname,
|
|
|
|
charset,
|
|
|
|
document_charset,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
|
|
|
} else if (dom_string_caseless_isequal(inputtype,
|
|
|
|
corestring_dom_reset) ||
|
|
|
|
dom_string_caseless_isequal(inputtype,
|
|
|
|
corestring_dom_button)) {
|
|
|
|
/* Skip these */
|
|
|
|
NSLOG(netsurf, INFO, "Skipping RESET and BUTTON");
|
|
|
|
res = NSERROR_OK;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Everything else is treated as text values */
|
|
|
|
res = form_dom_to_data_input_text(input_element,
|
|
|
|
inputname,
|
|
|
|
charset,
|
|
|
|
document_charset,
|
|
|
|
fetch_data_next_ptr);
|
|
|
|
|
2014-11-15 21:23:44 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
dom_string_unref(inputtype);
|
|
|
|
dom_string_unref(inputname);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* process form HTMLButtonElement into multipart data.
|
|
|
|
*
|
2019-02-23 00:11:34 +03:00
|
|
|
* https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element
|
|
|
|
*
|
2018-09-29 18:31:09 +03:00
|
|
|
* \param button_element The form button DOM element to convert.
|
|
|
|
* \param form_charset The form character set
|
|
|
|
* \param doc_charset The document character set for fallback
|
|
|
|
* \param submit_button The DOM element of the button submitting the form
|
|
|
|
* \param fetch_data_next_ptr The multipart data list being constructed.
|
|
|
|
* \return NSERROR_OK on success or appropriate error code.
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data_button(dom_html_button_element *button_element,
|
|
|
|
const char *form_charset,
|
|
|
|
const char *doc_charset,
|
|
|
|
dom_html_element **submit_button,
|
|
|
|
struct fetch_multipart_data ***fetch_data_next_ptr)
|
|
|
|
{
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
bool element_disabled;
|
|
|
|
dom_string *inputname;
|
|
|
|
dom_string *inputvalue;
|
|
|
|
dom_string *inputtype;
|
|
|
|
nserror res = NSERROR_OK;
|
|
|
|
|
|
|
|
/* check if element is disabled */
|
|
|
|
exp = dom_html_button_element_get_disabled(button_element,
|
|
|
|
&element_disabled);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
2019-02-23 00:11:34 +03:00
|
|
|
"Unable to get disabled property. exp %d", exp);
|
2018-09-29 18:31:09 +03:00
|
|
|
return NSERROR_DOM;
|
2014-11-15 21:23:44 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
if (element_disabled) {
|
|
|
|
/* allow enumeration to continue after disabled element */
|
|
|
|
return NSERROR_OK;
|
2014-11-15 21:23:44 +03:00
|
|
|
}
|
|
|
|
|
2019-02-23 00:11:34 +03:00
|
|
|
/* get the type attribute */
|
2018-09-29 18:31:09 +03:00
|
|
|
exp = dom_html_button_element_get_type(button_element, &inputtype);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get button element type");
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
2019-02-23 00:11:34 +03:00
|
|
|
/* If the type attribute is "reset" or "button" the element is
|
|
|
|
* barred from constraint validation. Specification says
|
|
|
|
* default and invalid values result in submit which will
|
|
|
|
* be considered.
|
|
|
|
*/
|
|
|
|
if (dom_string_caseless_isequal(inputtype, corestring_dom_reset)) {
|
|
|
|
/* multipart data entry not required for reset type */
|
|
|
|
dom_string_unref(inputtype);
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
if (dom_string_caseless_isequal(inputtype, corestring_dom_button)) {
|
|
|
|
/* multipart data entry not required for button type */
|
2014-01-05 19:50:23 +04:00
|
|
|
dom_string_unref(inputtype);
|
2018-09-29 18:31:09 +03:00
|
|
|
return NSERROR_OK;
|
2014-11-15 21:23:44 +03:00
|
|
|
}
|
2018-09-29 18:31:09 +03:00
|
|
|
dom_string_unref(inputtype);
|
2014-11-15 21:23:44 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* only submision button generates an element */
|
|
|
|
if (*submit_button == NULL) {
|
|
|
|
/* no submission button selected yet so use this one */
|
|
|
|
*submit_button = (dom_html_element *)button_element;
|
|
|
|
}
|
|
|
|
if (*submit_button != (dom_html_element *)button_element) {
|
|
|
|
return NSERROR_OK;
|
2014-11-15 21:23:44 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* obtain name property */
|
|
|
|
exp = dom_html_button_element_get_name(button_element, &inputname);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Could not get button name property. exp %d", exp);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
if (inputname == NULL) {
|
|
|
|
/* allow enumeration to continue after element with no name */
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
/* get button value and add to fetch data list */
|
|
|
|
exp = dom_html_button_element_get_value(button_element, &inputvalue);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get submit button value");
|
|
|
|
res = NSERROR_DOM;
|
|
|
|
} else {
|
|
|
|
res = fetch_data_list_add(inputname,
|
|
|
|
inputvalue,
|
|
|
|
NULL,
|
|
|
|
form_charset,
|
|
|
|
doc_charset,
|
|
|
|
fetch_data_next_ptr);
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2014-01-05 19:50:23 +04:00
|
|
|
dom_string_unref(inputvalue);
|
2018-09-29 18:31:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dom_string_unref(inputname);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct multipart data list from 'successful' controls via the DOM.
|
|
|
|
*
|
|
|
|
* All text strings in the successful controls list will be in the charset most
|
|
|
|
* appropriate for submission. Therefore, no utf8_to_* processing should be
|
|
|
|
* performed upon them.
|
|
|
|
*
|
|
|
|
* \todo The chosen charset needs to be made available such that it can be
|
|
|
|
* included in the submission request (e.g. in the fetch's Content-Type header)
|
|
|
|
*
|
|
|
|
* See HTML 4.01 section 17.13.2.
|
|
|
|
*
|
|
|
|
* \note care is taken to abort even if the error is recoverable as it
|
|
|
|
* is not desirable to submit incomplete form data.
|
|
|
|
*
|
|
|
|
* \param[in] form form to search for successful controls
|
|
|
|
* \param[in] submit_button control used to submit the form, if any
|
|
|
|
* \param[out] fetch_data_out updated to point to linked list of
|
|
|
|
* fetch_multipart_data, NULL if no controls
|
|
|
|
* \return NSERROR_OK on success or appropriate error code
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
form_dom_to_data(struct form *form,
|
|
|
|
struct form_control *submit_control,
|
|
|
|
struct fetch_multipart_data **fetch_data_out)
|
|
|
|
{
|
|
|
|
nserror res = NSERROR_OK;
|
|
|
|
char *charset; /* form characterset */
|
|
|
|
dom_exception exp; /* the result from DOM operations */
|
|
|
|
dom_html_collection *elements = NULL; /* the dom form elements */
|
|
|
|
uint32_t element_count; /* the number of elements in the DOM form */
|
|
|
|
uint32_t element_idx; /* the index of thr enumerated element */
|
|
|
|
dom_node *element = NULL; /* the DOM form element */
|
|
|
|
dom_string *nodename = NULL; /* the DOM node name of the element */
|
|
|
|
struct fetch_multipart_data *fetch_data = NULL; /* fetch data list */
|
|
|
|
struct fetch_multipart_data **fetch_data_next = &fetch_data;
|
|
|
|
dom_html_element *submit_button;
|
|
|
|
|
|
|
|
/* obtain the submit_button DOM node from the control */
|
|
|
|
if (submit_control != NULL) {
|
|
|
|
submit_button = submit_control->node;
|
|
|
|
} else {
|
|
|
|
submit_button = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \todo Replace this call with something DOMish */
|
|
|
|
charset = form_acceptable_charset(form);
|
|
|
|
if (charset == NULL) {
|
|
|
|
NSLOG(netsurf, INFO, "failed to find charset");
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* obtain the form elements and count */
|
|
|
|
exp = dom_html_form_element_get_elements(form->node, &elements);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get form elements");
|
|
|
|
free(charset);
|
|
|
|
return NSERROR_DOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = dom_html_collection_get_length(elements, &element_count);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO, "Could not get form element count");
|
|
|
|
res = NSERROR_DOM;
|
|
|
|
goto form_dom_to_data_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (element_idx = 0; element_idx < element_count; element_idx++) {
|
|
|
|
/* obtain a form element */
|
|
|
|
exp = dom_html_collection_item(elements, element_idx, &element);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"retrieving form element %d failed with %d",
|
|
|
|
element_idx, exp);
|
|
|
|
res = NSERROR_DOM;
|
|
|
|
goto form_dom_to_data_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* node name from element */
|
|
|
|
exp = dom_node_get_node_name(element, &nodename);
|
|
|
|
if (exp != DOM_NO_ERR) {
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"getting element node name %d failed with %d",
|
|
|
|
element_idx, exp);
|
|
|
|
dom_node_unref(element);
|
|
|
|
res = NSERROR_DOM;
|
|
|
|
goto form_dom_to_data_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dom_string_isequal(nodename, corestring_dom_TEXTAREA)) {
|
|
|
|
/* Form element is HTMLTextAreaElement */
|
|
|
|
res = form_dom_to_data_textarea(
|
|
|
|
(dom_html_text_area_element *)element,
|
|
|
|
charset,
|
|
|
|
form->document_charset,
|
|
|
|
&fetch_data_next);
|
|
|
|
|
|
|
|
} else if (dom_string_isequal(nodename, corestring_dom_SELECT)) {
|
|
|
|
/* Form element is HTMLSelectElement */
|
|
|
|
res = form_dom_to_data_select(
|
|
|
|
(dom_html_select_element *)element,
|
|
|
|
charset,
|
|
|
|
form->document_charset,
|
|
|
|
&fetch_data_next);
|
|
|
|
|
|
|
|
} else if (dom_string_isequal(nodename, corestring_dom_INPUT)) {
|
|
|
|
/* Form element is HTMLInputElement */
|
|
|
|
res = form_dom_to_data_input(
|
|
|
|
(dom_html_input_element *)element,
|
|
|
|
charset,
|
|
|
|
form->document_charset,
|
|
|
|
&submit_button,
|
|
|
|
&fetch_data_next);
|
|
|
|
|
|
|
|
} else if (dom_string_isequal(nodename, corestring_dom_BUTTON)) {
|
|
|
|
/* Form element is HTMLButtonElement */
|
|
|
|
res = form_dom_to_data_button(
|
|
|
|
(dom_html_button_element *)element,
|
|
|
|
charset,
|
|
|
|
form->document_charset,
|
|
|
|
&submit_button,
|
|
|
|
&fetch_data_next);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Form element is not handled */
|
|
|
|
NSLOG(netsurf, INFO,
|
|
|
|
"Unhandled element type: %*s",
|
|
|
|
(int)dom_string_byte_length(nodename),
|
|
|
|
dom_string_data(nodename));
|
|
|
|
res = NSERROR_DOM;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dom_string_unref(nodename);
|
|
|
|
dom_node_unref(element);
|
|
|
|
|
|
|
|
/* abort form element enumeration on error */
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
goto form_dom_to_data_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*fetch_data_out = fetch_data;
|
|
|
|
dom_html_collection_unref(elements);
|
|
|
|
free(charset);
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
|
|
|
|
form_dom_to_data_error:
|
|
|
|
fetch_multipart_data_destroy(fetch_data);
|
|
|
|
dom_html_collection_unref(elements);
|
|
|
|
free(charset);
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2018-09-29 18:31:09 +03:00
|
|
|
return res;
|
2014-01-05 19:50:23 +04:00
|
|
|
}
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
/**
|
|
|
|
* Encode controls using application/x-www-form-urlencoded.
|
2004-08-08 23:13:40 +04:00
|
|
|
*
|
2018-09-26 19:14:25 +03:00
|
|
|
* \param[in] form form to which successful controls relate
|
|
|
|
* \param[in] control linked list of fetch_multipart_data
|
|
|
|
* \param[out] encoded_out URL-encoded form data
|
|
|
|
* \return NSERROR_OK on success and \a encoded_out updated else appropriate error code
|
2003-10-25 04:35:49 +04:00
|
|
|
*/
|
2018-09-26 19:14:25 +03:00
|
|
|
static nserror
|
|
|
|
form_url_encode(struct form *form,
|
2012-10-11 23:02:52 +04:00
|
|
|
struct fetch_multipart_data *control,
|
2018-09-26 19:14:25 +03:00
|
|
|
char **encoded_out)
|
2003-10-25 04:35:49 +04:00
|
|
|
{
|
2007-02-26 03:32:07 +03:00
|
|
|
char *name, *value;
|
2012-10-11 23:02:52 +04:00
|
|
|
char *s, *s2;
|
2012-10-16 21:32:14 +04:00
|
|
|
unsigned int len, len1, len_init;
|
2018-09-26 19:14:25 +03:00
|
|
|
nserror res;
|
2003-10-25 04:35:49 +04:00
|
|
|
|
2018-09-26 19:14:25 +03:00
|
|
|
s = malloc(1);
|
2012-10-11 23:02:52 +04:00
|
|
|
|
2018-09-26 19:14:25 +03:00
|
|
|
if (s == NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
2012-10-11 23:02:52 +04:00
|
|
|
}
|
2004-08-08 23:13:40 +04:00
|
|
|
|
2018-09-26 19:14:25 +03:00
|
|
|
s[0] = '\0';
|
|
|
|
len_init = len = 0;
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
for (; control; control = control->next) {
|
2018-09-26 19:14:25 +03:00
|
|
|
res = url_escape(control->name, true, NULL, &name);
|
|
|
|
if (res != NSERROR_OK) {
|
2005-06-27 02:18:37 +04:00
|
|
|
free(s);
|
2018-09-26 19:14:25 +03:00
|
|
|
return res;
|
2005-06-27 02:18:37 +04:00
|
|
|
}
|
|
|
|
|
2018-09-26 19:14:25 +03:00
|
|
|
res = url_escape(control->value, true, NULL, &value);
|
|
|
|
if (res != NSERROR_OK) {
|
2005-06-27 02:18:37 +04:00
|
|
|
free(name);
|
2005-04-16 09:09:33 +04:00
|
|
|
free(s);
|
2018-09-26 19:14:25 +03:00
|
|
|
return res;
|
2005-04-16 09:09:33 +04:00
|
|
|
}
|
2005-06-27 02:18:37 +04:00
|
|
|
|
2018-08-24 13:49:56 +03:00
|
|
|
/* resize string to allow for new key/value pair,
|
|
|
|
* equals, amphersand and terminator
|
|
|
|
*/
|
2003-10-25 04:35:49 +04:00
|
|
|
len1 = len + strlen(name) + strlen(value) + 2;
|
2004-08-08 23:13:40 +04:00
|
|
|
s2 = realloc(s, len1 + 1);
|
2018-09-26 19:14:25 +03:00
|
|
|
if (s2 == NULL) {
|
2005-06-27 02:18:37 +04:00
|
|
|
free(value);
|
|
|
|
free(name);
|
2004-08-08 23:13:40 +04:00
|
|
|
free(s);
|
2018-09-26 19:14:25 +03:00
|
|
|
return NSERROR_NOMEM;
|
2004-08-08 23:13:40 +04:00
|
|
|
}
|
|
|
|
s = s2;
|
2018-08-24 13:49:56 +03:00
|
|
|
|
|
|
|
snprintf(s + len, (len1 + 1) - len, "%s=%s&", name, value);
|
2003-10-25 04:35:49 +04:00
|
|
|
len = len1;
|
2005-06-27 02:18:37 +04:00
|
|
|
free(name);
|
|
|
|
free(value);
|
2003-10-25 04:35:49 +04:00
|
|
|
}
|
2005-06-26 05:55:20 +04:00
|
|
|
|
2014-11-15 21:23:44 +03:00
|
|
|
if (len > len_init) {
|
2012-10-16 21:32:14 +04:00
|
|
|
/* Replace trailing '&' */
|
2012-10-11 23:02:52 +04:00
|
|
|
s[len - 1] = '\0';
|
2014-11-15 21:23:44 +03:00
|
|
|
}
|
2018-09-26 19:14:25 +03:00
|
|
|
|
|
|
|
*encoded_out = s;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
2003-10-25 04:35:49 +04:00
|
|
|
}
|
|
|
|
|
2005-06-26 05:55:20 +04:00
|
|
|
/**
|
|
|
|
* Find an acceptable character set encoding with which to submit the form
|
|
|
|
*
|
|
|
|
* \param form The form
|
|
|
|
* \return Pointer to charset name (on heap, caller should free) or NULL
|
|
|
|
*/
|
|
|
|
char *form_acceptable_charset(struct form *form)
|
|
|
|
{
|
|
|
|
char *temp, *c;
|
|
|
|
|
|
|
|
if (!form->accept_charsets) {
|
|
|
|
/* no accept-charsets attribute for this form */
|
2018-09-26 19:14:25 +03:00
|
|
|
if (form->document_charset) {
|
2005-06-26 05:55:20 +04:00
|
|
|
/* document charset present, so use it */
|
|
|
|
return strdup(form->document_charset);
|
2018-09-26 19:14:25 +03:00
|
|
|
} else {
|
2005-06-26 05:55:20 +04:00
|
|
|
/* no document charset, so default to 8859-1 */
|
|
|
|
return strdup("ISO-8859-1");
|
2018-09-26 19:14:25 +03:00
|
|
|
}
|
2005-06-26 05:55:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make temporary copy of accept-charsets attribute */
|
|
|
|
temp = strdup(form->accept_charsets);
|
|
|
|
if (!temp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* make it upper case */
|
2016-09-13 00:43:39 +03:00
|
|
|
for (c = temp; *c; c++) {
|
|
|
|
*c = ascii_to_upper(*c);
|
|
|
|
}
|
2005-06-26 05:55:20 +04:00
|
|
|
|
|
|
|
/* is UTF-8 specified? */
|
|
|
|
c = strstr(temp, "UTF-8");
|
|
|
|
if (c) {
|
|
|
|
free(temp);
|
|
|
|
return strdup("UTF-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dispense with temporary copy */
|
|
|
|
free(temp);
|
|
|
|
|
|
|
|
/* according to RFC2070, the accept-charsets attribute of the
|
|
|
|
* form element contains a space and/or comma separated list */
|
|
|
|
c = form->accept_charsets;
|
|
|
|
|
2016-09-13 00:43:39 +03:00
|
|
|
/** \todo an improvement would be to choose an encoding
|
2005-06-26 05:55:20 +04:00
|
|
|
* acceptable to the server which covers as much of the input
|
2016-09-13 00:43:39 +03:00
|
|
|
* values as possible. Additionally, we need to handle the
|
|
|
|
* case where none of the acceptable encodings cover all the
|
|
|
|
* textual input values. For now, we just extract the first
|
|
|
|
* element of the charset list
|
2005-06-26 05:55:20 +04:00
|
|
|
*/
|
2016-09-13 00:43:39 +03:00
|
|
|
while (*c && !ascii_is_space(*c)) {
|
2005-06-26 05:55:20 +04:00
|
|
|
if (*c == ',')
|
|
|
|
break;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strndup(form->accept_charsets, c - form->accept_charsets);
|
|
|
|
}
|
2007-02-26 03:32:07 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a string from UTF-8 to the specified charset
|
|
|
|
* As a final fallback, this will attempt to convert to ISO-8859-1.
|
|
|
|
*
|
|
|
|
* \todo Return charset used?
|
|
|
|
*
|
|
|
|
* \param item String to convert
|
2014-01-05 19:50:23 +04:00
|
|
|
* \param len Length of string to convert
|
2007-02-26 03:32:07 +03:00
|
|
|
* \param charset Destination charset
|
|
|
|
* \param fallback Fallback charset (may be NULL),
|
|
|
|
* used iff converting to charset fails
|
|
|
|
* \return Pointer to converted string (on heap, caller frees), or NULL
|
|
|
|
*/
|
2018-09-29 18:31:09 +03:00
|
|
|
char *
|
|
|
|
form_encode_item(const char *item,
|
|
|
|
uint32_t len,
|
|
|
|
const char *charset,
|
|
|
|
const char *fallback)
|
2007-02-26 03:32:07 +03:00
|
|
|
{
|
2014-01-29 01:40:13 +04:00
|
|
|
nserror err;
|
2007-02-26 03:32:07 +03:00
|
|
|
char *ret = NULL;
|
2007-03-11 16:58:15 +03:00
|
|
|
char cset[256];
|
2007-02-26 03:32:07 +03:00
|
|
|
|
|
|
|
if (!item || !charset)
|
|
|
|
return NULL;
|
|
|
|
|
2007-03-11 16:58:15 +03:00
|
|
|
snprintf(cset, sizeof cset, "%s//TRANSLIT", charset);
|
2007-02-26 03:32:07 +03:00
|
|
|
|
2007-03-11 16:58:15 +03:00
|
|
|
err = utf8_to_enc(item, cset, 0, &ret);
|
2014-01-29 01:40:13 +04:00
|
|
|
if (err == NSERROR_BAD_ENCODING) {
|
2007-09-27 17:57:29 +04:00
|
|
|
/* charset not understood, try without transliteration */
|
|
|
|
snprintf(cset, sizeof cset, "%s", charset);
|
2014-01-05 19:50:23 +04:00
|
|
|
err = utf8_to_enc(item, cset, len, &ret);
|
2007-09-27 17:57:29 +04:00
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
if (err == NSERROR_BAD_ENCODING) {
|
2007-09-27 17:57:29 +04:00
|
|
|
/* nope, try fallback charset (if any) */
|
|
|
|
if (fallback) {
|
2018-05-10 13:34:26 +03:00
|
|
|
snprintf(cset, sizeof cset,
|
2007-09-27 17:57:29 +04:00
|
|
|
"%s//TRANSLIT", fallback);
|
|
|
|
err = utf8_to_enc(item, cset, 0, &ret);
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
if (err == NSERROR_BAD_ENCODING) {
|
2007-09-27 17:57:29 +04:00
|
|
|
/* and without transliteration */
|
|
|
|
snprintf(cset, sizeof cset,
|
|
|
|
"%s", fallback);
|
|
|
|
err = utf8_to_enc(item, cset, 0, &ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 01:40:13 +04:00
|
|
|
if (err == NSERROR_BAD_ENCODING) {
|
2007-09-27 17:57:29 +04:00
|
|
|
/* that also failed, use 8859-1 */
|
|
|
|
err = utf8_to_enc(item, "ISO-8859-1//TRANSLIT",
|
|
|
|
0, &ret);
|
2014-01-29 01:40:13 +04:00
|
|
|
if (err == NSERROR_BAD_ENCODING) {
|
2007-09-27 17:57:29 +04:00
|
|
|
/* and without transliteration */
|
|
|
|
err = utf8_to_enc(item, "ISO-8859-1",
|
|
|
|
0, &ret);
|
|
|
|
}
|
|
|
|
}
|
2007-03-11 16:58:15 +03:00
|
|
|
}
|
2007-02-26 03:32:07 +03:00
|
|
|
}
|
2014-01-29 01:40:13 +04:00
|
|
|
if (err == NSERROR_NOMEM) {
|
2007-02-26 03:32:07 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-09-27 17:57:29 +04:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
/* exported interface documented in html/form_internal.h */
|
2019-11-07 21:52:49 +03:00
|
|
|
nserror
|
|
|
|
form_open_select_menu(void *client_data,
|
|
|
|
struct form_control *control,
|
|
|
|
select_menu_redraw_callback callback,
|
|
|
|
struct content *c)
|
2009-08-14 14:37:33 +04:00
|
|
|
{
|
2011-07-27 01:30:38 +04:00
|
|
|
int line_height_with_spacing;
|
2009-08-14 14:37:33 +04:00
|
|
|
struct box *box;
|
|
|
|
plot_font_style_t fstyle;
|
|
|
|
int total_height;
|
|
|
|
struct form_select_menu *menu;
|
2018-01-04 02:58:18 +03:00
|
|
|
html_content *html = (html_content *)c;
|
2019-11-07 21:52:49 +03:00
|
|
|
nserror res;
|
2009-08-14 14:37:33 +04:00
|
|
|
|
|
|
|
/* if the menu is opened for the first time */
|
|
|
|
if (control->data.select.menu == NULL) {
|
2011-06-28 15:15:39 +04:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
menu = calloc(1, sizeof (struct form_select_menu));
|
|
|
|
if (menu == NULL) {
|
2019-11-07 21:52:49 +03:00
|
|
|
return NSERROR_NOMEM;
|
2009-08-14 14:37:33 +04:00
|
|
|
}
|
2011-06-28 15:15:39 +04:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
control->data.select.menu = menu;
|
2011-06-28 15:15:39 +04:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
box = control->box;
|
|
|
|
|
|
|
|
menu->width = box->width +
|
2019-11-07 21:52:49 +03:00
|
|
|
box->border[RIGHT].width + box->padding[RIGHT] +
|
|
|
|
box->border[LEFT].width + box->padding[LEFT];
|
2011-06-28 15:15:39 +04:00
|
|
|
|
2018-01-04 02:58:18 +03:00
|
|
|
font_plot_style_from_css(&html->len_ctx, control->box->style,
|
2009-08-14 14:37:33 +04:00
|
|
|
&fstyle);
|
|
|
|
menu->f_size = fstyle.size;
|
2011-06-28 15:15:39 +04:00
|
|
|
|
|
|
|
menu->line_height = FIXTOINT(FDIV((FMUL(FLTTOFIX(1.2),
|
|
|
|
FMUL(nscss_screen_dpi,
|
2018-05-23 13:48:35 +03:00
|
|
|
INTTOFIX(fstyle.size / PLOT_STYLE_SCALE)))),
|
2011-06-28 15:15:39 +04:00
|
|
|
F_72));
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
line_height_with_spacing = menu->line_height +
|
|
|
|
menu->line_height *
|
|
|
|
SELECT_LINE_SPACING;
|
2011-06-28 15:15:39 +04:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
total_height = control->data.select.num_items *
|
|
|
|
line_height_with_spacing;
|
|
|
|
menu->height = total_height;
|
2011-06-28 15:15:39 +04:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
if (menu->height > MAX_SELECT_HEIGHT) {
|
|
|
|
menu->height = MAX_SELECT_HEIGHT;
|
|
|
|
}
|
2019-11-07 21:52:49 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->client_data = client_data;
|
|
|
|
menu->callback = callback;
|
2019-11-07 21:52:49 +03:00
|
|
|
res = scrollbar_create(false,
|
|
|
|
menu->height,
|
|
|
|
total_height,
|
|
|
|
menu->height,
|
|
|
|
control,
|
|
|
|
form_select_menu_scroll_callback,
|
|
|
|
&(menu->scrollbar));
|
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
control->data.select.menu = NULL;
|
2009-08-14 14:37:33 +04:00
|
|
|
free(menu);
|
2019-11-07 21:52:49 +03:00
|
|
|
return res;
|
2009-08-14 14:37:33 +04:00
|
|
|
}
|
2011-06-28 15:15:39 +04:00
|
|
|
menu->c = c;
|
2019-11-07 21:52:49 +03:00
|
|
|
} else {
|
|
|
|
menu = control->data.select.menu;
|
2009-08-14 14:37:33 +04:00
|
|
|
}
|
2011-06-28 15:15:39 +04:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->callback(client_data, 0, 0, menu->width, menu->height);
|
2011-06-28 15:15:39 +04:00
|
|
|
|
2019-11-07 21:52:49 +03:00
|
|
|
return NSERROR_OK;
|
2009-08-14 14:37:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
/* exported interface documented in html/form_internal.h */
|
2009-08-14 14:37:33 +04:00
|
|
|
void form_free_select_menu(struct form_control *control)
|
|
|
|
{
|
2011-05-10 02:49:17 +04:00
|
|
|
if (control->data.select.menu->scrollbar != NULL)
|
|
|
|
scrollbar_destroy(control->data.select.menu->scrollbar);
|
2009-08-14 14:37:33 +04:00
|
|
|
free(control->data.select.menu);
|
|
|
|
control->data.select.menu = NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-12 14:45:07 +03:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
/* exported interface documented in html/form_internal.h */
|
2009-08-14 14:37:33 +04:00
|
|
|
bool form_redraw_select_menu(struct form_control *control, int x, int y,
|
2011-06-30 19:48:07 +04:00
|
|
|
float scale, const struct rect *clip,
|
|
|
|
const struct redraw_context *ctx)
|
2009-08-14 14:37:33 +04:00
|
|
|
{
|
|
|
|
struct box *box;
|
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
|
|
|
struct form_option *option;
|
|
|
|
int line_height, line_height_with_spacing;
|
|
|
|
int width, height;
|
|
|
|
int x0, y0, x1, scrollbar_x, y1, y2, y3;
|
|
|
|
int item_y;
|
|
|
|
int text_pos_offset, text_x;
|
|
|
|
int scrollbar_width = SCROLLBAR_WIDTH;
|
|
|
|
int i;
|
|
|
|
int scroll;
|
|
|
|
int x_cp, y_cp;
|
2011-02-15 01:05:39 +03:00
|
|
|
struct rect r;
|
2017-02-11 16:54:08 +03:00
|
|
|
struct rect rect;
|
|
|
|
nserror res;
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
box = control->box;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
x_cp = x;
|
|
|
|
y_cp = y;
|
|
|
|
width = menu->width;
|
|
|
|
height = menu->height;
|
|
|
|
line_height = menu->line_height;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
line_height_with_spacing = line_height +
|
|
|
|
line_height * SELECT_LINE_SPACING;
|
2011-05-10 02:49:17 +04:00
|
|
|
scroll = scrollbar_get_offset(menu->scrollbar);
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
if (scale != 1.0) {
|
|
|
|
x *= scale;
|
|
|
|
y *= scale;
|
|
|
|
width *= scale;
|
|
|
|
height *= scale;
|
|
|
|
scrollbar_width *= scale;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
i = scroll / line_height_with_spacing;
|
|
|
|
scroll -= i * line_height_with_spacing;
|
|
|
|
line_height *= scale;
|
|
|
|
line_height_with_spacing *= scale;
|
|
|
|
scroll *= scale;
|
|
|
|
scroll += i * line_height_with_spacing;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
x0 = x;
|
|
|
|
y0 = y;
|
|
|
|
x1 = x + width - 1;
|
|
|
|
y1 = y + height - 1;
|
|
|
|
scrollbar_x = x1 - scrollbar_width;
|
2011-02-15 01:05:39 +03:00
|
|
|
|
|
|
|
r.x0 = x0;
|
|
|
|
r.y0 = y0;
|
|
|
|
r.x1 = x1 + 1;
|
|
|
|
r.y1 = y1 + 1;
|
2017-02-11 16:54:08 +03:00
|
|
|
res = ctx->plot->clip(ctx, &r);
|
|
|
|
if (res != NSERROR_OK) {
|
2009-08-14 14:37:33 +04:00
|
|
|
return false;
|
2017-02-11 16:54:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rect.x0 = x0;
|
|
|
|
rect.y0 = y0;
|
|
|
|
rect.x1 = x1;
|
|
|
|
rect.y1 = y1;
|
|
|
|
res = ctx->plot->rectangle(ctx, plot_style_stroke_darkwbasec, &rect);
|
|
|
|
if (res != NSERROR_OK) {
|
2009-08-14 14:37:33 +04:00
|
|
|
return false;
|
2017-02-11 16:54:08 +03:00
|
|
|
}
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
x0 = x0 + SELECT_BORDER_WIDTH;
|
|
|
|
y0 = y0 + SELECT_BORDER_WIDTH;
|
|
|
|
x1 = x1 - SELECT_BORDER_WIDTH;
|
|
|
|
y1 = y1 - SELECT_BORDER_WIDTH;
|
|
|
|
height = height - 2 * SELECT_BORDER_WIDTH;
|
|
|
|
|
2011-02-15 01:05:39 +03:00
|
|
|
r.x0 = x0;
|
|
|
|
r.y0 = y0;
|
|
|
|
r.x1 = x1 + 1;
|
|
|
|
r.y1 = y1 + 1;
|
2017-02-11 16:54:08 +03:00
|
|
|
res = ctx->plot->clip(ctx, &r);
|
|
|
|
if (res != NSERROR_OK) {
|
2009-08-14 14:37:33 +04:00
|
|
|
return false;
|
2017-02-11 16:54:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
res = ctx->plot->rectangle(ctx, plot_style_fill_lightwbasec, &r);
|
|
|
|
if (res != NSERROR_OK) {
|
2009-08-14 14:37:33 +04:00
|
|
|
return false;
|
2017-02-11 16:54:08 +03:00
|
|
|
}
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
option = control->data.select.items;
|
|
|
|
item_y = line_height_with_spacing;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
while (item_y < scroll) {
|
|
|
|
option = option->next;
|
|
|
|
item_y += line_height_with_spacing;
|
|
|
|
}
|
|
|
|
item_y -= line_height_with_spacing;
|
|
|
|
text_pos_offset = y - scroll +
|
|
|
|
(int) (line_height * (0.75 + SELECT_LINE_SPACING));
|
|
|
|
text_x = x + (box->border[LEFT].width + box->padding[LEFT]) * scale;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
plot_fstyle_entry.size = menu->f_size;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
while (option && item_y - scroll < height) {
|
2017-02-11 16:54:08 +03:00
|
|
|
|
2018-05-10 13:34:26 +03:00
|
|
|
if (option->selected) {
|
|
|
|
y2 = y + item_y - scroll;
|
|
|
|
y3 = y + item_y + line_height_with_spacing - scroll;
|
2017-02-11 16:54:08 +03:00
|
|
|
|
|
|
|
rect.x0 = x0;
|
|
|
|
rect.y0 = y0 > y2 ? y0 : y2;
|
|
|
|
rect.x1 = scrollbar_x + 1;
|
|
|
|
rect.y1 = y3 < y1 + 1 ? y3 : y1 + 1;
|
|
|
|
res = ctx->plot->rectangle(ctx, &plot_style_fill_selected, &rect);
|
|
|
|
if (res != NSERROR_OK) {
|
2009-08-14 14:37:33 +04:00
|
|
|
return false;
|
2017-02-11 16:54:08 +03:00
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
}
|
2017-02-11 16:54:08 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
y2 = text_pos_offset + item_y;
|
2017-02-11 16:54:08 +03:00
|
|
|
res = ctx->plot->text(ctx,
|
|
|
|
&plot_fstyle_entry,
|
|
|
|
text_x, y2,
|
|
|
|
option->text, strlen(option->text));
|
|
|
|
if (res != NSERROR_OK) {
|
2009-08-14 14:37:33 +04:00
|
|
|
return false;
|
2017-02-11 16:54:08 +03:00
|
|
|
}
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
item_y += line_height_with_spacing;
|
|
|
|
option = option->next;
|
|
|
|
}
|
2017-02-11 16:54:08 +03:00
|
|
|
|
2017-03-02 02:30:29 +03:00
|
|
|
res = scrollbar_redraw(menu->scrollbar,
|
|
|
|
x_cp + menu->width - SCROLLBAR_WIDTH,
|
|
|
|
y_cp,
|
|
|
|
clip, scale, ctx);
|
|
|
|
if (res != NSERROR_OK) {
|
2009-08-14 14:37:33 +04:00
|
|
|
return false;
|
2017-03-02 02:30:29 +03:00
|
|
|
}
|
2017-02-11 16:54:08 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether a clipping rectangle is completely contained in the
|
|
|
|
* select menu.
|
|
|
|
*
|
2014-11-12 14:45:07 +03:00
|
|
|
* \param control the select menu to check the clipping rectangle for
|
|
|
|
* \param scale the current browser window scale
|
|
|
|
* \param clip the clipping rectangle
|
|
|
|
* \return true if inside false otherwise
|
2009-08-14 14:37:33 +04:00
|
|
|
*/
|
|
|
|
bool form_clip_inside_select_menu(struct form_control *control, float scale,
|
2011-02-15 01:05:39 +03:00
|
|
|
const struct rect *clip)
|
2009-08-14 14:37:33 +04:00
|
|
|
{
|
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
|
|
|
int width, height;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
|
|
|
|
width = menu->width;
|
|
|
|
height = menu->height;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
if (scale != 1.0) {
|
|
|
|
width *= scale;
|
|
|
|
height *= scale;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2011-02-15 01:05:39 +03:00
|
|
|
if (clip->x0 >= 0 && clip->x1 <= width &&
|
|
|
|
clip->y0 >= 0 && clip->y1 <= height)
|
2009-08-14 14:37:33 +04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-17 18:20:53 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a selection from a form select menu.
|
|
|
|
*
|
2014-11-12 01:58:34 +03:00
|
|
|
* \param html The html content handle for the form
|
2012-08-17 18:20:53 +04:00
|
|
|
* \param control form control with menu
|
|
|
|
* \param item index of item selected from the menu
|
2014-11-12 01:58:34 +03:00
|
|
|
* \return NSERROR_OK or appropriate error code.
|
2012-08-17 18:20:53 +04:00
|
|
|
*/
|
2014-11-12 01:58:34 +03:00
|
|
|
static nserror form__select_process_selection(html_content *html,
|
2012-08-17 18:20:53 +04:00
|
|
|
struct form_control *control, int item)
|
|
|
|
{
|
|
|
|
struct box *inline_box;
|
|
|
|
struct form_option *o;
|
|
|
|
int count;
|
2014-11-12 01:58:34 +03:00
|
|
|
nserror ret = NSERROR_OK;
|
2012-08-17 18:20:53 +04:00
|
|
|
|
|
|
|
assert(control != NULL);
|
|
|
|
assert(html != NULL);
|
|
|
|
|
|
|
|
/** \todo Even though the form code is effectively part of the html
|
2014-11-12 01:58:34 +03:00
|
|
|
* content handler, poking around inside contents is not good
|
|
|
|
*/
|
2012-08-17 18:20:53 +04:00
|
|
|
|
|
|
|
inline_box = control->box->children->children;
|
|
|
|
|
|
|
|
for (count = 0, o = control->data.select.items;
|
|
|
|
o != NULL;
|
|
|
|
count++, o = o->next) {
|
2014-01-24 18:21:27 +04:00
|
|
|
if (!control->data.select.multiple && o->selected) {
|
2012-08-17 18:20:53 +04:00
|
|
|
o->selected = false;
|
2014-01-21 19:01:18 +04:00
|
|
|
dom_html_option_element_set_selected(o->node, false);
|
2014-01-24 18:21:27 +04:00
|
|
|
}
|
2014-11-12 01:58:34 +03:00
|
|
|
|
2012-08-17 18:20:53 +04:00
|
|
|
if (count == item) {
|
|
|
|
if (control->data.select.multiple) {
|
|
|
|
if (o->selected) {
|
|
|
|
o->selected = false;
|
2014-01-21 19:01:18 +04:00
|
|
|
dom_html_option_element_set_selected(
|
|
|
|
o->node, false);
|
2012-08-17 18:20:53 +04:00
|
|
|
control->data.select.num_selected--;
|
|
|
|
} else {
|
|
|
|
o->selected = true;
|
2014-01-21 19:01:18 +04:00
|
|
|
dom_html_option_element_set_selected(
|
|
|
|
o->node, true);
|
2012-08-17 18:20:53 +04:00
|
|
|
control->data.select.num_selected++;
|
|
|
|
}
|
|
|
|
} else {
|
2014-01-21 19:01:18 +04:00
|
|
|
dom_html_option_element_set_selected(
|
|
|
|
o->node, true);
|
2012-08-17 18:20:53 +04:00
|
|
|
o->selected = true;
|
|
|
|
}
|
|
|
|
}
|
2014-11-12 01:58:34 +03:00
|
|
|
|
|
|
|
if (o->selected) {
|
2012-08-17 18:20:53 +04:00
|
|
|
control->data.select.current = o;
|
2014-11-12 01:58:34 +03:00
|
|
|
}
|
2012-08-17 18:20:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
talloc_free(inline_box->text);
|
|
|
|
inline_box->text = 0;
|
2014-11-12 01:58:34 +03:00
|
|
|
|
|
|
|
if (control->data.select.num_selected == 0) {
|
2012-10-05 17:41:40 +04:00
|
|
|
inline_box->text = talloc_strdup(html->bctx,
|
2012-08-17 18:20:53 +04:00
|
|
|
messages_get("Form_None"));
|
2014-11-12 01:58:34 +03:00
|
|
|
} else if (control->data.select.num_selected == 1) {
|
2012-10-05 17:41:40 +04:00
|
|
|
inline_box->text = talloc_strdup(html->bctx,
|
2012-08-17 18:20:53 +04:00
|
|
|
control->data.select.current->text);
|
2014-11-12 01:58:34 +03:00
|
|
|
} else {
|
2012-10-05 17:41:40 +04:00
|
|
|
inline_box->text = talloc_strdup(html->bctx,
|
2012-08-17 18:20:53 +04:00
|
|
|
messages_get("Form_Many"));
|
2014-11-12 01:58:34 +03:00
|
|
|
}
|
|
|
|
|
2012-08-17 18:20:53 +04:00
|
|
|
if (!inline_box->text) {
|
2014-11-12 01:58:34 +03:00
|
|
|
ret = NSERROR_NOMEM;
|
2012-08-17 18:20:53 +04:00
|
|
|
inline_box->length = 0;
|
2014-11-12 01:58:34 +03:00
|
|
|
} else {
|
2012-08-17 18:20:53 +04:00
|
|
|
inline_box->length = strlen(inline_box->text);
|
2014-11-12 01:58:34 +03:00
|
|
|
}
|
2012-08-17 18:20:53 +04:00
|
|
|
inline_box->width = control->box->width;
|
|
|
|
|
|
|
|
html__redraw_a_box(html, control->box);
|
|
|
|
|
2014-11-12 01:58:34 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2012-08-17 18:20:53 +04:00
|
|
|
|
2016-06-14 14:39:54 +03:00
|
|
|
/* exported interface documented in netsurf/form.h */
|
2014-11-12 01:58:34 +03:00
|
|
|
nserror form_select_process_selection(struct form_control *control, int item)
|
2012-08-17 18:20:53 +04:00
|
|
|
{
|
2014-02-08 21:11:55 +04:00
|
|
|
assert(control != NULL);
|
2012-08-17 18:20:53 +04:00
|
|
|
|
2014-11-12 01:58:34 +03:00
|
|
|
return form__select_process_selection(control->html, control, item);
|
2012-08-17 18:20:53 +04:00
|
|
|
}
|
|
|
|
|
2016-06-14 14:39:54 +03:00
|
|
|
/* exported interface documented in netsurf/form.h */
|
2014-11-14 00:52:08 +03:00
|
|
|
struct form_option *
|
|
|
|
form_select_get_option(struct form_control *control, int item)
|
|
|
|
{
|
|
|
|
struct form_option *opt;
|
|
|
|
|
|
|
|
opt = control->data.select.items;
|
|
|
|
while ((opt != NULL) && (item > 0)) {
|
|
|
|
opt = opt->next;
|
|
|
|
item--;
|
|
|
|
}
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
|
2016-06-14 14:39:54 +03:00
|
|
|
/* exported interface documented in netsurf/form.h */
|
2014-11-14 00:52:08 +03:00
|
|
|
char *form_control_get_name(struct form_control *control)
|
|
|
|
{
|
|
|
|
return control->name;
|
|
|
|
}
|
|
|
|
|
2016-06-14 14:39:54 +03:00
|
|
|
/* exported interface documented in netsurf/form.h */
|
2014-11-14 01:15:32 +03:00
|
|
|
nserror form_control_bounding_rect(struct form_control *control, struct rect *r)
|
|
|
|
{
|
|
|
|
box_bounds( control->box, r );
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
/**
|
|
|
|
* Handle a click on the area of the currently opened select menu.
|
2018-05-10 13:34:26 +03:00
|
|
|
*
|
2009-08-14 14:37:33 +04:00
|
|
|
* \param control the select menu which received the click
|
|
|
|
* \param x X coordinate of click
|
|
|
|
* \param y Y coordinate of click
|
|
|
|
*/
|
|
|
|
void form_select_menu_clicked(struct form_control *control, int x, int y)
|
2018-05-10 13:34:26 +03:00
|
|
|
{
|
2009-08-14 14:37:33 +04:00
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
2011-06-28 15:15:39 +04:00
|
|
|
struct form_option *option;
|
|
|
|
html_content *html = (html_content *)menu->c;
|
2018-05-10 13:34:26 +03:00
|
|
|
int line_height, line_height_with_spacing;
|
2009-08-14 14:37:33 +04:00
|
|
|
int item_bottom_y;
|
|
|
|
int scroll, i;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2011-05-10 02:49:17 +04:00
|
|
|
scroll = scrollbar_get_offset(menu->scrollbar);
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
line_height = menu->line_height;
|
|
|
|
line_height_with_spacing = line_height +
|
|
|
|
line_height * SELECT_LINE_SPACING;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
option = control->data.select.items;
|
|
|
|
item_bottom_y = line_height_with_spacing;
|
|
|
|
i = 0;
|
|
|
|
while (option && item_bottom_y < scroll + y) {
|
|
|
|
item_bottom_y += line_height_with_spacing;
|
|
|
|
option = option->next;
|
|
|
|
i++;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2011-06-28 15:15:39 +04:00
|
|
|
if (option != NULL) {
|
2012-08-17 18:20:53 +04:00
|
|
|
form__select_process_selection(html, control, i);
|
2011-06-28 15:15:39 +04:00
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->callback(menu->client_data, 0, 0, menu->width, menu->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle mouse action for the currently opened select menu.
|
|
|
|
*
|
|
|
|
* \param control the select menu which received the mouse action
|
|
|
|
* \param mouse current mouse state
|
|
|
|
* \param x X coordinate of click
|
|
|
|
* \param y Y coordinate of click
|
|
|
|
* \return text for the browser status bar or NULL if the menu has
|
2018-05-10 13:34:26 +03:00
|
|
|
* to be closed
|
2009-08-14 14:37:33 +04:00
|
|
|
*/
|
|
|
|
const char *form_select_mouse_action(struct form_control *control,
|
|
|
|
browser_mouse_state mouse, int x, int y)
|
|
|
|
{
|
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
|
|
|
int x0, y0, x1, y1, scrollbar_x;
|
|
|
|
const char *status = NULL;
|
|
|
|
bool multiple = control->data.select.multiple;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
x0 = 0;
|
|
|
|
y0 = 0;
|
|
|
|
x1 = menu->width;
|
|
|
|
y1 = menu->height;
|
|
|
|
scrollbar_x = x1 - SCROLLBAR_WIDTH;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
if (menu->scroll_capture ||
|
|
|
|
(x > scrollbar_x && x < x1 && y > y0 && y < y1)) {
|
|
|
|
/* The scroll is currently capturing all events or the mouse
|
|
|
|
* event is taking place on the scrollbar widget area
|
|
|
|
*/
|
|
|
|
x -= scrollbar_x;
|
2013-03-11 16:57:33 +04:00
|
|
|
return scrollbar_mouse_status_to_message(
|
|
|
|
scrollbar_mouse_action(menu->scrollbar,
|
|
|
|
mouse, x, y));
|
2009-08-14 14:37:33 +04:00
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
if (x > x0 && x < scrollbar_x && y > y0 && y < y1) {
|
|
|
|
/* over option area */
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
if (mouse & (BROWSER_MOUSE_CLICK_1 | BROWSER_MOUSE_CLICK_2))
|
|
|
|
/* button 1 or 2 click */
|
|
|
|
form_select_menu_clicked(control, x, y);
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
if (!(mouse & BROWSER_MOUSE_CLICK_1 && !multiple))
|
|
|
|
/* anything but a button 1 click over a single select
|
|
|
|
menu */
|
|
|
|
status = messages_get(control->data.select.multiple ?
|
|
|
|
"SelectMClick" : "SelectClick");
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
} else if (!(mouse & (BROWSER_MOUSE_CLICK_1 | BROWSER_MOUSE_CLICK_2)))
|
|
|
|
/* if not a button 1 or 2 click*/
|
|
|
|
status = messages_get("SelectClose");
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle mouse drag end for the currently opened select menu.
|
|
|
|
*
|
|
|
|
* \param control the select menu which received the mouse drag end
|
|
|
|
* \param mouse current mouse state
|
|
|
|
* \param x X coordinate of drag end
|
|
|
|
* \param y Y coordinate of drag end
|
|
|
|
*/
|
|
|
|
void form_select_mouse_drag_end(struct form_control *control,
|
|
|
|
browser_mouse_state mouse, int x, int y)
|
|
|
|
{
|
|
|
|
int x0, y0, x1, y1;
|
2010-06-04 13:35:08 +04:00
|
|
|
int box_x, box_y;
|
|
|
|
struct box *box;
|
2009-08-14 14:37:33 +04:00
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
2010-06-04 13:35:08 +04:00
|
|
|
|
|
|
|
box = control->box;
|
|
|
|
|
|
|
|
/* Get global coords of scrollbar */
|
|
|
|
box_coords(box, &box_x, &box_y);
|
|
|
|
box_x -= box->border[LEFT].width;
|
|
|
|
box_y += box->height + box->border[BOTTOM].width +
|
|
|
|
box->padding[BOTTOM] + box->padding[TOP];
|
|
|
|
|
|
|
|
/* Get drag end coords relative to scrollbar */
|
|
|
|
x = x - box_x;
|
|
|
|
y = y - box_y;
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
if (menu->scroll_capture) {
|
|
|
|
x -= menu->width - SCROLLBAR_WIDTH;
|
2011-05-10 02:49:17 +04:00
|
|
|
scrollbar_mouse_drag_end(menu->scrollbar, mouse, x, y);
|
2009-08-14 14:37:33 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
x0 = 0;
|
|
|
|
y0 = 0;
|
|
|
|
x1 = menu->width;
|
|
|
|
y1 = menu->height;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
if (x > x0 && x < x1 - SCROLLBAR_WIDTH && y > y0 && y < y1)
|
|
|
|
/* handle drag end above the option area like a regular click */
|
|
|
|
form_select_menu_clicked(control, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for the select menus scroll
|
|
|
|
*/
|
|
|
|
void form_select_menu_scroll_callback(void *client_data,
|
2011-05-10 02:49:17 +04:00
|
|
|
struct scrollbar_msg_data *scrollbar_data)
|
2009-08-14 14:37:33 +04:00
|
|
|
{
|
|
|
|
struct form_control *control = client_data;
|
|
|
|
struct form_select_menu *menu = control->data.select.menu;
|
2011-06-28 15:15:39 +04:00
|
|
|
html_content *html = (html_content *)menu->c;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2011-05-10 02:49:17 +04:00
|
|
|
switch (scrollbar_data->msg) {
|
|
|
|
case SCROLLBAR_MSG_MOVED:
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->callback(menu->client_data,
|
2018-05-10 13:34:26 +03:00
|
|
|
0, 0,
|
2012-08-16 16:13:25 +04:00
|
|
|
menu->width,
|
2018-05-10 13:34:26 +03:00
|
|
|
menu->height);
|
2009-08-14 14:37:33 +04:00
|
|
|
break;
|
2011-05-10 02:49:17 +04:00
|
|
|
case SCROLLBAR_MSG_SCROLL_START:
|
2012-01-11 02:02:19 +04:00
|
|
|
{
|
|
|
|
struct rect rect = {
|
|
|
|
.x0 = scrollbar_data->x0,
|
|
|
|
.y0 = scrollbar_data->y0,
|
|
|
|
.x1 = scrollbar_data->x1,
|
|
|
|
.y1 = scrollbar_data->y1
|
|
|
|
};
|
|
|
|
|
2012-01-11 18:20:26 +04:00
|
|
|
browser_window_set_drag_type(html->bw,
|
|
|
|
DRAGGING_CONTENT_SCROLLBAR, &rect);
|
2011-06-27 21:29:49 +04:00
|
|
|
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->scroll_capture = true;
|
2012-01-11 02:02:19 +04:00
|
|
|
}
|
2009-08-14 14:37:33 +04:00
|
|
|
break;
|
2011-05-10 02:49:17 +04:00
|
|
|
case SCROLLBAR_MSG_SCROLL_FINISHED:
|
2009-08-14 14:37:33 +04:00
|
|
|
menu->scroll_capture = false;
|
2012-01-11 18:20:26 +04:00
|
|
|
|
|
|
|
browser_window_set_drag_type(html->bw,
|
|
|
|
DRAGGING_NONE, NULL);
|
2009-08-14 14:37:33 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the dimensions of a select menu.
|
|
|
|
*
|
|
|
|
* \param control the select menu to get the dimensions of
|
|
|
|
* \param width gets updated to menu width
|
|
|
|
* \param height gets updated to menu height
|
|
|
|
*/
|
|
|
|
void form_select_get_dimensions(struct form_control *control,
|
|
|
|
int *width, int *height)
|
|
|
|
{
|
|
|
|
*width = control->data.select.menu->width;
|
|
|
|
*height = control->data.select.menu->height;
|
|
|
|
}
|
2010-06-04 13:35:08 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for the core select menu.
|
|
|
|
*/
|
|
|
|
void form_select_menu_callback(void *client_data,
|
|
|
|
int x, int y, int width, int height)
|
|
|
|
{
|
2011-06-28 15:15:39 +04:00
|
|
|
html_content *html = client_data;
|
2010-06-04 13:35:08 +04:00
|
|
|
int menu_x, menu_y;
|
|
|
|
struct box *box;
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2011-06-28 15:15:39 +04:00
|
|
|
box = html->visible_select_menu->box;
|
2010-06-04 13:35:08 +04:00
|
|
|
box_coords(box, &menu_x, &menu_y);
|
2018-05-10 13:34:26 +03:00
|
|
|
|
2010-06-04 13:35:08 +04:00
|
|
|
menu_x -= box->border[LEFT].width;
|
|
|
|
menu_y += box->height + box->border[BOTTOM].width +
|
|
|
|
box->padding[BOTTOM] +
|
|
|
|
box->padding[TOP];
|
2011-06-28 15:15:39 +04:00
|
|
|
content__request_redraw((struct content *)html, menu_x + x, menu_y + y,
|
2010-06-04 13:35:08 +04:00
|
|
|
width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a radio form control and clear the others in the group.
|
|
|
|
*
|
2014-11-12 14:45:07 +03:00
|
|
|
* \param radio form control of type GADGET_RADIO
|
2010-06-04 13:35:08 +04:00
|
|
|
*/
|
|
|
|
|
2014-02-08 21:11:55 +04:00
|
|
|
void form_radio_set(struct form_control *radio)
|
2010-06-04 13:35:08 +04:00
|
|
|
{
|
|
|
|
struct form_control *control;
|
|
|
|
|
|
|
|
assert(radio);
|
|
|
|
if (!radio->form)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (radio->selected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (control = radio->form->controls; control;
|
|
|
|
control = control->next) {
|
|
|
|
if (control->type != GADGET_RADIO)
|
|
|
|
continue;
|
|
|
|
if (control == radio)
|
|
|
|
continue;
|
|
|
|
if (strcmp(control->name, radio->name) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (control->selected) {
|
|
|
|
control->selected = false;
|
2014-01-04 21:06:10 +04:00
|
|
|
dom_html_input_element_set_checked(control->node, false);
|
2014-02-17 18:38:53 +04:00
|
|
|
html__redraw_a_box(radio->html, control->box);
|
2010-06-04 13:35:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
radio->selected = true;
|
2014-01-04 21:06:10 +04:00
|
|
|
dom_html_input_element_set_checked(radio->node, true);
|
2014-02-17 18:38:53 +04:00
|
|
|
html__redraw_a_box(radio->html, radio->box);
|
2010-06-04 13:35:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-26 19:14:25 +03:00
|
|
|
/* private interface described in html/form_internal.h */
|
|
|
|
nserror
|
|
|
|
form_submit(nsurl *page_url,
|
|
|
|
struct browser_window *target,
|
|
|
|
struct form *form,
|
|
|
|
struct form_control *submit_button)
|
2010-06-04 13:35:08 +04:00
|
|
|
{
|
2018-09-26 19:14:25 +03:00
|
|
|
nserror res;
|
|
|
|
char *data = NULL; /* encoded form data */
|
|
|
|
struct fetch_multipart_data *success = NULL; /* gcc is incapable of correctly reasoning about use and generates "maybe used uninitialised" warnings */
|
2013-02-14 19:09:28 +04:00
|
|
|
nsurl *action_url;
|
2018-09-26 19:14:25 +03:00
|
|
|
nsurl *query_url;
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2011-02-25 22:57:23 +03:00
|
|
|
assert(form != NULL);
|
2010-06-04 13:35:08 +04:00
|
|
|
|
2018-09-26 19:14:25 +03:00
|
|
|
/* obtain list of controls from DOM */
|
2018-09-29 18:31:09 +03:00
|
|
|
res = form_dom_to_data(form, submit_button, &success);
|
2018-09-26 19:14:25 +03:00
|
|
|
if (res != NSERROR_OK) {
|
|
|
|
return res;
|
2010-06-04 13:35:08 +04:00
|
|
|
}
|
|
|
|
|
2013-02-14 19:09:28 +04:00
|
|
|
/* Decompose action */
|
2018-09-26 19:14:25 +03:00
|
|
|
res = nsurl_create(form->action, &action_url);
|
|
|
|
if (res != NSERROR_OK) {
|
2013-02-14 19:09:28 +04:00
|
|
|
fetch_multipart_data_destroy(success);
|
2018-09-26 19:14:25 +03:00
|
|
|
return res;
|
2013-02-14 19:09:28 +04:00
|
|
|
}
|
|
|
|
|
2010-06-04 13:35:08 +04:00
|
|
|
switch (form->method) {
|
2011-02-25 22:57:23 +03:00
|
|
|
case method_GET:
|
2018-09-26 19:14:25 +03:00
|
|
|
res = form_url_encode(form, success, &data);
|
|
|
|
if (res == NSERROR_OK) {
|
|
|
|
/* Replace query segment */
|
|
|
|
res = nsurl_replace_query(action_url, data, &query_url);
|
|
|
|
if (res == NSERROR_OK) {
|
|
|
|
res = browser_window_navigate(target,
|
|
|
|
query_url,
|
|
|
|
page_url,
|
|
|
|
BW_NAVIGATE_HISTORY,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
nsurl_unref(query_url);
|
|
|
|
}
|
2011-02-25 22:57:23 +03:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case method_POST_URLENC:
|
2018-09-26 19:14:25 +03:00
|
|
|
res = form_url_encode(form, success, &data);
|
|
|
|
if (res == NSERROR_OK) {
|
|
|
|
res = browser_window_navigate(target,
|
|
|
|
action_url,
|
|
|
|
page_url,
|
|
|
|
BW_NAVIGATE_HISTORY,
|
|
|
|
data,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
free(data);
|
2011-02-25 22:57:23 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case method_POST_MULTIPART:
|
2018-09-26 19:14:25 +03:00
|
|
|
res = browser_window_navigate(target,
|
|
|
|
action_url,
|
|
|
|
page_url,
|
|
|
|
BW_NAVIGATE_HISTORY,
|
|
|
|
NULL,
|
|
|
|
success,
|
|
|
|
NULL);
|
2013-02-14 19:09:28 +04:00
|
|
|
|
2011-02-25 22:57:23 +03:00
|
|
|
break;
|
2010-06-04 13:35:08 +04:00
|
|
|
}
|
|
|
|
|
2013-02-14 19:09:28 +04:00
|
|
|
nsurl_unref(action_url);
|
2010-06-04 13:35:08 +04:00
|
|
|
fetch_multipart_data_destroy(success);
|
2018-09-26 19:14:25 +03:00
|
|
|
|
|
|
|
return res;
|
2010-06-04 13:35:08 +04:00
|
|
|
}
|
2014-01-04 20:30:09 +04:00
|
|
|
|
2014-02-08 21:11:55 +04:00
|
|
|
void form_gadget_update_value(struct form_control *control, char *value)
|
2014-01-04 20:30:09 +04:00
|
|
|
{
|
|
|
|
switch (control->type) {
|
|
|
|
case GADGET_HIDDEN:
|
|
|
|
case GADGET_TEXTBOX:
|
|
|
|
case GADGET_TEXTAREA:
|
|
|
|
case GADGET_PASSWORD:
|
|
|
|
case GADGET_FILE:
|
|
|
|
if (control->value != NULL) {
|
|
|
|
free(control->value);
|
|
|
|
}
|
|
|
|
control->value = value;
|
|
|
|
if (control->node != NULL) {
|
|
|
|
dom_exception err;
|
|
|
|
dom_string *str;
|
|
|
|
err = dom_string_create((uint8_t *)value,
|
|
|
|
strlen(value), &str);
|
|
|
|
if (err == DOM_NO_ERR) {
|
|
|
|
if (control->type == GADGET_TEXTAREA)
|
|
|
|
err = dom_html_text_area_element_set_value(
|
|
|
|
(dom_html_text_area_element *)(control->node),
|
|
|
|
str);
|
|
|
|
else
|
|
|
|
err = dom_html_input_element_set_value(
|
|
|
|
(dom_html_input_element *)(control->node),
|
|
|
|
str);
|
|
|
|
dom_string_unref(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Do nothing */
|
|
|
|
break;
|
|
|
|
}
|
2019-08-05 00:07:42 +03:00
|
|
|
|
|
|
|
/* Finally, sync this with the DOM */
|
|
|
|
form_gadget_sync_with_dom(control);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Exported API, see form_internal.h */
|
|
|
|
void
|
|
|
|
form_gadget_sync_with_dom(struct form_control *control)
|
|
|
|
{
|
|
|
|
dom_exception exc;
|
|
|
|
dom_string *value = NULL;
|
|
|
|
bool changed_dom = false;
|
|
|
|
|
|
|
|
if (control->syncing ||
|
|
|
|
(control->type != GADGET_TEXTBOX &&
|
|
|
|
control->type != GADGET_PASSWORD &&
|
|
|
|
control->type != GADGET_HIDDEN &&
|
|
|
|
control->type != GADGET_TEXTAREA)) {
|
|
|
|
/* Not a control we support, or the control is already
|
|
|
|
* mid-sync so we don't want to disrupt that
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
control->syncing = true;
|
|
|
|
|
|
|
|
/* If we've changed value, sync that toward the DOM */
|
2019-08-05 00:12:10 +03:00
|
|
|
if ((control->last_synced_value == NULL &&
|
|
|
|
control->value != NULL &&
|
|
|
|
control->value[0] != '\0') ||
|
|
|
|
(control->last_synced_value != NULL &&
|
|
|
|
control->value != NULL &&
|
|
|
|
strcmp(control->value, control->last_synced_value) != 0)) {
|
2019-08-05 00:07:42 +03:00
|
|
|
char *dup = strdup(control->value);
|
|
|
|
if (dup == NULL) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (control->last_synced_value != NULL) {
|
|
|
|
free(control->last_synced_value);
|
|
|
|
}
|
|
|
|
control->last_synced_value = dup;
|
|
|
|
exc = dom_string_create((uint8_t *)(control->value),
|
|
|
|
strlen(control->value), &value);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (control->node_value != NULL) {
|
|
|
|
dom_string_unref(control->node_value);
|
|
|
|
}
|
|
|
|
control->node_value = value;
|
|
|
|
value = NULL;
|
|
|
|
if (control->type == GADGET_TEXTAREA) {
|
|
|
|
exc = dom_html_text_area_element_set_value(control->node, control->node_value);
|
|
|
|
} else {
|
|
|
|
exc = dom_html_input_element_set_value(control->node, control->node_value);
|
|
|
|
}
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
changed_dom = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now check if the DOM has changed since our last go */
|
|
|
|
if (control->type == GADGET_TEXTAREA) {
|
|
|
|
exc = dom_html_text_area_element_get_value(control->node, &value);
|
|
|
|
} else {
|
|
|
|
exc = dom_html_input_element_get_value(control->node, &value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
/* Nothing much we can do here */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dom_string_isequal(control->node_value, value)) {
|
|
|
|
/* The DOM has changed */
|
|
|
|
if (!changed_dom) {
|
|
|
|
/* And it wasn't us */
|
|
|
|
char *value_s = strndup(
|
|
|
|
dom_string_data(value),
|
|
|
|
dom_string_byte_length(value));
|
|
|
|
char *dup = NULL;
|
|
|
|
if (value_s == NULL) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
dup = strdup(value_s);
|
|
|
|
if (dup == NULL) {
|
|
|
|
free(value_s);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
free(control->value);
|
|
|
|
control->value = value_s;
|
|
|
|
free(control->last_synced_value);
|
|
|
|
control->last_synced_value = dup;
|
|
|
|
if (control->type != GADGET_HIDDEN &&
|
|
|
|
control->data.text.ta != NULL) {
|
|
|
|
textarea_set_text(control->data.text.ta,
|
|
|
|
value_s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
control->node_value = value;
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (value != NULL)
|
|
|
|
dom_string_unref(value);
|
|
|
|
control->syncing = false;
|
2014-01-04 20:30:09 +04:00
|
|
|
}
|