LibWeb: Hoist overloaded ImageProvider methods to base class

All of these overloads did the same thing so lets just define them in
the base class
This commit is contained in:
Callum Law 2026-06-12 21:50:56 +12:00 committed by Jelle Raaijmakers
parent 60c6cc2f0f
commit a40b1183ad
11 changed files with 39 additions and 226 deletions

View file

@ -310,46 +310,6 @@ void HTMLImageElement::adjust_computed_style(CSS::ComputedProperties& style)
style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::None)));
}
Optional<Gfx::DecodedImageFrame> HTMLImageElement::default_image_frame_sized(Gfx::IntSize size) const
{
if (auto data = m_current_request->image_data())
return data->frame(0, size);
return {};
}
bool HTMLImageElement::is_image_available() const
{
return m_current_request && m_current_request->is_available();
}
Optional<CSSPixels> HTMLImageElement::intrinsic_width() const
{
if (auto image_data = m_current_request->image_data())
return image_data->intrinsic_width();
return {};
}
Optional<CSSPixels> HTMLImageElement::intrinsic_height() const
{
if (auto image_data = m_current_request->image_data())
return image_data->intrinsic_height();
return {};
}
Optional<CSSPixelFraction> HTMLImageElement::intrinsic_aspect_ratio() const
{
if (auto image_data = m_current_request->image_data())
return image_data->intrinsic_aspect_ratio();
return {};
}
Optional<Gfx::DecodedImageFrame> HTMLImageElement::current_image_frame_sized(Gfx::IntSize size) const
{
if (auto data = m_current_request->image_data())
return data->frame(m_current_frame_index, size);
return {};
}
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-width
WebIDL::UnsignedLong HTMLImageElement::width() const
{

View file

@ -49,8 +49,6 @@ public:
String alt() const { return get_attribute_value(HTML::AttributeNames::alt); }
virtual Optional<Gfx::DecodedImageFrame> default_image_frame_sized(Gfx::IntSize) const override;
WebIDL::UnsignedLong width() const;
void set_width(WebIDL::UnsignedLong);
@ -107,11 +105,6 @@ public:
bool allows_auto_sizes() const;
// ^Layout::ImageProvider
virtual bool is_image_available() const override;
virtual Optional<CSSPixels> intrinsic_width() const override;
virtual Optional<CSSPixels> intrinsic_height() const override;
virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
virtual Optional<Gfx::DecodedImageFrame> current_image_frame_sized(Gfx::IntSize) const override;
virtual GC::Ptr<DecodedImageData> decoded_image_data() const override;
virtual void visit_edges(Cell::Visitor&) override;

View file

@ -2314,39 +2314,6 @@ GC::Ptr<DecodedImageData> HTMLInputElement::image_data() const
return nullptr;
}
bool HTMLInputElement::is_image_available() const
{
return image_data() != nullptr;
}
Optional<CSSPixels> HTMLInputElement::intrinsic_width() const
{
if (auto image_data = this->image_data())
return image_data->intrinsic_width();
return {};
}
Optional<CSSPixels> HTMLInputElement::intrinsic_height() const
{
if (auto image_data = this->image_data())
return image_data->intrinsic_height();
return {};
}
Optional<CSSPixelFraction> HTMLInputElement::intrinsic_aspect_ratio() const
{
if (auto image_data = this->image_data())
return image_data->intrinsic_aspect_ratio();
return {};
}
Optional<Gfx::DecodedImageFrame> HTMLInputElement::current_image_frame_sized(Gfx::IntSize size) const
{
if (auto image_data = this->image_data())
return image_data->frame(0, size);
return {};
}
// https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
i32 HTMLInputElement::default_tab_index_value() const
{

View file

@ -287,11 +287,6 @@ private:
virtual bool supports_dimension_attributes() const override { return type_state() == TypeAttributeState::ImageButton; }
// ^Layout::ImageProvider
virtual bool is_image_available() const override;
virtual Optional<CSSPixels> intrinsic_width() const override;
virtual Optional<CSSPixels> intrinsic_height() const override;
virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
virtual Optional<Gfx::DecodedImageFrame> current_image_frame_sized(Gfx::IntSize) const override;
virtual size_t current_frame_index() const override { return 0; }
virtual GC::Ptr<HTML::DecodedImageData> decoded_image_data() const override { return image_data(); }

View file

@ -594,37 +594,4 @@ GC::Ptr<DecodedImageData> HTMLObjectElement::image_data() const
return m_resource_request->image_data();
}
bool HTMLObjectElement::is_image_available() const
{
return image_data() != nullptr;
}
Optional<CSSPixels> HTMLObjectElement::intrinsic_width() const
{
if (auto image_data = this->image_data())
return image_data->intrinsic_width();
return {};
}
Optional<CSSPixels> HTMLObjectElement::intrinsic_height() const
{
if (auto image_data = this->image_data())
return image_data->intrinsic_height();
return {};
}
Optional<CSSPixelFraction> HTMLObjectElement::intrinsic_aspect_ratio() const
{
if (auto image_data = this->image_data())
return image_data->intrinsic_aspect_ratio();
return {};
}
Optional<Gfx::DecodedImageFrame> HTMLObjectElement::current_image_frame_sized(Gfx::IntSize size) const
{
if (auto image_data = this->image_data())
return image_data->frame(0, size);
return {};
}
}

