/src/serenity/Userland/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibJS/Heap/Handle.h> |
10 | | #include <LibWeb/Bindings/PlatformObject.h> |
11 | | #include <LibWeb/IntersectionObserver/IntersectionObserverEntry.h> |
12 | | #include <LibWeb/PixelUnits.h> |
13 | | |
14 | | namespace Web::IntersectionObserver { |
15 | | |
16 | | struct IntersectionObserverInit { |
17 | | Optional<Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>>> root; |
18 | | String root_margin { "0px"_string }; |
19 | | Variant<double, Vector<double>> threshold { 0 }; |
20 | | }; |
21 | | |
22 | | // https://www.w3.org/TR/intersection-observer/#intersectionobserverregistration |
23 | | struct IntersectionObserverRegistration { |
24 | | // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserverregistration-observer |
25 | | // [A]n observer property holding an IntersectionObserver. |
26 | | JS::NonnullGCPtr<IntersectionObserver> observer; |
27 | | |
28 | | // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserverregistration-observer |
29 | | // NOTE: Optional is used in place of the spec using -1 to indicate no previous index. |
30 | | // [A] previousThresholdIndex property holding a number between -1 and the length of the observer’s thresholds property (inclusive). |
31 | | Optional<size_t> previous_threshold_index; |
32 | | |
33 | | // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserverregistration-previousisintersecting |
34 | | // [A] previousIsIntersecting property holding a boolean. |
35 | | bool previous_is_intersecting { false }; |
36 | | }; |
37 | | |
38 | | // https://w3c.github.io/IntersectionObserver/#intersection-observer-interface |
39 | | class IntersectionObserver final : public Bindings::PlatformObject { |
40 | | WEB_PLATFORM_OBJECT(IntersectionObserver, Bindings::PlatformObject); |
41 | | JS_DECLARE_ALLOCATOR(IntersectionObserver); |
42 | | |
43 | | public: |
44 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<IntersectionObserver>> construct_impl(JS::Realm&, JS::GCPtr<WebIDL::CallbackType> callback, IntersectionObserverInit const& options = {}); |
45 | | |
46 | | virtual ~IntersectionObserver() override; |
47 | | |
48 | | void observe(DOM::Element& target); |
49 | | void unobserve(DOM::Element& target); |
50 | | void disconnect(); |
51 | | Vector<JS::Handle<IntersectionObserverEntry>> take_records(); |
52 | | |
53 | 0 | Vector<JS::NonnullGCPtr<DOM::Element>> const& observation_targets() const { return m_observation_targets; } |
54 | | |
55 | | Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>, Empty> root() const; |
56 | 0 | Vector<double> const& thresholds() const { return m_thresholds; } |
57 | | |
58 | | Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>> intersection_root() const; |
59 | | CSSPixelRect root_intersection_rectangle() const; |
60 | | |
61 | | void queue_entry(Badge<DOM::Document>, JS::NonnullGCPtr<IntersectionObserverEntry>); |
62 | | |
63 | 0 | WebIDL::CallbackType& callback() { return *m_callback; } |
64 | | |
65 | | private: |
66 | | explicit IntersectionObserver(JS::Realm&, JS::GCPtr<WebIDL::CallbackType> callback, Optional<Variant<JS::Handle<DOM::Element>, JS::Handle<DOM::Document>>> const& root, Vector<double>&& thresholds); |
67 | | |
68 | | virtual void initialize(JS::Realm&) override; |
69 | | virtual void visit_edges(JS::Cell::Visitor&) override; |
70 | | virtual void finalize() override; |
71 | | |
72 | | // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-callback-slot |
73 | | JS::GCPtr<WebIDL::CallbackType> m_callback; |
74 | | |
75 | | // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-root |
76 | | JS::GCPtr<DOM::Node> m_root; |
77 | | |
78 | | // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-thresholds |
79 | | Vector<double> m_thresholds; |
80 | | |
81 | | // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-queuedentries-slot |
82 | | Vector<JS::NonnullGCPtr<IntersectionObserverEntry>> m_queued_entries; |
83 | | |
84 | | // https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-observationtargets-slot |
85 | | Vector<JS::NonnullGCPtr<DOM::Element>> m_observation_targets; |
86 | | |
87 | | // AD-HOC: This is the document where we've registered the IntersectionObserver. |
88 | | WeakPtr<DOM::Document> m_document; |
89 | | }; |
90 | | |
91 | | } |