Auto merge of #13396 - jdm:FormData_iterable, r=jdm

Added FormData Iterable

Implement FormData's iterator
Rebased from #13104.

- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #13020
- [X] There are tests for these changes (It adds `./mach test-wpt tests/wpt/web-platform-tests/XMLHttpRequest/formdata-foreach.html`)

Notice: Our `FormData` is implemented by `HashMap` ,  which is different from [Gecko's array implementation](3c6ff93c8f/dom/base/FormData.h (L160)). So our `FormData`'s iterator order is different from Gecko's, as there is no way to keep our key's original insertion order.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/13396)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-09-24 03:54:18 -05:00 committed by GitHub
commit 89804bb251
5 changed files with 116 additions and 9 deletions

View file

@ -3,11 +3,12 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::FormDataBinding;
use dom::bindings::codegen::Bindings::FormDataBinding::FormDataMethods;
use dom::bindings::codegen::Bindings::FormDataBinding::FormDataWrap;
use dom::bindings::codegen::UnionTypes::FileOrUSVString;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::iterable::Iterable;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::{DOMString, USVString};
@ -16,6 +17,7 @@ use dom::file::File;
use dom::htmlformelement::{HTMLFormElement, FormDatumValue, FormDatum};
use std::collections::HashMap;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::iter;
use string_cache::Atom;
#[dom_struct]
@ -45,7 +47,7 @@ impl FormData {
pub fn new(form: Option<&HTMLFormElement>, global: GlobalRef) -> Root<FormData> {
reflect_dom_object(box FormData::new_inherited(form),
global, FormDataBinding::Wrap)
global, FormDataWrap)
}
pub fn Constructor(global: GlobalRef, form: Option<&HTMLFormElement>) -> Fallible<Root<FormData>> {
@ -156,11 +158,40 @@ impl FormData {
}
pub fn datums(&self) -> Vec<FormDatum> {
let mut ret = vec![];
for values in self.data.borrow().values() {
ret.append(&mut values.clone());
}
ret
self.data.borrow().values()
.flat_map(|value| value.iter())
.map(|value| value.clone())
.collect()
}
}
impl Iterable for FormData {
type Key = USVString;
type Value = FileOrUSVString;
fn get_iterable_length(&self) -> u32 {
self.data.borrow().values().map(|value| value.len()).sum::<usize>() as u32
}
fn get_value_at_index(&self, n: u32) -> FileOrUSVString {
let data = self.data.borrow();
let value = &data.values()
.flat_map(|value| value.iter())
.nth(n as usize)
.unwrap()
.value;
match *value {
FormDatumValue::String(ref s) => FileOrUSVString::USVString(USVString(s.to_string())),
FormDatumValue::File(ref b) => FileOrUSVString::File(Root::from_ref(&*b)),
}
}
fn get_key_at_index(&self, n: u32) -> USVString {
let data = self.data.borrow();
let value = &data.iter()
.flat_map(|(key, value)| iter::repeat(key).take(value.len()))
.nth(n as usize)
.unwrap();
USVString(value.to_string())
}
}

View file

@ -19,5 +19,5 @@ interface FormData {
boolean has(USVString name);
void set(USVString name, USVString value);
void set(USVString name, Blob value, optional USVString filename);
// iterable<USVString, FormDataEntryValue>;
iterable<USVString, FormDataEntryValue>;
};