1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-10 03:37:35 +00:00

LibWeb: Port IDL implementations Blob and File to new String

This commit is contained in:
Kenneth Myhra 2023-02-25 10:27:38 +01:00 committed by Linus Groh
parent 31a9bd2bfd
commit 9a5a8d617d
10 changed files with 63 additions and 52 deletions

View file

@ -108,7 +108,7 @@ WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes,
case PackageDataType::Blob: { case PackageDataType::Blob: {
// Return a Blob whose contents are bytes and type attribute is mimeType. // Return a Blob whose contents are bytes and type attribute is mimeType.
// NOTE: If extracting the mime type returns failure, other browsers set it to an empty string - not sure if that's spec'd. // NOTE: If extracting the mime type returns failure, other browsers set it to an empty string - not sure if that's spec'd.
auto mime_type_string = mime_type.has_value() ? mime_type->serialized() : DeprecatedString::empty(); auto mime_type_string = mime_type.has_value() ? TRY_OR_THROW_OOM(vm, String::from_deprecated_string(mime_type->serialized())) : String {};
return TRY(FileAPI::Blob::create(realm, move(bytes), move(mime_type_string))); return TRY(FileAPI::Blob::create(realm, move(bytes), move(mime_type_string)));
} }
case PackageDataType::FormData: case PackageDataType::FormData:

View file

