mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 16:03:10 +00:00
Capture and authorize the Window receiver before converting scroll arguments, then apply the operation only to that Window's surviving execution context. Keep popup offsets on their canonical Window instead of the creator global. Use derived ScrollToOptions and unrestricted-double converters, including overload selection, conversion order, exception propagation and non-finite normalization. Return successful Promises in the receiver realm and use the shared Promise binding adapter for receiver and conversion rejections. Cover borrowed methods, native/forged/proxy receivers, argument reentrancy and receiver removal. The upstream window-scroll-arguments WPT improves from 6/12 to 12/12; the 104-case comparison has no lost passing cases or subtests.
165 lines
4.9 KiB
Rust
165 lines
4.9 KiB
Rust
use moli_webidl as webidl;
|
|
|
|
#[derive(webidl::WebIdlEnum)]
|
|
#[webidl(name = "ScrollBehavior", rename_all = "kebab-case")]
|
|
enum ScrollBehavior {
|
|
Auto,
|
|
Instant,
|
|
Smooth,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlDictionary)]
|
|
#[webidl(prefix = "ScrollOptions")]
|
|
struct ScrollOptions {
|
|
// The native scroll algorithm retains its existing immediate-scroll policy.
|
|
#[webidl(name = "behavior", converter = "enum", default = ScrollBehavior::Auto)]
|
|
_behavior: ScrollBehavior,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlDictionary)]
|
|
#[webidl(prefix = "ScrollToOptions")]
|
|
struct ScrollToOptions {
|
|
#[webidl(inherit)]
|
|
_base: ScrollOptions,
|
|
#[webidl(converter = "unrestricted_double")]
|
|
left: Option<f64>,
|
|
#[webidl(converter = "unrestricted_double")]
|
|
top: Option<f64>,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlArgs)]
|
|
#[webidl(prefix = "scroll")]
|
|
struct ScrollToArgs {
|
|
#[webidl(dictionary)]
|
|
options: ScrollToOptions,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlArgs)]
|
|
#[webidl(prefix = "scroll")]
|
|
struct ScrollNumericArgs {
|
|
#[webidl(required, converter = "unrestricted_double")]
|
|
x: f64,
|
|
#[webidl(required, converter = "unrestricted_double")]
|
|
y: f64,
|
|
}
|
|
|
|
pub struct ScrollCoordinates {
|
|
left: Option<f64>,
|
|
top: Option<f64>,
|
|
}
|
|
|
|
impl ScrollCoordinates {
|
|
pub fn resolve(self, current_x: f64, current_y: f64, relative: bool) -> (f64, f64) {
|
|
let normalize = |value: f64| if value.is_finite() { value } else { 0.0 };
|
|
let left = self.left.map(normalize);
|
|
let top = self.top.map(normalize);
|
|
if relative {
|
|
(
|
|
current_x + left.unwrap_or(0.0),
|
|
current_y + top.unwrap_or(0.0),
|
|
)
|
|
} else {
|
|
(left.unwrap_or(current_x), top.unwrap_or(current_y))
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn coordinates<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: &v8::FunctionCallbackArguments<'s>,
|
|
) -> Option<ScrollCoordinates> {
|
|
// The legacy scroll bindings silently cancel failed argument conversion.
|
|
// Keep the catch here so native layout failures remain visible to callers.
|
|
let try_catch = std::pin::pin!(v8::TryCatch::new(scope));
|
|
let mut scope = try_catch.init();
|
|
let parsed = try_coordinates(&mut scope, args);
|
|
if scope.has_caught() {
|
|
scope.reset();
|
|
return None;
|
|
}
|
|
parsed.ok()
|
|
}
|
|
|
|
/// Convert shared ScrollToOptions without consuming a pending exception.
|
|
/// Window promise bindings propagate conversion failures in the callee realm.
|
|
pub fn try_coordinates<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: &v8::FunctionCallbackArguments<'s>,
|
|
) -> Result<ScrollCoordinates, webidl::WebIdlError> {
|
|
if args.length() >= 2 {
|
|
webidl::try_parse_args::<ScrollNumericArgs>(scope, args).map(|args| ScrollCoordinates {
|
|
left: Some(args.x),
|
|
top: Some(args.y),
|
|
})
|
|
} else {
|
|
webidl::try_parse_args::<ScrollToArgs>(scope, args).map(|args| ScrollCoordinates {
|
|
left: args.options.left,
|
|
top: args.options.top,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, webidl::WebIdlEnum)]
|
|
#[webidl(name = "ScrollLogicalPosition", rename_all = "kebab-case")]
|
|
pub enum ScrollLogicalPosition {
|
|
Start,
|
|
Center,
|
|
End,
|
|
Nearest,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlEnum)]
|
|
#[webidl(name = "ScrollIntoViewContainer", rename_all = "kebab-case")]
|
|
enum ScrollIntoViewContainer {
|
|
All,
|
|
Nearest,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlDictionary)]
|
|
#[webidl(prefix = "ScrollIntoViewOptions")]
|
|
struct ScrollIntoViewOptions {
|
|
#[webidl(inherit)]
|
|
_base: ScrollOptions,
|
|
#[webidl(converter = "enum", default = ScrollLogicalPosition::Start)]
|
|
block: ScrollLogicalPosition,
|
|
// The native bubbling algorithm retains its existing all-containers policy.
|
|
#[webidl(name = "container", converter = "enum", default = ScrollIntoViewContainer::All)]
|
|
_container: ScrollIntoViewContainer,
|
|
#[webidl(converter = "enum", default = ScrollLogicalPosition::Nearest)]
|
|
inline: ScrollLogicalPosition,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlArgs)]
|
|
#[webidl(prefix = "Element.scrollIntoView")]
|
|
struct ScrollIntoViewArgs {
|
|
#[webidl(dictionary)]
|
|
options: ScrollIntoViewOptions,
|
|
}
|
|
|
|
pub fn into_view_alignments<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: &v8::FunctionCallbackArguments<'s>,
|
|
) -> Option<(ScrollLogicalPosition, ScrollLogicalPosition)> {
|
|
let value = args.get(0);
|
|
if !value.is_null_or_undefined() && !value.is_object() {
|
|
return Some((
|
|
ScrollLogicalPosition::Nearest,
|
|
if value.boolean_value(scope) {
|
|
ScrollLogicalPosition::Start
|
|
} else {
|
|
ScrollLogicalPosition::End
|
|
},
|
|
));
|
|
}
|
|
let try_catch = std::pin::pin!(v8::TryCatch::new(scope));
|
|
let mut scope = try_catch.init();
|
|
let parsed = webidl::try_parse_args::<ScrollIntoViewArgs>(&mut scope, args);
|
|
if scope.has_caught() {
|
|
scope.reset();
|
|
return None;
|
|
}
|
|
parsed
|
|
.ok()
|
|
.map(|args| (args.options.inline, args.options.block))
|
|
}
|