mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 12:48:10 +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:
parent
cf3c125e77
commit
0f393771b7
5 changed files with 17 additions and 9 deletions
|
@ -2273,7 +2273,7 @@ RefPtr<StyleValue> Parser::parse_image_value(ParsingContext const& context, Styl
|
|||
{
|
||||
auto url = parse_url_function(context, component_value);
|
||||
if (url.has_value())
|
||||
return ImageStyleValue::create(url.value(), context.document());
|
||||
return ImageStyleValue::create(url.value());
|
||||
// FIXME: Handle gradients.
|
||||
|
||||
return {};
|
||||
|
|
|
@ -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)
|
||||
, 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));
|
||||
}
|
||||
|
||||
|
|
|
@ -891,15 +891,16 @@ class ImageStyleValue final
|
|||
: public StyleValue
|
||||
, public ImageResourceClient {
|
||||
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 { }
|
||||
|
||||
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:
|
||||
ImageStyleValue(AK::URL const&, DOM::Document*);
|
||||
ImageStyleValue(AK::URL const&);
|
||||
|
||||
// ^ResourceClient
|
||||
virtual void resource_did_load() override;
|
||||
|
|
|
@ -55,7 +55,7 @@ void HTMLBodyElement::parse_attribute(const FlyString& name, const String& value
|
|||
if (color.has_value())
|
||||
document().set_visited_link_color(color.value());
|
||||
} 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -218,6 +218,7 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
|
|||
auto bgimage = specified_style.property(CSS::PropertyID::BackgroundImage);
|
||||
if (bgimage.has_value() && bgimage.value()->is_image()) {
|
||||
m_background_image = bgimage.value()->as_image();
|
||||
m_background_image->load_bitmap(document());
|
||||
}
|
||||
|
||||
computed_values.set_box_sizing(specified_style.box_sizing());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue