diff --git a/components/script/dom/htmlformelement.rs b/components/script/dom/htmlformelement.rs index a1fdd34e82a..f2e72bb5f4e 100644 --- a/components/script/dom/htmlformelement.rs +++ b/components/script/dom/htmlformelement.rs @@ -270,9 +270,9 @@ impl HTMLFormElement { } // https://html.spec.whatwg.org/multipage/#multipart/form-data-encoding-algorithm - fn encode_form_data(&self, form_data: &mut Vec, - encoding: Option, - boundary: String) -> String { + fn encode_multipart_form_data(&self, form_data: &mut Vec, + encoding: Option, + boundary: String) -> String { // Step 1 let mut result = "".to_owned(); @@ -321,7 +321,37 @@ impl HTMLFormElement { result.push_str(&*format!("\r\n--{}--", boundary)); - return result; + result + } + + // https://html.spec.whatwg.org/multipage/#text/plain-encoding-algorithm + fn encode_plaintext(&self, form_data: &mut Vec) -> String { + // Step 1 + let mut result = String::new(); + + // Step 2 + let encoding = self.pick_encoding(); + + // Step 3 + let charset = &*encoding.whatwg_name().unwrap(); + + for entry in form_data.iter_mut() { + // Step 4 + if entry.name == "_charset_" && entry.ty == "hidden" { + entry.value = FormDatumValue::String(DOMString::from(charset.clone())); + } + + // Step 5 + if entry.ty == "file" { + entry.value = FormDatumValue::String(DOMString::from(entry.value_str())); + } + + // Step 6 + result.push_str(&*format!("{}={}\r\n", entry.name, entry.value_str())); + } + + // Step 7 + result } /// [Form submission](https://html.spec.whatwg.org/multipage/#concept-form-submit) @@ -377,6 +407,7 @@ impl HTMLFormElement { load_data.headers.set(ContentType::form_url_encoded()); form_urlencoded::Serializer::new(String::new()) + .encoding_override(Some(self.pick_encoding())) .extend_pairs(form_data.into_iter().map(|field| (field.name.clone(), field.value_str()))) .finish() } @@ -385,10 +416,13 @@ impl HTMLFormElement { let mime = mime!(Multipart / FormData; Boundary =(&boundary)); load_data.headers.set(ContentType(mime)); - self.encode_form_data(&mut form_data, None, boundary) + self.encode_multipart_form_data(&mut form_data, None, boundary) + } + FormEncType::TextPlainEncoded => { + load_data.headers.set(ContentType(mime!(Text / Plain))); + + self.encode_plaintext(&mut form_data) } - // TODO: Support plain text encoding - FormEncType::TextPlainEncoded => "".to_owned() }; // Step 18 diff --git a/tests/wpt/web-platform-tests/common/form-submission.py b/tests/wpt/web-platform-tests/common/form-submission.py index eb9c654444b..78f96fe3145 100644 --- a/tests/wpt/web-platform-tests/common/form-submission.py +++ b/tests/wpt/web-platform-tests/common/form-submission.py @@ -4,6 +4,11 @@ def main(request, response): return 'OK' else: return 'FAIL' + elif request.headers.get('Content-Type') == 'text/plain': + if request.body == 'qux=baz\r\n': + return 'OK' + else: + return 'FAIL' else: if request.POST.first('foo') == 'bar': return 'OK' diff --git a/tests/wpt/web-platform-tests/html/semantics/forms/form-submission-0/submit-entity-body.html b/tests/wpt/web-platform-tests/html/semantics/forms/form-submission-0/submit-entity-body.html index 3c6e0b725c2..736ef1318b1 100644 --- a/tests/wpt/web-platform-tests/html/semantics/forms/form-submission-0/submit-entity-body.html +++ b/tests/wpt/web-platform-tests/html/semantics/forms/form-submission-0/submit-entity-body.html @@ -18,6 +18,13 @@ var simple_tests = [ submitelement: "", submitaction: function(doc) { doc.getElementById("testform").submit(); } }, + { + name: "form submission from form should navigate to url with text/plain", + input: "", + enctype: "text/plain", + submitelement: "", + submitaction: function(doc) { doc.getElementById("testform").submit(); } + }, { name: "form submission from button should navigate to url with x-www-form-urlencoded", input: "", @@ -32,6 +39,13 @@ var simple_tests = [ submitelement: "", submitaction: function(doc) { doc.getElementById("buttonsubmit").click(); } }, + { + name: "form submission from button should navigate to url with text/plain", + input: "", + enctype: "text/plain", + submitelement: "", + submitaction: function(doc) { doc.getElementById("buttonsubmit").click(); } + }, { name: "form submission from input should navigate to url with x-www-form-urlencoded", input: "", @@ -46,6 +60,13 @@ var simple_tests = [ submitelement: "Submit", submitaction: function(doc) { doc.getElementById("inputsubmit").click(); } }, + { + name: "form submission from input should navigate to url with text/plain", + input: "", + enctype: "text/plain", + submitelement: "Submit", + submitaction: function(doc) { doc.getElementById("inputsubmit").click(); } + } ]; simple_tests.forEach(function(test_obj) { test_obj.test = async_test(test_obj.name);