mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 06:47:34 +00:00
LibWeb: Generalize ImageBox and ImagePaintable for any ImageProvider
They currently assume the DOM node is an HTMLImageElement with respect to handling the alt attribute. The HTMLInputElement will require the same behavior.
This commit is contained in:
parent
c4295edc81
commit
45a47cb32b
10 changed files with 45 additions and 8 deletions
|
@ -8,6 +8,7 @@
|
|||
#include <LibWeb/HTML/DecodedImageData.h>
|
||||
#include <LibWeb/HTML/ImageRequest.h>
|
||||
#include <LibWeb/Layout/ImageBox.h>
|
||||
#include <LibWeb/Layout/ImageProvider.h>
|
||||
#include <LibWeb/Painting/ImagePaintable.h>
|
||||
#include <LibWeb/Platform/FontPlugin.h>
|
||||
|
||||
|
@ -28,9 +29,9 @@ void ImageBox::prepare_for_replaced_layout()
|
|||
set_natural_aspect_ratio(m_image_provider.intrinsic_aspect_ratio());
|
||||
|
||||
if (renders_as_alt_text()) {
|
||||
auto& image_element = verify_cast<HTML::HTMLImageElement>(dom_node());
|
||||
auto const& element = verify_cast<HTML::HTMLElement>(dom_node());
|
||||
auto& font = Platform::FontPlugin::the().default_font();
|
||||
auto alt = image_element.alt();
|
||||
auto alt = element.get_attribute_value(HTML::AttributeNames::alt);
|
||||
|
||||
CSSPixels alt_text_width = 0;
|
||||
if (!m_cached_alt_text_width.has_value())
|
||||
|
@ -46,15 +47,15 @@ void ImageBox::prepare_for_replaced_layout()
|
|||
}
|
||||
}
|
||||
|
||||
void ImageBox::dom_node_did_update_alt_text(Badge<HTML::HTMLImageElement>)
|
||||
void ImageBox::dom_node_did_update_alt_text(Badge<ImageProvider>)
|
||||
{
|
||||
m_cached_alt_text_width = {};
|
||||
}
|
||||
|
||||
bool ImageBox::renders_as_alt_text() const
|
||||
{
|
||||
if (is<HTML::HTMLImageElement>(dom_node()))
|
||||
return !static_cast<HTML::HTMLImageElement const&>(dom_node()).current_request().is_available();
|
||||
if (auto const* image_provider = dynamic_cast<ImageProvider const*>(&dom_node()))
|
||||
return !image_provider->is_image_available();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ public:
|
|||
auto const& image_provider() const { return m_image_provider; }
|
||||
auto& image_provider() { return m_image_provider; }
|
||||
|
||||
void dom_node_did_update_alt_text(Badge<HTML::HTMLImageElement>);
|
||||
void dom_node_did_update_alt_text(Badge<ImageProvider>);
|
||||
|
||||
private:
|
||||
ImageProvider const& m_image_provider;
|
||||
|
|
17
Userland/Libraries/LibWeb/Layout/ImageProvider.cpp
Normal file
17
Userland/Libraries/LibWeb/Layout/ImageProvider.cpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/Layout/ImageBox.h>
|
||||
#include <LibWeb/Layout/ImageProvider.h>
|
||||
|
||||
namespace Web::Layout {
|
||||
|
||||
void ImageProvider::did_update_alt_text(ImageBox& layout_node)
|
||||
{
|
||||
layout_node.dom_node_did_update_alt_text({});
|
||||
}
|
||||
|
||||
}
|
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <LibGfx/Size.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/PixelUnits.h>
|
||||
|
||||
namespace Web::Layout {
|
||||
|
@ -15,12 +16,17 @@ class ImageProvider {
|
|||
public:
|
||||
virtual ~ImageProvider() { }
|
||||
|
||||
virtual bool is_image_available() const = 0;
|
||||
|
||||
virtual Optional<CSSPixels> intrinsic_width() const = 0;
|
||||
virtual Optional<CSSPixels> intrinsic_height() const = 0;
|
||||
virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const = 0;
|
||||
|
||||
virtual RefPtr<Gfx::ImmutableBitmap> current_image_bitmap(Gfx::IntSize) const = 0;
|
||||
virtual void set_visible_in_viewport(bool) = 0;
|
||||
|
||||
protected:
|
||||
static void did_update_alt_text(ImageBox&);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue