From c038a8c9c93a5a3a8b02e89f95f2a0167d1cbc46 Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Sun, 24 Jul 2022 16:05:25 +0200 Subject: [PATCH] LibWeb: Make process_blob_parts() publicly accessible This pulls process_blob_parts() out of the Blob class and makes it publicly accessible. --- Userland/Libraries/LibWeb/FileAPI/Blob.cpp | 67 +++++++++++----------- Userland/Libraries/LibWeb/FileAPI/Blob.h | 3 +- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp index 5f9a1bce82..b473f78e09 100644 --- a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp +++ b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp @@ -12,6 +12,40 @@ namespace Web::FileAPI { +// https://w3c.github.io/FileAPI/#process-blob-parts +ErrorOr process_blob_parts(Vector const& blob_parts) +{ + // 1. Let bytes be an empty sequence of bytes. + ByteBuffer bytes {}; + + // 2. For each element in parts: + for (auto const& blob_part : 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. + + // FIXME: 2. If the endings member of options is "native", set s to the result of converting line endings to native of element. + + // 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()); + }, + // 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 { + auto data_buffer = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell())); + return bytes.try_append(data_buffer.bytes()); + }, + // 3. If element is a Blob, append the bytes it represents to bytes. + [&](NonnullRefPtr const& blob) -> ErrorOr { + return bytes.try_append(blob->bytes()); + })); + } + // 3. Return bytes. + return bytes; +} + Blob::Blob(ByteBuffer byte_buffer, String type) : m_byte_buffer(move(byte_buffer)) , m_type(move(type)) @@ -49,39 +83,6 @@ DOM::ExceptionOr> Blob::create_with_global_object(Bindings:: return Blob::create(blob_parts, options); } -// https://w3c.github.io/FileAPI/#process-blob-parts -ErrorOr Blob::process_blob_parts(Vector const& blob_parts) -{ - // 1. Let bytes be an empty sequence of bytes. - ByteBuffer bytes {}; - - // 2. For each element in parts: - for (auto const& blob_part : 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. - - // FIXME: 2. If the endings member of options is "native", set s to the result of converting line endings to native of element. - - // 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()); - }, - // 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 { - auto data_buffer = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell())); - return bytes.try_append(data_buffer.bytes()); - }, - // 3. If element is a Blob, append the bytes it represents to bytes. - [&](NonnullRefPtr const& blob) -> ErrorOr { - return bytes.try_append(blob->bytes()); - })); - } - return bytes; -} - // https://w3c.github.io/FileAPI/#dfn-slice DOM::ExceptionOr> Blob::slice(Optional start, Optional end, Optional const& content_type) { diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.h b/Userland/Libraries/LibWeb/FileAPI/Blob.h index 4947323544..9c78ced66a 100644 --- a/Userland/Libraries/LibWeb/FileAPI/Blob.h +++ b/Userland/Libraries/LibWeb/FileAPI/Blob.h @@ -26,6 +26,8 @@ struct BlobPropertyBag { Bindings::EndingType endings; }; +[[nodiscard]] ErrorOr process_blob_parts(Vector const& blob_parts); + class Blob : public RefCounted , public Weakable @@ -52,7 +54,6 @@ public: private: Blob() = default; - static ErrorOr process_blob_parts(Vector const& blob_parts); ByteBuffer m_byte_buffer {}; String m_type {};