servo: Merge #9979 - Implementation of Blob Constructor with test changes (from stspyder:master); r=jdm

Apologize for the late PR. I see that #9977 already overlaps with some of the work. If that is accepted, then I'll change my PR to reflect just the WPT changes.

Source-Repo: https://github.com/servo/servo
Source-Revision: f3abfeeadd5c5b00b17e48c186e957ba10b6e33b
This commit is contained in:
St.Spyder 2016-03-23 08:38:38 +05:01
parent 9a7523f50e
commit 9ca7983c7b
2 changed files with 21 additions and 7 deletions

View File

@ -4,11 +4,14 @@
use dom::bindings::codegen::Bindings::BlobBinding;
use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods;
use dom::bindings::codegen::UnionTypes::BlobOrString;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::trace::JSTraceable;
use encoding::all::UTF_8;
use encoding::types::{EncoderTrap, Encoding};
use num::ToPrimitive;
use std::ascii::AsciiExt;
use std::borrow::ToOwned;
@ -120,16 +123,29 @@ impl Blob {
// https://w3c.github.io/FileAPI/#constructorBlob
pub fn Constructor_(global: GlobalRef,
blobParts: DOMString,
blobParts: Vec<BlobOrString>,
blobPropertyBag: &BlobBinding::BlobPropertyBag)
-> Fallible<Root<Blob>> {
// TODO: accept other blobParts types - ArrayBuffer or ArrayBufferView or Blob
// TODO: accept other blobParts types - ArrayBuffer or ArrayBufferView
let bytes: Vec<u8> = blobParts.iter()
.flat_map(|bPart| {
match bPart {
&BlobOrString::String(ref s) => {
UTF_8.encode(s, EncoderTrap::Replace).unwrap()
},
&BlobOrString::Blob(ref b) => {
b.get_data().get_bytes().to_vec()
},
}
})
.collect();
let typeString = if is_ascii_printable(&blobPropertyBag.type_) {
&*blobPropertyBag.type_
} else {
""
};
Ok(Blob::new(global, blobParts.into(), &typeString.to_ascii_lowercase()))
Ok(Blob::new(global, bytes, &typeString.to_ascii_lowercase()))
}
pub fn get_data(&self) -> &DataSlice {

View File

@ -4,11 +4,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// http://dev.w3.org/2006/webapi/FileAPI/#dfn-Blob
//[Exposed=Window,Worker][Constructor,
// Constructor(sequence<(ArrayBuffer or ArrayBufferView or Blob or DOMString)> blobParts,
// optional BlobPropertyBag options)]
[Constructor,
Constructor(DOMString blobParts, optional BlobPropertyBag options),
Constructor(sequence<(/*ArrayBuffer or ArrayBufferView or */Blob or DOMString)> blobParts,
optional BlobPropertyBag options),
Exposed=Window/*,Worker*/]
interface Blob {