From b39fdcfec23c914035d50957e356ee5f53554e5a Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Fri, 29 Nov 2024 17:56:24 +0000 Subject: [PATCH] LibWeb: Implement the `HTMLInputElement.height` attribute This allows the height of an image button input to be set and queried. --- Libraries/LibWeb/HTML/HTMLInputElement.cpp | 35 +++++++++++++++++++ Libraries/LibWeb/HTML/HTMLInputElement.h | 3 ++ Libraries/LibWeb/HTML/HTMLInputElement.idl | 2 +- .../HTML/unsigned-long-reflection.txt | 20 +++++++++++ .../input/HTML/unsigned-long-reflection.html | 1 + 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp index 333e0b0be87..9ff16d191cb 100644 --- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -1882,6 +1882,41 @@ WebIDL::ExceptionOr HTMLInputElement::set_size(WebIDL::UnsignedLong value) return set_attribute(HTML::AttributeNames::size, String::number(value)); } +// https://html.spec.whatwg.org/multipage/input.html#dom-input-height +WebIDL::UnsignedLong HTMLInputElement::height() const +{ + const_cast(document()).update_layout(); + + // When the input element's type attribute is not in the Image Button state, then no image is available. + if (type_state() != TypeAttributeState::ImageButton) + return 0; + + // Return the rendered height of the image, in CSS pixels, if the image is being rendered. + if (auto* paintable_box = this->paintable_box()) + return paintable_box->content_height().to_int(); + + // On setting [the width or height IDL attribute], they must act as if they reflected the respective content attributes of the same name. + if (auto height_string = get_attribute(HTML::AttributeNames::height); height_string.has_value()) { + if (auto height = parse_non_negative_integer(*height_string); height.has_value() && *height <= 2147483647) + return *height; + } + + // ...or else the natural height and height of the image, in CSS pixels, if an image is available but not being rendered + if (auto bitmap = current_image_bitmap()) + return bitmap->height(); + + // ...or else 0, if the image is not available or does not have intrinsic dimensions. + return 0; +} + +WebIDL::ExceptionOr HTMLInputElement::set_height(WebIDL::UnsignedLong value) +{ + if (value > 2147483647) + value = 0; + + return set_attribute(HTML::AttributeNames::height, String::number(value)); +} + // https://html.spec.whatwg.org/multipage/input.html#dom-input-width WebIDL::UnsignedLong HTMLInputElement::width() const { diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.h b/Libraries/LibWeb/HTML/HTMLInputElement.h index 1a6dc787bdc..30369be9abe 100644 --- a/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -128,6 +128,9 @@ public: WebIDL::UnsignedLong size() const; WebIDL::ExceptionOr set_size(WebIDL::UnsignedLong value); + WebIDL::UnsignedLong height() const; + WebIDL::ExceptionOr set_height(WebIDL::UnsignedLong value); + WebIDL::UnsignedLong width() const; WebIDL::ExceptionOr set_width(WebIDL::UnsignedLong value); diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.idl b/Libraries/LibWeb/HTML/HTMLInputElement.idl index eba5df54d9d..c2824993ba7 100644 --- a/Libraries/LibWeb/HTML/HTMLInputElement.idl +++ b/Libraries/LibWeb/HTML/HTMLInputElement.idl @@ -22,7 +22,7 @@ interface HTMLInputElement : HTMLElement { [CEReactions, Reflect=formmethod, Enumerated=FormMethodAttribute] attribute DOMString formMethod; [CEReactions, Reflect=formnovalidate] attribute boolean formNoValidate; [CEReactions, Reflect=formtarget] attribute DOMString formTarget; - [FIXME, CEReactions] attribute unsigned long height; + [CEReactions] attribute unsigned long height; attribute boolean indeterminate; [FIXME] readonly attribute HTMLDataListElement? list; [CEReactions, Reflect] attribute DOMString max; diff --git a/Tests/LibWeb/Text/expected/HTML/unsigned-long-reflection.txt b/Tests/LibWeb/Text/expected/HTML/unsigned-long-reflection.txt index d1c0fac3f76..2ccb8a17b86 100644 --- a/Tests/LibWeb/Text/expected/HTML/unsigned-long-reflection.txt +++ b/Tests/LibWeb/Text/expected/HTML/unsigned-long-reflection.txt @@ -37,6 +37,26 @@ input.getAttribute("size") after input.setAttribute("size", "4294967295"): 42949 input.size after input.setAttribute("size", "4294967295"): 20 input.getAttribute("size") after input.size = 4294967295: 20 input.size after input.size = 4294967295: 20 +input.getAttribute("height") after input.setAttribute("height", "0"): 0 +input.height after input.setAttribute("height", "0"): 0 +input.getAttribute("height") after input.height = 0: 0 +input.height after input.height = 0: 0 +input.getAttribute("height") after input.setAttribute("height", "1"): 1 +input.height after input.setAttribute("height", "1"): 1 +input.getAttribute("height") after input.height = 1: 1 +input.height after input.height = 1: 0 +input.getAttribute("height") after input.setAttribute("height", "2147483647"): 2147483647 +input.height after input.setAttribute("height", "2147483647"): 2147483647 +input.getAttribute("height") after input.height = 2147483647: 2147483647 +input.height after input.height = 2147483647: 0 +input.getAttribute("height") after input.setAttribute("height", "2147483648"): 2147483648 +input.height after input.setAttribute("height", "2147483648"): 0 +input.getAttribute("height") after input.height = 2147483648: 0 +input.height after input.height = 2147483648: 0 +input.getAttribute("height") after input.setAttribute("height", "4294967295"): 4294967295 +input.height after input.setAttribute("height", "4294967295"): 0 +input.getAttribute("height") after input.height = 4294967295: 0 +input.height after input.height = 4294967295: 0 input.getAttribute("width") after input.setAttribute("width", "0"): 0 input.width after input.setAttribute("width", "0"): 0 input.getAttribute("width") after input.width = 0: 0 diff --git a/Tests/LibWeb/Text/input/HTML/unsigned-long-reflection.html b/Tests/LibWeb/Text/input/HTML/unsigned-long-reflection.html index 9b8177a6d21..2a65060b79b 100644 --- a/Tests/LibWeb/Text/input/HTML/unsigned-long-reflection.html +++ b/Tests/LibWeb/Text/input/HTML/unsigned-long-reflection.html @@ -44,6 +44,7 @@ testProperty("img", "hspace", (img) => img.hspace, (img, value) => img.hspace = value); testProperty("input", "size", (input) => input.size, (input, value) => input.size = value); + testProperty(imageButtonInputFactory, "height", (input) => input.height, (input, value) => input.height = value); testProperty(imageButtonInputFactory, "width", (input) => input.width, (input, value) => input.width = value); testProperty("marquee", "scrollAmount", (marquee) => marquee.scrollAmount, (marquee, value) => marquee.scrollAmount = value); testProperty("marquee", "scrollDelay", (marquee) => marquee.scrollDelay, (marquee, value) => marquee.scrollDelay = value);