From 17f34488f6629d07476f4bc191276df0b10d541e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 25 Feb 2022 18:42:37 +0100 Subject: LibWeb: Implement HTMLImageElement.width and HTMLImageElement.height --- .../Libraries/LibWeb/HTML/HTMLImageElement.cpp | 46 ++++++++++++++++++++++ Userland/Libraries/LibWeb/HTML/HTMLImageElement.h | 6 +++ .../Libraries/LibWeb/HTML/HTMLImageElement.idl | 3 ++ 3 files changed, 55 insertions(+) (limited to 'Userland/Libraries/LibWeb/HTML') diff --git a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp index dbb71c812e..b2e3c4d94b 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp @@ -78,4 +78,50 @@ const Gfx::Bitmap* HTMLImageElement::bitmap() const return m_image_loader.bitmap(m_image_loader.current_frame_index()); } +// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-width +unsigned HTMLImageElement::width() const +{ + const_cast(document()).update_layout(); + + // Return the rendered width of the image, in CSS pixels, if the image is being rendered. + if (layout_node() && is(*layout_node())) + return static_cast(*layout_node()).content_width(); + + // ...or else the density-corrected intrinsic width and height of the image, in CSS pixels, + // if the image has intrinsic dimensions and is available but not being rendered. + if (m_image_loader.has_image()) + return m_image_loader.width(); + + // ...or else 0, if the image is not available or does not have intrinsic dimensions. + return 0; +} + +void HTMLImageElement::set_width(unsigned width) +{ + set_attribute(HTML::AttributeNames::width, String::number(width)); +} + +// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-height +unsigned HTMLImageElement::height() const +{ + const_cast(document()).update_layout(); + + // Return the rendered height of the image, in CSS pixels, if the image is being rendered. + if (layout_node() && is(*layout_node())) + return static_cast(*layout_node()).content_height(); + + // ...or else the density-corrected intrinsic height and height of the image, in CSS pixels, + // if the image has intrinsic dimensions and is available but not being rendered. + if (m_image_loader.has_image()) + return m_image_loader.height(); + + // ...or else 0, if the image is not available or does not have intrinsic dimensions. + return 0; +} + +void HTMLImageElement::set_height(unsigned height) +{ + set_attribute(HTML::AttributeNames::height, String::number(height)); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.h b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.h index 710db31f20..29531f56f2 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.h @@ -28,6 +28,12 @@ public: const Gfx::Bitmap* bitmap() const; + unsigned width() const; + void set_width(unsigned); + + unsigned height() const; + void set_height(unsigned); + private: virtual void apply_presentational_hints(CSS::StyleProperties&) const override; diff --git a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.idl index 5e8d6f508d..f805d45036 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLImageElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLImageElement.idl @@ -13,4 +13,7 @@ interface HTMLImageElement : HTMLElement { [Reflect] attribute DOMString align; [LegacyNullToEmptyString, Reflect] attribute DOMString border; + [CEReactions] attribute unsigned long width; + [CEReactions] attribute unsigned long height; + }; -- cgit v1.2.3