From d1c0af9ae79087f4d9f637d7294b613dd8600087 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Sun, 1 May 2016 02:58:37 -0400 Subject: [PATCH] Implement text/plain form encoding --- components/script/dom/htmlformelement.rs | 37 ++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/components/script/dom/htmlformelement.rs b/components/script/dom/htmlformelement.rs index a318b9d0006..f2e72bb5f4e 100644 --- a/components/script/dom/htmlformelement.rs +++ b/components/script/dom/htmlformelement.rs @@ -324,6 +324,36 @@ impl HTMLFormElement { 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) pub fn submit(&self, submit_method_flag: SubmittedFrom, submitter: FormSubmitter) { // Step 1 @@ -388,8 +418,11 @@ impl HTMLFormElement { self.encode_multipart_form_data(&mut form_data, None, boundary) } - // TODO: Support plain text encoding - FormEncType::TextPlainEncoded => "".to_owned() + FormEncType::TextPlainEncoded => { + load_data.headers.set(ContentType(mime!(Text / Plain))); + + self.encode_plaintext(&mut form_data) + } }; // Step 18