mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:48:12 +00:00
LibWeb: Cache width of "alt" text in ImageBox
We were constantly measuring and re-measuring the "alt" attribute text of ImageBox layout nodes, even when the alt text didn't change. By caching this, we avoid a *lot* of repeated text measurement work.
This commit is contained in:
parent
514fa83708
commit
faf9746244
3 changed files with 21 additions and 1 deletions
|
@ -78,6 +78,11 @@ void HTMLImageElement::parse_attribute(FlyString const& name, String const& valu
|
||||||
|
|
||||||
if (name == HTML::AttributeNames::src && !value.is_empty())
|
if (name == HTML::AttributeNames::src && !value.is_empty())
|
||||||
m_image_loader.load(document().parse_url(value));
|
m_image_loader.load(document().parse_url(value));
|
||||||
|
|
||||||
|
if (name == HTML::AttributeNames::alt) {
|
||||||
|
if (layout_node())
|
||||||
|
verify_cast<Layout::ImageBox>(*layout_node()).dom_node_did_update_alt_text({});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Layout::Node> HTMLImageElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
|
RefPtr<Layout::Node> HTMLImageElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
|
||||||
|
|
|
@ -59,7 +59,13 @@ void ImageBox::prepare_for_replaced_layout()
|
||||||
auto alt = image_element.alt();
|
auto alt = image_element.alt();
|
||||||
if (alt.is_empty())
|
if (alt.is_empty())
|
||||||
alt = image_element.src();
|
alt = image_element.src();
|
||||||
set_intrinsic_width(font.width(alt) + 16);
|
|
||||||
|
float alt_text_width = 0;
|
||||||
|
if (!m_cached_alt_text_width.has_value())
|
||||||
|
m_cached_alt_text_width = font.width(alt);
|
||||||
|
alt_text_width = m_cached_alt_text_width.value();
|
||||||
|
|
||||||
|
set_intrinsic_width(alt_text_width + 16);
|
||||||
set_intrinsic_height(font.pixel_size() + 16);
|
set_intrinsic_height(font.pixel_size() + 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,6 +74,11 @@ void ImageBox::prepare_for_replaced_layout()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ImageBox::dom_node_did_update_alt_text(Badge<HTML::HTMLImageElement>)
|
||||||
|
{
|
||||||
|
m_cached_alt_text_width = {};
|
||||||
|
}
|
||||||
|
|
||||||
bool ImageBox::renders_as_alt_text() const
|
bool ImageBox::renders_as_alt_text() const
|
||||||
{
|
{
|
||||||
if (is<HTML::HTMLImageElement>(dom_node()))
|
if (is<HTML::HTMLImageElement>(dom_node()))
|
||||||
|
|
|
@ -29,6 +29,8 @@ public:
|
||||||
|
|
||||||
auto const& image_loader() const { return m_image_loader; }
|
auto const& image_loader() const { return m_image_loader; }
|
||||||
|
|
||||||
|
void dom_node_did_update_alt_text(Badge<HTML::HTMLImageElement>);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// ^BrowsingContext::ViewportClient
|
// ^BrowsingContext::ViewportClient
|
||||||
virtual void browsing_context_did_set_viewport_rect(Gfx::IntRect const&) final;
|
virtual void browsing_context_did_set_viewport_rect(Gfx::IntRect const&) final;
|
||||||
|
@ -37,6 +39,8 @@ private:
|
||||||
int preferred_height() const;
|
int preferred_height() const;
|
||||||
|
|
||||||
ImageLoader const& m_image_loader;
|
ImageLoader const& m_image_loader;
|
||||||
|
|
||||||
|
Optional<float> m_cached_alt_text_width;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue