2021-10-13 15:20:50 -03:00
|
|
|
/*
|
2022-01-31 15:07:22 -03:00
|
|
|
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
2021-10-13 15:20:50 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2026-04-16 03:34:54 -03:00
|
|
|
#include <AK/Optional.h>
|
2022-09-04 09:14:22 -03:00
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
2023-07-06 19:44:07 -03:00
|
|
|
#include <LibWeb/IntersectionObserver/IntersectionObserverEntry.h>
|
|
|
|
|
#include <LibWeb/PixelUnits.h>
|
2021-10-13 15:20:50 -03:00
|
|
|
|
|
|
|
|
namespace Web::IntersectionObserver {
|
|
|
|
|
|
2026-05-20 15:58:46 -03:00
|
|
|
using NullableIntersectionObserverRoot = Variant<GC::Ref<DOM::Element>, GC::Ref<DOM::Document>, Empty>;
|
2026-05-01 14:01:23 -03:00
|
|
|
using IntersectionObserverRoot = NullableIntersectionObserverRoot;
|
2026-02-17 11:57:03 -03:00
|
|
|
|
2026-04-16 03:34:54 -03:00
|
|
|
struct ObservationTarget {
|
|
|
|
|
GC::Ref<DOM::Element> target;
|
|
|
|
|
Optional<size_t> previous_threshold_index;
|
|
|
|
|
bool previous_is_intersecting { false };
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-13 15:20:50 -03:00
|
|
|
// https://w3c.github.io/IntersectionObserver/#intersection-observer-interface
|
2023-07-06 19:44:07 -03:00
|
|
|
class IntersectionObserver final : public Bindings::PlatformObject {
|
2022-09-04 09:14:22 -03:00
|
|
|
WEB_PLATFORM_OBJECT(IntersectionObserver, Bindings::PlatformObject);
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DECLARE_ALLOCATOR(IntersectionObserver);
|
2022-09-04 09:14:22 -03:00
|
|
|
|
2021-10-13 15:20:50 -03:00
|
|
|
public:
|
2025-12-28 21:33:12 -03:00
|
|
|
static constexpr bool OVERRIDES_FINALIZE = true;
|
|
|
|
|
|
2026-05-01 14:01:23 -03:00
|
|
|
static WebIDL::ExceptionOr<GC::Ref<IntersectionObserver>> construct_impl(JS::Realm&, GC::Ptr<WebIDL::CallbackType> callback, Bindings::IntersectionObserverInit const& options);
|
2021-10-13 15:20:50 -03:00
|
|
|
|
2022-09-04 09:14:22 -03:00
|
|
|
virtual ~IntersectionObserver() override;
|
2021-10-13 15:20:50 -03:00
|
|
|
|
|
|
|
|
void observe(DOM::Element& target);
|
|
|
|
|
void unobserve(DOM::Element& target);
|
|
|
|
|
void disconnect();
|
2024-11-14 12:01:23 -03:00
|
|
|
Vector<GC::Root<IntersectionObserverEntry>> take_records();
|
2023-07-06 19:44:07 -03:00
|
|
|
|
2026-04-16 03:34:54 -03:00
|
|
|
Vector<ObservationTarget>& observation_targets() { return m_observation_targets; }
|
|
|
|
|
Vector<ObservationTarget> const& observation_targets() const { return m_observation_targets; }
|
2023-07-06 19:44:07 -03:00
|
|
|
|
2026-02-17 11:57:03 -03:00
|
|
|
NullableIntersectionObserverRoot root() const;
|
2024-11-22 08:01:42 -03:00
|
|
|
String root_margin() const;
|
|
|
|
|
String scroll_margin() const;
|
2026-03-22 10:58:44 -03:00
|
|
|
Vector<CSS::LengthPercentage> const& scroll_margin_values() const { return m_scroll_margin; }
|
2023-07-06 19:44:07 -03:00
|
|
|
Vector<double> const& thresholds() const { return m_thresholds; }
|
2024-11-22 08:01:42 -03:00
|
|
|
long delay() const { return m_delay; }
|
|
|
|
|
bool track_visibility() const { return m_track_visibility; }
|
2023-07-06 19:44:07 -03:00
|
|
|
|
2026-05-20 15:58:46 -03:00
|
|
|
Variant<GC::Ref<DOM::Element>, GC::Ref<DOM::Document>> intersection_root() const;
|
2026-03-22 11:11:37 -03:00
|
|
|
GC::Ref<DOM::Node> intersection_root_node() const;
|
|
|
|
|
bool is_implicit_root() const { return !m_root; }
|
2023-07-06 19:44:07 -03:00
|
|
|
CSSPixelRect root_intersection_rectangle() const;
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
void queue_entry(Badge<DOM::Document>, GC::Ref<IntersectionObserverEntry>);
|
2023-07-06 19:44:07 -03:00
|
|
|
|
|
|
|
|
WebIDL::CallbackType& callback() { return *m_callback; }
|
2022-09-04 09:14:22 -03:00
|
|
|
|
|
|
|
|
private:
|
2026-05-01 14:01:23 -03:00
|
|
|
explicit IntersectionObserver(JS::Realm&, GC::Ptr<WebIDL::CallbackType> callback, IntersectionObserverRoot const& root, Vector<CSS::LengthPercentage> root_margin, Vector<CSS::LengthPercentage> scroll_margin, Vector<double>&& thresholds, double debug, bool track_visibility);
|
2023-01-10 08:28:20 -03:00
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
2023-07-06 19:44:07 -03:00
|
|
|
virtual void visit_edges(JS::Cell::Visitor&) override;
|
2023-08-09 17:08:52 -03:00
|
|
|
virtual void finalize() override;
|
2023-07-06 19:44:07 -03:00
|
|
|
|
2024-11-22 08:01:42 -03:00
|
|
|
static Optional<Vector<CSS::LengthPercentage>> parse_a_margin(JS::Realm&, String);
|
|
|
|
|
|
2023-07-06 19:44:07 -03:00
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-callback-slot
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ptr<WebIDL::CallbackType> m_callback;
|
2023-07-06 19:44:07 -03:00
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-root
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ptr<DOM::Node> m_root;
|
2023-07-06 19:44:07 -03:00
|
|
|
|
2024-11-22 08:01:42 -03:00
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-rootmargin
|
|
|
|
|
Vector<CSS::LengthPercentage> m_root_margin;
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin
|
|
|
|
|
Vector<CSS::LengthPercentage> m_scroll_margin;
|
|
|
|
|
|
2023-07-06 19:44:07 -03:00
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-thresholds
|
|
|
|
|
Vector<double> m_thresholds;
|
|
|
|
|
|
2024-11-22 08:01:42 -03:00
|
|
|
// https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-delay
|
|
|
|
|
long m_delay;
|
|
|
|
|
|
|
|
|
|
// https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-trackvisibility
|
|
|
|
|
bool m_track_visibility;
|
|
|
|
|
|
2023-07-06 19:44:07 -03:00
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-queuedentries-slot
|
2024-11-14 12:01:23 -03:00
|
|
|
Vector<GC::Ref<IntersectionObserverEntry>> m_queued_entries;
|
2023-07-06 19:44:07 -03:00
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-observationtargets-slot
|
2026-04-16 03:34:54 -03:00
|
|
|
Vector<ObservationTarget> m_observation_targets;
|
2023-11-22 19:57:34 -03:00
|
|
|
|
|
|
|
|
// AD-HOC: This is the document where we've registered the IntersectionObserver.
|
2025-10-16 06:13:54 -03:00
|
|
|
GC::Weak<DOM::Document> m_document;
|
2021-10-13 15:20:50 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|