Skip to content

Commit 8d1e43a

Browse files
natechapinchromium-wpt-export-bot
authored andcommitted
Add options to NavigatePrecommitHandler.redirect()
Recently introduced in the spec PR: https://whatpr.org/html/10919/nav-history-apis.html#dom-navigationprecommitcontroller-redirect Explainer: https://github.com/WICG/navigation-api?tab=readme-ov-file#redirects-during-deferred-commit Change-Id: Ie88b404f5e68b6c830b1b03f01408659641e206d Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6456002 Reviewed-by: Domenic Denicola <[email protected]> Commit-Queue: Nate Chapin <[email protected]> Cr-Commit-Position: refs/heads/main@{#1450636}
1 parent c4588f6 commit 8d1e43a

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<!doctype html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<body>
5+
<script>
6+
promise_test(async t => {
7+
// Wait for after the load event so that the navigation doesn't get converted
8+
// into a replace navigation.
9+
await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0));
10+
11+
let start_length = navigation.entries().length;
12+
let start_hash = location.hash;
13+
14+
let navInfo0 = { nav : "info0" };
15+
let navState0 = { statevar: "state0" };
16+
17+
let navInfo1 = { nav : "info1" };
18+
let navState1 = { statevar: "state1" };
19+
20+
let navInfo2 = { nav : "info2" };
21+
let navState2 = { statevar: "state2" };
22+
23+
navigation.onnavigate = t.step_func(e => {
24+
e.intercept({
25+
precommitHandler: t.step_func(controller => {
26+
assert_equals(location.hash, start_hash);
27+
assert_equals(new URL(e.destination.url).hash, "#push");
28+
assert_equals(e.info, navInfo0);
29+
assert_not_equals(e.destination.getState(), navState0);
30+
assert_equals(e.destination.getState().statevar, navState0.statevar);
31+
32+
// Redirect without options should not overwrite the original options.
33+
controller.redirect("#redirect0");
34+
assert_equals(location.hash, start_hash);
35+
assert_equals(new URL(e.destination.url).hash, "#redirect0");
36+
assert_equals(e.info, navInfo0);
37+
assert_not_equals(e.destination.getState(), navState0);
38+
assert_equals(e.destination.getState().statevar, navState0.statevar);
39+
40+
// Update both info and state.
41+
controller.redirect("#redirect1", { info: navInfo1, state: navState1});
42+
assert_equals(location.hash, start_hash);
43+
assert_equals(new URL(e.destination.url).hash, "#redirect1");
44+
assert_equals(e.info, navInfo1);
45+
assert_not_equals(e.destination.getState(), navState1);
46+
assert_equals(e.destination.getState().statevar, navState1.statevar);
47+
48+
// Update just info
49+
controller.redirect("#redirect2", { info: navInfo2 });
50+
assert_equals(location.hash, start_hash);
51+
assert_equals(new URL(e.destination.url).hash, "#redirect2");
52+
assert_equals(e.info, navInfo2);
53+
assert_not_equals(e.destination.getState(), navState1);
54+
assert_equals(e.destination.getState().statevar, navState1.statevar);
55+
56+
// Update just state - this also tests the case where the url does
57+
// not change.
58+
controller.redirect("#redirect2", { state: navState2 });
59+
assert_equals(location.hash, start_hash);
60+
assert_equals(new URL(e.destination.url).hash, "#redirect2");
61+
assert_equals(e.info, navInfo2);
62+
assert_not_equals(e.destination.getState(), navState2);
63+
assert_equals(e.destination.getState().statevar, navState2.statevar);
64+
65+
// Explicit undefined is treated as absent.
66+
controller.redirect("#redirect2", { info: undefined, state: undefined });
67+
assert_equals(location.hash, start_hash);
68+
assert_equals(new URL(e.destination.url).hash, "#redirect2");
69+
assert_equals(e.info, navInfo2);
70+
assert_not_equals(e.destination.getState(), navState2);
71+
assert_equals(e.destination.getState().statevar, navState2.statevar);
72+
}),
73+
});
74+
});
75+
await navigation.navigate("#push", { info: navInfo0, state: navState0 }).committed;
76+
assert_equals(location.hash, "#redirect2");
77+
assert_equals(navigation.entries().length, start_length + 1);
78+
assert_equals(navigation.currentEntry.getState().statevar, navState2.statevar);
79+
}, "precommitHandler redirect() with options dictionary");
80+
</script>
81+
</body>

navigation-api/precommit-handler/precommitHandler-redirect-throws.html

+14
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@
6060
await navigation.navigate("#").finished;
6161
}, "redirect() to cross-origin url");
6262

63+
promise_test(async t => {
64+
// See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()`
65+
const buffer = new WebAssembly.Memory({ shared:true, initial:1, maximum:1 }).buffer;
66+
67+
navigation.onnavigate = t.step_func(e => {
68+
e.intercept({ precommitHandler: t.step_func(controller => {
69+
assert_throws_dom("DataCloneError", () => {
70+
controller.redirect(location.href, { state: buffer });
71+
});
72+
})});
73+
});
74+
await navigation.navigate("#").finished;
75+
}, "redirect() with an unserializable state object");
76+
6377
promise_test(async t => {
6478
navigation.onnavigate = t.step_func(e => {
6579
e.intercept({ precommitHandler: t.step_func(controller => {

0 commit comments

Comments
 (0)