summaryrefslogtreecommitdiff
path: root/render/form.c
blob: 6eee708b2c3ed0a0f8ba0a963fe11ff71d86dccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2003 James Bursa <bursa@users.sourceforge.net>
 * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
 */

/** \file
 * Form handling functions (implementation).
 */

#include <assert.h>
#include <string.h>
#include "curl/curl.h"
#include "netsurf/render/box.h"
#include "netsurf/render/form.h"
#include "netsurf/utils/utils.h"


static char *form_textarea_value(struct form_control *textarea);

/**
 * Add a control to the list of controls in a form.
 */

void form_add_control(struct form *form, struct form_control *control)
{
	control->form = form;
	if (form->controls) {
		assert(form->last_control);
		form->last_control->next = control;
		control->next = 0;
		form->last_control = control;
	} else {
		form->controls = form->last_control = control;
	}
}


/**
 * Identify 'successful' controls.
 *
 * See HTML 4.01 section 17.13.2.
 */

struct form_successful_control *form_successful_controls(struct form *form,
		struct form_control *submit_button)
{
	struct form_control *control;
	struct form_option *option;
	struct form_successful_control sentinel, *last_success;
	last_success = &sentinel;

	for (control = form->controls; control; control = control->next) {
		struct form_successful_control *success_new;

		/* ignore disabled controls */
		if (control->disabled)
			continue;

		/* ignore controls with no name */
		if (!control->name)
			continue;

		/* only the activated submit button is successful */
		if (control->type == GADGET_SUBMIT && control != submit_button)
			continue;

		/* ignore checkboxes and radio buttons which aren't selected */
		if (control->type == GADGET_CHECKBOX && !control->data.checkbox.selected)
			continue;
		if (control->type == GADGET_RADIO && !control->data.radio.selected)
			continue;

		/* select */
		if (control->type == GADGET_SELECT) {
			for (option = control->data.select.items; option;
					option = option->next) {
				if (option->selected) {
					success_new = xcalloc(1, sizeof(*success_new));
					success_new->name = xstrdup(control->name);
					success_new->value = xstrdup(option->value);
					success_new->next = 0;
					last_success->next = success_new;
					last_success = success_new;
				}
			}
			continue;
		}

		/* textarea */
		if (control->type == GADGET_TEXTAREA) {
			success_new = xcalloc(1, sizeof(*success_new));
			success_new->name = xstrdup(control->name);
			success_new->value = form_textarea_value(control);
			success_new->next = 0;
			last_success->next = success_new;
			last_success = success_new;
			continue;
		}

		/* image */
		if (control->type == GADGET_IMAGE) {
			unsigned int len = strlen(control->name) + 3;
			/* x */
			success_new = xcalloc(1, sizeof(*success_new));
			success_new->name = xcalloc(1, len);
			sprintf(success_new->name, "%s.x", control->name);
			success_new->value = xcalloc(1, 20);
			sprintf(success_new->value, "%i", control->data.image.mx);
			success_new->next = 0;
			last_success->next = success_new;
			last_success = success_new;
			/* y */
			success_new = xcalloc(1, sizeof(*success_new));
			success_new->name = xcalloc(1, len);
			sprintf(success_new->name, "%s.y", control->name);
			success_new->value = xcalloc(1, 20);
			sprintf(success_new->value, "%i", control->data.image.my);
			success_new->next = 0;
			last_success->next = success_new;
			last_success = success_new;
		}

		/* ignore reset */
		if (control->type == GADGET_RESET)
			continue;

		/* all others added if they have a value */
		if (control->value) {
			success_new = xcalloc(1, sizeof(*success_new));
			success_new->name = xstrdup(control->name);
			success_new->value = xstrdup(control->value);
			success_new->next = 0;
			last_success->next = success_new;
			last_success = success_new;
		}
	}

	return sentinel.next;
}


/**
 * Find the value for a textarea control.
 */

char *form_textarea_value(struct form_control *textarea)
{
	unsigned int len = 1;
	char *value, *s;
	struct box *inline_container, *text_box;

	/* find required length */
	for (inline_container = textarea->box->children;
			inline_container;
			inline_container = inline_container->next) {
		for (text_box = inline_container->children;
				text_box;
				text_box = text_box->next) {
			len += text_box->length + 1;
		}
		len += 2;
	}

	/* construct value */
	s = value = xcalloc(1, len);
	for (inline_container = textarea->box->children;
			inline_container;
			inline_container = inline_container->next) {
		for (text_box = inline_container->children;
				text_box;
				text_box = text_box->next) {
			strncpy(s, text_box->text, text_box->length);
			s += text_box->length;
			*s++ = ' ';
		}
		*s++ = '\r';
		*s++ = '\n';
	}
	*s = 0;

	return value;
}


/**
 * Encode controls using application/x-www-form-urlencoded.
 */

char *form_url_encode(struct form_successful_control *control)
{
	char *s = xcalloc(1, 0);
	unsigned int len = 0, len1;

	for (; control; control = control->next) {
		char *name = curl_escape(control->name, 0);
		char *value = curl_escape(control->value, 0);
		len1 = len + strlen(name) + strlen(value) + 2;
		s = xrealloc(s, len1 + 1);
		sprintf(s + len, "%s=%s&", name, value);
		len = len1;
                curl_free(name);
                curl_free(value);
	}
	if (len)
		s[len - 1] = 0;
	return s;
}


/**
 * Free a linked list of form_successful_control.
 */

void form_free_successful(struct form_successful_control *control)
{
	struct form_successful_control *next;
	for (; control; control = next) {
		next = control->next;
		free(control->name);
		free(control->value);
		free(control);
	}
}