LibWeb: Handle endings member of options being "native"

This patch passes the options argument to process_blob_parts() and makes
use of the "convert line endings to native" algorithm when the endings
member of options (BlobPropertyBag) is set to "native".
This commit is contained in:
Kenneth Myhra 2022-07-30 10:36:18 +02:00 committed by Linus Groh
parent 516ea4d758
commit bbd9490683
Notes: sideshowbarker 2024-07-17 08:30:06 +09:00
3 changed files with 9 additions and 7 deletions

View File

@ -61,7 +61,7 @@ ErrorOr<String> convert_line_endings_to_native(String const& string)
}
// https://w3c.github.io/FileAPI/#process-blob-parts
ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts)
ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options)
{
// 1. Let bytes be an empty sequence of bytes.
ByteBuffer bytes {};
@ -71,14 +71,16 @@ ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts)
TRY(blob_part.visit(
// 1. If element is a USVString, run the following sub-steps:
[&](String const& string) -> ErrorOr<void> {
// NOTE: This step is handled by the lambda expression.
// 1. Let s be element.
auto s = string;
// FIXME: 2. If the endings member of options is "native", set s to the result of converting line endings to native of element.
// 2. If the endings member of options is "native", set s to the result of converting line endings to native of element.
if (options.has_value() && options->endings == Bindings::EndingType::Native)
s = TRY(convert_line_endings_to_native(s));
// NOTE: The AK::String is always UTF-8.
// 3. Append the result of UTF-8 encoding s to bytes.
return bytes.try_append(string.to_byte_buffer());
return bytes.try_append(s.to_byte_buffer());
},
// 2. If element is a BufferSource, get a copy of the bytes held by the buffer source, and append those bytes to bytes.
[&](JS::Handle<JS::Object> const& buffer_source) -> ErrorOr<void> {
@ -115,7 +117,7 @@ DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::create(Optional<Vector<BlobPart>> co
ByteBuffer byte_buffer {};
// 2. Let bytes be the result of processing blob parts given blobParts and options.
if (blob_parts.has_value()) {
byte_buffer = TRY_OR_RETURN_OOM(process_blob_parts(blob_parts.value()));
byte_buffer = TRY_OR_RETURN_OOM(process_blob_parts(blob_parts.value(), options));
}
String type = String::empty();

View File

@ -27,7 +27,7 @@ struct BlobPropertyBag {
};
[[nodiscard]] ErrorOr<String> convert_line_endings_to_native(String const& string);
[[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts);
[[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {});
class Blob
: public RefCounted<Blob>

View File

@ -28,7 +28,7 @@ File::File(ByteBuffer byte_buffer, String file_name, String type, i64 last_modif
DOM::ExceptionOr<NonnullRefPtr<File>> File::create(Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
{
// 1. Let bytes be the result of processing blob parts given fileBits and options.
auto bytes = TRY_OR_RETURN_OOM(process_blob_parts(file_bits));
auto bytes = TRY_OR_RETURN_OOM(process_blob_parts(file_bits, static_cast<Optional<BlobPropertyBag> const&>(*options)));
// 2. Let n be the fileName argument to the constructor.
// NOTE: Underlying OS filesystems use differing conventions for file name; with constructed files, mandating UTF-16 lessens ambiquity when file names are converted to byte sequences.