Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webidl for Blob arraybuffer / text #2008

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/web-sys/tests/wasm/blob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function new_blob() {
return new Blob([ 1, 2, 3 ]);
}
65 changes: 65 additions & 0 deletions crates/web-sys/tests/wasm/blob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use js_sys::{Array, ArrayBuffer};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::*;
use web_sys::Blob;

#[wasm_bindgen(module = "/tests/wasm/blob.js")]
extern "C" {
fn new_blob() -> Blob;
}

#[wasm_bindgen_test]
fn test_blob_from_js() {
let blob = new_blob();
assert!(blob.is_instance_of::<Blob>());
assert_eq!(blob.size(), 3.0);
}

#[wasm_bindgen_test]
fn test_blob_from_bytes() {
let bytes = Array::new();
bytes.push(&1.into());
bytes.push(&2.into());
bytes.push(&3.into());

let blob = Blob::new_with_u8_array_sequence(&bytes.into()).unwrap();
assert!(blob.is_instance_of::<Blob>());
assert_eq!(blob.size(), 3.0);
}

#[wasm_bindgen_test]
fn test_blob_empty() {
let blob = Blob::new().unwrap();
assert!(blob.is_instance_of::<Blob>());
assert_eq!(blob.size(), 0.0);
}

#[wasm_bindgen_test]
async fn test_blob_array_buffer() {
let bytes = Array::new();
bytes.push(&1.into());
bytes.push(&2.into());
bytes.push(&3.into());

let blob = Blob::new_with_u8_array_sequence(&bytes.into()).unwrap();

let buffer: ArrayBuffer = JsFuture::from(blob.array_buffer()).await.unwrap().into();

assert!(blob.is_instance_of::<Blob>());
assert!(buffer.is_instance_of::<ArrayBuffer>());
assert_eq!(blob.size(), buffer.byte_length() as f64);
}

#[wasm_bindgen_test]
async fn test_blob_text() {
let strings = Array::new();
strings.push(&"hello".into());

let blob = Blob::new_with_str_sequence(&strings.into()).unwrap();
let string = JsFuture::from(blob.text()).await.unwrap();

assert!(blob.is_instance_of::<Blob>());
assert_eq!(string, "hello")
}
1 change: 1 addition & 0 deletions crates/web-sys/tests/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod anchor_element;
pub mod body_element;
pub mod br_element;
pub mod button_element;
pub mod blob;
pub mod console;
pub mod div_element;
pub mod element;
Expand Down
3 changes: 3 additions & 0 deletions crates/web-sys/webidls/enabled/Blob.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ interface Blob {
Blob slice([Clamp] optional long long start,
[Clamp] optional long long end,
optional DOMString contentType);

[NewObject] Promise<DOMString> text();
[NewObject] Promise<ArrayBuffer> arrayBuffer();
};

enum EndingTypes { "transparent", "native" };
Expand Down