View file

@ -83,11 +83,6 @@ private:
virtual i32 default_tab_index_value() const override;
// ^Layout::ImageProvider
virtual bool is_image_available() const override;
virtual Optional<CSSPixels> intrinsic_width() const override;
virtual Optional<CSSPixels> intrinsic_height() const override;
virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
virtual Optional<Gfx::DecodedImageFrame> current_image_frame_sized(Gfx::IntSize) const override;
virtual size_t current_frame_index() const override { return 0; }
virtual GC::Ptr<DecodedImageData> decoded_image_data() const override { return image_data(); }

View file

@ -5,6 +5,7 @@
*/
#include <LibGfx/DecodedImageFrame.h>
#include <LibWeb/HTML/DecodedImageData.h>
#include <LibWeb/Layout/ImageBox.h>
#include <LibWeb/Layout/ImageProvider.h>
@ -15,6 +16,27 @@ void ImageProvider::did_update_alt_text(ImageBox& layout_node)
layout_node.dom_node_did_update_alt_text({});
}
Optional<CSSPixels> ImageProvider::intrinsic_width() const
{
if (auto const& data = decoded_image_data())
return data->intrinsic_width();
return {};
}
Optional<CSSPixels> ImageProvider::intrinsic_height() const
{
if (auto const& data = decoded_image_data())
return data->intrinsic_height();
return {};
}
Optional<CSSPixelFraction> ImageProvider::intrinsic_aspect_ratio() const
{
if (auto const& data = decoded_image_data())
return data->intrinsic_aspect_ratio();
return {};
}
Optional<CSSPixelSize> ImageProvider::intrinsic_size() const
{
auto width = intrinsic_width();
@ -30,10 +52,18 @@ Optional<Gfx::DecodedImageFrame> ImageProvider::current_image_frame() const
return current_image_frame_sized(intrinsic_size().value_or({}).to_type<int>());
}
Optional<Gfx::DecodedImageFrame> ImageProvider::current_image_frame_sized(Gfx::IntSize size) const
{
if (auto const& data = decoded_image_data())
return data->frame(current_frame_index(), size);
return {};
}
Optional<Gfx::DecodedImageFrame> ImageProvider::default_image_frame_sized(Gfx::IntSize size) const
{
// Defer to the current image by default.
return current_image_frame_sized(size);
if (auto const& data = decoded_image_data())
return data->frame(0, size);
return {};
}
}

View file

