Bug #1820, add the optional "type" parameter to Blob

This commit is contained in:
Shing Lyu 2014-12-22 23:45:00 +08:00
parent 37a97f3273
commit 6df9b7fd3a
6 changed files with 66 additions and 51 deletions

View file

@ -12,6 +12,7 @@ use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods;
use servo_util::str::DOMString; use servo_util::str::DOMString;
use std::cmp::{min, max}; use std::cmp::{min, max};
use std::ascii::AsciiExt;
#[jstraceable] #[jstraceable]
pub enum BlobTypeId { pub enum BlobTypeId {
@ -29,34 +30,46 @@ pub struct Blob {
// isClosed_: bool // isClosed_: bool
} }
fn is_ascii_printable(string: &DOMString) -> bool{
// Step 5.1 in Sec 5.1 of File API spec
// http://dev.w3.org/2006/webapi/FileAPI/#constructorBlob
return string.chars().all(|c| { c >= '\x20' && c <= '\x7E' })
}
impl Blob { impl Blob {
pub fn new_inherited(global: &GlobalRef, type_: BlobTypeId, pub fn new_inherited(global: &GlobalRef, type_: BlobTypeId,
bytes: Option<Vec<u8>>) -> Blob { bytes: Option<Vec<u8>>, typeString: &str) -> Blob {
Blob { Blob {
reflector_: Reflector::new(), reflector_: Reflector::new(),
type_: type_, type_: type_,
bytes: bytes, bytes: bytes,
typeString: "".into_string(), typeString: typeString.into_string(),
global: GlobalField::from_rooted(global) global: GlobalField::from_rooted(global)
//isClosed_: false //isClosed_: false
} }
} }
pub fn new(global: &GlobalRef, bytes: Option<Vec<u8>>) -> Temporary<Blob> { pub fn new(global: &GlobalRef, bytes: Option<Vec<u8>>,
reflect_dom_object(box Blob::new_inherited(global, BlobTypeId::Blob, bytes), typeString: &str) -> Temporary<Blob> {
reflect_dom_object(box Blob::new_inherited(global, BlobTypeId::Blob, bytes, typeString),
*global, *global,
BlobBinding::Wrap) BlobBinding::Wrap)
} }
pub fn Constructor(global: &GlobalRef) -> Fallible<Temporary<Blob>> { pub fn Constructor(global: &GlobalRef) -> Fallible<Temporary<Blob>> {
Ok(Blob::new(global, None)) Ok(Blob::new(global, None, ""))
} }
pub fn Constructor_(global: &GlobalRef, blobParts: DOMString) -> Fallible<Temporary<Blob>> { pub fn Constructor_(global: &GlobalRef, blobParts: DOMString, blobPropertyBag: &BlobBinding::BlobPropertyBag) -> Fallible<Temporary<Blob>> {
//TODO: accept other blobParts types - ArrayBuffer or ArrayBufferView or Blob //TODO: accept other blobParts types - ArrayBuffer or ArrayBufferView or Blob
//TODO: accept options parameter
let bytes: Option<Vec<u8>> = Some(blobParts.into_bytes()); let bytes: Option<Vec<u8>> = Some(blobParts.into_bytes());
Ok(Blob::new(global, bytes)) let typeString = if is_ascii_printable(&blobPropertyBag.type_) {
blobPropertyBag.type_.as_slice()
} else {
""
};
let typeStrLower = typeString.as_slice().to_ascii_lower();
Ok(Blob::new(global, bytes, typeStrLower.as_slice()))
} }
} }
@ -73,7 +86,7 @@ impl<'a> BlobMethods for JSRef<'a, Blob> {
} }
fn Slice(self, start: Option<i64>, end: Option<i64>, fn Slice(self, start: Option<i64>, end: Option<i64>,
_contentType: Option<DOMString>) -> Temporary<Blob> { contentType: Option<DOMString>) -> Temporary<Blob> {
let size: i64 = self.Size().to_i64().unwrap(); let size: i64 = self.Size().to_i64().unwrap();
let relativeStart: i64 = match start { let relativeStart: i64 = match start {
None => 0, None => 0,
@ -95,23 +108,26 @@ impl<'a> BlobMethods for JSRef<'a, Blob> {
} }
} }
}; };
/*
let relativeContentType = match contentType { let relativeContentType = match contentType {
None => "".into_string(), None => "".into_string(),
Some(str) => str Some(str) => {
if is_ascii_printable(&str) {
str.as_slice().to_ascii_lower().into_string()
} else {
"".into_string()
}
}
}; };
*/
//TODO: actually use relativeContentType in constructor
let span: i64 = max(relativeEnd - relativeStart, 0); let span: i64 = max(relativeEnd - relativeStart, 0);
let global = self.global.root(); let global = self.global.root();
match self.bytes { match self.bytes {
None => Blob::new(&global.root_ref(), None), None => Blob::new(&global.root_ref(), None, relativeContentType.as_slice()),
Some(ref vec) => { Some(ref vec) => {
let start = relativeStart.to_uint().unwrap(); let start = relativeStart.to_uint().unwrap();
let end = (relativeStart + span).to_uint().unwrap(); let end = (relativeStart + span).to_uint().unwrap();
let mut bytes: Vec<u8> = Vec::new(); let mut bytes: Vec<u8> = Vec::new();
bytes.push_all(vec.slice(start, end)); bytes.push_all(vec.slice(start, end));
Blob::new(&global.root_ref(), Some(bytes)) Blob::new(&global.root_ref(), Some(bytes), relativeContentType.as_slice())
} }
} }
} }

