Files
moli/moli-webapi/src/resize_observer_entry.rs
T
ldm0 9e964905cd fix(observers): align notifications with document rendering
Run ResizeObserver after animation callbacks using depth-limited broadcasts
and freshly published layout, then apply focus fixup and sample intersections.
Deliver IntersectionObserver callbacks in a separate exact-Document task.

Preserve constructor order and active observer lifetime in the native registry;
validate Document and realm ownership across callbacks and bound reentrant work.
Sample padding, logical axes, inline boxes and device-pixel zoom from frozen
layout, including iframe geometry, and wake rendering after CSSOM changes.

Validation: fmt and workspace Clippy passed; Nextest 20,240 passed, 16 skipped.
Selected ResizeObserver WPT: 7 pages / 46 subtests passed. 153 original WPT pages
have no new failures; focus and navigation probes passed 31 and 152 cases.
2026-11-22 02:01:55 +08:00

174 lines
6.5 KiB
Rust

use crate::dom_rect::build_dom_rect_readonly_object;
use crate::interfaces as web_api_interfaces;
use moli_v8_util::{callback_data_index_value, callback_data_item, get_private_value};
use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
const TARGET_SLOT: &str = "__moliResizeObserverEntryTarget";
const CONTENT_RECT_SLOT: &str = "__moliResizeObserverEntryContentRect";
const CONTENT_BOX_SIZE_SLOT: &str = "__moliResizeObserverEntryContentBoxSize";
const BORDER_BOX_SIZE_SLOT: &str = "__moliResizeObserverEntryBorderBoxSize";
const DEVICE_PIXEL_CONTENT_BOX_SIZE_SLOT: &str =
"__moliResizeObserverEntryDevicePixelContentBoxSize";
const INLINE_SIZE_SLOT: &str = "__moliResizeObserverSizeInlineSize";
const BLOCK_SIZE_SLOT: &str = "__moliResizeObserverSizeBlockSize";
const ATTRIBUTE_SLOTS: &[&str] = &[
TARGET_SLOT,
CONTENT_RECT_SLOT,
CONTENT_BOX_SIZE_SLOT,
BORDER_BOX_SIZE_SLOT,
DEVICE_PIXEL_CONTENT_BOX_SIZE_SLOT,
INLINE_SIZE_SLOT,
BLOCK_SIZE_SLOT,
];
// Sampling retains native dimensions. JS wrappers are created only for active
// observations, in the callback's relevant realm when they are delivered.
pub struct ResizeObserverEntryData<'s> {
pub target: v8::Local<'s, v8::Value>,
pub content_rect: (f64, f64, f64, f64),
pub content_box_size: (f64, f64),
pub border_box_size: (f64, f64),
pub device_pixel_content_box_size: (f64, f64),
}
impl<'s> ResizeObserverEntryData<'s> {
pub fn into_object(self, scope: &mut v8::PinScope<'s, '_>) -> v8::Local<'s, v8::Object> {
let (x, y, width, height) = self.content_rect;
let content_rect = build_dom_rect_readonly_object(scope, x, y, width, height);
let content_box_size = frozen_box_size(scope, self.content_box_size);
let border_box_size = frozen_box_size(scope, self.border_box_size);
let device_pixel_content_box_size =
frozen_box_size(scope, self.device_pixel_content_box_size);
ResizeObserverEntryDeclaration {
target: self.target,
content_rect,
content_box_size,
border_box_size,
device_pixel_content_box_size,
}
.bind(scope)
.expect("ResizeObserverEntry declaration should bind")
}
}
#[derive(WebApiObject)]
#[webapi(interface = web_api_interfaces::ResizeObserverEntry)]
struct ResizeObserverEntryDeclaration<'s> {
#[webapi(slot = TARGET_SLOT)]
target: v8::Local<'s, v8::Value>,
#[webapi(slot = CONTENT_RECT_SLOT)]
content_rect: v8::Local<'s, v8::Object>,
#[webapi(slot = CONTENT_BOX_SIZE_SLOT)]
content_box_size: v8::Local<'s, v8::Array>,
#[webapi(slot = BORDER_BOX_SIZE_SLOT)]
border_box_size: v8::Local<'s, v8::Array>,
#[webapi(slot = DEVICE_PIXEL_CONTENT_BOX_SIZE_SLOT)]
device_pixel_content_box_size: v8::Local<'s, v8::Array>,
}
#[derive(WebApiObject)]
#[webapi(interface = web_api_interfaces::ResizeObserverSize)]
struct ResizeObserverSizeDeclaration {
#[webapi(slot = INLINE_SIZE_SLOT)]
inline_size: f64,
#[webapi(slot = BLOCK_SIZE_SLOT)]
block_size: f64,
}
fn frozen_box_size<'s>(
scope: &mut v8::PinScope<'s, '_>,
(inline_size, block_size): (f64, f64),
) -> v8::Local<'s, v8::Array> {
let size = ResizeObserverSizeDeclaration::new(inline_size, block_size)
.bind(scope)
.expect("ResizeObserverSize declaration should bind");
let array = v8::Array::new_with_elements(scope, &[size.into()]);
assert_eq!(
array.set_integrity_level(scope, v8::IntegrityLevel::Frozen),
Some(true),
"new ResizeObserver box-size array should freeze"
);
array
}
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::ResizeObserverEntry, enumerable)]
struct ResizeObserverEntryPrototypeDeclaration {
#[webapi(accessor_property, getter = result_attribute_getter, data = callback_data_index_value(scope, 0))]
target: (),
#[webapi(accessor_property, getter = result_attribute_getter, data = callback_data_index_value(scope, 1))]
content_rect: (),
#[webapi(accessor_property, getter = result_attribute_getter, data = callback_data_index_value(scope, 2))]
content_box_size: (),
#[webapi(accessor_property, getter = result_attribute_getter, data = callback_data_index_value(scope, 3))]
border_box_size: (),
#[webapi(accessor_property, getter = result_attribute_getter, data = callback_data_index_value(scope, 4))]
device_pixel_content_box_size: (),
}
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::ResizeObserverSize, enumerable)]
struct ResizeObserverSizePrototypeDeclaration {
#[webapi(accessor_property, getter = result_attribute_getter, data = callback_data_index_value(scope, 5))]
inline_size: (),
#[webapi(accessor_property, getter = result_attribute_getter, data = callback_data_index_value(scope, 6))]
block_size: (),
}
pub fn install_resize_observer_entry_template_bindings<'s>(
scope: &mut v8::PinScope<'s, '_, ()>,
template: v8::Local<'s, v8::FunctionTemplate>,
name: &str,
) {
let prototype = template.prototype_template(scope);
match name {
"ResizeObserverEntry" => {
ResizeObserverEntryPrototypeDeclaration::initialize_prototype_template(
scope, prototype,
);
}
"ResizeObserverSize" => {
ResizeObserverSizePrototypeDeclaration::initialize_prototype_template(scope, prototype);
}
_ => {}
}
}
fn result_attribute_getter<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
let Some(slot) =
callback_data_item(scope, &args, ATTRIBUTE_SLOTS, "ResizeObserver result slots")
else {
return;
};
rv.set(
get_private_value(scope, args.this(), slot)
.expect("branded ResizeObserver result should retain its attributes"),
);
}
use moli_webidl as webidl;
pub const RESIZE_OBSERVER_RECORD_BOX_SLOT: &str = "__moliResizeObserverRecordBox";
#[derive(Clone, Copy, Default, PartialEq, webidl::WebIdlEnum)]
#[webidl(name = "ResizeObserverBoxOptions", rename_all = "kebab-case")]
pub enum ResizeObserverBoxOptions {
#[default]
ContentBox,
BorderBox,
DevicePixelContentBox,
}
#[derive(Default, webidl::WebIdlDictionary)]
#[webidl(prefix = "ResizeObserverOptions", snapshot)]
pub struct ResizeObserverOptions {
#[webidl(name = "box", converter = "enum", default = ResizeObserverBoxOptions::ContentBox)]
#[webidl(snapshot_slot = RESIZE_OBSERVER_RECORD_BOX_SLOT)]
pub observed_box: ResizeObserverBoxOptions,
}