servo: Merge #16325 - Fix formdata-blob.htm (from KiChjang:fix-formdata-wpt); r=cbrewster
authorKeith Yeung <kungfukeith11@gmail.com>
Mon, 10 Apr 2017 09:27:01 -0500
changeset 560209 290dd813cbed201c39e1aa78a116ebfcf9b3c21d
parent 560208 b24055689799403926a7913fb8ce38b51c68eb7f
child 560210 30fc8aa8a780a437fc1a091ca92783edecb784b3
push id53365
push userjichen@mozilla.com
push dateTue, 11 Apr 2017 08:35:12 +0000
reviewerscbrewster
milestone55.0a1
servo: Merge #16325 - Fix formdata-blob.htm (from KiChjang:fix-formdata-wpt); r=cbrewster The other failure is a legitimate WPT bug, will fix upstream. Source-Repo: https://github.com/servo/servo Source-Revision: 638e1dc40bc70c3c0d9cd2efdaef852fdb371382
servo/components/script/dom/bindings/structuredclone.rs
servo/components/script/dom/blob.rs
servo/components/script/dom/formdata.rs
--- a/servo/components/script/dom/bindings/structuredclone.rs
+++ b/servo/components/script/dom/bindings/structuredclone.rs
@@ -82,17 +82,17 @@ unsafe fn read_blob(cx: *mut JSContext,
     return blob.reflector().get_jsobject().get()
 }
 
 unsafe fn write_blob(blob: Root<Blob>,
                      w: *mut JSStructuredCloneWriter)
                      -> Result<(), ()> {
     let blob_vec = try!(blob.get_bytes());
     let blob_length = blob_vec.len();
-    let type_string_bytes = blob.get_type_string().as_bytes().to_vec();
+    let type_string_bytes = blob.type_string().as_bytes().to_vec();
     let type_string_length = type_string_bytes.len();
     assert!(JS_WriteUint32Pair(w, StructuredCloneTags::DomBlob as u32, 0));
     write_length(w, blob_length);
     write_length(w, type_string_length);
     assert!(JS_WriteBytes(w, blob_vec.as_ptr() as *const raw::c_void, blob_length));
     assert!(JS_WriteBytes(w, type_string_bytes.as_ptr() as *const raw::c_void, type_string_length));
     return Ok(())
 }
--- a/servo/components/script/dom/blob.rs
+++ b/servo/components/script/dom/blob.rs
@@ -159,17 +159,17 @@ impl Blob {
                     let range = rel_pos.to_abs_range(v.len());
                     v.index(range).to_vec()
                 })
             }
         }
     }
 
     /// Get a copy of the type_string
-    pub fn get_type_string(&self) -> String {
+    pub fn type_string(&self) -> String {
         self.type_string.clone()
     }
 
     /// Get a FileID representing the Blob content,
     /// used by URL.createObjectURL
     pub fn get_blob_url_id(&self) -> Uuid {
         let opt_sliced_parent = match *self.blob_impl.borrow() {
             BlobImpl::Sliced(ref parent, ref rel_pos) => {
--- a/servo/components/script/dom/formdata.rs
+++ b/servo/components/script/dom/formdata.rs
@@ -2,16 +2,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 use dom::bindings::cell::DOMRefCell;
 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::inheritance::Castable;
 use dom::bindings::iterable::Iterable;
 use dom::bindings::js::Root;
 use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
 use dom::bindings::str::{DOMString, USVString};
 use dom::blob::{Blob, BlobImpl};
 use dom::file::File;
 use dom::globalscope::GlobalScope;
 use dom::htmlformelement::{HTMLFormElement, FormDatumValue, FormDatum};
@@ -74,17 +75,17 @@ impl FormDataMethods for FormData {
     }
 
     #[allow(unrooted_must_root)]
     // https://xhr.spec.whatwg.org/#dom-formdata-append
     fn Append_(&self, name: USVString, blob: &Blob, filename: Option<USVString>) {
         let datum = FormDatum {
             ty: DOMString::from("file"),
             name: DOMString::from(name.0.clone()),
-            value: FormDatumValue::File(Root::from_ref(&*self.get_file(blob, filename))),
+            value: FormDatumValue::File(Root::from_ref(&*self.create_an_entry(blob, filename))),
         };
 
         let mut data = self.data.borrow_mut();
 
         match data.entry(LocalName::from(name.0)) {
             Occupied(entry) => entry.into_mut().push(datum),
             Vacant(entry) => { entry.insert(vec!(datum)); },
         }
@@ -132,33 +133,36 @@ impl FormDataMethods for FormData {
     }
 
     #[allow(unrooted_must_root)]
     // https://xhr.spec.whatwg.org/#dom-formdata-set
     fn Set_(&self, name: USVString, blob: &Blob, filename: Option<USVString>) {
         self.data.borrow_mut().insert(LocalName::from(name.0.clone()), vec![FormDatum {
             ty: DOMString::from("file"),
             name: DOMString::from(name.0),
-            value: FormDatumValue::File(Root::from_ref(&*self.get_file(blob, filename))),
+            value: FormDatumValue::File(Root::from_ref(&*self.create_an_entry(blob, filename))),
         }]);
     }
 
 }
 
 
 impl FormData {
-    fn get_file(&self, blob: &Blob, opt_filename: Option<USVString>) -> Root<File> {
+    // https://xhr.spec.whatwg.org/#create-an-entry
+    // Steps 3-4.
+    fn create_an_entry(&self, blob: &Blob, opt_filename: Option<USVString>) -> Root<File> {
         let name = match opt_filename {
             Some(filename) => DOMString::from(filename.0),
+            None if blob.downcast::<File>().is_none() => DOMString::from("blob"),
             None => DOMString::from(""),
         };
 
         let bytes = blob.get_bytes().unwrap_or(vec![]);
 
-        File::new(&self.global(), BlobImpl::new_from_bytes(bytes), name, None, "")
+        File::new(&self.global(), BlobImpl::new_from_bytes(bytes), name, None, &blob.type_string())
     }
 
     pub fn datums(&self) -> Vec<FormDatum> {
         self.data.borrow().values()
             .flat_map(|value| value.iter())
             .map(|value| value.clone())
             .collect()
     }