script: Replace usage of IntersectionObserverRootMargin with Stylo'sIntersectionObserverMargin (#38519)

@stevennovaryo 
Created wrapper for Stylo's IntersectionObserverMargin and cleaned up
repeated code.
Testing: Code compiles and `./mach test-unit tests/unit/style/` doesn't
have any errors. intersectionobserver.rs is able to utilize the struct.
Fixes: https://github.com/servo/servo/issues/35907

---------------------------
Signed-off-by: samir <samir.khan720a@gmail.com>

---------

Signed-off-by: samir <samir.khan720a@gmail.com>
This commit is contained in:
Labros 2025-08-10 03:01:44 +09:30 committed by GitHub
parent 0b2c0cd055
commit 7b057be780
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 20 additions and 117 deletions

View file

@ -10,11 +10,12 @@ use app_units::Au;
use base::cross_process_instant::CrossProcessInstant;
use cssparser::{Parser, ParserInput};
use dom_struct::dom_struct;
use euclid::default::{Rect, Size2D};
use euclid::default::{Rect, SideOffsets2D, Size2D};
use js::rust::{HandleObject, MutableHandleValue};
use style::context::QuirksMode;
use style::parser::{Parse, ParserContext};
use style::stylesheets::{CssRuleType, Origin};
use style::values::specified::intersection_observer::IntersectionObserverMargin;
use style_traits::{ParsingMode, ToCss};
use url::Url;
@ -36,7 +37,6 @@ use crate::dom::document::Document;
use crate::dom::domrectreadonly::DOMRectReadOnly;
use crate::dom::element::Element;
use crate::dom::intersectionobserverentry::IntersectionObserverEntry;
use crate::dom::intersectionobserverrootmargin::IntersectionObserverRootMargin;
use crate::dom::node::{Node, NodeTraits};
use crate::dom::window::Window;
use crate::script_runtime::{CanGc, JSContext};
@ -82,12 +82,12 @@ pub(crate) struct IntersectionObserver {
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-rootmargin-slot>
#[no_trace]
#[ignore_malloc_size_of = "Defined in style"]
root_margin: RefCell<IntersectionObserverRootMargin>,
root_margin: RefCell<IntersectionObserverMargin>,
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-scrollmargin-slot>
#[no_trace]
#[ignore_malloc_size_of = "Defined in style"]
scroll_margin: RefCell<IntersectionObserverRootMargin>,
scroll_margin: RefCell<IntersectionObserverMargin>,
/// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-thresholds-slot>
thresholds: RefCell<Vec<Finite<f64>>>,
@ -104,8 +104,8 @@ impl IntersectionObserver {
window: &Window,
callback: Rc<IntersectionObserverCallback>,
root: IntersectionRoot,
root_margin: IntersectionObserverRootMargin,
scroll_margin: IntersectionObserverRootMargin,
root_margin: IntersectionObserverMargin,
scroll_margin: IntersectionObserverMargin,
) -> Self {
Self {
reflector_: Reflector::new(),
@ -462,10 +462,7 @@ impl IntersectionObserver {
// > the width of the undilated rectangle.
// TODO(stevennovaryo): add check for same-origin-domain
intersection_rectangle.map(|intersection_rectangle| {
let margin = self
.root_margin
.borrow()
.resolve_percentages_with_basis(intersection_rectangle);
let margin = self.resolve_percentages_with_basis(intersection_rectangle);
intersection_rectangle.outer_rect(margin)
})
}
@ -661,6 +658,16 @@ impl IntersectionObserver {
.set(intersection_output.is_visible);
}
}
fn resolve_percentages_with_basis(&self, containing_block: Rect<Au>) -> SideOffsets2D<Au> {
let inner = &self.root_margin.borrow().0;
SideOffsets2D::new(
inner.0.to_used_value(containing_block.height()),
inner.1.to_used_value(containing_block.width()),
inner.2.to_used_value(containing_block.height()),
inner.3.to_used_value(containing_block.width()),
)
}
}
impl IntersectionObserverMethods<crate::DomTypeHolder> for IntersectionObserver {
@ -799,7 +806,7 @@ impl IntersectionObserverRegistration {
}
/// <https://w3c.github.io/IntersectionObserver/#parse-a-margin>
fn parse_a_margin(value: Option<&DOMString>) -> Result<IntersectionObserverRootMargin, ()> {
fn parse_a_margin(value: Option<&DOMString>) -> Result<IntersectionObserverMargin, ()> {
// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverinit-rootmargin> &&
// <https://w3c.github.io/IntersectionObserver/#dom-intersectionobserverinit-scrollmargin>
// > ... defaulting to "0px".
@ -808,7 +815,7 @@ fn parse_a_margin(value: Option<&DOMString>) -> Result<IntersectionObserverRootM
_ => "0px",
};
// Create necessary style ParserContext and utilize stylo's IntersectionObserverRootMargin
// Create necessary style ParserContext and utilize stylo's IntersectionObserverMargin
let mut input = ParserInput::new(value);
let mut parser = Parser::new(&mut input);
@ -825,7 +832,7 @@ fn parse_a_margin(value: Option<&DOMString>) -> Result<IntersectionObserverRootM
);
parser
.parse_entirely(|p| IntersectionObserverRootMargin::parse(&context, p))
.parse_entirely(|p| IntersectionObserverMargin::parse(&context, p))
.map_err(|_| ())
}