LibWeb/CSS: Fetch ImageStyleValue images closer to spec

We now don't absolutize the URL during parsing, keeping it as a CSS::URL
object which we then pass to the "fetch an external image for a
stylesheet" algorithm. Our version of this algorithm is a bit ad-hoc,
in order to make use of SharedResourceRequest. To try and reduce
duplication, I've pulled all of fetch_a_style_resource() into a static
function, apart from the "actually do the fetch" step.
This commit is contained in:
Sam Atkins 2025-04-10 16:04:34 +01:00
commit c224644bed
Notes: github-actions[bot] 2025-04-15 09:30:29 +00:00
5 changed files with 120 additions and 57 deletions

View file

@ -1,18 +1,17 @@
/*
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ImageStyleValue.h"
#include <LibWeb/CSS/ComputedValues.h>
#include <LibWeb/CSS/Serialize.h>
#include <LibWeb/CSS/Fetch.h>
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/DecodedImageData.h>
#include <LibWeb/HTML/ImageRequest.h>
#include <LibWeb/HTML/PotentialCORSRequest.h>
#include <LibWeb/HTML/SharedResourceRequest.h>
#include <LibWeb/Painting/DisplayListRecorder.h>
@ -21,7 +20,17 @@
namespace Web::CSS {
ImageStyleValue::ImageStyleValue(::URL::URL const& url)
ValueComparingNonnullRefPtr<ImageStyleValue> ImageStyleValue::create(URL const& url)
{
return adopt_ref(*new (nothrow) ImageStyleValue(url));
}
ValueComparingNonnullRefPtr<ImageStyleValue> ImageStyleValue::create(::URL::URL const& url)
{
return adopt_ref(*new (nothrow) ImageStyleValue(URL { url.to_string() }));
}
ImageStyleValue::ImageStyleValue(URL const& url)
: AbstractImageStyleValue(Type::Image)
, m_url(url)
{
@ -35,6 +44,7 @@ void ImageStyleValue::visit_edges(JS::Cell::Visitor& visitor) const
// FIXME: visit_edges in non-GC allocated classes is confusing pattern.
// Consider making CSSStyleValue to be GC allocated instead.
visitor.visit(m_resource_request);
visitor.visit(m_style_sheet);
visitor.visit(m_timer);
}
@ -44,37 +54,26 @@ void ImageStyleValue::load_any_resources(DOM::Document& document)
return;
m_document = &document;
m_resource_request = HTML::SharedResourceRequest::get_or_create(document.realm(), document.page(), m_url);
m_resource_request->add_callbacks(
[this, weak_this = make_weak_ptr()] {
if (!weak_this)
return;
if (m_style_sheet) {
m_resource_request = fetch_an_external_image_for_a_stylesheet(m_url, { *m_style_sheet });
} else {
m_resource_request = fetch_an_external_image_for_a_stylesheet(m_url, { document });
}
if (m_resource_request) {
m_resource_request->add_callbacks(
[this, weak_this = make_weak_ptr()] {
if (!weak_this || !m_document)
return;
if (!m_document)
return;
if (auto navigable = m_document->navigable()) {
// Once the image has loaded, we need to re-resolve CSS properties that depend on the image's dimensions.
m_document->set_needs_to_resolve_paint_only_properties();
// FIXME: Do less than a full repaint if possible?
m_document->set_needs_display();
}
auto image_data = m_resource_request->image_data();
if (image_data->is_animated() && image_data->frame_count() > 1) {
m_timer = Platform::Timer::create(m_document->heap());
m_timer->set_interval(image_data->frame_duration(0));
m_timer->on_timeout = GC::create_function(m_document->heap(), [this] { animate(); });
m_timer->start();
}
},
nullptr);
if (m_resource_request->needs_fetching()) {
auto request = HTML::create_potential_CORS_request(document.vm(), m_url, Fetch::Infrastructure::Request::Destination::Image, HTML::CORSSettingAttribute::NoCORS);
request->set_client(&document.relevant_settings_object());
m_resource_request->fetch_resource(document.realm(), request);
auto image_data = m_resource_request->image_data();
if (image_data->is_animated() && image_data->frame_count() > 1) {
m_timer = Platform::Timer::create(m_document->heap());
m_timer->set_interval(image_data->frame_duration(0));
m_timer->on_timeout = GC::create_function(m_document->heap(), [this] { animate(); });
m_timer->start();
}
},
nullptr);
}
}
@ -116,7 +115,7 @@ Gfx::ImmutableBitmap const* ImageStyleValue::bitmap(size_t frame_index, Gfx::Int
String ImageStyleValue::to_string(SerializationMode) const
{
return serialize_a_url(m_url.to_string());
return m_url.to_string();
}
bool ImageStyleValue::equals(CSSStyleValue const& other) const
@ -177,4 +176,10 @@ Optional<Gfx::Color> ImageStyleValue::color_if_single_pixel_bitmap() const
return {};
}
void ImageStyleValue::set_style_sheet(GC::Ptr<CSSStyleSheet> style_sheet)
{
Base::set_style_sheet(style_sheet);
m_style_sheet = style_sheet;
}
}