LibWeb: Parse mimetype when creating Blob and File

This commit is contained in:
stelar7 2024-10-04 00:35:39 +02:00 committed by Andreas Kling
parent 0739f1b1e6
commit 193c0dcafc
Notes: github-actions[bot] 2024-10-04 18:57:01 +00:00
2 changed files with 19 additions and 19 deletions

View file

@ -17,6 +17,7 @@
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h> #include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/StructuredSerialize.h> #include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/Infra/Strings.h> #include <LibWeb/Infra/Strings.h>
#include <LibWeb/MimeSniff/MimeType.h>
#include <LibWeb/Streams/AbstractOperations.h> #include <LibWeb/Streams/AbstractOperations.h>
#include <LibWeb/Streams/ReadableStreamDefaultReader.h> #include <LibWeb/Streams/ReadableStreamDefaultReader.h>
#include <LibWeb/WebIDL/AbstractOperations.h> #include <LibWeb/WebIDL/AbstractOperations.h>
@ -197,17 +198,17 @@ JS::NonnullGCPtr<Blob> Blob::create(JS::Realm& realm, Optional<Vector<BlobPart>>
auto type = String {}; 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. // FIXME: 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member.
// If t contains any characters outside the range U+0020 to U+007E, then set t to the empty string and return from these substeps. // If t contains any characters outside the range U+0020 to U+007E, then set t to the empty string and return from these substeps.
// NOTE: t is set to empty string at declaration. // FIXME: 2. Convert every character in t to ASCII lowercase.
if (!options->type.is_empty()) {
if (is_basic_latin(options->type))
type = options->type;
}
// 2. Convert every character in t to ASCII lowercase. // NOTE: The spec is out of date, and we are supposed to call into the MimeType parser here.
if (!type.is_empty()) if (!options->type.is_empty()) {
type = MUST(Infra::to_ascii_lowercase(type)); auto maybe_parsed_type = MUST(Web::MimeSniff::MimeType::parse(options->type));
if (maybe_parsed_type.has_value())
type = MUST(maybe_parsed_type->serialized());
}
} }
// 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.

View file

@ -10,6 +10,7 @@
#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> #include <LibWeb/Infra/Strings.h>
#include <LibWeb/MimeSniff/MimeType.h>
namespace Web::FileAPI { namespace Web::FileAPI {
@ -46,7 +47,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vecto
auto& vm = realm.vm(); 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(vm, process_blob_parts(file_bits, options.has_value() ? static_cast<BlobPropertyBag const&>(*options) : Optional<BlobPropertyBag> {}));
// 2. Let n be the fileName argument to the constructor. // 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. // 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.
@ -56,17 +57,15 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vecto
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()) {
// 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member. // FIXME: 1. If the type member is provided and is not the empty string, let t be set to the type dictionary member.
// If t contains any characters outside the range U+0020 to U+007E, then set t to the empty string and return from these substeps. // If t contains any characters outside the range U+0020 to U+007E, then set t to the empty string and return from these substeps.
// NOTE: t is set to empty string at declaration. // FIXME: 2. Convert every character in t to ASCII lowercase.
if (!options->type.is_empty()) {
if (is_basic_latin(options->type))
type = options->type;
}
// 2. Convert every character in t to ASCII lowercase. // NOTE: The spec is out of date, and we are supposed to call into the MimeType parser here.
if (!type.is_empty()) auto maybe_parsed_type = MUST(Web::MimeSniff::MimeType::parse(options->type));
type = TRY_OR_THROW_OOM(vm, Infra::to_ascii_lowercase(type));
if (maybe_parsed_type.has_value())
type = MUST(maybe_parsed_type->serialized());
// 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].