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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use markup5ever::{LocalName, local_name};
use crate::{
BaseDocument, ElementData,
traversal::{AncestorTraverser, TreeTraverser},
};
use blitz_traits::{
navigation::NavigationOptions,
net::{Body, Entry, EntryValue, FormData, Method},
};
use core::str::FromStr;
use std::fmt::Display;
/// https://url.spec.whatwg.org/#default-encode-set
const DEFAULT_ENCODE_SET: percent_encoding::AsciiSet = percent_encoding::CONTROLS
// Query Set
.add(b' ')
.add(b'"')
.add(b'#')
.add(b'<')
.add(b'>')
// Path Set
.add(b'?')
.add(b'`')
.add(b'{')
.add(b'}');
impl BaseDocument {
/// Resets the form owner for a given node by either using an explicit form attribute
/// or finding the nearest ancestor form element
///
/// # Arguments
/// * `node_id` - The ID of the node whose form owner needs to be reset
///
/// <https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#reset-the-form-owner>
pub fn reset_form_owner(&mut self, node_id: usize) {
let node = &self.nodes[node_id];
let Some(element) = node.element_data() else {
return;
};
// First try explicit form attribute
let final_owner_id = element
.attr(local_name!("form"))
.and_then(|owner| self.nodes_to_id.get(owner))
.copied()
.filter(|owner_id| {
self.get_node(*owner_id)
.is_some_and(|node| node.data.is_element_with_tag_name(&local_name!("form")))
})
.or_else(|| {
AncestorTraverser::new(self, node_id).find(|ancestor_id| {
self.nodes[*ancestor_id]
.data
.is_element_with_tag_name(&local_name!("form"))
})
});
if let Some(final_owner_id) = final_owner_id {
self.controls_to_form.insert(node_id, final_owner_id);
}
}
/// Submits a form with the given form node ID and submitter node ID
///
/// # Arguments
/// * `node_id` - The ID of the form node to submit
/// * `submitter_id` - The ID of the node that triggered the submission
///
/// <https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#form-submission-algorithm>
pub fn submit_form(&self, node_id: usize, submitter_id: usize) {
let node = &self.nodes[node_id];
let Some(element) = node.element_data() else {
return;
};
let entry = construct_entry_list(self, node_id, submitter_id);
let method = get_form_attr(
self,
element,
local_name!("method"),
submitter_id,
local_name!("formmethod"),
)
.and_then(|method| method.parse::<FormMethod>().ok())
.unwrap_or(FormMethod::Get);
let action = get_form_attr(
self,
element,
local_name!("action"),
submitter_id,
local_name!("formaction"),
)
.unwrap_or_default();
let mut parsed_action = self.resolve_url(action);
let scheme = parsed_action.scheme();
let enctype = get_form_attr(
self,
element,
local_name!("enctype"),
submitter_id,
local_name!("formenctype"),
)
.and_then(|enctype| enctype.parse::<RequestContentType>().ok())
.unwrap_or(RequestContentType::FormUrlEncoded);
let mut post_resource = Body::Empty;
match (scheme, method) {
("http" | "https" | "data", FormMethod::Get) => {
let pairs = convert_to_list_of_name_value_pairs(entry);
let mut query = String::new();
url::form_urlencoded::Serializer::new(&mut query).extend_pairs(pairs);
parsed_action.set_query(Some(&query));
}
("http" | "https", FormMethod::Post) => post_resource = Body::Form(entry),
("mailto", FormMethod::Get) => {
let pairs = convert_to_list_of_name_value_pairs(entry);
parsed_action.query_pairs_mut().extend_pairs(pairs);
}
("mailto", FormMethod::Post) => {
let pairs = convert_to_list_of_name_value_pairs(entry);
let body = match enctype {
RequestContentType::TextPlain => {
let body = encode_text_plain(&pairs);
percent_encoding::utf8_percent_encode(&body, &DEFAULT_ENCODE_SET)
.to_string()
}
_ => {
let mut body = String::new();
url::form_urlencoded::Serializer::new(&mut body).extend_pairs(pairs);
body
}
};
let mut query = if let Some(query) = parsed_action.query() {
let mut query = query.to_string();
query.push('&');
query
} else {
String::new()
};
query.push_str("body=");
query.push_str(&body);
parsed_action.set_query(Some(&query));
}
_ => {
#[cfg(feature = "tracing")]
tracing::warn!(
"Scheme {} with method {:?} is not implemented",
scheme,
method
);
return;
}
}
let method = method.try_into().unwrap_or_default();
let navigation_options =
NavigationOptions::new(parsed_action, enctype.to_string(), self.id())
.set_document_resource(post_resource)
.set_method(method);
self.navigation_provider.navigate_to(navigation_options)
}
}
/// Constructs a list of form entries from form controls
///
/// # Arguments
/// * `doc` - Reference to the base document
/// * `form_id` - ID of the form element
/// * `submitter_id` - ID of the element that triggered form submission
///
/// # Returns
/// Returns an EntryList containing all valid form control entries
///
/// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-form-data-set
fn construct_entry_list(doc: &BaseDocument, form_id: usize, submitter_id: usize) -> FormData {
let mut entry_list = FormData::new();
let mut create_entry = |name: &str, value: EntryValue| {
entry_list.0.push(Entry {
name: name.to_string(),
value,
});
};
fn datalist_ancestor(doc: &BaseDocument, node_id: usize) -> bool {
AncestorTraverser::new(doc, node_id).any(|node_id| {
doc.nodes[node_id]
.data
.is_element_with_tag_name(&local_name!("datalist"))
})
}
// For each element field in controls, in tree order:
for control_id in TreeTraverser::new(doc) {
let Some(node) = doc.get_node(control_id) else {
continue;
};
let Some(element) = node.element_data() else {
continue;
};
// Check if the form owner is same as form_id
if doc
.controls_to_form
.get(&control_id)
.map(|owner_id| *owner_id != form_id)
.unwrap_or(true)
{
continue;
}
let element_type = element.attr(local_name!("type"));
// If any of the following are true:
// field has a datalist element ancestor;
// field is disabled;
// field is a button but it is not submitter;
// field is an input element whose type attribute is in the Checkbox state and whose checkedness is false; or
// field is an input element whose type attribute is in the Radio Button state and whose checkedness is false,
// then continue.
if datalist_ancestor(doc, node.id)
|| element.attr(local_name!("disabled")).is_some()
|| (element.name.local == local_name!("button") && node.id != submitter_id)
|| element.name.local == local_name!("input")
&& ((matches!(element_type, Some("checkbox" | "radio"))
&& !element.checkbox_input_checked().unwrap_or(false))
|| matches!(element_type, Some("submit" | "button")))
{
continue;
}
// If the field element is an input element whose type attribute is in the Image Button state, then:
if element_type == Some("image") {
// If the field element is not submitter, then continue.
if node.id != submitter_id {
continue;
}
// TODO: If the field element has a name attribute specified and its value is not the empty string, let name be that value followed by U+002E (.). Otherwise, let name be the empty string.
// Let namex be the concatenation of name and U+0078 (x).
// Let namey be the concatenation of name and U+0079 (y).
// Let (x, y) be the selected coordinate.
// Create an entry with namex and x, and append it to entry list.
// Create an entry with namey and y, and append it to entry list.
// Continue.
continue;
}
// TODO: If the field is a form-associated custom element,
// then perform the entry construction algorithm given field and entry list,
// then continue.
// If either the field element does not have a name attribute specified, or its name attribute's value is the empty string, then continue.
// Let name be the value of the field element's name attribute.
let Some(name) = element
.attr(local_name!("name"))
.filter(|str| !str.is_empty())
else {
continue;
};
// TODO: If the field element is a select element,
// then for each option element in the select element's
// list of options whose selectedness is true and that is not disabled,
// create an entry with name and the value of the option element,
// and append it to entry list.
// Otherwise, if the field element is an input element whose type attribute is in the Checkbox state or the Radio Button state, then:
if element.name.local == local_name!("input")
&& matches!(element_type, Some("checkbox" | "radio"))
{
// If the field element has a value attribute specified, then let value be the value of that attribute; otherwise, let value be the string "on".
let value = element.attr(local_name!("value")).unwrap_or("on");
// Create an entry with name and value, and append it to entry list.
create_entry(name, value.into());
continue;
}
// Otherwise, if the field element is an input element whose type attribute is in the File Upload state, then:
#[cfg(feature = "file_input")]
if element.name.local == local_name!("input") && matches!(element_type, Some("file")) {
// If there are no selected files, then create an entry with name and a new File object with an empty name, application/octet-stream as type, and an empty body, and append it to entry list.
let Some(files) = element.file_data() else {
create_entry(name, EntryValue::EmptyFile);
continue;
};
if files.is_empty() {
create_entry(name, EntryValue::EmptyFile);
}
// Otherwise, for each file in selected files, create an entry with name and a File object representing the file, and append it to entry list.
else {
for path_buf in files.iter() {
create_entry(name, path_buf.clone().into());
}
}
continue;
}
//Otherwise, if the field element is an input element whose type attribute is in the Hidden state and name is an ASCII case-insensitive match for "_charset_":
if element.name.local == local_name!("input")
&& element_type == Some("hidden")
&& name.eq_ignore_ascii_case("_charset_")
{
// Let charset be the name of encoding.
let charset = "UTF-8"; // TODO: Support multiple encodings.
// Create an entry with name and charset, and append it to entry list.
create_entry(name, charset.into());
}
// Otherwise, create an entry with name and the value of the field element, and append it to entry list.
else if let Some(text) = element.text_input_data() {
create_entry(name, text.editor.text().to_string().as_str().into());
} else if let Some(value) = element.attr(local_name!("value")) {
create_entry(name, value.into());
}
}
entry_list
}
fn get_form_attr<'a>(
doc: &'a BaseDocument,
form: &'a ElementData,
form_local: impl PartialEq<LocalName>,
submitter_id: usize,
submitter_local: impl PartialEq<LocalName>,
) -> Option<&'a str> {
get_submitter_attr(doc, submitter_id, submitter_local).or_else(|| form.attr(form_local))
}
fn get_submitter_attr(
doc: &BaseDocument,
submitter_id: usize,
local_name: impl PartialEq<LocalName>,
) -> Option<&str> {
doc.get_node(submitter_id)
.and_then(|node| node.element_data())
.and_then(|element_data| {
if element_data.name.local == local_name!("button")
&& element_data.attr(local_name!("type")) == Some("submit")
{
element_data.attr(local_name)
} else {
None
}
})
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum FormMethod {
Get,
Post,
Dialog,
}
impl FromStr for FormMethod {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s.to_lowercase().as_str() {
"get" => FormMethod::Get,
"post" => FormMethod::Post,
"dialog" => FormMethod::Dialog,
_ => return Err(()),
})
}
}
impl TryFrom<FormMethod> for Method {
type Error = &'static str;
fn try_from(method: FormMethod) -> Result<Self, Self::Error> {
Ok(match method {
FormMethod::Get => Method::GET,
FormMethod::Post => Method::POST,
FormMethod::Dialog => return Err("Dialog is not an HTTP method"),
})
}
}
/// Supported content types for HTTP requests
#[derive(Debug, Clone)]
pub enum RequestContentType {
/// application/x-www-form-urlencoded
FormUrlEncoded,
/// multipart/form-data
MultipartFormData,
/// text/plain
TextPlain,
}
impl FromStr for RequestContentType {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"application/x-www-form-urlencoded" => RequestContentType::FormUrlEncoded,
"multipart/form-data" => RequestContentType::MultipartFormData,
"text/plain" => RequestContentType::TextPlain,
_ => return Err(()),
})
}
}
impl Display for RequestContentType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RequestContentType::FormUrlEncoded => write!(f, "application/x-www-form-urlencoded"),
RequestContentType::MultipartFormData => write!(f, "multipart/form-data"),
RequestContentType::TextPlain => write!(f, "text/plain"),
}
}
}
/// Converts the entry list to a vector of name-value pairs with normalized line endings
/// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#converting-an-entry-list-to-a-list-of-name-value-pairs
fn convert_to_list_of_name_value_pairs(form_data: FormData) -> Vec<(String, String)> {
form_data
.iter()
.map(|Entry { name, value }| {
let name = normalize_line_endings(name.as_ref());
let value = normalize_line_endings(value.as_ref());
(name, value)
})
.collect()
}
/// Normalizes line endings in a string according to HTML spec
/// Converts single CR or LF to CRLF pairs according to HTML form submission requirements
fn normalize_line_endings(input: &str) -> String {
// Replace every occurrence of U+000D (CR) not followed by U+000A (LF),
// and every occurrence of U+000A (LF) not preceded by U+000D (CR),
// in value, by a string consisting of U+000D (CR) and U+000A (LF).
let mut result = String::with_capacity(input.len());
let mut chars = input.chars().peekable();
while let Some(current) = chars.next() {
match (current, chars.peek()) {
('\r', Some('\n')) => {
result.push_str("\r\n");
chars.next();
}
('\r' | '\n', _) => {
result.push_str("\r\n");
}
_ => result.push(current),
}
}
result
}
/// Encodes form data as text/plain according to HTML spec given an slice of name-value pairs
/// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#text/plain-encoding-algorithm
fn encode_text_plain<T: AsRef<str>, U: AsRef<str>>(input: &[(T, U)]) -> String {
let mut out = String::new();
for (name, value) in input {
out.push_str(name.as_ref());
out.push('=');
out.push_str(value.as_ref());
out.push_str("\r\n");
}
out
}