mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
dom: Implement minimal IntersectionObserver workflow (#35551)
* Add very rough implemnentation of observation steps Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Fix entry reflection and propagate can_gc Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Fix BorrowError and add fragment find descendant Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Implement is descendant in containing block path correctly Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Fix unrooted error and tidy issues Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Fix comments Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Remove is descendant of other node query I suppose these changes is better separated to other PRs. Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Fix intersection and refactor registration Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Use AppUnit more and propagate GlobalScope better Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Update WPT expectations Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Revert delay changes Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Align compute intersection algo to other browser actual behavior Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Align processing documents and note several issues Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Update WPT Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Minor lint Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Fix top level browsing context Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Make Registration rootable Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Avoid reflow inside observation step algo Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Using borrow for iterating registration Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Fix document disconnect Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Update WPT Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Address comments and minor quality suggestions Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Root the observer before nofifying any of it Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Tidy docs Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Account not found element and refactor observation step Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Fix documentations Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Ignore position of document viewport Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Refactor root intersection rectangle Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Add can GC note to the callback Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Fix top-level browsing context term Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> * Fix minor comments Signed-off-by: stevennovaryo <steven.novaryo@gmail.com> --------- Signed-off-by: stevennovaryo <steven.novaryo@gmail.com>
This commit is contained in:
parent
2113e54819
commit
67a5f285ed
48 changed files with 850 additions and 175 deletions
|
@ -2,6 +2,8 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
use dom_struct::dom_struct;
|
||||
use js::rust::HandleObject;
|
||||
|
||||
|
@ -9,7 +11,8 @@ use super::bindings::codegen::Bindings::IntersectionObserverEntryBinding::{
|
|||
IntersectionObserverEntryInit, IntersectionObserverEntryMethods,
|
||||
};
|
||||
use super::bindings::num::Finite;
|
||||
use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
|
||||
use crate::dom::bindings::codegen::Bindings::DOMRectReadOnlyBinding::DOMRectInit;
|
||||
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
|
||||
use crate::dom::bindings::root::{Dom, DomRoot};
|
||||
use crate::dom::domrectreadonly::DOMRectReadOnly;
|
||||
use crate::dom::element::Element;
|
||||
|
@ -22,24 +25,100 @@ use crate::script_runtime::CanGc;
|
|||
#[dom_struct]
|
||||
pub(crate) struct IntersectionObserverEntry {
|
||||
reflector_: Reflector,
|
||||
// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-time>
|
||||
time: Cell<Finite<f64>>,
|
||||
// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-rootbounds>
|
||||
root_bounds: Option<Dom<DOMRectReadOnly>>,
|
||||
// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-boundingclientrect>
|
||||
bounding_client_rect: Dom<DOMRectReadOnly>,
|
||||
// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-intersectionrect>
|
||||
intersection_rect: Dom<DOMRectReadOnly>,
|
||||
// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-isintersecting>
|
||||
is_intersecting: Cell<bool>,
|
||||
// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-isvisible>
|
||||
is_visible: Cell<bool>,
|
||||
// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-intersectionratio>
|
||||
intersection_ratio: Cell<Finite<f64>>,
|
||||
// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-target>
|
||||
target: Dom<Element>,
|
||||
}
|
||||
|
||||
impl IntersectionObserverEntry {
|
||||
pub(crate) fn new_inherited(init: &IntersectionObserverEntryInit) -> Self {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn new_inherited(
|
||||
time: Finite<f64>,
|
||||
root_bounds: Option<&DOMRectReadOnly>,
|
||||
bounding_client_rect: &DOMRectReadOnly,
|
||||
intersection_rect: &DOMRectReadOnly,
|
||||
is_intersecting: bool,
|
||||
is_visible: bool,
|
||||
intersection_ratio: Finite<f64>,
|
||||
target: &Element,
|
||||
) -> Self {
|
||||
Self {
|
||||
reflector_: Reflector::new(),
|
||||
target: init.target.as_traced(),
|
||||
target: Dom::from_ref(target),
|
||||
time: Cell::new(time),
|
||||
root_bounds: root_bounds.map(Dom::from_ref),
|
||||
bounding_client_rect: Dom::from_ref(bounding_client_rect),
|
||||
intersection_rect: Dom::from_ref(intersection_rect),
|
||||
is_intersecting: Cell::new(is_intersecting),
|
||||
is_visible: Cell::new(is_visible),
|
||||
intersection_ratio: Cell::new(intersection_ratio),
|
||||
}
|
||||
}
|
||||
|
||||
fn new(
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn new(
|
||||
window: &Window,
|
||||
proto: Option<HandleObject>,
|
||||
time: Finite<f64>,
|
||||
root_bounds: Option<&DOMRectReadOnly>,
|
||||
bounding_client_rect: &DOMRectReadOnly,
|
||||
intersection_rect: &DOMRectReadOnly,
|
||||
is_intersecting: bool,
|
||||
is_visible: bool,
|
||||
intersection_ratio: Finite<f64>,
|
||||
target: &Element,
|
||||
can_gc: CanGc,
|
||||
) -> DomRoot<Self> {
|
||||
let observer = Box::new(Self::new_inherited(
|
||||
time,
|
||||
root_bounds,
|
||||
bounding_client_rect,
|
||||
intersection_rect,
|
||||
is_intersecting,
|
||||
is_visible,
|
||||
intersection_ratio,
|
||||
target,
|
||||
));
|
||||
reflect_dom_object_with_proto(observer, window, proto, can_gc)
|
||||
}
|
||||
|
||||
fn new_from_dictionary(
|
||||
window: &Window,
|
||||
proto: Option<HandleObject>,
|
||||
init: &IntersectionObserverEntryInit,
|
||||
can_gc: CanGc,
|
||||
) -> DomRoot<Self> {
|
||||
let observer = Box::new(Self::new_inherited(init));
|
||||
let domrectreadonly_from_dictionary = |dictionary: &DOMRectInit| {
|
||||
DOMRectReadOnly::new_from_dictionary(
|
||||
window.as_global_scope(),
|
||||
proto,
|
||||
dictionary,
|
||||
can_gc,
|
||||
)
|
||||
};
|
||||
let observer = Box::new(Self::new_inherited(
|
||||
init.time,
|
||||
Some(&*domrectreadonly_from_dictionary(&init.rootBounds)),
|
||||
&domrectreadonly_from_dictionary(&init.boundingClientRect),
|
||||
&domrectreadonly_from_dictionary(&init.intersectionRect),
|
||||
init.isIntersecting,
|
||||
init.isVisible,
|
||||
init.intersectionRatio,
|
||||
&init.target,
|
||||
));
|
||||
reflect_dom_object_with_proto(observer, window, proto, can_gc)
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +130,7 @@ impl IntersectionObserverEntryMethods<crate::DomTypeHolder> for IntersectionObse
|
|||
///
|
||||
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-time>
|
||||
fn Time(&self) -> Finite<f64> {
|
||||
Finite::new(0.).unwrap()
|
||||
self.time.get()
|
||||
}
|
||||
|
||||
/// > For a same-origin-domain target, this will be the root intersection rectangle.
|
||||
|
@ -61,14 +140,14 @@ impl IntersectionObserverEntryMethods<crate::DomTypeHolder> for IntersectionObse
|
|||
///
|
||||
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-rootbounds>
|
||||
fn GetRootBounds(&self) -> Option<DomRoot<DOMRectReadOnly>> {
|
||||
None
|
||||
self.root_bounds.as_ref().map(|rect| rect.as_rooted())
|
||||
}
|
||||
|
||||
/// > A DOMRectReadOnly obtained by getting the bounding box for target.
|
||||
///
|
||||
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-boundingclientrect>
|
||||
fn BoundingClientRect(&self) -> DomRoot<DOMRectReadOnly> {
|
||||
DOMRectReadOnly::new(&self.global(), None, 0., 0., 0., 0., CanGc::note())
|
||||
self.bounding_client_rect.as_rooted()
|
||||
}
|
||||
|
||||
/// > boundingClientRect, intersected by each of target's ancestors' clip rects (up to
|
||||
|
@ -78,7 +157,7 @@ impl IntersectionObserverEntryMethods<crate::DomTypeHolder> for IntersectionObse
|
|||
///
|
||||
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-intersectionrect>
|
||||
fn IntersectionRect(&self) -> DomRoot<DOMRectReadOnly> {
|
||||
DOMRectReadOnly::new(&self.global(), None, 0., 0., 0., 0., CanGc::note())
|
||||
self.intersection_rect.as_rooted()
|
||||
}
|
||||
|
||||
/// > True if the target intersects with the root; false otherwise. This flag makes it
|
||||
|
@ -90,14 +169,14 @@ impl IntersectionObserverEntryMethods<crate::DomTypeHolder> for IntersectionObse
|
|||
///
|
||||
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-isintersecting>
|
||||
fn IsIntersecting(&self) -> bool {
|
||||
false
|
||||
self.is_intersecting.get()
|
||||
}
|
||||
|
||||
/// > Contains the result of running the visibility algorithm on target.
|
||||
///
|
||||
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-isvisible>
|
||||
fn IsVisible(&self) -> bool {
|
||||
false
|
||||
self.is_visible.get()
|
||||
}
|
||||
|
||||
/// > If the boundingClientRect has non-zero area, this will be the ratio of
|
||||
|
@ -106,7 +185,7 @@ impl IntersectionObserverEntryMethods<crate::DomTypeHolder> for IntersectionObse
|
|||
///
|
||||
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverentry-intersectionratio>
|
||||
fn IntersectionRatio(&self) -> Finite<f64> {
|
||||
Finite::new(0.).unwrap()
|
||||
self.intersection_ratio.get()
|
||||
}
|
||||
|
||||
/// > The Element whose intersection with the intersection root changed.
|
||||
|
@ -123,6 +202,6 @@ impl IntersectionObserverEntryMethods<crate::DomTypeHolder> for IntersectionObse
|
|||
can_gc: CanGc,
|
||||
init: &IntersectionObserverEntryInit,
|
||||
) -> DomRoot<IntersectionObserverEntry> {
|
||||
Self::new(window, proto, init, can_gc)
|
||||
Self::new_from_dictionary(window, proto, init, can_gc)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue