Skip to main content

script/dom/intersectionobserver/
intersectionobserver.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::{Cell, RefCell};
6use std::time::Duration;
7
8use app_units::Au;
9use cssparser::Parser;
10use dom_struct::dom_struct;
11use euclid::{Rect, SideOffsets2D, Size2D, Vector2D};
12use js::context::{JSContext, NoGC};
13use js::rust::{HandleObject, MutableHandleValue};
14use script_bindings::callback::{RootedCallback, TracedCallback};
15use script_bindings::cell::DomRefCell;
16use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto};
17use servo_base::cross_process_instant::CrossProcessInstant;
18use servo_geometry::f32_rect_to_au_rect;
19use style::parser::Parse;
20use style::stylesheets::CssRuleType;
21use style::values::computed::Overflow;
22use style::values::specified::intersection_observer::IntersectionObserverMargin;
23use style_traits::{CSSPixel, ParsingMode, ToCss};
24
25use crate::css::css::{ANONYMOUS_CONTENT_URL_DATA, parser_context_for_anonymous_content};
26use crate::dom::bindings::callback::ExceptionHandling;
27use crate::dom::bindings::codegen::Bindings::IntersectionObserverBinding::{
28    IntersectionObserverCallback, IntersectionObserverInit, IntersectionObserverMethods,
29};
30use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
31use crate::dom::bindings::codegen::UnionTypes::{DoubleOrDoubleSequence, ElementOrDocument};
32use crate::dom::bindings::error::{Error, Fallible};
33use crate::dom::bindings::inheritance::Castable;
34use crate::dom::bindings::num::Finite;
35use crate::dom::bindings::root::{Dom, DomRoot};
36use crate::dom::bindings::str::DOMString;
37use crate::dom::bindings::utils::to_frozen_array;
38use crate::dom::document::{Document, RenderingUpdateReason};
39use crate::dom::domrectreadonly::DOMRectReadOnly;
40use crate::dom::element::Element;
41use crate::dom::intersectionobserverentry::IntersectionObserverEntry;
42use crate::dom::node::{Node, NodeTraits};
43use crate::dom::window::Window;
44
45#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
46#[derive(JSTraceable, MallocSizeOf)]
47pub(crate) enum UnrootedElementOrDocument {
48    Element(Dom<Element>),
49    Document(Dom<Document>),
50}
51
52impl From<&ElementOrDocument> for UnrootedElementOrDocument {
53    fn from(value: &ElementOrDocument) -> Self {
54        match value {
55            ElementOrDocument::Document(document) => {
56                UnrootedElementOrDocument::Document(document.as_traced())
57            },
58            ElementOrDocument::Element(element) => {
59                UnrootedElementOrDocument::Element(element.as_traced())
60            },
61        }
62    }
63}
64
65impl From<&UnrootedElementOrDocument> for ElementOrDocument {
66    fn from(value: &UnrootedElementOrDocument) -> Self {
67        match value {
68            UnrootedElementOrDocument::Document(document) => {
69                ElementOrDocument::Document(document.as_rooted())
70            },
71            UnrootedElementOrDocument::Element(element) => {
72                ElementOrDocument::Element(element.as_rooted())
73            },
74        }
75    }
76}
77
78/// > The intersection root for an IntersectionObserver is the value of its root attribute if the attribute is non-null;
79/// > otherwise, it is the top-level browsing context’s document node, referred to as the implicit root.
80///
81/// <https://w3c.github.io/IntersectionObserver/#intersectionobserver-intersection-root>
82pub type IntersectionRoot = Option<UnrootedElementOrDocument>;
83
84/// The Intersection Observer interface
85///
86/// > The IntersectionObserver interface can be used to observe changes in the intersection
87/// > of an intersection root and one or more target Elements.
88///
89/// <https://w3c.github.io/IntersectionObserver/#intersection-observer-interface>
90#[dom_struct]
91pub(crate) struct IntersectionObserver {
92    reflector_: Reflector,
93
94    /// [`Document`] that should process this observer's observation steps.
95    /// Following Chrome and Firefox, it is the current document on construction.
96    /// <https://github.com/w3c/IntersectionObserver/issues/525>
97    owner_doc: Dom<Document>,
98
99    /// > The root provided to the IntersectionObserver constructor, or null if none was provided.
100    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-root>
101    root: IntersectionRoot,
102
103    /// > This callback will be invoked when there are changes to a target’s intersection
104    /// > with the intersection root, as per the processing model.
105    ///
106    /// <https://w3c.github.io/IntersectionObserver/#intersection-observer-callback>
107    callback: TracedCallback<IntersectionObserverCallback>,
108
109    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-queuedentries-slot>
110    queued_entries: DomRefCell<Vec<Dom<IntersectionObserverEntry>>>,
111
112    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-observationtargets-slot>
113    observation_targets: DomRefCell<Vec<Dom<Element>>>,
114
115    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-rootmargin-slot>
116    #[no_trace]
117    #[ignore_malloc_size_of = "Defined in style"]
118    root_margin: RefCell<IntersectionObserverMargin>,
119
120    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-scrollmargin-slot>
121    #[no_trace]
122    #[ignore_malloc_size_of = "Defined in style"]
123    scroll_margin: RefCell<IntersectionObserverMargin>,
124
125    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-thresholds-slot>
126    thresholds: RefCell<Vec<Finite<f64>>>,
127
128    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-delay-slot>
129    delay: Cell<i32>,
130
131    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-trackvisibility-slot>
132    track_visibility: Cell<bool>,
133
134    /// Whether or not this [`IntersectionObserver`] is connected to its owning [`Document`].
135    connected_to_document: Cell<bool>,
136}
137
138impl IntersectionObserver {
139    fn new_inherited(
140        window: &Window,
141        callback: RootedCallback<IntersectionObserverCallback>,
142        root: &Option<ElementOrDocument>,
143        root_margin: IntersectionObserverMargin,
144        scroll_margin: IntersectionObserverMargin,
145    ) -> Self {
146        Self {
147            reflector_: Reflector::new(),
148            owner_doc: window.Document().as_traced(),
149            root: root.as_ref().map(|root| root.into()),
150            callback: callback.to_traced(),
151            queued_entries: Default::default(),
152            observation_targets: Default::default(),
153            root_margin: RefCell::new(root_margin),
154            scroll_margin: RefCell::new(scroll_margin),
155            thresholds: Default::default(),
156            delay: Default::default(),
157            track_visibility: Default::default(),
158            connected_to_document: Cell::new(false),
159        }
160    }
161
162    /// <https://w3c.github.io/IntersectionObserver/#initialize-new-intersection-observer>
163    fn new(
164        cx: &mut JSContext,
165        window: &Window,
166        proto: Option<HandleObject>,
167        callback: RootedCallback<IntersectionObserverCallback>,
168        init: &IntersectionObserverInit,
169    ) -> Fallible<DomRoot<Self>> {
170        // Step 3.
171        // > Attempt to parse a margin from options.rootMargin. If a list is returned,
172        // > set this’s internal [[rootMargin]] slot to that. Otherwise, throw a SyntaxError exception.
173        let root_margin = if let Ok(margin) = parse_a_margin(init.rootMargin.as_ref()) {
174            margin
175        } else {
176            return Err(Error::Syntax(None));
177        };
178
179        // Step 4.
180        // > Attempt to parse a margin from options.scrollMargin. If a list is returned,
181        // > set this’s internal [[scrollMargin]] slot to that. Otherwise, throw a SyntaxError exception.
182        let scroll_margin = if let Ok(margin) = parse_a_margin(init.scrollMargin.as_ref()) {
183            margin
184        } else {
185            return Err(Error::Syntax(None));
186        };
187
188        // Step 1 and step 2, 3, 4 setter
189        // > 1. Let this be a new IntersectionObserver object
190        // > 2. Set this’s internal [[callback]] slot to callback.
191        // > 3. ... set this’s internal [[rootMargin]] slot to that.
192        // > 4. ... set this’s internal [[scrollMargin]] slot to that.
193        let observer = reflect_dom_object_with_proto(
194            cx,
195            Box::new(Self::new_inherited(
196                window,
197                callback,
198                &init.root,
199                root_margin,
200                scroll_margin,
201            )),
202            window,
203            proto,
204        );
205
206        // Step 5-13
207        observer.init_observer(init)?;
208
209        Ok(observer)
210    }
211
212    /// Step 5-13 of <https://w3c.github.io/IntersectionObserver/#initialize-new-intersection-observer>
213    fn init_observer(&self, init: &IntersectionObserverInit) -> Fallible<()> {
214        // Step 5
215        // > Let thresholds be a list equal to options.threshold.
216        //
217        // Non-sequence value should be converted into Vec.
218        // Default value of thresholds is [0].
219        let mut thresholds = match &init.threshold {
220            Some(DoubleOrDoubleSequence::Double(num)) => vec![*num],
221            Some(DoubleOrDoubleSequence::DoubleSequence(sequence)) => sequence.clone(),
222            None => vec![Finite::wrap(0.)],
223        };
224
225        // Step 6
226        // > If any value in thresholds is less than 0.0 or greater than 1.0, throw a RangeError exception.
227        for num in &thresholds {
228            if **num < 0.0 || **num > 1.0 {
229                return Err(Error::Range(
230                    c"Value in thresholds should not be less than 0.0 or greater than 1.0"
231                        .to_owned(),
232                ));
233            }
234        }
235
236        // Step 7
237        // > Sort thresholds in ascending order.
238        thresholds.sort_by(|lhs, rhs| lhs.partial_cmp(&**rhs).unwrap());
239
240        // Step 8
241        // > If thresholds is empty, append 0 to thresholds.
242        if thresholds.is_empty() {
243            thresholds.push(Finite::wrap(0.));
244        }
245
246        // Step 9
247        // > The thresholds attribute getter will return this sorted thresholds list.
248        //
249        // Set this internal [[thresholds]] slot to the sorted thresholds list
250        // and getter will return the internal [[thresholds]] slot.
251        self.thresholds.replace(thresholds);
252
253        // Step 10
254        // > Let delay be the value of options.delay.
255        //
256        // Default value of delay is 0.
257        let mut delay = init.delay.unwrap_or(0);
258
259        // Step 11
260        // > If options.trackVisibility is true and delay is less than 100, set delay to 100.
261        //
262        // In Chromium, the minimum delay required is 100 milliseconds for observation that consider trackVisibilty.
263        // Currently, visibility is not implemented.
264        if init.trackVisibility {
265            delay = delay.max(100);
266        }
267
268        // Step 12
269        // > Set this’s internal [[delay]] slot to options.delay to delay.
270        self.delay.set(delay);
271
272        // Step 13
273        // > Set this’s internal [[trackVisibility]] slot to options.trackVisibility.
274        self.track_visibility.set(init.trackVisibility);
275
276        Ok(())
277    }
278
279    /// <https://w3c.github.io/IntersectionObserver/#observe-target-element>
280    fn observe_target_element(&self, target: &Element, no_gc: &NoGC) {
281        // Step 1
282        // > If target is in observer’s internal [[ObservationTargets]] slot, return.
283        let is_present = self
284            .observation_targets
285            .borrow()
286            .iter()
287            .any(|element| &**element == target);
288        if is_present {
289            return;
290        }
291
292        // Step 2
293        // > Let intersectionObserverRegistration be an IntersectionObserverRegistration record with
294        // > an observer property set to observer, a previousThresholdIndex property set to -1,
295        // > a previousIsIntersecting property set to false, and a previousIsVisible property set to false.
296        // Step 3
297        // > Append intersectionObserverRegistration to target’s internal [[RegisteredIntersectionObservers]] slot.
298        target.add_initial_intersection_observer_registration(self, no_gc);
299
300        if self.observation_targets.borrow().is_empty() {
301            self.connect_to_owner();
302        }
303
304        // Step 4
305        // > Add target to observer’s internal [[ObservationTargets]] slot.
306        self.observation_targets
307            .safe_borrow_mut(no_gc)
308            .push(Dom::from_ref(target));
309
310        target
311            .owner_window()
312            .Document()
313            .add_rendering_update_reason(
314                RenderingUpdateReason::IntersectionObserverStartedObservingTarget,
315            );
316    }
317
318    /// <https://w3c.github.io/IntersectionObserver/#unobserve-target-element>
319    fn unobserve_target_element(&self, target: &Element, no_gc: &NoGC) {
320        // Step 1
321        // > Remove the IntersectionObserverRegistration record whose observer property is equal to
322        // > this from target’s internal [[RegisteredIntersectionObservers]] slot, if present.
323        target
324            .registered_intersection_observers_mut(no_gc)
325            .retain(|registration| &*registration.observer != self);
326
327        // Step 2
328        // > Remove target from this’s internal [[ObservationTargets]] slot, if present
329        self.observation_targets
330            .safe_borrow_mut(no_gc)
331            .retain(|element| &**element != target);
332
333        // Should disconnect from owner if it is not observing anything.
334        if self.observation_targets.borrow().is_empty() {
335            self.disconnect_from_owner();
336        }
337    }
338
339    /// <https://w3c.github.io/IntersectionObserver/#queue-an-intersectionobserverentry>
340    #[allow(clippy::too_many_arguments)]
341    fn queue_an_intersectionobserverentry(
342        &self,
343        cx: &mut JSContext,
344        document: &Document,
345        time: CrossProcessInstant,
346        root_bounds: Rect<Au, CSSPixel>,
347        bounding_client_rect: Rect<Au, CSSPixel>,
348        intersection_rect: Rect<Au, CSSPixel>,
349        is_intersecting: bool,
350        is_visible: bool,
351        intersection_ratio: f64,
352        target: &Element,
353    ) {
354        let mut rect_to_domrectreadonly = |rect: Rect<Au, CSSPixel>| {
355            DOMRectReadOnly::new(
356                cx,
357                self.owner_doc.window().as_global_scope(),
358                None,
359                rect.origin.x.to_f64_px(),
360                rect.origin.y.to_f64_px(),
361                rect.size.width.to_f64_px(),
362                rect.size.height.to_f64_px(),
363            )
364        };
365
366        let root_bounds = rect_to_domrectreadonly(root_bounds);
367        let bounding_client_rect = rect_to_domrectreadonly(bounding_client_rect);
368        let intersection_rect = rect_to_domrectreadonly(intersection_rect);
369
370        // Step 1. Construct an IntersectionObserverEntry, passing in time, rootBounds,
371        // >    boundingClientRect, intersectionRect, isIntersecting, and target.
372        let time = document
373            .owner_global()
374            .performance(cx)
375            .to_dom_high_res_time_stamp(time);
376        let entry = IntersectionObserverEntry::new(
377            cx,
378            self.owner_doc.window(),
379            None,
380            time,
381            Some(&root_bounds),
382            &bounding_client_rect,
383            &intersection_rect,
384            is_intersecting,
385            is_visible,
386            Finite::wrap(intersection_ratio),
387            target,
388        );
389
390        // Step 2. Append it to observer's internal [[QueuedEntries]] slot.
391        self.queued_entries
392            .safe_borrow_mut(cx.no_gc())
393            .push(entry.as_traced());
394
395        // Step 3. Queue an intersection observer task for document.
396        document.queue_an_intersection_observer_task();
397    }
398
399    /// Step 3.1-3.5 of <https://w3c.github.io/IntersectionObserver/#notify-intersection-observers-algo>
400    pub(crate) fn invoke_callback_if_necessary(&self, cx: &mut js::context::JSContext) {
401        // Step 1
402        // > If observer’s internal [[QueuedEntries]] slot is empty, continue.
403        if self.queued_entries.borrow().is_empty() {
404            return;
405        }
406
407        // Step 2-3
408        // We trivially moved the entries and root them.
409        let queued_entries = self
410            .queued_entries
411            .take()
412            .iter_mut()
413            .map(|entry| entry.as_rooted())
414            .collect();
415
416        // Step 4-5
417        let _ = self
418            .callback
419            .Call_(cx, self, queued_entries, self, ExceptionHandling::Report);
420    }
421
422    /// Connect the observer itself into owner doc if it is unconnected.
423    /// If the [`IntersectionObserver`] is already connected, do nothing.
424    fn connect_to_owner(&self) {
425        if !self.connected_to_document.get() {
426            self.owner_doc.add_intersection_observer(self);
427            self.connected_to_document.set(true);
428        }
429    }
430
431    /// Disconnect the observer itself from owner doc.
432    /// If not connected to a [`Document`], do nothing.
433    fn disconnect_from_owner(&self) {
434        if self.connected_to_document.get() {
435            self.owner_doc.remove_intersection_observer(self);
436            self.connected_to_document.set(false);
437        }
438    }
439
440    /// <https://w3c.github.io/IntersectionObserver/#ref-for-intersectionobserver-content-clip>
441    /// An Element is defined as having a content clip if its computed style has overflow properties
442    /// that cause its content to be clipped to the element’s padding edge.
443    // TODO: this is not clear for `overflow: clip` since it is clipped based on overflow clip rect.
444    fn has_content_clip(element: &Element) -> bool {
445        element
446            .upcast::<Node>()
447            .effective_overflow_without_reflow()
448            .is_some_and(|overflow_axes| {
449                overflow_axes.x != Overflow::Visible || overflow_axes.y != Overflow::Visible
450            })
451    }
452
453    /// > The root intersection rectangle for an IntersectionObserver is
454    /// > the rectangle we’ll use to check against the targets.
455    ///
456    /// <https://w3c.github.io/IntersectionObserver/#intersectionobserver-root-intersection-rectangle>
457    pub(crate) fn root_intersection_rectangle(&self) -> Option<Rect<Au, CSSPixel>> {
458        let intersection_rectangle = match self.concrete_root() {
459            // Handle if root is an element.
460            Some(ElementOrDocument::Element(element)) => {
461                // TODO: recheck scrollbar approach and clip-path clipping from Chromium implementation.
462                if IntersectionObserver::has_content_clip(&element) {
463                    // > Otherwise, if the intersection root has a content clip, it’s the element’s padding area.
464                    element.upcast::<Node>().padding_box_without_reflow()
465                } else {
466                    // > Otherwise, it’s the result of getting the bounding box for the intersection root.
467                    element.upcast::<Node>().border_box_without_reflow()
468                }
469            },
470            // Handle if root is a Document, which includes implicit root and explicit Document root.
471            Some(ElementOrDocument::Document(document)) => {
472                // > If the intersection root is a document, it’s the size of the document's viewport
473                // > (note that this processing step can only be reached if the document is fully active).
474                // TODO: viewport should consider native scrollbar if exist. Recheck Servo's scrollbar approach.
475                let viewport = document.window().viewport_details().size;
476                Some(Rect::from_size(Size2D::new(
477                    Au::from_f32_px(viewport.width),
478                    Au::from_f32_px(viewport.height),
479                )))
480            },
481            None => None,
482        };
483
484        // > When calculating the root intersection rectangle for a same-origin-domain target,
485        // > the rectangle is then expanded according to the offsets in the IntersectionObserver’s
486        // > [[rootMargin]] slot in a manner similar to CSS’s margin property, with the four values
487        // > indicating the amount the top, right, bottom, and left edges, respectively, are offset by,
488        // > with positive lengths indicating an outward offset. Percentages are resolved relative to
489        // > the width of the undilated rectangle.
490        // TODO(stevennovaryo): add check for same-origin-domain
491        intersection_rectangle.map(|intersection_rectangle| {
492            let margin = Self::resolve_percentages_with_basis(
493                &self.root_margin.borrow(),
494                intersection_rectangle,
495            );
496            intersection_rectangle.outer_rect(margin)
497        })
498    }
499
500    /// Return root or try to get the top-level browsing context document in case if this is a implicit root.
501    /// <https://w3c.github.io/IntersectionObserver/#intersectionobserver-intersection-root>
502    // TODO: Currently we are unable to get the cross `ScriptThread` document.
503    fn concrete_root(&self) -> Option<ElementOrDocument> {
504        match &self.root {
505            Some(root) => Some(root.into()),
506            None => self
507                .owner_doc
508                .window()
509                .top_level_document_if_local()
510                .map(ElementOrDocument::Document),
511        }
512    }
513
514    /// Step 2.2.4-2.2.21 of <https://w3c.github.io/IntersectionObserver/#update-intersection-observations-algo>
515    ///
516    /// If some conditions require to skips "processing further", we will skips those steps and
517    /// return default values conformant to step 2.2.4. See [`IntersectionObservationOutput::default_skipped`].
518    ///
519    /// Note that current draft specs skipped wrong steps, as it should skip computing fields that
520    /// would result in different intersection entry other than the default entry per published spec.
521    /// <https://www.w3.org/TR/intersection-observer/>
522    fn maybe_compute_intersection_output(
523        &self,
524        target: &Element,
525        maybe_root_bounds: Option<Rect<Au, CSSPixel>>,
526    ) -> IntersectionObservationOutput {
527        // Step 5
528        // > If the intersection root is not the implicit root, and target is not in
529        // > the same document as the intersection root, skip to step 11.
530        // Step 6
531        // > If the intersection root is an Element, and target is not a descendant of
532        // > the intersection root in the containing block chain, skip to step 11.
533        match &self.root {
534            Some(UnrootedElementOrDocument::Document(document))
535                if document.as_rooted() != target.owner_document() =>
536            {
537                return IntersectionObservationOutput::default_skipped();
538            },
539            Some(UnrootedElementOrDocument::Element(element)) => {
540                // To ensure consistency, we also check for elements right now, but we can depend on the
541                // layout query later.
542                if element.owner_document() != target.owner_document() {
543                    return IntersectionObservationOutput::default_skipped();
544                }
545                if !element
546                    .owner_window()
547                    .is_containing_block_descendant_query_without_reflow(
548                        element.upcast(),
549                        target.upcast(),
550                    )
551                {
552                    return IntersectionObservationOutput::default_skipped();
553                }
554            },
555            _ => {},
556        }
557
558        // Step 7
559        // > Set targetRect to the DOMRectReadOnly obtained by getting the bounding box for target.
560        let maybe_target_rect = target.upcast::<Node>().border_box_without_reflow();
561
562        // Following the implementation of Gecko, we will skip further processing if these
563        // information not available. This would also handle display none element.
564        let (Some(root_bounds), Some(target_rect), Some(root_intersection)) =
565            (maybe_root_bounds, maybe_target_rect, self.concrete_root())
566        else {
567            return IntersectionObservationOutput::default_skipped();
568        };
569
570        // TODO(stevennovaryo): we should probably also consider adding visibity check, ideally
571        //                      it would require new query from LayoutThread.
572
573        // Step 8
574        // > Let intersectionRect be the result of running the compute the intersection algorithm on
575        // > target and observer’s intersection root.
576        let maybe_intersection_rect = compute_the_intersection(
577            target,
578            &root_intersection,
579            root_bounds,
580            target_rect,
581            &self.scroll_margin.borrow(),
582        );
583        let intersection_rect = maybe_intersection_rect.unwrap_or_default();
584
585        // Step 9
586        // > Let targetArea be targetRect’s area.
587        // Step 10
588        // > Let intersectionArea be intersectionRect’s area.
589        // These steps are folded in Step 12, rewriting (w1 * h1) / (w2 * h2) as (w1 / w2) * (h1 / h2)
590        // to avoid multiplication overflows.
591
592        // Step 11
593        // > Let isIntersecting be true if targetRect and rootBounds intersect or are edge-adjacent,
594        // > even if the intersection has zero area (because rootBounds or targetRect have zero area).
595        // Because we are considering edge-adjacent, instead of checking whether the rectangle is empty,
596        // we are checking whether the rectangle is negative or not.
597        let is_intersecting = maybe_intersection_rect.is_some();
598
599        // Step 12
600        // > If targetArea is non-zero, let intersectionRatio be intersectionArea divided by targetArea.
601        // > Otherwise, let intersectionRatio be 1 if isIntersecting is true, or 0 if isIntersecting is false.
602        let intersection_ratio = if target_rect.size.width.0 == 0 || target_rect.size.height.0 == 0
603        {
604            is_intersecting.into()
605        } else {
606            (intersection_rect.size.width.0 as f64 / target_rect.size.width.0 as f64) *
607                (intersection_rect.size.height.0 as f64 / target_rect.size.height.0 as f64)
608        };
609
610        // Step 13
611        // > Set thresholdIndex to the index of the first entry in observer.thresholds whose value is
612        // > greater than intersectionRatio, or the length of observer.thresholds if intersectionRatio is
613        // > greater than or equal to the last entry in observer.thresholds.
614        let threshold_index = self
615            .thresholds
616            .borrow()
617            .iter()
618            .position(|threshold| **threshold > intersection_ratio)
619            .unwrap_or(self.thresholds.borrow().len());
620
621        // If the index is 0, the first threshold value is greater
622        // than the observed ratio, so we're not actually matching yet.
623        // The spec differentiates between this case and the case where
624        // there is no intersection, but other browser engines do not.
625        let threshold_index = if is_intersecting && threshold_index > 0 {
626            ThresholdIndex::Matching(threshold_index)
627        } else {
628            ThresholdIndex::NotMatching
629        };
630
631        // Step 14
632        // > Let isVisible be the result of running the visibility algorithm on target.
633        // TODO: Implement visibility algorithm
634        let is_visible = false;
635
636        // We never report isIntersecting as true unless we have exceeded a threshold,
637        // which matches other browser eengines.
638        // See https://github.com/w3c/IntersectionObserver/issues/432 for background.
639        let is_intersecting = matches!(threshold_index, ThresholdIndex::Matching(..));
640
641        IntersectionObservationOutput::new_computed(
642            threshold_index,
643            is_intersecting,
644            target_rect,
645            intersection_rect,
646            intersection_ratio,
647            is_visible,
648            root_bounds,
649        )
650    }
651
652    /// Step 2.2.1-2.2.21 of <https://w3c.github.io/IntersectionObserver/#update-intersection-observations-algo>
653    pub(crate) fn update_intersection_observations_steps(
654        &self,
655        cx: &mut JSContext,
656        document: &Document,
657        time: CrossProcessInstant,
658        root_bounds: Option<Rect<Au, CSSPixel>>,
659    ) {
660        for target in &*self.observation_targets.borrow() {
661            // Step 1
662            // > Let registration be the IntersectionObserverRegistration record in target’s internal
663            // > [[RegisteredIntersectionObservers]] slot whose observer property is equal to observer.
664            let registration = target.get_intersection_observer_registration(self).unwrap();
665
666            // Step 2
667            // > If (time - registration.lastUpdateTime < observer.delay), skip further processing for target.
668            if time - registration.last_update_time.get() <
669                Duration::from_millis(self.delay.get().max(0) as u64)
670            {
671                return;
672            }
673
674            // Step 3
675            // > Set registration.lastUpdateTime to time.
676            registration.last_update_time.set(time);
677
678            // step 4-14
679            let intersection_output = self.maybe_compute_intersection_output(target, root_bounds);
680
681            // Step 15-17
682            // > 15. Let previousThresholdIndex be the registration’s previousThresholdIndex property.
683            // > 16. Let previousIsIntersecting be the registration’s previousIsIntersecting property.
684            // > 17. Let previousIsVisible be the registration’s previousIsVisible property.
685            let previous_threshold_index = registration.previous_threshold_index.get();
686            let previous_is_intersecting = registration.previous_is_intersecting.get();
687            let previous_is_visible = registration.previous_is_visible.get();
688
689            // Step 18
690            // > If thresholdIndex does not equal previousThresholdIndex, or
691            // > if isIntersecting does not equal previousIsIntersecting, or
692            // > if isVisible does not equal previousIsVisible,
693            // > queue an IntersectionObserverEntry, passing in observer, time, rootBounds,
694            // > targetRect, intersectionRect, isIntersecting, isVisible, and target.
695            if Some(intersection_output.threshold_index) != previous_threshold_index ||
696                intersection_output.is_intersecting != previous_is_intersecting ||
697                intersection_output.is_visible != previous_is_visible
698            {
699                // TODO(stevennovaryo): Per IntersectionObserverEntry interface, the rootBounds
700                //                      should be null for cross-origin-domain target.
701                self.queue_an_intersectionobserverentry(
702                    cx,
703                    document,
704                    time,
705                    intersection_output.root_bounds,
706                    intersection_output.target_rect,
707                    intersection_output.intersection_rect,
708                    intersection_output.is_intersecting,
709                    intersection_output.is_visible,
710                    intersection_output.intersection_ratio,
711                    target,
712                );
713            }
714
715            // Step 19-21
716            // > 19. Assign thresholdIndex to registration’s previousThresholdIndex property.
717            // > 20. Assign isIntersecting to registration’s previousIsIntersecting property.
718            // > 21. Assign isVisible to registration’s previousIsVisible property.
719            registration
720                .previous_threshold_index
721                .set(Some(intersection_output.threshold_index));
722            registration
723                .previous_is_intersecting
724                .set(intersection_output.is_intersecting);
725            registration
726                .previous_is_visible
727                .set(intersection_output.is_visible);
728        }
729    }
730
731    fn resolve_percentages_with_basis(
732        margin: &IntersectionObserverMargin,
733        containing_block: Rect<Au, CSSPixel>,
734    ) -> SideOffsets2D<Au, CSSPixel> {
735        let inner = &margin.0;
736        SideOffsets2D::new(
737            inner.0.to_used_value(containing_block.height()),
738            inner.1.to_used_value(containing_block.width()),
739            inner.2.to_used_value(containing_block.height()),
740            inner.3.to_used_value(containing_block.width()),
741        )
742    }
743}
744
745impl IntersectionObserverMethods<crate::DomTypeHolder> for IntersectionObserver {
746    /// > The root provided to the IntersectionObserver constructor, or null if none was provided.
747    ///
748    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-root>
749    fn GetRoot(&self) -> Option<ElementOrDocument> {
750        self.root.as_ref().map(|root| root.into())
751    }
752
753    /// > Offsets applied to the root intersection rectangle, effectively growing or
754    /// > shrinking the box that is used to calculate intersections. These offsets are only
755    /// > applied when handling same-origin-domain targets; for cross-origin-domain targets
756    /// > they are ignored.
757    ///
758    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-rootmargin>
759    fn RootMargin(&self) -> DOMString {
760        self.root_margin.borrow().to_css_string().into()
761    }
762
763    /// > Offsets are applied to scrollports on the path from intersection root to target,
764    /// > effectively growing or shrinking the clip rects used to calculate intersections.
765    ///
766    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-scrollmargin>
767    fn ScrollMargin(&self) -> DOMString {
768        self.scroll_margin.borrow().to_css_string().into()
769    }
770
771    /// > A list of thresholds, sorted in increasing numeric order, where each threshold
772    /// > is a ratio of intersection area to bounding box area of an observed target.
773    /// > Notifications for a target are generated when any of the thresholds are crossed
774    /// > for that target. If no options.threshold was provided to the IntersectionObserver
775    /// > constructor, or the sequence is empty, the value of this attribute will be `[0]`.
776    ///
777    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-thresholds>
778    fn Thresholds(&self, cx: &mut JSContext, retval: MutableHandleValue) {
779        to_frozen_array(cx, &self.thresholds.borrow(), retval);
780    }
781
782    /// > A number indicating the minimum delay in milliseconds between notifications from
783    /// > this observer for a given target.
784    ///
785    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-delay>
786    fn Delay(&self) -> i32 {
787        self.delay.get()
788    }
789
790    /// > A boolean indicating whether this IntersectionObserver will track changes in a target’s visibility.
791    ///
792    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-trackvisibility>
793    fn TrackVisibility(&self) -> bool {
794        self.track_visibility.get()
795    }
796
797    /// > Run the observe a target Element algorithm, providing this and target.
798    ///
799    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-observe>
800    fn Observe(&self, no_gc: &NoGC, target: &Element) {
801        self.observe_target_element(target, no_gc);
802    }
803
804    /// > Run the unobserve a target Element algorithm, providing this and target.
805    ///
806    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-unobserve>
807    fn Unobserve(&self, no_gc: &NoGC, target: &Element) {
808        self.unobserve_target_element(target, no_gc);
809    }
810
811    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-disconnect>
812    fn Disconnect(&self, no_gc: &NoGC) {
813        // > For each target in this’s internal [[ObservationTargets]] slot:
814        self.observation_targets.borrow().iter().for_each(|target| {
815            // > 1. Remove the IntersectionObserverRegistration record whose observer property is equal to
816            // >    this from target’s internal [[RegisteredIntersectionObservers]] slot.
817            target.remove_intersection_observer(self, no_gc);
818        });
819        // > 2. Remove target from this’s internal [[ObservationTargets]] slot.
820        self.observation_targets.safe_borrow_mut(no_gc).clear();
821
822        // We should remove this observer from the event loop.
823        self.disconnect_from_owner();
824    }
825
826    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-takerecords>
827    fn TakeRecords(&self) -> Vec<DomRoot<IntersectionObserverEntry>> {
828        // Step 1-3.
829        self.queued_entries
830            .take()
831            .iter()
832            .map(|entry| entry.as_rooted())
833            .collect()
834    }
835
836    /// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-intersectionobserver>
837    fn Constructor(
838        cx: &mut JSContext,
839        window: &Window,
840        proto: Option<HandleObject>,
841        callback: RootedCallback<IntersectionObserverCallback>,
842        init: &IntersectionObserverInit,
843    ) -> Fallible<DomRoot<IntersectionObserver>> {
844        Self::new(cx, window, proto, callback, init)
845    }
846}
847
848#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)]
849enum ThresholdIndex {
850    NotMatching,
851    Matching(usize),
852}
853
854/// <https://w3c.github.io/IntersectionObserver/#intersectionobserverregistration>
855#[derive(Clone, JSTraceable, MallocSizeOf)]
856#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
857pub(crate) struct IntersectionObserverRegistration {
858    pub(crate) observer: Dom<IntersectionObserver>,
859    previous_threshold_index: Cell<Option<ThresholdIndex>>,
860    previous_is_intersecting: Cell<bool>,
861    #[no_trace]
862    last_update_time: Cell<CrossProcessInstant>,
863    previous_is_visible: Cell<bool>,
864}
865
866impl IntersectionObserverRegistration {
867    /// Initial value of [`IntersectionObserverRegistration`] according to
868    /// step 2 of <https://w3c.github.io/IntersectionObserver/#observe-target-element>.
869    /// > Let intersectionObserverRegistration be an IntersectionObserverRegistration record with
870    /// > an observer property set to observer, a previousThresholdIndex property set to -1,
871    /// > a previousIsIntersecting property set to false, and a previousIsVisible property set to false.
872    pub(crate) fn new_initial(observer: &IntersectionObserver) -> Self {
873        IntersectionObserverRegistration {
874            observer: Dom::from_ref(observer),
875            previous_threshold_index: Cell::new(None),
876            previous_is_intersecting: Cell::new(false),
877            last_update_time: Cell::new(CrossProcessInstant::epoch()),
878            previous_is_visible: Cell::new(false),
879        }
880    }
881}
882
883/// <https://w3c.github.io/IntersectionObserver/#parse-a-margin>
884fn parse_a_margin(value: Option<&DOMString>) -> Result<IntersectionObserverMargin, ()> {
885    // <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverinit-rootmargin> &&
886    // <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverinit-scrollmargin>
887    // > ... defaulting to "0px".
888    let value = match value {
889        Some(str) => &str.str(),
890        _ => "0px",
891    };
892
893    // Create necessary style ParserContext and utilize stylo's IntersectionObserverMargin
894    let mut parser = Parser::new(value);
895
896    let context = parser_context_for_anonymous_content(
897        CssRuleType::Style,
898        ParsingMode::DEFAULT,
899        &ANONYMOUS_CONTENT_URL_DATA,
900    );
901
902    parser
903        .parse_entirely(|p| IntersectionObserverMargin::parse(&context, p))
904        .map_err(|_| ())
905}
906
907/// In terms of intersection observer, we consider zero-area rectangles as long as the area is not negative.
908fn intersect_rectangle(
909    lhs: &Rect<Au, CSSPixel>,
910    rhs: &Rect<Au, CSSPixel>,
911) -> Option<Rect<Au, CSSPixel>> {
912    let box_result = lhs.to_box2d().intersection_unchecked(&rhs.to_box2d());
913    if box_result.is_negative() {
914        None
915    } else {
916        Some(box_result.to_rect())
917    }
918}
919
920/// Compute the intersection rectangle of the target [`Element`] returning the results of intersection in the coordinate
921/// space of the target's owning [`Document`]. Additionally, we assume that both the target and the root is connected.
922/// <https://w3c.github.io/IntersectionObserver/#compute-the-intersection>
923fn compute_the_intersection(
924    target: &Element,
925    root: &ElementOrDocument,
926    root_bounds: Rect<Au, CSSPixel>,
927    mut intersection_rect: Rect<Au, CSSPixel>,
928    scroll_margin: &IntersectionObserverMargin,
929) -> Option<Rect<Au, CSSPixel>> {
930    // > 1. Let intersectionRect be the result of getting the bounding box for target.
931    // We had delegated the computation of this to the caller of the function.
932
933    // > 2. Let container be the containing block of target.
934    let mut container = match target
935        .upcast::<Node>()
936        .containing_block_node_without_reflow()
937    {
938        Some(node) => ElementOrDocument::Element(DomRoot::downcast(node).unwrap()),
939        None => ElementOrDocument::Document(target.owner_document()),
940    };
941
942    // Total offsets gained from traversing through multiple navigables. We use this to map the coordinate space.
943    // TODO: We should store the product sum of transformation matrices instead. But this should be enough to handle
944    // scrolling, simple translation, and offset from containing block.
945    let mut total_inter_document_offset = Vector2D::zero();
946
947    // > 3. While container is not root:
948    while container != *root {
949        let containing_element = match container {
950            ElementOrDocument::Document(ref containing_document) => {
951                // > 3.1. If container is the document of a nested browsing context, update intersectionRect by clipping
952                // >      to the viewport of the document, and update container to be the browsing context container of container.
953                if let Some(frame_container) = containing_document
954                    .browsing_context()
955                    .and_then(|window| window.frame_element().map(DomRoot::from_ref))
956                {
957                    let viewport_rect = f32_rect_to_au_rect(Rect::from_size(
958                        containing_document.window().viewport_details().size,
959                    ));
960
961                    intersection_rect = intersect_rectangle(&intersection_rect, &viewport_rect)?;
962
963                    let current_offset = frame_container
964                        .upcast::<Node>()
965                        .padding_box()
966                        .unwrap()
967                        .origin
968                        .to_vector();
969                    intersection_rect.origin += current_offset;
970                    total_inter_document_offset += current_offset;
971
972                    frame_container
973                } else {
974                    // TODO: Theoritically, this shouldn't be reachable as we have ensured that the root is reachable
975                    // in the previous steps. But we are still unable to iterate through cross-origin ancestor iframes,
976                    // and we will need to stop the iteration for that case.
977                    break;
978                }
979            },
980            ElementOrDocument::Element(ref root) => root.clone(),
981        };
982
983        // > 3.2. Map intersectionRect to the coordinate space of container.
984        // TODO(#35767): We don't map the coordinate space per each iteration yet, instead all the rectangles are
985        // in the viewport coordinate space. But this would cause the scroll margin calculation to be inaccurate
986        // with respect to transforms.
987
988        // > 3.3. If container is a scroll container, apply the IntersectionObserver’s [[scrollMargin]]
989        // >      to the container’s clip rect as described in apply scroll margin to a scrollport.
990        // > 3.4. If container has a content clip or a css clip-path property, update intersectionRect
991        // >      by applying container’s clip.
992        // TODO(#35767): handle `overflow: clip` and resolve clipping for x-axis and y-axis independently.
993        // Additionally, handle css `clip-path` as well.
994        if IntersectionObserver::has_content_clip(&containing_element) &&
995            let Some(container_padding_box) = containing_element
996                .upcast::<Node>()
997                .padding_box_without_reflow()
998        {
999            let container_padding_box =
1000                if containing_element.establishes_scroll_container_without_reflow() {
1001                    let margin = IntersectionObserver::resolve_percentages_with_basis(
1002                        scroll_margin,
1003                        container_padding_box,
1004                    );
1005                    container_padding_box.outer_rect(margin)
1006                } else {
1007                    container_padding_box
1008                };
1009
1010            intersection_rect = intersect_rectangle(&intersection_rect, &container_padding_box)?;
1011        }
1012
1013        // > 3.5. If container is the root element of a browsing context, update container to be the
1014        // >      browsing context’s document; otherwise, update container to be the containing block
1015        // >      of container.
1016        // Additionally, for a node that doesn't have an element that establishes its containing block, we should
1017        // refer to the browsing context's document.
1018        container = match containing_element
1019            .upcast::<Node>()
1020            .containing_block_node_without_reflow()
1021            .and_then(DomRoot::downcast::<Element>)
1022        {
1023            Some(element) => ElementOrDocument::Element(element),
1024            None => ElementOrDocument::Document(containing_element.owner_document()),
1025        };
1026    }
1027
1028    // Step 4
1029    // > Map intersectionRect to the coordinate space of root.
1030    // TODO(#35767): we don't map the coordinate space per each iteration yet, instead all the rectangles are
1031    // in the viewport coordinate space.
1032
1033    // Step 5
1034    // > Update intersectionRect by intersecting it with the root intersection rectangle.
1035    intersection_rect = intersect_rectangle(&intersection_rect, &root_bounds)?;
1036
1037    // Step 6
1038    // > Map intersectionRect to the coordinate space of the viewport of the document containing target.
1039    // Offset the intersectionRect back to the coordinate space of target's document.
1040    intersection_rect.origin -= total_inter_document_offset;
1041
1042    // Step 7
1043    // > Return intersectionRect.
1044    Some(intersection_rect)
1045}
1046
1047/// The values from computing step 2.2.4-2.2.14 in
1048/// <https://w3c.github.io/IntersectionObserver/#update-intersection-observations-algo>.
1049/// See [`IntersectionObserver::maybe_compute_intersection_output`].
1050struct IntersectionObservationOutput {
1051    pub(crate) threshold_index: ThresholdIndex,
1052    pub(crate) is_intersecting: bool,
1053    pub(crate) target_rect: Rect<Au, CSSPixel>,
1054    pub(crate) intersection_rect: Rect<Au, CSSPixel>,
1055    pub(crate) intersection_ratio: f64,
1056    pub(crate) is_visible: bool,
1057
1058    /// The root intersection rectangle [`IntersectionObserver::root_intersection_rectangle`].
1059    /// If the processing is skipped, computation should report the default zero value.
1060    pub(crate) root_bounds: Rect<Au, CSSPixel>,
1061}
1062
1063impl IntersectionObservationOutput {
1064    /// Default values according to
1065    /// <https://w3c.github.io/IntersectionObserver/#update-intersection-observations-algo>.
1066    /// Step 4.
1067    /// > Let:
1068    /// > - thresholdIndex be 0.
1069    /// > - isIntersecting be false.
1070    /// > - targetRect be a DOMRectReadOnly with x, y, width, and height set to 0.
1071    /// > - intersectionRect be a DOMRectReadOnly with x, y, width, and height set to 0.
1072    ///
1073    /// For fields that the default values is not directly mentioned, the values conformant
1074    /// to current browser implementation or WPT test is used instead.
1075    fn default_skipped() -> Self {
1076        Self {
1077            threshold_index: ThresholdIndex::NotMatching,
1078            is_intersecting: false,
1079            target_rect: Rect::zero(),
1080            intersection_rect: Rect::zero(),
1081            intersection_ratio: 0.,
1082            is_visible: false,
1083            root_bounds: Rect::zero(),
1084        }
1085    }
1086
1087    fn new_computed(
1088        threshold_index: ThresholdIndex,
1089        is_intersecting: bool,
1090        target_rect: Rect<Au, CSSPixel>,
1091        intersection_rect: Rect<Au, CSSPixel>,
1092        intersection_ratio: f64,
1093        is_visible: bool,
1094        root_bounds: Rect<Au, CSSPixel>,
1095    ) -> Self {
1096        Self {
1097            threshold_index,
1098            is_intersecting,
1099            target_rect,
1100            intersection_rect,
1101            intersection_ratio,
1102            is_visible,
1103            root_bounds,
1104        }
1105    }
1106}
1107
1108impl script_bindings::callback::OwnerWindow<crate::DomTypeHolder> for IntersectionObserver {}