From bbd9490683262592d64fc71471c19c204f7b15bd Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Sat, 30 Jul 2022 10:36:18 +0200 Subject: [PATCH] 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". --- Userland/Libraries/LibWeb/FileAPI/Blob.cpp | 12 +++++++----- Userland/Libraries/LibWeb/FileAPI/Blob.h | 2 +- Userland/Libraries/LibWeb/FileAPI/File.cpp | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp index b5f99deb47..d8ba575232 100644 --- a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp +++ b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp @@ -61,7 +61,7 @@ ErrorOr convert_line_endings_to_native(String const& string) } // https://w3c.github.io/FileAPI/#process-blob-parts -ErrorOr process_blob_parts(Vector const& blob_parts) +ErrorOr process_blob_parts(Vector const& blob_parts, Optional const& options) { // 1. Let bytes be an empty sequence of bytes. ByteBuffer bytes {}; @@ -71,14 +71,16 @@ ErrorOr process_blob_parts(Vector const& blob_parts) TRY(blob_part.visit( // 1. If element is a USVString, run the following sub-steps: [&](String const& string) -> ErrorOr { - // 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 const& buffer_source) -> ErrorOr { @@ -115,7 +117,7 @@ DOM::ExceptionOr> Blob::create(Optional> 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(); diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.h b/Userland/Libraries/LibWeb/FileAPI/Blob.h index 9d2e2c20b5..4bd8a56864 100644 --- a/Userland/Libraries/LibWeb/FileAPI/Blob.h +++ b/Userland/Libraries/LibWeb/FileAPI/Blob.h @@ -27,7 +27,7 @@ struct BlobPropertyBag { }; [[nodiscard]] ErrorOr convert_line_endings_to_native(String const& string); -[[nodiscard]] ErrorOr process_blob_parts(Vector const& blob_parts); +[[nodiscard]] ErrorOr process_blob_parts(Vector const& blob_parts, Optional const& options = {}); class Blob : public RefCounted diff --git a/Userland/Libraries/LibWeb/FileAPI/File.cpp b/Userland/Libraries/LibWeb/FileAPI/File.cpp index 68d9aebb5f..9548f3f7a7 100644 --- a/Userland/Libraries/LibWeb/FileAPI/File.cpp +++ b/Userland/Libraries/LibWeb/FileAPI/File.cpp @@ -28,7 +28,7 @@ File::File(ByteBuffer byte_buffer, String file_name, String type, i64 last_modif DOM::ExceptionOr> File::create(Vector const& file_bits, String const& file_name, Optional 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 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.