View file

@ -20,7 +20,8 @@ impl File {
fn new_inherited(global: &GlobalRef, type_: BlobTypeId, fn new_inherited(global: &GlobalRef, type_: BlobTypeId,
_file_bits: JSRef<Blob>, name: DOMString) -> File { _file_bits: JSRef<Blob>, name: DOMString) -> File {
File { File {
blob: Blob::new_inherited(global, type_, None), //TODO: get type from the underlying filesystem instead of "".to_string()
blob: Blob::new_inherited(global, type_, None, ""),
name: name, name: name,
} }
// XXXManishearth Once Blob is able to store data // XXXManishearth Once Blob is able to store data

View file

@ -59,7 +59,7 @@ impl<'a> TestBindingMethods for JSRef<'a, TestBinding> {
fn SetEnumAttribute(self, _: TestEnum) {} fn SetEnumAttribute(self, _: TestEnum) {}
fn InterfaceAttribute(self) -> Temporary<Blob> { fn InterfaceAttribute(self) -> Temporary<Blob> {
let global = self.global.root(); let global = self.global.root();
Blob::new(&global.root_ref(), None) Blob::new(&global.root_ref(), None, "")
} }
fn SetInterfaceAttribute(self, _: JSRef<Blob>) {} fn SetInterfaceAttribute(self, _: JSRef<Blob>) {}
fn UnionAttribute(self) -> HTMLElementOrLong { eLong(0) } fn UnionAttribute(self) -> HTMLElementOrLong { eLong(0) }
@ -99,7 +99,7 @@ impl<'a> TestBindingMethods for JSRef<'a, TestBinding> {
fn GetEnumAttributeNullable(self) -> Option<TestEnum> { Some(_empty) } fn GetEnumAttributeNullable(self) -> Option<TestEnum> { Some(_empty) }
fn GetInterfaceAttributeNullable(self) -> Option<Temporary<Blob>> { fn GetInterfaceAttributeNullable(self) -> Option<Temporary<Blob>> {
let global = self.global.root(); let global = self.global.root();
Some(Blob::new(&global.root_ref(), None)) Some(Blob::new(&global.root_ref(), None, ""))
} }
fn SetInterfaceAttributeNullable(self, _: Option<JSRef<Blob>>) {} fn SetInterfaceAttributeNullable(self, _: Option<JSRef<Blob>>) {}
fn GetUnionAttributeNullable(self) -> Option<HTMLElementOrLong> { Some(eLong(0)) } fn GetUnionAttributeNullable(self) -> Option<HTMLElementOrLong> { Some(eLong(0)) }
@ -123,7 +123,7 @@ impl<'a> TestBindingMethods for JSRef<'a, TestBinding> {
fn ReceiveEnum(self) -> TestEnum { _empty } fn ReceiveEnum(self) -> TestEnum { _empty }
fn ReceiveInterface(self) -> Temporary<Blob> { fn ReceiveInterface(self) -> Temporary<Blob> {
let global = self.global.root(); let global = self.global.root();
Blob::new(&global.root_ref(), None) Blob::new(&global.root_ref(), None, "")
} }
fn ReceiveAny(self, _: *mut JSContext) -> JSVal { NullValue() } fn ReceiveAny(self, _: *mut JSContext) -> JSVal { NullValue() }
fn ReceiveUnion(self) -> HTMLElementOrLong { eLong(0) } fn ReceiveUnion(self) -> HTMLElementOrLong { eLong(0) }
@ -145,7 +145,7 @@ impl<'a> TestBindingMethods for JSRef<'a, TestBinding> {
fn ReceiveNullableEnum(self) -> Option<TestEnum> { Some(_empty) } fn ReceiveNullableEnum(self) -> Option<TestEnum> { Some(_empty) }
fn ReceiveNullableInterface(self) -> Option<Temporary<Blob>> { fn ReceiveNullableInterface(self) -> Option<Temporary<Blob>> {
let global = self.global.root(); let global = self.global.root();
Some(Blob::new(&global.root_ref(), None)) Some(Blob::new(&global.root_ref(), None, ""))
} }
fn ReceiveNullableUnion(self) -> Option<HTMLElementOrLong> { Some(eLong(0)) } fn ReceiveNullableUnion(self) -> Option<HTMLElementOrLong> { Some(eLong(0)) }
fn ReceiveNullableUnion2(self) -> Option<EventOrString> { Some(eString("".into_string())) } fn ReceiveNullableUnion2(self) -> Option<EventOrString> { Some(eString("".into_string())) }

View file

@ -7,7 +7,7 @@
//[Exposed=Window,Worker][Constructor, //[Exposed=Window,Worker][Constructor,
// Constructor(sequence<(ArrayBuffer or ArrayBufferView or Blob or DOMString)> blobParts, optional BlobPropertyBag options)] // Constructor(sequence<(ArrayBuffer or ArrayBufferView or Blob or DOMString)> blobParts, optional BlobPropertyBag options)]
[Constructor, [Constructor,
Constructor(DOMString blobParts)] Constructor(DOMString blobParts, optional BlobPropertyBag options)]
interface Blob { interface Blob {
readonly attribute unsigned long long size; readonly attribute unsigned long long size;

View file

@ -10,6 +10,34 @@
var bs = b.slice(0, 5); var bs = b.slice(0, 5);
is(bs.size, 5); is(bs.size, 5);
is(b.type, ""); is(b.type, "");
var bc = new Blob(testData, {type:"text/plain"}); // the blob
is(bc.size, 32);
is(bc.type, "text/plain");
var bss = new Blob(testData, {type:" text/plain "}); // spaces
is(bss.size, 32);
is(bss.type, " text/plain ");
var bcs = bc.slice(0, 7);
is(bcs.size, 7);
is(bcs.type, "");
var bcsc = bc.slice(0, 7, "text/xml");
is(bcsc.size, 7);
is(bcsc.type, "text/xml");
var bu = new Blob(testData, {type:"TEXT/PLAIN"}); // the blob
is(bu.size, 32);
is(bu.type, "text/plain");
var bj = new Blob(testData, {type:"☃"}); // the blob
is(bj.size, 32);
is(bj.type, "");
var bjs = bj.slice(0, 7, "☃");
is(bjs.size, 7);
is(bjs.type, "");
</script> </script>
</head> </head>
</html> </html>

View file

@ -1,7 +1,5 @@
[Blob-constructor.html] [Blob-constructor.html]
type: testharness type: testharness
[Blob interface object]
expected: FAIL
[Passing non-objects, Dates and RegExps for blobParts should throw a TypeError.] [Passing non-objects, Dates and RegExps for blobParts should throw a TypeError.]
expected: FAIL expected: FAIL
@ -78,9 +76,6 @@
[Array with mixed types] [Array with mixed types]
expected: FAIL expected: FAIL
[options properties should be accessed in lexicographic order.]
expected: FAIL
[Passing null (index 0) for options should use the defaults.] [Passing null (index 0) for options should use the defaults.]
expected: FAIL expected: FAIL
@ -122,28 +117,3 @@
[Newlines should not change when endings is \'native\'.] [Newlines should not change when endings is \'native\'.]
expected: FAIL expected: FAIL
[Blob with type "a"]
expected: FAIL
[Blob with type "A"]
expected: FAIL
[Blob with type "text/html"]
expected: FAIL
[Blob with type "TEXT/HTML"]
expected: FAIL
[Blob with type " image/gif "]
expected: FAIL
[Blob with type "unknown/unknown"]
expected: FAIL
[Blob with type "text/plain"]
expected: FAIL
[Blob with type "image/png"]
expected: FAIL