Skip to content

dom: IntersectionObserver initialization#35314

Merged
jdm merged 11 commits intoservo:mainfrom
stevennovaryo:intersection-observer
Feb 13, 2025
Merged

dom: IntersectionObserver initialization#35314
jdm merged 11 commits intoservo:mainfrom
stevennovaryo:intersection-observer

Conversation

@stevennovaryo
Copy link
Copy Markdown
Contributor

@stevennovaryo stevennovaryo commented Feb 6, 2025

Depends on Stylo's Bug 1946679 - IntersectionObserverRootMargin.

Implement initialization and interfaces of IntersectionObserver following the #intersection-observer-interface and #intersection-observer-processing-model. Specifically, the processing model implementation includes:

The remaining implementation of IntersectionObserver will be more related to the Event Loop #update-the-rendering (see step 19). Which will be my next work.

Feedback are appreciated about #lifefime, fields of the objects, and overall rust codes.

cc: @xiaochengh

Try


  • ./mach build -d does not report any errors
  • ./mach test-tidy does not report any errors
  • These changes are part of Implement IntersectionObserver #31021
  • Some changes is included in WPT, further business logic will be testable once IntersectionObserver is implemented as a whole. I would be glad to add any tests if it deemed necessary.

Signed-off-by: stevennovaryo <[email protected]>
@stevennovaryo
Copy link
Copy Markdown
Contributor Author

stevennovaryo commented Feb 11, 2025

I think it is quite ready for review. I am open to expand this PR's scope. I will make IntersectionObserverMargin refer to stylo's soon, after https://phabricator.services.mozilla.com/D237514 is up.

@stevennovaryo stevennovaryo marked this pull request as ready for review February 11, 2025 06:11
Copy link
Copy Markdown
Member

@jdm jdm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was straightforward to review! Thank you!

use crate::dom::bindings::str::DOMString;
use crate::dom::window::Window;
use crate::script_runtime::{CanGc, JSContext};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: revert this removal.

callback: Rc<IntersectionObserverCallback>,

/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-queuedentries-slot>
queued_entries: DomRefCell<Vec<DomRoot<IntersectionObserverEntry>>>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a Dom instead of a DomRoot?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thanks, it should make GC works more seamlessly.

/// <https://w3c.github.io/IntersectionObserver/#intersectionobserverregistration>
#[derive(JSTraceable, MallocSizeOf)]
pub(crate) struct IntersectionObserverRegistration {
observer: DomRoot<IntersectionObserver>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a Dom and add #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root] to the struct?

/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-rootmargin-slot>
#[no_trace]
#[ignore_malloc_size_of = "Defined in style"]
root_margin: DomRefCell<IntersectionObserverRootMargin>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiousity, can this and scroll_margin use Cell instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current object it is compatible, as it does not implement Copy trait. Previously, I just changed it into RefCell, but I think it could use Cell<T> as well, because the underlying type is quite basic.

};

// > 6. If any value in thresholds is less than 0.0 or greater than 1.0, throw a RangeError exception.
threshold.iter().try_for_each(|num| {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be easier to read as a for loop:

for num in &threshold {
    if **num < 0.0 || **num > 1.0 {
        return Err(Error::Range("Value in thresholds should not be less than 0.0 or greater than 1.0".to_owned()));
    }
}

Self::AbsolutePixelLength(0.0)
// > 11. If options.trackVisibility is true and delay is less than 100, set delay to 100.
if init.trackVisibility {
delay = delay.min(100);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we need delay.max(100) here, otherwise we'll take the lower value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks! My bad for the silly mistake.

Comment on lines +291 to +294
match parser.parse_entirely(|p| IntersectionObserverRootMargin::parse(&context, p)) {
Ok(margin) => Ok(margin),
_ => Err(()),
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parser.parse_entirely(|p| IntersectionObserverRootMargin::parse(&context, p))
    .map_err(|_| ())


/// > The root provided to the IntersectionObserver constructor, or null if none was provided.
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-root>
#[ignore_malloc_size_of = "mozjs"]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

@stevennovaryo stevennovaryo Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that would make the implementation much more clean. Thanks!

}
}

impl Clone for ElementOrDocument {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdm jdm enabled auto-merge February 13, 2025 07:20
@jdm jdm added this pull request to the merge queue Feb 13, 2025
Merged via the queue into servo:main with commit becd097 Feb 13, 2025
22 checks passed
@stevennovaryo stevennovaryo deleted the intersection-observer branch February 13, 2025 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants