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

LibWeb: Extract shared lazy-loading behavior into a base class

`<iframe>` and `<img>` tags share the same spec for several aspects of
lazy-loading: how the `loading` attribute works, the "will lazy load
element" steps, and a member for storing the lazy-load resumption
steps. So let's share the implementation by using a base class.

This mostly involves moving things around. However, we also change the
`start_intersection_observing_a_lazy_loading_element()` method to take
a LazyLoadingElement, and operate on one, instead of always casting to
HTMLImageElement.

We do unfortunately have to do some shenanigans to make the cast work,
by adding a virtual function stub in DOM::Element.
This commit is contained in:
Sam Atkins 2023-11-21 18:50:09 +00:00 committed by Andreas Kling
parent cc2008ea0d
commit cc633123ca
7 changed files with 111 additions and 56 deletions

View file

@ -17,6 +17,7 @@
#include <LibWeb/HTML/CORSSettingAttribute.h>
#include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/HTML/LazyLoadingElement.h>
#include <LibWeb/HTML/SourceSet.h>
#include <LibWeb/Layout/ImageProvider.h>
@ -25,11 +26,13 @@ namespace Web::HTML {
class HTMLImageElement final
: public HTMLElement
, public FormAssociatedElement
, public LazyLoadingElement<HTMLImageElement>
, public Layout::ImageProvider
, public DOM::Document::ViewportClient {
WEB_PLATFORM_OBJECT(HTMLImageElement, HTMLElement);
JS_DECLARE_ALLOCATOR(HTMLImageElement);
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLImageElement)
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLImageElement);
LAZY_LOADING_ELEMENT(HTMLImageElement);
public:
virtual ~HTMLImageElement() override;
@ -77,12 +80,6 @@ public:
ImageRequest const& current_request() const { return *m_current_request; }
size_t current_frame_index() const { return m_current_frame_index; }
enum class LazyLoading {
Lazy,
Eager,
};
[[nodiscard]] LazyLoading lazy_loading() const;
[[nodiscard]] bool will_lazy_load() const;
// https://html.spec.whatwg.org/multipage/images.html#upgrade-the-pending-request-to-the-current-request
void upgrade_pending_request_to_current_request();
@ -94,9 +91,6 @@ public:
virtual RefPtr<Gfx::ImmutableBitmap> current_image_bitmap(Gfx::IntSize = {}) const override;
virtual void set_visible_in_viewport(bool) override;
void set_lazy_load_resumption_steps(Function<void()>);
JS::GCPtr<JS::HeapFunction<void()>> take_lazy_load_resumption_steps(Badge<DOM::Document>);
virtual void visit_edges(Cell::Visitor&) override;
private:
@ -139,10 +133,6 @@ private:
SourceSet m_source_set;
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-load-resumption-steps
// Each img and iframe element has associated lazy load resumption steps, initially null.
JS::GCPtr<JS::HeapFunction<void()>> m_lazy_load_resumption_steps;
CSSPixelSize m_last_seen_viewport_size;
};