@ -19,21 +19,21 @@ class ImageProvider {
public:
virtual ~ImageProvider() { }
virtual bool is_image_available() const = 0;
bool is_image_available() const { return decoded_image_data() != nullptr; }
virtual size_t current_frame_index() const = 0;
virtual GC::Ptr<HTML::DecodedImageData> decoded_image_data() const = 0;
virtual Optional<CSSPixels> intrinsic_width() const = 0;
virtual Optional<CSSPixels> intrinsic_height() const = 0;
Optional<CSSPixels> intrinsic_width() const;
Optional<CSSPixels> intrinsic_height() const;
Optional<CSSPixelSize> intrinsic_size() const;
virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const = 0;
Optional<CSSPixelFraction> intrinsic_aspect_ratio() const;
virtual Optional<Gfx::DecodedImageFrame> current_image_frame() const;
virtual Optional<Gfx::DecodedImageFrame> current_image_frame_sized(Gfx::IntSize) const = 0;
Optional<Gfx::DecodedImageFrame> current_image_frame() const;
Optional<Gfx::DecodedImageFrame> current_image_frame_sized(Gfx::IntSize) const;
virtual Optional<Gfx::DecodedImageFrame> default_image_frame_sized(Gfx::IntSize) const;
Optional<Gfx::DecodedImageFrame> default_image_frame_sized(Gfx::IntSize) const;
virtual void layout_node_was_detached() const { }

View file

@ -203,43 +203,6 @@ public:
unregister_image_style_value_client();
}
virtual bool is_image_available() const override
{
if (auto document = this->document())
return m_image->is_paintable(*document);
return false;
}
virtual Optional<CSSPixels> intrinsic_width() const override
{
if (auto document = this->document())
return m_image->natural_width(*document);
return {};
}
virtual Optional<CSSPixels> intrinsic_height() const override
{
if (auto document = this->document())
return m_image->natural_height(*document);
return {};
}
virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override
{
if (auto document = this->document())
return m_image->natural_aspect_ratio(*document);
return {};
}
virtual Optional<Gfx::DecodedImageFrame> current_image_frame_sized(Gfx::IntSize size) const override
{
auto document = this->document();
if (!document)
return {};
auto rect = DevicePixelRect { DevicePixelPoint {}, size.to_type<DevicePixels>() };
return m_image->current_frame(*document, rect);
}
virtual void layout_node_was_detached() const override
{
unregister_image_style_value_client();

View file

@ -218,56 +218,6 @@ RefPtr<Layout::Node> SVGImageElement::create_layout_node(CSS::ComputedProperties
return make_ref_counted<Layout::SVGImageBox>(document(), *this, style);
}
bool SVGImageElement::is_image_available() const
{
return m_resource_request && m_resource_request->image_data();
}
Optional<CSSPixels> SVGImageElement::intrinsic_width() const
{
if (!m_resource_request)
return {};
if (auto image_data = m_resource_request->image_data())
return image_data->intrinsic_width();
return {};
}
Optional<CSSPixels> SVGImageElement::intrinsic_height() const
{
if (!m_resource_request)
return {};
if (auto image_data = m_resource_request->image_data())
return image_data->intrinsic_height();
return {};
}
Optional<CSSPixelFraction> SVGImageElement::intrinsic_aspect_ratio() const
{
if (!m_resource_request)
return {};
if (auto image_data = m_resource_request->image_data())
return image_data->intrinsic_aspect_ratio();
return {};
}
Optional<Gfx::DecodedImageFrame> SVGImageElement::default_image_frame_sized(Gfx::IntSize size) const
{
if (!m_resource_request)
return {};
if (auto data = m_resource_request->image_data())
return data->frame(0, size);
return {};
}
Optional<Gfx::DecodedImageFrame> SVGImageElement::current_image_frame_sized(Gfx::IntSize size) const
{
if (!m_resource_request)
return {};
if (auto data = m_resource_request->image_data())
return data->frame(m_current_frame_index, size);
return {};
}
void SVGImageElement::animate()
{
auto image_data = m_resource_request->image_data();

View file

@ -33,14 +33,7 @@ public:
Gfx::FloatRect bounding_box() const;
virtual Optional<Gfx::DecodedImageFrame> default_image_frame_sized(Gfx::IntSize) const override;
// ^Layout::ImageProvider
virtual bool is_image_available() const override;
virtual Optional<CSSPixels> intrinsic_width() const override;
virtual Optional<CSSPixels> intrinsic_height() const override;
virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
virtual Optional<Gfx::DecodedImageFrame> current_image_frame_sized(Gfx::IntSize) const override;
virtual size_t current_frame_index() const override { return m_current_frame_index; }
virtual GC::Ptr<HTML::DecodedImageData> decoded_image_data() const override;