From 2c925244fd5d195639481b9d159f716e3355d7b7 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Thu, 14 Nov 2024 14:14:36 +0100 Subject: [PATCH 1/2] AK: Add Flatten Variant helper This will be used in future commits It has to be present for types, that are generated by the idl since the idl flattens variants of variants --- AK/Variant.h | 15 +++++++++++++++ Tests/AK/TestVariant.cpp | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/AK/Variant.h b/AK/Variant.h index 7ae5619ef63..58a014da765 100644 --- a/AK/Variant.h +++ b/AK/Variant.h @@ -499,9 +499,24 @@ private: template struct TypeList> : TypeList { }; +namespace Detail { +template +struct FlattenVariant; + +template +struct FlattenVariant<::AK::Variant, ::AK::Variant> { + using type = ::AK::Variant; +}; + +} + +template +using FlattenVariant = Detail::FlattenVariant::type; + } #if USING_AK_GLOBALLY using AK::Empty; +using AK::FlattenVariant; using AK::Variant; #endif diff --git a/Tests/AK/TestVariant.cpp b/Tests/AK/TestVariant.cpp index 44806a43101..63d61b7f6f3 100644 --- a/Tests/AK/TestVariant.cpp +++ b/Tests/AK/TestVariant.cpp @@ -302,3 +302,19 @@ TEST_CASE(variant_equality) EXPECT_EQ(variant1, variant2); } } + +TEST_CASE(flatten_variant) +{ + using InnerVariant = Variant; + using OuterVariant = FlattenVariant>; + using MyVariant = Variant; + + EXPECT_EQ((TypeList::size), 3u); + EXPECT_EQ((TypeList::size), 3u); + EXPECT((IsSame)); + + using OuterList = TypeList; + EXPECT((IsSame, Empty>)); + EXPECT((IsSame, int>)); + EXPECT((IsSame, float>)); +} From ad2bce7fa02bad416e7d83d0bfdd523f043e324d Mon Sep 17 00:00:00 2001 From: Totto16 Date: Thu, 14 Nov 2024 16:25:38 +0100 Subject: [PATCH 2/2] LibWeb: Add CanvasImageSource to ImageBitmapSource typedef --- Libraries/LibWeb/HTML/HTMLOrSVGElement.idl | 2 ++ Libraries/LibWeb/HTML/ImageBitmap.h | 2 +- Libraries/LibWeb/HTML/ImageBitmap.idl | 8 ++++-- .../LibWeb/HTML/WindowOrWorkerGlobalScope.cpp | 25 ++++++++++++++----- Libraries/LibWeb/HTML/WorkerGlobalScope.idl | 1 + Libraries/LibWeb/SVG/SVGElement.idl | 3 ++- ...age-bitmap-from-invalid-types-no-crash.txt | 8 +++--- ...ge-bitmap-from-invalid-types-no-crash.html | 2 +- 8 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Libraries/LibWeb/HTML/HTMLOrSVGElement.idl b/Libraries/LibWeb/HTML/HTMLOrSVGElement.idl index 88c4b22c5ad..3f7641e4c65 100644 --- a/Libraries/LibWeb/HTML/HTMLOrSVGElement.idl +++ b/Libraries/LibWeb/HTML/HTMLOrSVGElement.idl @@ -1,3 +1,5 @@ +#import + // https://html.spec.whatwg.org/#htmlorsvgelement interface mixin HTMLOrSVGElement { [SameObject] readonly attribute DOMStringMap dataset; diff --git a/Libraries/LibWeb/HTML/ImageBitmap.h b/Libraries/LibWeb/HTML/ImageBitmap.h index c1ede98b76e..4e1ad6641bd 100644 --- a/Libraries/LibWeb/HTML/ImageBitmap.h +++ b/Libraries/LibWeb/HTML/ImageBitmap.h @@ -16,7 +16,7 @@ namespace Web::HTML { -using ImageBitmapSource = Variant, GC::Root>; +using ImageBitmapSource = FlattenVariant, GC::Root>>; // https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#imagebitmapoptions struct ImageBitmapOptions { diff --git a/Libraries/LibWeb/HTML/ImageBitmap.idl b/Libraries/LibWeb/HTML/ImageBitmap.idl index d2ec371ab18..644655485fd 100644 --- a/Libraries/LibWeb/HTML/ImageBitmap.idl +++ b/Libraries/LibWeb/HTML/ImageBitmap.idl @@ -1,5 +1,7 @@ #import #import +#import +#import // https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#images-2 [Exposed=(Window,Worker), Serializable, Transferable] @@ -9,8 +11,10 @@ interface ImageBitmap { undefined close(); }; -// FIXME: This should also includes CanvasImageSource -typedef (Blob or + +// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#imagebitmapsource +typedef (CanvasImageSource or + Blob or ImageData) ImageBitmapSource; enum ImageOrientation { "from-image", "flipY" }; diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp index 4d93c84cc5d..04f19d618fa 100644 --- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp +++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp @@ -133,12 +133,25 @@ GC::Ref WindowOrWorkerGlobalScopeMixin::create_image_bitmap_imp (void)options; // 3. Check the usability of the image argument. If this throws an exception or returns bad, then return a promise rejected with an "InvalidStateError" DOMException. - // FIXME: "Check the usability of the image argument" is only defined for CanvasImageSource, let's skip it for other types - if (image.has()) { - if (auto usability = check_usability_of_image(image.get()); usability.is_error() or usability.value() == CanvasImageSourceUsability::Bad) { - auto error = WebIDL::InvalidStateError::create(this_impl().realm(), "image argument is not usable"_string); - return WebIDL::create_rejected_promise_from_exception(realm, error); - } + auto error_promise = image.visit( + [](GC::Root&) -> Optional> { + return {}; + }, + [](GC::Root&) -> Optional> { + return {}; + }, + [&](auto& canvas_image_source) -> Optional> { + // Note: "Check the usability of the image argument" is only defined for CanvasImageSource + if (auto usability = check_usability_of_image(canvas_image_source); usability.is_error() or usability.value() == CanvasImageSourceUsability::Bad) { + auto error = WebIDL::InvalidStateError::create(this_impl().realm(), "image argument is not usable"_string); + return WebIDL::create_rejected_promise_from_exception(realm, error); + } + + return {}; + }); + + if (error_promise.has_value()) { + return error_promise.release_value(); } // 4. Let p be a new promise. diff --git a/Libraries/LibWeb/HTML/WorkerGlobalScope.idl b/Libraries/LibWeb/HTML/WorkerGlobalScope.idl index 07c1901deb4..c801754609f 100644 --- a/Libraries/LibWeb/HTML/WorkerGlobalScope.idl +++ b/Libraries/LibWeb/HTML/WorkerGlobalScope.idl @@ -3,6 +3,7 @@ #import #import #import +#import #import #import #import diff --git a/Libraries/LibWeb/SVG/SVGElement.idl b/Libraries/LibWeb/SVG/SVGElement.idl index d132fa57892..383667be6ed 100644 --- a/Libraries/LibWeb/SVG/SVGElement.idl +++ b/Libraries/LibWeb/SVG/SVGElement.idl @@ -1,6 +1,7 @@ #import #import -#import +#import +#import #import #import #import diff --git a/Tests/LibWeb/Text/expected/HTML/image-bitmap-from-invalid-types-no-crash.txt b/Tests/LibWeb/Text/expected/HTML/image-bitmap-from-invalid-types-no-crash.txt index 43be68f8969..e9d593bdc3f 100644 --- a/Tests/LibWeb/Text/expected/HTML/image-bitmap-from-invalid-types-no-crash.txt +++ b/Tests/LibWeb/Text/expected/HTML/image-bitmap-from-invalid-types-no-crash.txt @@ -1,7 +1,7 @@ Blob [Success]: [object ImageBitmap] ImageData [ Error ]: Error: Not Implemented: createImageBitmap() for non-blob types -HTMLImageElement [ Error ]: TypeError: No union types matched -SVGImageElement [ Error ]: TypeError: No union types matched -HTMLCanvasElement [ Error ]: TypeError: No union types matched +HTMLImageElement [ Error ]: InvalidStateError: image argument is not usable +SVGImageElement [ Error ]: InvalidStateError: image argument is not usable +HTMLCanvasElement [ Error ]: Error: Not Implemented: createImageBitmap() for non-blob types ImageBitmap [ Error ]: TypeError: No union types matched -HTMLVideoElement [ Error ]: TypeError: No union types matched +HTMLVideoElement [ Error ]: InvalidStateError: image argument is not usable diff --git a/Tests/LibWeb/Text/input/HTML/image-bitmap-from-invalid-types-no-crash.html b/Tests/LibWeb/Text/input/HTML/image-bitmap-from-invalid-types-no-crash.html index 4d9bb8a276d..7c5ba4d8418 100644 --- a/Tests/LibWeb/Text/input/HTML/image-bitmap-from-invalid-types-no-crash.html +++ b/Tests/LibWeb/Text/input/HTML/image-bitmap-from-invalid-types-no-crash.html @@ -22,7 +22,7 @@ 255, 10, 8, 16, 0, 9, 8, 6, 1, 0, 40, 0, 75, 56, 73, 152, 108, 128, 253, 145, 96, 0, ]), ]); - let imageBitmap = createImageBitmap(file, 0, 0, 0, 0); + let imageBitmap = createImageBitmap(file, 0, 0, 20, 20); const types = [ [file, "Blob"],