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

LibWeb/FileAPI: Implement FileReader readAsBinaryString #3343

Merged
merged 1 commit into from
Jan 28, 2025
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
8 changes: 6 additions & 2 deletions Libraries/LibWeb/FileAPI/FileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ WebIDL::ExceptionOr<FileReader::Result> FileReader::blob_package_data(JS::Realm&
// Return a new ArrayBuffer whose contents are bytes.
return JS::ArrayBuffer::create(realm, move(bytes));
case Type::BinaryString:
// FIXME: Return bytes as a binary string, in which every byte is represented by a code unit of equal value [0..255].
return WebIDL::NotSupportedError::create(realm, "BinaryString not supported yet"_string);
// Return bytes as a binary string, in which every byte is represented by a code unit of equal value [0..255].
Vector<u16> builder;
builder.ensure_capacity(bytes.size());
for (auto byte : bytes.bytes())
builder.unchecked_append(byte);
return MUST(Utf16View { builder }.to_utf8(Utf16View::AllowInvalidCodeUnits::Yes));
}
VERIFY_NOT_REACHED();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Harness status: OK

Found 1 tests

1 Pass
Pass FileAPI Test: filereader_readAsBinaryString
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<meta charset=utf-8>
<title>FileAPI Test: filereader_readAsBinaryString</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>

<div id=log></div>
<script src="../../FileAPI/reading-data-section/filereader_readAsBinaryString.any.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// META: title=FileAPI Test: filereader_readAsBinaryString

async_test(t => {
const blob = new Blob(["σ"]);
const reader = new FileReader();

reader.onload = t.step_func_done(() => {
assert_equals(typeof reader.result, "string", "The result is string");
assert_equals(reader.result.length, 2, "The result length is 2");
assert_equals(reader.result, "\xcf\x83", "The result is \xcf\x83");
assert_equals(reader.readyState, reader.DONE);
});

reader.onloadstart = t.step_func(() => {
assert_equals(reader.readyState, reader.LOADING);
});

reader.onprogress = t.step_func(() => {
assert_equals(reader.readyState, reader.LOADING);
});

reader.readAsBinaryString(blob);
});
Loading