fix(navigation): resolve empty Location URLs against the entry base

Extracted from wpt-misc-fix commit 0a4bc3da81cdc888bb4c316fcb46e9547ca263f4.
This commit is contained in:
ldm0
2026-11-18 21:51:15 +08:00
committed by Donough Liu
parent 4b42f9d3a0
commit df82b2ba02
2 changed files with 33 additions and 5 deletions
@@ -13,11 +13,7 @@ pub(in crate::context_bootstrap) fn resolve_location_navigation_target(
url::Url::parse(raw_target.as_deref().unwrap_or(current_href)).ok()
}
LocationNavigationKind::Assign | LocationNavigationKind::Replace => {
let raw_target = raw_target.unwrap_or_default();
if raw_target.is_empty() {
return url::Url::parse(current_href).ok();
}
let raw_target = raw_target?;
if let Ok(absolute) = url::Url::parse(&raw_target) {
return Some(absolute);
}
@@ -592,3 +592,35 @@ fn location_hash_empty_fragment_serializes_empty_and_clears_target() {
assert_eq!(result, "#target/true|true||false|true");
}
#[test]
fn empty_location_urls_follow_the_live_entry_document_base() {
for operation in [
"location.href = input",
"location.assign(input)",
"location.replace(input)",
] {
let mut vm = new_parsed_test_vm(
"https://navigation-base.test/entry/page.html",
"<!doctype html><head><base href='/initial/'></head><body></body>",
);
vm.eval(&format!(
r#"
const input = {{toString() {{
document.querySelector('base').href = '/converted/target.html?q=empty#discard';
return '';
}}}};
{operation};
"#
))
.unwrap();
let pending = vm
.take_pending_location_navigation_with_seed()
.expect("empty URL resolves to a cross-document navigation");
assert_eq!(
pending.url.as_str(),
"https://navigation-base.test/converted/target.html?q=empty",
"{operation}"
);
}
}