1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 17:17:45 +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

@ -3310,6 +3310,8 @@ void Document::run_the_update_intersection_observations_steps(HighResolutionTime
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#start-intersection-observing-a-lazy-loading-element
void Document::start_intersection_observing_a_lazy_loading_element(Element& element)
{
VERIFY(element.is_lazy_loading());
auto& realm = this->realm();
// 1. Let doc be element's node document.
@ -3333,7 +3335,8 @@ void Document::start_intersection_observing_a_lazy_loading_element(Element& elem
// 2. If entry.isIntersecting is true, then set resumptionSteps to entry.target's lazy load resumption steps.
if (entry.is_intersecting()) {
// 5. Set entry.target's lazy load resumption steps to null.
resumption_steps = verify_cast<HTML::HTMLImageElement>(*entry.target()).take_lazy_load_resumption_steps({});
VERIFY(entry.target()->is_lazy_loading());
resumption_steps = entry.target()->take_lazy_load_resumption_steps({});
}
// 3. If resumptionSteps is null, then return.

View file

@ -27,6 +27,7 @@
#include <LibWeb/HTML/DocumentReadyState.h>
#include <LibWeb/HTML/HTMLScriptElement.h>
#include <LibWeb/HTML/History.h>
#include <LibWeb/HTML/LazyLoadingElement.h>
#include <LibWeb/HTML/Origin.h>
#include <LibWeb/HTML/SandboxingFlagSet.h>
#include <LibWeb/HTML/Scripting/Environments.h>
@ -519,7 +520,7 @@ public:
void run_the_update_intersection_observations_steps(HighResolutionTime::DOMHighResTimeStamp time);
void start_intersection_observing_a_lazy_loading_element(Element& element);
void start_intersection_observing_a_lazy_loading_element(Element&);
void shared_declarative_refresh_steps(StringView input, JS::GCPtr<HTML::HTMLMetaElement const> meta_element = nullptr);

View file

@ -20,6 +20,7 @@
#include <LibWeb/DOM/Slottable.h>
#include <LibWeb/HTML/AttributeNames.h>
#include <LibWeb/HTML/EventLoop/Task.h>
#include <LibWeb/HTML/LazyLoadingElement.h>
#include <LibWeb/HTML/ScrollOptions.h>
#include <LibWeb/HTML/TagNames.h>
#include <LibWeb/IntersectionObserver/IntersectionObserver.h>
@ -372,6 +373,11 @@ public:
Optional<FlyString> const& id() const { return m_id; }
virtual JS::GCPtr<JS::HeapFunction<void()>> take_lazy_load_resumption_steps(Badge<DOM::Document>)
{
return nullptr;
}
protected:
Element(Document&, DOM::QualifiedName);
virtual void initialize(JS::Realm&) override;

View file

@ -105,6 +105,7 @@ public:
virtual bool is_html_button_element() const { return false; }
virtual bool is_html_slot_element() const { return false; }
virtual bool is_navigable_container() const { return false; }
virtual bool is_lazy_loading() const { return false; }
WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> pre_insert(JS::NonnullGCPtr<Node>, JS::GCPtr<Node>);
WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> pre_remove(JS::NonnullGCPtr<Node>);