Skip to content

Truly independent web browser

LibWeb: Implement Blob.textStream()

Shannon Booth46d518b8c0db
diff --git a/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder_stream.rs b/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder_stream.rs
index 145f7a5f2c5..7f5a7c7fb60 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder_stream.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder_stream.rs
@@ -62,7 +62,7 @@ impl TextDecoderStream {
}
// https://encoding.spec.whatwg.org/#set-up-a-text-decoder-stream
- fn set_up(cx: &CallContext, encoding: &'static Encoding, fatal: bool, ignore_bom: bool) -> Self {
+ pub(crate) fn set_up(cx: &CallContext, encoding: &'static Encoding, fatal: bool, ignore_bom: bool) -> Self {
// 1. Assert: encoding is not replacement.
assert!(
encoding != encoding_rs::REPLACEMENT,
@@ -95,6 +95,10 @@ impl TextDecoderStream {
}
}
+ pub(crate) fn transform(&self) -> Ref<TransformStream> {
+ self.transform
+ }
+
// https://encoding.spec.whatwg.org/#dom-textdecoder-encoding
pub(crate) fn encoding(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {
// The encoding getter steps are to return this's encoding's name, ASCII lowercased.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs b/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
index 5fd6acb987d..6be78083e67 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
@@ -20,6 +20,7 @@ use crate::bindings::interface::{Inherits, Ref, Wrappable, allocate, cell_from_a
use crate::bindings::promise::{WebIdlPromise, promise_from_abi};
use crate::bindings::structured_data::{Serializable, SerializationReader, SerializationWriter};
use crate::bindings::{CallContext, WebIdlResult};
+use crate::encoding::text_decoder_stream::TextDecoderStream;
use crate::streams::readable_stream::ReadableStream;
unsafe extern "C" {
@@ -495,6 +496,27 @@ impl Blob {
})
}
+ // https://w3c.github.io/FileAPI/#dom-blob-textstream
+ pub(crate) fn text_stream(&self, cx: &mut CallContext) -> WebIdlResult<Ref<ReadableStream>> {
+ // 1. Let stream be the result of calling get stream on this.
+ let stream = self.stream(cx)?;
+
+ // 2. Let decoder be a new TextDecoderStream in this's relevant realm.
+ // 3. Set up decoder with UTF-8.
+ let decoder = TextDecoderStream::set_up(cx, encoding_rs::UTF_8, false, false);
+
+ // 4. Return the result of calling stream, piped through decoder.
+ Ok(ReadableStream::piped_through(
+ stream,
+ cx.vm(),
+ decoder.transform(),
+ false,
+ false,
+ false,
+ None,
+ ))
+ }
+
// https://w3c.github.io/FileAPI/#dom-blob-bytes
pub(crate) fn bytes(&self, cx: &mut CallContext) -> WebIdlResult<WebIdlPromise> {
// SAFETY: The realm is live, LibWeb copies the bytes, and it returns a new promise.
diff --git a/Libraries/LibWeb/FileAPI/Blob.idl b/Libraries/LibWeb/FileAPI/Blob.idl
index 2540c51cc6d..c4bf7776495 100644
--- a/Libraries/LibWeb/FileAPI/Blob.idl
+++ b/Libraries/LibWeb/FileAPI/Blob.idl
@@ -13,6 +13,7 @@ interface Blob {
[NewObject] ReadableStream stream();
[NewObject] Promise<USVString> text();
[NewObject] Promise<ArrayBuffer> arrayBuffer();
+ [NewObject] ReadableStream textStream();
[NewObject] Promise<Uint8Array> bytes();
};
diff --git a/Tests/LibWeb/Text/expected/wpt-import/FileAPI/blob/Blob-textStream.any.txt b/Tests/LibWeb/Text/expected/wpt-import/FileAPI/blob/Blob-textStream.any.txt
new file mode 100644
index 00000000000..dbfeb04edf2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/FileAPI/blob/Blob-textStream.any.txt
@@ -0,0 +1,13 @@
+Harness status: OK
+
+Found 8 tests
+
+8 Pass
+Pass textStream method existence
+Pass Blob.textStream() basic functionality
+Pass Blob.textStream() with empty blob
+Pass Blob.textStream() with multi-part blob
+Pass Blob.textStream() ignores type charset (UTF-16LE)
+Pass Blob.textStream() ignores invalid type charset (invalid-charset)
+Pass Blob.textStream() ignores type charset (iso-8859-1)
+Pass Blob.textStream() can be called multiple times
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/FileAPI/blob/Blob-textStream.any.html b/Tests/LibWeb/Text/input/wpt-import/FileAPI/blob/Blob-textStream.any.html
new file mode 100644
index 00000000000..2df58d77552
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/FileAPI/blob/Blob-textStream.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>Blob textStream</title>
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../support/Blob.js"></script>
+<div id=log></div>
+<script src="../../FileAPI/blob/Blob-textStream.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/FileAPI/blob/Blob-textStream.any.js b/Tests/LibWeb/Text/input/wpt-import/FileAPI/blob/Blob-textStream.any.js
new file mode 100644
index 00000000000..7f22d8bb330
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/FileAPI/blob/Blob-textStream.any.js
@@ -0,0 +1,90 @@
+// META: title=Blob textStream
+// META: global=window,worker
+// META: script=../support/Blob.js
+'use strict';
+
+async function readAllChunks(stream) {
+ const reader = stream.getReader();
+ const chunks = [];
+ while (true) {
+ const {done, value} = await reader.read();
+ if (done) {
+ break;
+ }
+ chunks.push(value);
+ }
+ return chunks;
+}
+
+test(() => {
+ assert_true('textStream' in Blob.prototype, "textStream exists on Blob.prototype");
+ assert_equals(typeof Blob.prototype.textStream, "function", "Blob.prototype.textStream is a function");
+}, "textStream method existence");
+
+promise_test(async () => {
+ const blob = new Blob(["hello world"]);
+ const stream = blob.textStream();
+ assert_true(stream instanceof ReadableStream, "textStream() returns a ReadableStream");
+
+ const chunks = await readAllChunks(stream);
+ assert_greater_than(chunks.length, 0);
+ for (const chunk of chunks) {
+ assert_equals(typeof chunk, "string", "each chunk should be a string");
+ }
+ assert_equals(chunks.join(""), "hello world", "concatenated chunks match the blob content");
+}, "Blob.textStream() basic functionality");
+
+promise_test(async () => {
+ const blob = new Blob();
+ const stream = blob.textStream();
+ assert_true(stream instanceof ReadableStream);
+ const chunks = await readAllChunks(stream);
+ assert_equals(chunks.length, 0, "no chunks should be read from empty blob textStream");
+}, "Blob.textStream() with empty blob");
+
+promise_test(async () => {
+ const blob = new Blob(["hello ", "world"]);
+ const stream = blob.textStream();
+ const chunks = await readAllChunks(stream);
+ assert_equals(chunks.join(""), "hello world");
+}, "Blob.textStream() with multi-part blob");
+
+promise_test(async () => {
+ const buffer = new Uint8Array([0x68, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00]); // "hello" in UTF-16LE
+ const blob = new Blob([buffer], {
+ type: "text/plain; charset=utf-16le"
+ });
+ const stream = blob.textStream();
+ const chunks = await readAllChunks(stream);
+ assert_equals(chunks.join(""), "h\0e\0l\0l\0o\0", "ignores charset=utf-16le type parameter");
+}, "Blob.textStream() ignores type charset (UTF-16LE)");
+
+promise_test(async () => {
+ const blob = new Blob([new TextEncoder().encode("hello")], {
+ type: "text/plain; charset=invalid-charset"
+ });
+ const stream = blob.textStream();
+ const chunks = await readAllChunks(stream);
+ assert_equals(chunks.join(""), "hello", "ignores invalid-charset type parameter and decodes as UTF-8");
+}, "Blob.textStream() ignores invalid type charset (invalid-charset)");
+
+promise_test(async () => {
+ const blob = new Blob([new TextEncoder().encode("é")], {
+ type: "text/plain; charset=iso-8859-1"
+ });
+ const stream = blob.textStream();
+ const chunks = await readAllChunks(stream);
+ assert_equals(chunks.join(""), "é", "ignores charset=iso-8859-1 type parameter and decodes as UTF-8");
+}, "Blob.textStream() ignores type charset (iso-8859-1)");
+
+promise_test(async () => {
+ const blob = new Blob(["hello"]);
+ const stream1 = blob.textStream();
+ const stream2 = blob.textStream();
+ assert_not_equals(stream1, stream2, "multiple calls return new streams");
+
+ const chunks1 = await readAllChunks(stream1);
+ const chunks2 = await readAllChunks(stream2);
+ assert_equals(chunks1.join(""), "hello");
+ assert_equals(chunks2.join(""), "hello");
+}, "Blob.textStream() can be called multiple times");