mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Auto merge of #10953 - KiChjang:text-plain-encoding, r=jdm
Implement text/plain form encoding Fixes #3649. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10953) <!-- Reviewable:end -->
This commit is contained in:
commit
641b374f0b
3 changed files with 67 additions and 7 deletions
|
@ -270,9 +270,9 @@ impl HTMLFormElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#multipart/form-data-encoding-algorithm
|
// https://html.spec.whatwg.org/multipage/#multipart/form-data-encoding-algorithm
|
||||||
fn encode_form_data(&self, form_data: &mut Vec<FormDatum>,
|
fn encode_multipart_form_data(&self, form_data: &mut Vec<FormDatum>,
|
||||||
encoding: Option<EncodingRef>,
|
encoding: Option<EncodingRef>,
|
||||||
boundary: String) -> String {
|
boundary: String) -> String {
|
||||||
// Step 1
|
// Step 1
|
||||||
let mut result = "".to_owned();
|
let mut result = "".to_owned();
|
||||||
|
|
||||||
|
@ -321,7 +321,37 @@ impl HTMLFormElement {
|
||||||
|
|
||||||
result.push_str(&*format!("\r\n--{}--", boundary));
|
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<FormDatum>) -> 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)
|
/// [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());
|
load_data.headers.set(ContentType::form_url_encoded());
|
||||||
|
|
||||||
form_urlencoded::Serializer::new(String::new())
|
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())))
|
.extend_pairs(form_data.into_iter().map(|field| (field.name.clone(), field.value_str())))
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
|
@ -385,10 +416,13 @@ impl HTMLFormElement {
|
||||||
let mime = mime!(Multipart / FormData; Boundary =(&boundary));
|
let mime = mime!(Multipart / FormData; Boundary =(&boundary));
|
||||||
load_data.headers.set(ContentType(mime));
|
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
|
// Step 18
|
||||||
|
|
|
@ -4,6 +4,11 @@ def main(request, response):
|
||||||
return 'OK'
|
return 'OK'
|
||||||
else:
|
else:
|
||||||
return 'FAIL'
|
return 'FAIL'
|
||||||
|
elif request.headers.get('Content-Type') == 'text/plain':
|
||||||
|
if request.body == 'qux=baz\r\n':
|
||||||
|
return 'OK'
|
||||||
|
else:
|
||||||
|
return 'FAIL'
|
||||||
else:
|
else:
|
||||||
if request.POST.first('foo') == 'bar':
|
if request.POST.first('foo') == 'bar':
|
||||||
return 'OK'
|
return 'OK'
|
||||||
|
|
|
@ -18,6 +18,13 @@ var simple_tests = [
|
||||||
submitelement: "",
|
submitelement: "",
|
||||||
submitaction: function(doc) { doc.getElementById("testform").submit(); }
|
submitaction: function(doc) { doc.getElementById("testform").submit(); }
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "form submission from form should navigate to url with text/plain",
|
||||||
|
input: "<textarea name=qux>baz</textarea>",
|
||||||
|
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",
|
name: "form submission from button should navigate to url with x-www-form-urlencoded",
|
||||||
input: "<input name=foo value=bara>",
|
input: "<input name=foo value=bara>",
|
||||||
|
@ -32,6 +39,13 @@ var simple_tests = [
|
||||||
submitelement: "<button id=buttonsubmit type=\"submit\">Submit</button>",
|
submitelement: "<button id=buttonsubmit type=\"submit\">Submit</button>",
|
||||||
submitaction: function(doc) { doc.getElementById("buttonsubmit").click(); }
|
submitaction: function(doc) { doc.getElementById("buttonsubmit").click(); }
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "form submission from button should navigate to url with text/plain",
|
||||||
|
input: "<textarea name=qux>baz</textarea>",
|
||||||
|
enctype: "text/plain",
|
||||||
|
submitelement: "<button id=buttonsubmit type=\"submit\">Submit</button>",
|
||||||
|
submitaction: function(doc) { doc.getElementById("buttonsubmit").click(); }
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "form submission from input should navigate to url with x-www-form-urlencoded",
|
name: "form submission from input should navigate to url with x-www-form-urlencoded",
|
||||||
input: "<input name=foo value=bara>",
|
input: "<input name=foo value=bara>",
|
||||||
|
@ -46,6 +60,13 @@ var simple_tests = [
|
||||||
submitelement: "<input id=inputsubmit type=\"submit\">Submit</input>",
|
submitelement: "<input id=inputsubmit type=\"submit\">Submit</input>",
|
||||||
submitaction: function(doc) { doc.getElementById("inputsubmit").click(); }
|
submitaction: function(doc) { doc.getElementById("inputsubmit").click(); }
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "form submission from input should navigate to url with text/plain",
|
||||||
|
input: "<input name=qux value=baz>",
|
||||||
|
enctype: "text/plain",
|
||||||
|
submitelement: "<input id=inputsubmit type=\"submit\">Submit</input>",
|
||||||
|
submitaction: function(doc) { doc.getElementById("inputsubmit").click(); }
|
||||||
|
}
|
||||||
];
|
];
|
||||||
simple_tests.forEach(function(test_obj) {
|
simple_tests.forEach(function(test_obj) {
|
||||||
test_obj.test = async_test(test_obj.name);
|
test_obj.test = async_test(test_obj.name);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue