1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:58:12 +00:00

LibWeb: Move image resource request out of ImageStyleValue constructor

This always felt awkward to me, and required a few other hacks to make
it work. Now, the request is only started when `load_bitmap()` is
called, which we do inside `NodeWithStyle::apply_style()`.
This commit is contained in:
Sam Atkins 2021-10-22 20:55:27 +01:00 committed by Andreas Kling
parent cf3c125e77
commit 0f393771b7
5 changed files with 17 additions and 9 deletions

View file

@ -2273,7 +2273,7 @@ RefPtr<StyleValue> Parser::parse_image_value(ParsingContext const& context, Styl
{ {
auto url = parse_url_function(context, component_value); auto url = parse_url_function(context, component_value);
if (url.has_value()) if (url.has_value())
return ImageStyleValue::create(url.value(), context.document()); return ImageStyleValue::create(url.value());
// FIXME: Handle gradients. // FIXME: Handle gradients.
return {}; return {};

View file

@ -367,13 +367,19 @@ Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
} }
} }
ImageStyleValue::ImageStyleValue(AK::URL const& url, DOM::Document* document) ImageStyleValue::ImageStyleValue(AK::URL const& url)
: StyleValue(Type::Image) : StyleValue(Type::Image)
, m_url(url) , m_url(url)
, m_document(document)
{ {
// FIXME: This doesn't work right without a document. }
auto request = LoadRequest::create_for_url_on_page(url, document ? document->page() : nullptr);
void ImageStyleValue::load_bitmap(DOM::Document& document)
{
if (m_bitmap)
return;
m_document = &document;
auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request)); set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
} }

View file

@ -891,15 +891,16 @@ class ImageStyleValue final
: public StyleValue : public StyleValue
, public ImageResourceClient { , public ImageResourceClient {
public: public:
static NonnullRefPtr<ImageStyleValue> create(AK::URL const& url, DOM::Document* document) { return adopt_ref(*new ImageStyleValue(url, document)); } static NonnullRefPtr<ImageStyleValue> create(AK::URL const& url) { return adopt_ref(*new ImageStyleValue(url)); }
virtual ~ImageStyleValue() override { } virtual ~ImageStyleValue() override { }
String to_string() const override { return String::formatted("Image({})", m_url.to_string()); } String to_string() const override { return String::formatted("Image({})", m_url.to_string()); }
const Gfx::Bitmap* bitmap() const { return m_bitmap; } void load_bitmap(DOM::Document& document);
Gfx::Bitmap const* bitmap() const { return m_bitmap; }
private: private:
ImageStyleValue(AK::URL const&, DOM::Document*); ImageStyleValue(AK::URL const&);
// ^ResourceClient // ^ResourceClient
virtual void resource_did_load() override; virtual void resource_did_load() override;

View file

@ -55,7 +55,7 @@ void HTMLBodyElement::parse_attribute(const FlyString& name, const String& value
if (color.has_value()) if (color.has_value())
document().set_visited_link_color(color.value()); document().set_visited_link_color(color.value());
} else if (name.equals_ignoring_case("background")) { } else if (name.equals_ignoring_case("background")) {
m_background_style_value = CSS::ImageStyleValue::create(document().parse_url(value), &document()); m_background_style_value = CSS::ImageStyleValue::create(document().parse_url(value));
} }
} }

View file

@ -218,6 +218,7 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
auto bgimage = specified_style.property(CSS::PropertyID::BackgroundImage); auto bgimage = specified_style.property(CSS::PropertyID::BackgroundImage);
if (bgimage.has_value() && bgimage.value()->is_image()) { if (bgimage.has_value() && bgimage.value()->is_image()) {
m_background_image = bgimage.value()->as_image(); m_background_image = bgimage.value()->as_image();
m_background_image->load_bitmap(document());
} }
computed_values.set_box_sizing(specified_style.box_sizing()); computed_values.set_box_sizing(specified_style.box_sizing());