fix(webidl): honor NewTarget realm prototype fallback

This commit is contained in:
ldm0
2026-11-24 14:28:14 +08:00
parent adf1de7004
commit 611df91c5d
3 changed files with 105 additions and 2 deletions
+3 -2
View File
@@ -19,8 +19,8 @@ use super::{
},
},
util::{
context_host_ptr_from_global_bridge, get_private_object, get_private_value,
set_private_value, throw_type_error,
apply_webidl_constructor_prototype_fallback, context_host_ptr_from_global_bridge,
get_private_object, get_private_value, set_private_value, throw_type_error,
},
};
@@ -102,6 +102,7 @@ pub(super) fn dom_parser_constructor_callback<'s>(
}
let parser = args.this();
moli_webapi::dom_parser_values::initialize_dom_parser_object(scope, parser);
apply_webidl_constructor_prototype_fallback(scope, parser, args.new_target(), "DOMParser");
let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) else {
throw_type_error(scope, "DOMParser constructor has no associated Document");
return;
@@ -161,6 +161,107 @@ fn dom_parser_uses_its_constructor_realm_associated_document() {
);
}
#[test]
fn dom_parser_non_object_new_target_prototype_uses_new_target_realm_default() {
let mut vm = new_storage_test_vm("https://dom-parser-new-target.test/top.html");
let result = vm
.eval(
r#"
(() => {
const frame = document.createElement('iframe');
(document.body || document.documentElement || document).appendChild(frame);
const child = frame.contentWindow;
child.history.replaceState(null, '', '/child.html');
const TopBad = new Function();
TopBad.prototype = 7;
const ChildBad = new child.Function();
ChildBad.prototype = 7;
const BoundChild = Function.prototype.bind.call(new child.Function());
BoundChild.prototype = 7;
const BoundTop = child.Function.prototype.bind.call(new Function());
BoundTop.prototype = 7;
const ProxyChild = new Proxy(new child.Function(), {});
ProxyChild.prototype = 7;
const ProxyTop = new child.Proxy(new Function(), {});
ProxyTop.prototype = 7;
let getterCount = 0;
const GetterProxyChild = new Proxy(new child.Function(), {
get(target, property, receiver) {
if (property === 'prototype') {
getterCount += 1;
return 7;
}
return Reflect.get(target, property, receiver);
}
});
const parseUrl = parser =>
DOMParser.prototype.parseFromString.call(
parser,
'<html></html>',
'text/html'
).URL;
const check = (parser, expectedPrototype, expectedUrl) => [
Object.getPrototypeOf(parser) === expectedPrototype,
parseUrl(parser) === expectedUrl
];
const topUrl = location.href;
const childUrl = child.location.href;
return JSON.stringify({
directTop: check(
Reflect.construct(child.DOMParser, [], TopBad),
DOMParser.prototype,
childUrl
),
directChild: check(
Reflect.construct(DOMParser, [], ChildBad),
child.DOMParser.prototype,
topUrl
),
boundChild: check(
Reflect.construct(DOMParser, [], BoundChild),
child.DOMParser.prototype,
topUrl
),
boundTop: check(
Reflect.construct(child.DOMParser, [], BoundTop),
DOMParser.prototype,
childUrl
),
proxyChild: check(
Reflect.construct(DOMParser, [], ProxyChild),
child.DOMParser.prototype,
topUrl
),
proxyTop: check(
Reflect.construct(child.DOMParser, [], ProxyTop),
DOMParser.prototype,
childUrl
),
getterProxyChild: check(
Reflect.construct(DOMParser, [], GetterProxyChild),
child.DOMParser.prototype,
topUrl
),
getterCount
});
})()
"#,
)
.expect("DOMParser NewTarget realm prototype fallback probe should evaluate");
assert_eq!(
result,
r#"{"directTop":[true,true],"directChild":[true,true],"boundChild":[true,true],"boundTop":[true,true],"proxyChild":[true,true],"proxyTop":[true,true],"getterProxyChild":[true,true],"getterCount":1}"#
);
}
#[tokio::test(flavor = "current_thread")]
async fn dom_parser_retained_across_child_navigation_uses_original_document() {
const HOST: &str = "dom-parser-retained.test";
+1
View File
@@ -1,5 +1,6 @@
use super::document_runtime::DomHandle;
use super::native_bridge::PageScriptHost;
pub(crate) use moli_v8_util::apply_webidl_constructor_prototype_fallback;
pub(crate) use moli_v8_util::v8_string_from_utf16_units;
pub use moli_v8_util::{
array_contains_strict, array_push_value, call_object_method, callback_data_index_value,