From ad7f7235bcfc8d3537a3c121c21de297510323ae Mon Sep 17 00:00:00 2001 From: justus2510 Date: Fri, 8 Nov 2024 09:13:54 +0100 Subject: [PATCH] LibWeb: Fix Canvas.toDataURL and Canvas.toBlob signatures Fix the function signatures of Canvas.toDataURL() and Canvas.toBlob() and make both functions accept non-numbers as the quality parameter, in which case it will just use the default quality instead of raising an exception. This makes toDataURL.arguments.1.html, toDataURL.arguments.2.html and toDataURL.jpeg.quality.notnumber.html in wpt/html/semantics/embedded-content/the-canvas-element pass :^) --- .../Libraries/LibWeb/HTML/HTMLCanvasElement.cpp | 13 ++++++------- Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.h | 4 ++-- .../Libraries/LibWeb/HTML/HTMLCanvasElement.idl | 4 ++-- Userland/Libraries/LibWeb/WebDriver/Screenshot.cpp | 3 ++- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp index 9f9ca5d2edee..1407a4e3a82d 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp @@ -230,19 +230,18 @@ struct SerializeBitmapResult { }; // https://html.spec.whatwg.org/multipage/canvas.html#a-serialisation-of-the-bitmap-as-a-file -static ErrorOr serialize_bitmap(Gfx::Bitmap const& bitmap, StringView type, Optional quality) +static ErrorOr serialize_bitmap(Gfx::Bitmap const& bitmap, StringView type, JS::Value quality) { // If type is an image format that supports variable quality (such as "image/jpeg"), quality is given, and type is not "image/png", then, // if quality is a Number in the range 0.0 to 1.0 inclusive, the user agent must treat quality as the desired quality level. // Otherwise, the user agent must use its default quality value, as if the quality argument had not been given. - if (quality.has_value() && !(*quality >= 0.0 && *quality <= 1.0)) - quality = OptionalNone {}; + bool valid_quality = quality.is_number() && quality.as_double() >= 0.0 && quality.as_double() <= 1.0; if (type.equals_ignoring_ascii_case("image/jpeg"sv)) { AllocatingMemoryStream file; Gfx::JPEGWriter::Options jpeg_options; - if (quality.has_value()) - jpeg_options.quality = static_cast(quality.value() * 100); + if (valid_quality) + jpeg_options.quality = static_cast(quality.as_double() * 100); TRY(Gfx::JPEGWriter::encode(file, bitmap, jpeg_options)); return SerializeBitmapResult { TRY(file.read_until_eof()), "image/jpeg"sv }; } @@ -253,7 +252,7 @@ static ErrorOr serialize_bitmap(Gfx::Bitmap const& bitmap } // https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-todataurl -String HTMLCanvasElement::to_data_url(StringView type, Optional quality) +String HTMLCanvasElement::to_data_url(StringView type, JS::Value quality) { // It is possible the the canvas doesn't have a associated bitmap so create one if (!m_surface) { @@ -288,7 +287,7 @@ String HTMLCanvasElement::to_data_url(StringView type, Optional quality) } // https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-toblob -WebIDL::ExceptionOr HTMLCanvasElement::to_blob(JS::NonnullGCPtr callback, StringView type, Optional quality) +WebIDL::ExceptionOr HTMLCanvasElement::to_blob(JS::NonnullGCPtr callback, StringView type, JS::Value quality) { // It is possible the the canvas doesn't have a associated bitmap so create one if (!m_surface) { diff --git a/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.h b/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.h index 786c6f7dfdee..36d4a016e649 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.h @@ -35,8 +35,8 @@ class HTMLCanvasElement final : public HTMLElement { WebIDL::ExceptionOr set_width(unsigned); WebIDL::ExceptionOr set_height(unsigned); - String to_data_url(StringView type, Optional quality); - WebIDL::ExceptionOr to_blob(JS::NonnullGCPtr callback, StringView type, Optional quality); + String to_data_url(StringView type, JS::Value quality); + WebIDL::ExceptionOr to_blob(JS::NonnullGCPtr callback, StringView type, JS::Value quality); void present(); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.idl index 60c03dd4f7ac..4c18314788c2 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLCanvasElement.idl @@ -16,8 +16,8 @@ interface HTMLCanvasElement : HTMLElement { RenderingContext? getContext(DOMString contextId, optional any options = null); - USVString toDataURL(optional DOMString type = "image/png", optional double quality); - undefined toBlob(BlobCallback _callback, optional DOMString type = "image/png", optional double quality); + USVString toDataURL(optional DOMString type = "image/png", optional any quality); + undefined toBlob(BlobCallback _callback, optional DOMString type = "image/png", optional any quality); }; diff --git a/Userland/Libraries/LibWeb/WebDriver/Screenshot.cpp b/Userland/Libraries/LibWeb/WebDriver/Screenshot.cpp index 9c68bda7c537..791c79f3dcf1 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Screenshot.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/Screenshot.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -72,7 +73,7 @@ Response encode_canvas_element(HTML::HTMLCanvasElement& canvas) // 3. Let file be a serialization of the canvas element’s bitmap as a file, using "image/png" as an argument. // 4. Let data url be a data: URL representing file. [RFC2397] - auto data_url = canvas.to_data_url("image/png"sv, {}); + auto data_url = canvas.to_data_url("image/png"sv, JS::js_undefined()); // 5. Let index be the index of "," in data url. auto index = data_url.find_byte_offset(',');