Skip to main content

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