瀏覽代碼

LibWeb: Add CanvasImageSource to ImageBitmapSource typedef

Totto16 8 月之前
父節點
當前提交
9730e47d51

+ 2 - 0
Libraries/LibWeb/HTML/HTMLOrSVGElement.idl

@@ -1,3 +1,5 @@
+#import <HTML/DOMStringMap.idl>
+
 // https://html.spec.whatwg.org/#htmlorsvgelement
 interface mixin HTMLOrSVGElement {
     [SameObject] readonly attribute DOMStringMap dataset;

+ 1 - 1
Libraries/LibWeb/HTML/ImageBitmap.h

@@ -16,7 +16,7 @@
 
 namespace Web::HTML {
 
-using ImageBitmapSource = Variant<CanvasImageSource, GC::Root<FileAPI::Blob>, GC::Root<ImageData>>;
+using ImageBitmapSource = FlattenVariant<CanvasImageSource, Variant<GC::Root<FileAPI::Blob>, GC::Root<ImageData>>>;
 
 // https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#imagebitmapoptions
 struct ImageBitmapOptions {

+ 6 - 2
Libraries/LibWeb/HTML/ImageBitmap.idl

@@ -1,5 +1,7 @@
 #import <FileAPI/Blob.idl>
 #import <HTML/ImageData.idl>
+#import <HTML/CanvasRenderingContext2D.idl>
+#import <HTML/Canvas/CanvasDrawImage.idl>
 
 // 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" };

+ 19 - 6
Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp

@@ -129,12 +129,25 @@ GC::Ref<WebIDL::Promise> 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<CanvasImageSource>()) {
-        if (auto usability = check_usability_of_image(image.get<CanvasImageSource>()); 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<FileAPI::Blob>&) -> Optional<GC::Ref<WebIDL::Promise>> {
+            return {};
+        },
+        [](GC::Root<ImageData>&) -> Optional<GC::Ref<WebIDL::Promise>> {
+            return {};
+        },
+        [&](auto& canvas_image_source) -> Optional<GC::Ref<WebIDL::Promise>> {
+            // 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.

+ 1 - 0
Libraries/LibWeb/HTML/WorkerGlobalScope.idl

@@ -3,6 +3,7 @@
 #import <DOM/EventHandler.idl>
 #import <HTML/MessagePort.idl>
 #import <HTML/UniversalGlobalScope.idl>
+#import <HTML/Window.idl>
 #import <HTML/WindowOrWorkerGlobalScope.idl>
 #import <HTML/WorkerLocation.idl>
 #import <HTML/WorkerNavigator.idl>

+ 2 - 1
Libraries/LibWeb/SVG/SVGElement.idl

@@ -1,6 +1,7 @@
 #import <CSS/ElementCSSInlineStyle.idl>
 #import <DOM/Element.idl>
-#import <HTML/HTMLElement.idl>
+#import <DOM/EventHandler.idl>
+#import <HTML/HTMLOrSVGElement.idl>
 #import <HTML/DOMStringMap.idl>
 #import <SVG/SVGAnimatedString.idl>
 #import <SVG/SVGSVGElement.idl>

+ 4 - 4
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

+ 1 - 1
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"],