@ -75,7 +75,7 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm,
length = blob->size(); length = blob->size();
// If objects type attribute is not the empty byte sequence, set type to its value. // If objects type attribute is not the empty byte sequence, set type to its value.
if (!blob->type().is_empty()) if (!blob->type().is_empty())
type = blob->type().to_byte_buffer(); type = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(blob->type().bytes()));
return {}; return {};
}, },
[&](ReadonlyBytes bytes) -> WebIDL::ExceptionOr<void> { [&](ReadonlyBytes bytes) -> WebIDL::ExceptionOr<void> {

View file

@ -1,27 +1,28 @@
/* /*
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> * Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/GenericLexer.h> #include <AK/GenericLexer.h>
#include <AK/StdLibExtras.h>
#include <LibJS/Runtime/ArrayBuffer.h> #include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/Completion.h>
#include <LibWeb/Bindings/BlobPrototype.h> #include <LibWeb/Bindings/BlobPrototype.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/FileAPI/Blob.h> #include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/Infra/Strings.h>
#include <LibWeb/WebIDL/AbstractOperations.h> #include <LibWeb/WebIDL/AbstractOperations.h>
namespace Web::FileAPI { namespace Web::FileAPI {
WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, ByteBuffer byte_buffer, DeprecatedString type) WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, ByteBuffer byte_buffer, String type)
{ {
return MUST_OR_THROW_OOM(realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type))); return MUST_OR_THROW_OOM(realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type)));
} }
// https://w3c.github.io/FileAPI/#convert-line-endings-to-native // https://w3c.github.io/FileAPI/#convert-line-endings-to-native
ErrorOr<DeprecatedString> convert_line_endings_to_native(DeprecatedString const& string) ErrorOr<String> convert_line_endings_to_native(StringView string)
{ {
// 1. Let native line ending be be the code point U+000A LF. // 1. Let native line ending be be the code point U+000A LF.
auto native_line_ending = "\n"sv; auto native_line_ending = "\n"sv;
@ -33,7 +34,7 @@ ErrorOr<DeprecatedString> convert_line_endings_to_native(DeprecatedString const&
StringBuilder result; StringBuilder result;
// 4. Let position be a position variable for s, initially pointing at the start of s. // 4. Let position be a position variable for s, initially pointing at the start of s.
auto lexer = GenericLexer { string.view() }; auto lexer = GenericLexer { string };
// 5. Let token be the result of collecting a sequence of code points that are not equal to U+000A LF or U+000D CR from s given position. // 5. Let token be the result of collecting a sequence of code points that are not equal to U+000A LF or U+000D CR from s given position.
// 6. Append token to result. // 6. Append token to result.
@ -64,7 +65,7 @@ ErrorOr<DeprecatedString> convert_line_endings_to_native(DeprecatedString const&
TRY(result.try_append(lexer.consume_until(is_any_of("\n\r"sv)))); TRY(result.try_append(lexer.consume_until(is_any_of("\n\r"sv))));
} }
// 5. Return result. // 5. Return result.
return result.to_deprecated_string(); return result.to_string();
} }
// https://w3c.github.io/FileAPI/#process-blob-parts // https://w3c.github.io/FileAPI/#process-blob-parts
@ -77,7 +78,7 @@ ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optio
for (auto const& blob_part : blob_parts) { for (auto const& blob_part : blob_parts) {
TRY(blob_part.visit( TRY(blob_part.visit(
// 1. If element is a USVString, run the following sub-steps: // 1. If element is a USVString, run the following sub-steps:
[&](DeprecatedString const& string) -> ErrorOr<void> { [&](String const& string) -> ErrorOr<void> {
// 1. Let s be element. // 1. Let s be element.
auto s = string; auto s = string;
@ -85,9 +86,9 @@ ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optio
if (options.has_value() && options->endings == Bindings::EndingType::Native) if (options.has_value() && options->endings == Bindings::EndingType::Native)
s = TRY(convert_line_endings_to_native(s)); s = TRY(convert_line_endings_to_native(s));
// NOTE: The AK::DeprecatedString is always UTF-8. // NOTE: The AK::String is always UTF-8.
// 3. Append the result of UTF-8 encoding s to bytes. // 3. Append the result of UTF-8 encoding s to bytes.
return bytes.try_append(s.to_byte_buffer()); return bytes.try_append(s.bytes());
}, },
// 2. If element is a BufferSource, get a copy of the bytes held by the buffer source, and append those bytes to bytes. // 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> { [&](JS::Handle<JS::Object> const& buffer_source) -> ErrorOr<void> {
@ -117,7 +118,7 @@ Blob::Blob(JS::Realm& realm)
{ {
} }
Blob::Blob(JS::Realm& realm, ByteBuffer byte_buffer, DeprecatedString type) Blob::Blob(JS::Realm& realm, ByteBuffer byte_buffer, String type)
: PlatformObject(realm) : PlatformObject(realm)
, m_byte_buffer(move(byte_buffer)) , m_byte_buffer(move(byte_buffer))
, m_type(move(type)) , m_type(move(type))
@ -143,6 +144,8 @@ JS::ThrowCompletionOr<void> Blob::initialize(JS::Realm& realm)
// https://w3c.github.io/FileAPI/#ref-for-dom-blob-blob // https://w3c.github.io/FileAPI/#ref-for-dom-blob-blob
WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options) WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
{ {
auto& vm = realm.vm();
// 1. If invoked with zero parameters, return a new Blob object consisting of 0 bytes, with size set to 0, and with type set to the empty string. // 1. If invoked with zero parameters, return a new Blob object consisting of 0 bytes, with size set to 0, and with type set to the empty string.
if (!blob_parts.has_value() && !options.has_value()) if (!blob_parts.has_value() && !options.has_value())
return MUST_OR_THROW_OOM(realm.heap().allocate<Blob>(realm, realm)); return MUST_OR_THROW_OOM(realm.heap().allocate<Blob>(realm, realm));
@ -153,7 +156,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, Optio
byte_buffer = TRY_OR_THROW_OOM(realm.vm(), process_blob_parts(blob_parts.value(), options)); byte_buffer = TRY_OR_THROW_OOM(realm.vm(), process_blob_parts(blob_parts.value(), options));
} }
auto type = DeprecatedString::empty(); auto type = String {};
// 3. If the type member of the options argument is not the empty string, run the following sub-steps: // 3. If the type member of the options argument is not the empty string, run the following sub-steps:
if (options.has_value() && !options->type.is_empty()) { if (options.has_value() && !options->type.is_empty()) {
// 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member. // 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member.
@ -166,7 +169,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, Optio
// 2. Convert every character in t to ASCII lowercase. // 2. Convert every character in t to ASCII lowercase.
if (!type.is_empty()) if (!type.is_empty())
type = options->type.to_lowercase(); type = TRY_OR_THROW_OOM(vm, Infra::to_ascii_lower_case(type));
} }
// 4. Return a Blob object referring to bytes as its associated byte sequence, with its size set to the length of bytes, and its type set to the value of t from the substeps above. // 4. Return a Blob object referring to bytes as its associated byte sequence, with its size set to the length of bytes, and its type set to the value of t from the substeps above.
@ -179,8 +182,10 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::construct_impl(JS::Realm& real
} }
// https://w3c.github.io/FileAPI/#dfn-slice // https://w3c.github.io/FileAPI/#dfn-slice
WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::slice(Optional<i64> start, Optional<i64> end, Optional<DeprecatedString> const& content_type) WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::slice(Optional<i64> start, Optional<i64> end, Optional<String> const& content_type)
{ {
auto& vm = realm().vm();
// 1. The optional start parameter is a value for the start point of a slice() call, and must be treated as a byte-order position, with the zeroth position representing the first byte. // 1. The optional start parameter is a value for the start point of a slice() call, and must be treated as a byte-order position, with the zeroth position representing the first byte.
// User agents must process slice() with start normalized according to the following: // User agents must process slice() with start normalized according to the following:
i64 relative_start; i64 relative_start;
@ -218,17 +223,17 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::slice(Optional<i64> start, Opt
// 3. The optional contentType parameter is used to set the ASCII-encoded string in lower case representing the media type of the Blob. // 3. The optional contentType parameter is used to set the ASCII-encoded string in lower case representing the media type of the Blob.
// User agents must process the slice() with contentType normalized according to the following: // User agents must process the slice() with contentType normalized according to the following:
DeprecatedString relative_content_type; String relative_content_type;
if (!content_type.has_value()) { if (!content_type.has_value()) {
// a. If the contentType parameter is not provided, let relativeContentType be set to the empty string. // a. If the contentType parameter is not provided, let relativeContentType be set to the empty string.
relative_content_type = ""; relative_content_type = String {};
} else { } else {
// b. Else let relativeContentType be set to contentType and run the substeps below: // b. Else let relativeContentType be set to contentType and run the substeps below:
// FIXME: 1. If relativeContentType contains any characters outside the range of U+0020 to U+007E, then set relativeContentType to the empty string and return from these substeps. // FIXME: 1. If relativeContentType contains any characters outside the range of U+0020 to U+007E, then set relativeContentType to the empty string and return from these substeps.
// 2. Convert every character in relativeContentType to ASCII lowercase. // 2. Convert every character in relativeContentType to ASCII lowercase.
relative_content_type = content_type->to_lowercase(); relative_content_type = TRY_OR_THROW_OOM(vm, Infra::to_ascii_lower_case(content_type.value()));
} }
// 4. Let span be max((relativeEnd - relativeStart), 0). // 4. Let span be max((relativeEnd - relativeStart), 0).
@ -238,20 +243,23 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::slice(Optional<i64> start, Opt
// a. S refers to span consecutive bytes from this, beginning with the byte at byte-order position relativeStart. // a. S refers to span consecutive bytes from this, beginning with the byte at byte-order position relativeStart.
// b. S.size = span. // b. S.size = span.
// c. S.type = relativeContentType. // c. S.type = relativeContentType.
auto byte_buffer = TRY_OR_THROW_OOM(realm().vm(), m_byte_buffer.slice(relative_start, span)); auto byte_buffer = TRY_OR_THROW_OOM(vm, m_byte_buffer.slice(relative_start, span));
return MUST_OR_THROW_OOM(heap().allocate<Blob>(realm(), realm(), move(byte_buffer), move(relative_content_type))); return MUST_OR_THROW_OOM(heap().allocate<Blob>(realm(), realm(), move(byte_buffer), move(relative_content_type)));
} }
// https://w3c.github.io/FileAPI/#dom-blob-text // https://w3c.github.io/FileAPI/#dom-blob-text
JS::Promise* Blob::text() WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> Blob::text()
{ {
auto& realm = this->realm();
auto& vm = realm.vm();
// FIXME: 1. Let stream be the result of calling get stream on this. // FIXME: 1. Let stream be the result of calling get stream on this.
// FIXME: 2. Let reader be the result of getting a reader from stream. If that threw an exception, return a new promise rejected with that exception. // FIXME: 2. Let reader be the result of getting a reader from stream. If that threw an exception, return a new promise rejected with that exception.
// FIXME: We still need to implement ReadableStream for this step to be fully valid. // FIXME: We still need to implement ReadableStream for this step to be fully valid.
// 3. Let promise be the result of reading all bytes from stream with reader // 3. Let promise be the result of reading all bytes from stream with reader
auto promise = JS::Promise::create(realm()); auto promise = JS::Promise::create(realm);
auto result = JS::PrimitiveString::create(vm(), DeprecatedString { m_byte_buffer.bytes() }); auto result = TRY(Bindings::throw_dom_exception_if_needed(vm, [&]() { return JS::PrimitiveString::create(vm, StringView { m_byte_buffer.bytes() }); }));
// 4. Return the result of transforming promise by a fulfillment handler that returns the result of running UTF-8 decode on its first argument. // 4. Return the result of transforming promise by a fulfillment handler that returns the result of running UTF-8 decode on its first argument.
promise->fulfill(result); promise->fulfill(result);

View file

@ -1,12 +1,11 @@
/* /*
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> * Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#pragma once #pragma once
#include <AK/DeprecatedString.h>
#include <AK/NonnullRefPtr.h> #include <AK/NonnullRefPtr.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibWeb/Bindings/BlobPrototype.h> #include <LibWeb/Bindings/BlobPrototype.h>
@ -16,14 +15,14 @@
namespace Web::FileAPI { namespace Web::FileAPI {
using BlobPart = Variant<JS::Handle<JS::Object>, JS::Handle<Blob>, DeprecatedString>; using BlobPart = Variant<JS::Handle<JS::Object>, JS::Handle<Blob>, String>;
struct BlobPropertyBag { struct BlobPropertyBag {
DeprecatedString type = DeprecatedString::empty(); String type = String {};
Bindings::EndingType endings; Bindings::EndingType endings;
}; };
[[nodiscard]] ErrorOr<DeprecatedString> convert_line_endings_to_native(DeprecatedString const& string); [[nodiscard]] ErrorOr<String> convert_line_endings_to_native(StringView string);
[[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {}); [[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {});
[[nodiscard]] bool is_basic_latin(StringView view); [[nodiscard]] bool is_basic_latin(StringView view);
@ -33,24 +32,24 @@ class Blob : public Bindings::PlatformObject {
public: public:
virtual ~Blob() override; virtual ~Blob() override;
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(JS::Realm&, ByteBuffer, DeprecatedString type); static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(JS::Realm&, ByteBuffer, String type);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> construct_impl(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> construct_impl(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
// https://w3c.github.io/FileAPI/#dfn-size // https://w3c.github.io/FileAPI/#dfn-size
u64 size() const { return m_byte_buffer.size(); } u64 size() const { return m_byte_buffer.size(); }
// https://w3c.github.io/FileAPI/#dfn-type // https://w3c.github.io/FileAPI/#dfn-type
DeprecatedString const& type() const { return m_type; } String const& type() const { return m_type; }
WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<DeprecatedString> const& content_type = {}); WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {});
JS::Promise* text(); WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> text();
JS::Promise* array_buffer(); JS::Promise* array_buffer();
ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); } ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
protected: protected:
Blob(JS::Realm&, ByteBuffer, DeprecatedString type); Blob(JS::Realm&, ByteBuffer, String type);
Blob(JS::Realm&, ByteBuffer); Blob(JS::Realm&, ByteBuffer);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override; virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
@ -59,7 +58,7 @@ private:
explicit Blob(JS::Realm&); explicit Blob(JS::Realm&);
ByteBuffer m_byte_buffer {}; ByteBuffer m_byte_buffer {};
DeprecatedString m_type {}; String m_type {};
}; };
} }

View file

@ -1,4 +1,4 @@
[Exposed=(Window,Worker), Serializable] [Exposed=(Window,Worker), Serializable, UseNewAKString]
interface Blob { interface Blob {
constructor(optional sequence<BlobPart> blobParts, optional BlobPropertyBag options = {}); constructor(optional sequence<BlobPart> blobParts, optional BlobPropertyBag options = {});

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> * Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -8,10 +8,11 @@
#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/Completion.h>
#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/FileAPI/File.h> #include <LibWeb/FileAPI/File.h>
#include <LibWeb/Infra/Strings.h>
namespace Web::FileAPI { namespace Web::FileAPI {
File::File(JS::Realm& realm, ByteBuffer byte_buffer, DeprecatedString file_name, DeprecatedString type, i64 last_modified) File::File(JS::Realm& realm, ByteBuffer byte_buffer, String file_name, String type, i64 last_modified)
: Blob(realm, move(byte_buffer), move(type)) : Blob(realm, move(byte_buffer), move(type))
, m_name(move(file_name)) , m_name(move(file_name))
, m_last_modified(last_modified) , m_last_modified(last_modified)
@ -29,8 +30,10 @@ JS::ThrowCompletionOr<void> File::initialize(JS::Realm& realm)
File::~File() = default; File::~File() = default;
// https://w3c.github.io/FileAPI/#ref-for-dom-file-file // https://w3c.github.io/FileAPI/#ref-for-dom-file-file
WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vector<BlobPart> const& file_bits, DeprecatedString const& file_name, Optional<FilePropertyBag> const& options) WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
{ {
auto& vm = realm.vm();
// 1. Let bytes be the result of processing blob parts given fileBits and options. // 1. Let bytes be the result of processing blob parts given fileBits and options.
auto bytes = TRY_OR_THROW_OOM(realm.vm(), process_blob_parts(file_bits, options.has_value() ? static_cast<BlobPropertyBag const&>(*options) : Optional<BlobPropertyBag> {})); auto bytes = TRY_OR_THROW_OOM(realm.vm(), process_blob_parts(file_bits, options.has_value() ? static_cast<BlobPropertyBag const&>(*options) : Optional<BlobPropertyBag> {}));
@ -38,7 +41,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vecto
// 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. // 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.
auto name = file_name; auto name = file_name;
auto type = DeprecatedString::empty(); auto type = String {};
i64 last_modified = 0; i64 last_modified = 0;
// 3. Process FilePropertyBag dictionary argument by running the following substeps: // 3. Process FilePropertyBag dictionary argument by running the following substeps:
if (options.has_value()) { if (options.has_value()) {
@ -52,7 +55,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vecto
// 2. Convert every character in t to ASCII lowercase. // 2. Convert every character in t to ASCII lowercase.
if (!type.is_empty()) if (!type.is_empty())
type = options->type.to_lowercase(); type = TRY_OR_THROW_OOM(vm, Infra::to_ascii_lower_case(type));
// 3. If the lastModified member is provided, let d be set to the lastModified dictionary member. If it is not provided, set d to the current date and time represented as the number of milliseconds since the Unix Epoch (which is the equivalent of Date.now() [ECMA-262]). // 3. If the lastModified member is provided, let d be set to the lastModified dictionary member. If it is not provided, set d to the current date and time represented as the number of milliseconds since the Unix Epoch (which is the equivalent of Date.now() [ECMA-262]).
// Note: Since ECMA-262 Date objects convert to long long values representing the number of milliseconds since the Unix Epoch, the lastModified member could be a Date object [ECMA-262]. // Note: Since ECMA-262 Date objects convert to long long values representing the number of milliseconds since the Unix Epoch, the lastModified member could be a Date object [ECMA-262].
@ -69,7 +72,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vecto
return MUST_OR_THROW_OOM(realm.heap().allocate<File>(realm, realm, move(bytes), move(name), move(type), last_modified)); return MUST_OR_THROW_OOM(realm.heap().allocate<File>(realm, realm, move(bytes), move(name), move(type), last_modified));
} }
WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::construct_impl(JS::Realm& realm, Vector<BlobPart> const& file_bits, DeprecatedString const& file_name, Optional<FilePropertyBag> const& options) WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::construct_impl(JS::Realm& realm, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
{ {
return create(realm, file_bits, file_name, options); return create(realm, file_bits, file_name, options);
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> * Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -18,22 +18,22 @@ class File : public Blob {
WEB_PLATFORM_OBJECT(File, Blob); WEB_PLATFORM_OBJECT(File, Blob);
public: public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> create(JS::Realm&, Vector<BlobPart> const& file_bits, DeprecatedString const& file_name, Optional<FilePropertyBag> const& options = {}); static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> create(JS::Realm&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> construct_impl(JS::Realm&, Vector<BlobPart> const& file_bits, DeprecatedString const& file_name, Optional<FilePropertyBag> const& options = {}); static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> construct_impl(JS::Realm&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {});
virtual ~File() override; virtual ~File() override;
// https://w3c.github.io/FileAPI/#dfn-name // https://w3c.github.io/FileAPI/#dfn-name
DeprecatedString const& name() const { return m_name; } String const& name() const { return m_name; }
// https://w3c.github.io/FileAPI/#dfn-lastModified // https://w3c.github.io/FileAPI/#dfn-lastModified
i64 last_modified() const { return m_last_modified; } i64 last_modified() const { return m_last_modified; }
private: private:
File(JS::Realm&, ByteBuffer, DeprecatedString file_name, DeprecatedString type, i64 last_modified); File(JS::Realm&, ByteBuffer, String file_name, String type, i64 last_modified);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override; virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
DeprecatedString m_name; String m_name;
i64 m_last_modified { 0 }; i64 m_last_modified { 0 };
}; };

View file

@ -1,6 +1,6 @@
#import <FileAPI/Blob.idl> #import <FileAPI/Blob.idl>
[Exposed=(Window,Worker), Serializable] [Exposed=(Window,Worker), Serializable, UseNewAKString]
interface File : Blob { interface File : Blob {
constructor(sequence<BlobPart> fileBits, USVString fileName, optional FilePropertyBag options = {}); constructor(sequence<BlobPart> fileBits, USVString fileName, optional FilePropertyBag options = {});

View file

@ -36,7 +36,7 @@ WebIDL::ExceptionOr<XHR::FormDataEntry> create_entry(JS::Realm& realm, String co
name_attribute = filename.value(); name_attribute = filename.value();
else else
name_attribute = TRY_OR_THROW_OOM(vm, "blob"_string); name_attribute = TRY_OR_THROW_OOM(vm, "blob"_string);
return JS::make_handle(TRY(FileAPI::File::create(realm, { JS::make_handle(*blob) }, name_attribute.to_deprecated_string(), {}))); return JS::make_handle(TRY(FileAPI::File::create(realm, { JS::make_handle(*blob) }, move(name_attribute), {})));
})); }));
// 4. Return an entry whose name is name and whose value is value. // 4. Return an entry whose name is name and whose value is value.
@ -130,8 +130,8 @@ WebIDL::ExceptionOr<Optional<Vector<XHR::FormDataEntry>>> construct_entry_list(J
// 1. If there are no selected files, then create an entry with name and a new File object with an empty name, application/octet-stream as type, and an empty body, and append it to entry list. // 1. If there are no selected files, then create an entry with name and a new File object with an empty name, application/octet-stream as type, and an empty body, and append it to entry list.
if (file_element->files()->length() == 0) { if (file_element->files()->length() == 0) {
FileAPI::FilePropertyBag options {}; FileAPI::FilePropertyBag options {};
options.type = "application/octet-stream"; options.type = TRY_OR_THROW_OOM(vm, "application/octet-stream"_string);
auto file = TRY(FileAPI::File::create(realm, {}, "", options)); auto file = TRY(FileAPI::File::create(realm, {}, String {}, options));
TRY_OR_THROW_OOM(vm, entry_list.try_append(XHR::FormDataEntry { .name = move(name), .value = JS::make_handle(file) })); TRY_OR_THROW_OOM(vm, entry_list.try_append(XHR::FormDataEntry { .name = move(name), .value = JS::make_handle(file) }));
} }
// 2. Otherwise, for each file in selected files, create an entry with name and a File object representing the file, and append it to entry list. // 2. Otherwise, for each file in selected files, create an entry with name and a File object representing the file, and append it to entry list.

View file

@ -3,7 +3,7 @@
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org> * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org> * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org> * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> * Copyright (c) 2022-2023, Kenneth Myhra <kennethmyhra@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -161,7 +161,8 @@ WebIDL::ExceptionOr<JS::Value> XMLHttpRequest::response()
} }
// 6. Otherwise, if thiss response type is "blob", set thiss response object to a new Blob object representing thiss received bytes with type set to the result of get a final MIME type for this. // 6. Otherwise, if thiss response type is "blob", set thiss response object to a new Blob object representing thiss received bytes with type set to the result of get a final MIME type for this.
else if (m_response_type == Bindings::XMLHttpRequestResponseType::Blob) { else if (m_response_type == Bindings::XMLHttpRequestResponseType::Blob) {
auto blob_part = TRY(FileAPI::Blob::create(realm(), m_received_bytes, get_final_mime_type().type())); auto mime_type_as_string = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(get_final_mime_type().type()));
auto blob_part = TRY(FileAPI::Blob::create(realm(), m_received_bytes, move(mime_type_as_string)));
auto blob = TRY(FileAPI::Blob::create(realm(), Vector<FileAPI::BlobPart> { JS::make_handle(*blob_part) })); auto blob = TRY(FileAPI::Blob::create(realm(), Vector<FileAPI::BlobPart> { JS::make_handle(*blob_part) }));
m_response_object = JS::Value(blob.ptr()); m_response_object = JS::Value(blob.ptr());
} }