fix(dom): return null for inactive Document locations

This commit is contained in:
ldm0
2026-11-06 11:30:51 +08:00
committed by Donough Liu
parent 9bd3f6f0a3
commit 64ea17ee12
4 changed files with 80 additions and 20 deletions
@@ -1,7 +1,5 @@
use super::history_runtime::cancel_pending_precommit_history_traversal;
use super::location_navigation::{
LocationNavigationKind, NavigationNavigateHistoryKind, navigate_location_object,
};
use super::location_navigation::{LocationNavigationKind, NavigationNavigateHistoryKind};
use super::location_runtime::{is_same_document_fragment_navigation, location_href_slot};
use super::navigation_cross_document::handle_navigation_navigate_cross_document;
use super::navigation_entry::{history_entries, navigation_current_entry};
@@ -78,15 +78,22 @@ pub(in crate::context_bootstrap) fn document_location_getter<'s>(
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
let receiver = args.this();
if crate::native_bridge::node_runtime_and_handle_from_object_or_detached(scope, receiver)
.is_err()
{
if !crate::web_api_interfaces::Document::is_instance(scope, receiver) {
webidl::throw_type_error(
scope,
"Document.location getter called on incompatible receiver.",
);
return;
}
let Ok((host, document)) =
crate::native_bridge::node_runtime_and_handle_from_object_or_detached(scope, receiver)
else {
return;
};
if !unsafe { &*host }.document_is_fully_active(document) {
rv.set_null();
return;
}
match object_location_slot_value(scope, receiver).filter(|value| !value.is_undefined()) {
Some(value) => rv.set(value),
None => rv.set_null(),
@@ -99,30 +106,33 @@ pub(in crate::context_bootstrap) fn document_location_setter<'s>(
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
let receiver = args.this();
if crate::native_bridge::node_runtime_and_handle_from_object_or_detached(scope, receiver)
.is_err()
{
if !crate::web_api_interfaces::Document::is_instance(scope, receiver) {
webidl::throw_type_error(
scope,
"Document.location setter called on incompatible receiver.",
);
return;
}
let Some(location) = object_location_slot_value(scope, receiver)
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
let Ok((host, document)) =
crate::native_bridge::node_runtime_and_handle_from_object_or_detached(scope, receiver)
else {
rv.set_undefined();
return;
};
let value = args.get(0);
let href = match value.is_null_or_undefined() {
true => String::new(),
false => match value.to_string(scope) {
Some(value) => value.to_rust_string_lossy(scope),
None => return,
},
let location = unsafe { &*host }
.document_is_fully_active(document)
.then(|| object_location_slot_value(scope, receiver))
.flatten()
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok());
let Some(location) = location else {
webidl::throw_type_error(scope, "Cannot assign href to a null Location.");
return;
};
navigate_location_object(scope, location, LocationNavigationKind::Assign, Some(href));
// PutForwards gets the Location first and leaves conversion, exception
// identity and navigation to its existing href setter.
if location.set(scope, v8str(scope, "href").into(), args.get(0)) == Some(false) {
webidl::throw_type_error(scope, "Cannot assign href to Location.");
return;
}
rv.set_undefined();
}
@@ -0,0 +1,51 @@
use super::*;
#[test]
fn document_location_tracks_activity_and_forwards_original_href_values() {
let mut vm = new_parsed_test_vm(
"https://document-location.test/",
"<!doctype html><body></body>",
);
assert_eq!(
vm.eval(
r#"(() => {
const assert = (value, label) => { if (!value) throw new Error(label); };
const descriptor = Object.getOwnPropertyDescriptor(document, 'location');
const get = descriptor.get, set = descriptor.set;
const fails = action => {
let error;
try { action(); } catch (value) { error = value; }
assert(error instanceof TypeError, 'expected native TypeError');
};
assert(get.call(document) === location, 'live Document keeps its Location');
for (const value of [{}, Object.create(Document.prototype), document.body]) {
fails(() => get.call(value));
fails(() => set.call(value, 'ignored'));
}
const detached = document.implementation.createHTMLDocument('');
let converted = 0;
const href = {toString() { converted++; return '#forwarded'; }};
assert(get.call(detached) === null, 'windowless Document has no active Location');
fails(() => set.call(detached, href));
assert(converted === 0, 'null forwarding fails before string conversion');
const frame = document.createElement('iframe');
document.body.appendChild(frame);
const child = frame.contentDocument;
const nested = child.createElement('iframe');
child.body.appendChild(nested);
const descendant = nested.contentDocument;
assert(get.call(child) !== null && get.call(descendant) !== null, 'live descendants');
frame.remove();
assert(get.call(child) === null, 'removed frame Document');
assert(get.call(descendant) === null, 'inactive ancestor makes its descendant inactive');
fails(() => set.call(child, href));
assert(converted === 0, 'inactive Document does not convert the value');
set.call(document, href);
assert(converted === 1 && location.hash === '#forwarded', 'href owns one conversion');
return 'ok';
})()"#
)
.unwrap(),
"ok"
);
}
@@ -9,6 +9,7 @@ mod cross_document_drag_events;
mod cross_origin_navigation;
mod current_pass_css_fonts;
mod document_encodings;
mod document_location_activity;
mod drag_drop_outcomes;
mod idb_transaction_gc;
mod message_port_transfer;