Skip to content

Commit

Permalink
NavigateEvent sourceElement prototype
Browse files Browse the repository at this point in the history
Adds a new attribute to the NavigateEvent, sourceElement, which is
the Element that initiated the navigation (if any). Set for anchor
clicks (both HTML and SVG <a>) and form submissions.

Implemented behind the NavigateEventSourceElement flag.

I2P: https://groups.google.com/a/chromium.org/g/blink-dev/c/fJhFQV-2v38/m/p7F00AQhAAAJ
Explainer: WICG/navigation-api@85edaf1

Bug: 1483000
Change-Id: Icb09e7ac425aee1c7772c1bfcdafeaa0883bc072
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4385906
Auto-Submit: Nate Chapin <japhet@chromium.org>
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Reviewed-by: Domenic Denicola <domenic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1203020}
  • Loading branch information
natechapin authored and Lightning00Blade committed Dec 11, 2023
1 parent 56bd75c commit 57649bf
Show file tree
Hide file tree
Showing 35 changed files with 102 additions and 3 deletions.
12 changes: 9 additions & 3 deletions navigation-api/navigate-event/event-constructor.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
signal: (new AbortController()).signal,
formData: null,
downloadRequest: null,
info: null
info: null,
sourceElement: null
});
});
}, "destination is required");
Expand All @@ -35,7 +36,8 @@
hashChange: false,
formData: null,
downloadRequest: null,
info: null
info: null,
sourceElement: null
});
});
});
Expand All @@ -50,6 +52,7 @@
const signal = (new AbortController()).signal;
const downloadRequest = "abc";
const hasUAVisualTransition = true;
const sourceElement = document.createElement("a");

const event = new NavigateEvent("navigate", {
navigationType: "replace",
Expand All @@ -61,7 +64,8 @@
formData,
downloadRequest,
info,
hasUAVisualTransition
hasUAVisualTransition,
sourceElement
});

assert_equals(event.navigationType, "replace");
Expand All @@ -74,6 +78,7 @@
assert_equals(event.downloadRequest, downloadRequest);
assert_equals(event.info, info);
assert_equals(event.hasUAVisualTransition, hasUAVisualTransition);
assert_equals(event.sourceElement, sourceElement);
});
history.pushState(2, null, "#2");
}, "all properties are reflected back");
Expand All @@ -93,6 +98,7 @@
assert_equals(event.formData, null);
assert_equals(event.downloadRequest, null);
assert_equals(event.info, undefined);
assert_equals(event.sourceElement, null);
});
history.pushState(3, null, "#3");
}, "defaults are as expected");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, document.getElementById("a"));
e.preventDefault();
});
a.click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, document.getElementById("a"));
e.preventDefault();
t.step_timeout(t.step_func_done(() => assert_equals(location.hash, "")), 0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, a);
e.preventDefault();
});
a.click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, document.getElementById("a"));
e.preventDefault();
t.step_timeout(t.step_func_done(() => assert_equals(location.hash, "")), 0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, document.getElementById("a"));
e.preventDefault();
});
a.click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, document.getElementById("a"));
e.preventDefault();
t.step_timeout(t.step_func_done(() => assert_equals(location.hash, "")), 0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, null);
e.preventDefault();
});
a.click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, null);
});
navigation.back();
}), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, null);
});
navigation.navigate("#foo", { state: navState });
}, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, null);
e.intercept();
});
navigation.updateCurrentEntry({ state: navState });
Expand Down
1 change: 1 addition & 0 deletions navigation-api/navigate-event/navigate-form-get.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

// Because it's a GET, not a POST
assert_equals(e.formData, null);
assert_equals(e.sourceElement, form);
});
window.onload = t.step_func(() => form.submit());
}, "<form> submission with GET method fires navigate event but with formData null");
Expand Down
1 change: 1 addition & 0 deletions navigation-api/navigate-event/navigate-form-reload.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
iframe.contentWindow.navigation.onnavigate = t.step_func(e => {
assert_equals(e.navigationType, "push");
assert_not_equals(e.formData, null);
assert_equals(e.sourceElement, null);

iframe.onload = t.step_func(() => {
iframe.contentWindow.navigation.onnavigate = t.step_func_done(e => {
Expand Down
35 changes: 35 additions & 0 deletions navigation-api/navigate-event/navigate-form-requestSubmit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<form id="form" method="post" action="">
<input type="submit" id="submit1">
<input type="image" id="submit2">
<button type="submit" id="submit3">
</form>
<script>
promise_test(async () => {
await new Promise(r => window.onload = r);

let sourceElement;
navigation.onnavigate = e => {
e.preventDefault();
sourceElement = e.sourceElement;
};

form.requestSubmit(submit1);
await new Promise(r => navigation.onnavigateerror = r);
assert_equals(sourceElement, submit1);

form.requestSubmit(submit2);
await new Promise(r => navigation.onnavigateerror = r);
assert_equals(sourceElement, submit2);

form.requestSubmit(submit3);
await new Promise(r => navigation.onnavigateerror = r);
assert_equals(sourceElement, submit3);

form.requestSubmit();
await new Promise(r => navigation.onnavigateerror = r);
assert_equals(sourceElement, form);
}, "<form> requestSubmit() sets sourceElement");
</script>
1 change: 1 addition & 0 deletions navigation-api/navigate-event/navigate-form-traverse.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
iframe.contentWindow.navigation.onnavigate = t.step_func(e => {
assert_equals(e.navigationType, "push");
assert_not_equals(e.formData, null);
assert_equals(e.sourceElement, null);

iframe.onload = t.step_func(() => {
// Avoid the replace behavior that occurs if you navigate during the load handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_not_equals(e.formData, null);
assert_equals(e.sourceElement, submit);
});
window.onload = t.step_func(() => test_driver.click(submit));
}, "<form> submission fires navigate event");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_not_equals(e.formData, null);
assert_equals(e.sourceElement, null);
});
form.submit();
});
Expand Down
1 change: 1 addition & 0 deletions navigation-api/navigate-event/navigate-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_not_equals(e.formData, null);
assert_equals(e.sourceElement, form);
});
window.onload = t.step_func(() => form.submit());
}, "<form> submission fires navigate event");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
assert_equals(e.destination.id, target_id);
assert_equals(e.destination.index, 0);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
});

history.back();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
assert_equals(e.destination.id, target_id);
assert_equals(e.destination.index, 0);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
});

history.back();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
assert_equals(e.destination.index, 0);
assert_equals(e.formData, null);
assert_equals(e.info, undefined);
assert_equals(e.sourceElement, null);
});
assert_true(i.contentWindow.navigation.canGoBack);
i.contentWindow.history.back();
Expand Down
1 change: 1 addition & 0 deletions navigation-api/navigate-event/navigate-history-go-0.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
e.preventDefault();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
e.preventDefault();
t.step_timeout(t.step_func_done(() => {
assert_equals(location.hash, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
e.preventDefault();
t.step_timeout(t.step_func_done(() => {
assert_equals(location.hash, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
e.preventDefault();
t.step_timeout(t.step_func_done(() => assert_equals(location.hash, "")), 0);
});
Expand Down
1 change: 1 addition & 0 deletions navigation-api/navigate-event/navigate-location.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
});
location.href = "#1";
}, "location API fires navigate event");
Expand Down
1 change: 1 addition & 0 deletions navigation-api/navigate-event/navigate-meta-refresh.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
e.preventDefault();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
assert_equals(e.destination.index, 0);
assert_equals(e.formData, null);
assert_equals(e.info, "hi");
assert_equals(e.sourceElement, null);
});
i.contentWindow.onbeforeunload = () => beforeunload_called = true;
assert_true(i.contentWindow.navigation.canGoBack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
assert_equals(e.destination.index, 0);
assert_equals(e.formData, null);
assert_equals(e.info, "hi");
assert_equals(e.sourceElement, null);
}
await i.contentWindow.navigation.back({ info: "hi" }).finished;
}, "navigate event for navigation.back() - same-document in an iframe");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
assert_equals(e.formData, null);
assert_equals(e.info, "hi");
assert_not_equals(e.hasUAVisualTransition, undefined);
assert_equals(e.sourceElement, null);
});
assert_true(navigation.canGoBack);
navigation.back({ info: "hi" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
});
navigation.navigate("#foo");
}, "navigate event for navigation.navigate()");
Expand Down
26 changes: 26 additions & 0 deletions navigation-api/navigate-event/navigate-svg-anchor-fragment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<svg><a id="a" href="#1"></a></svg>
<script>
async_test(t => {
navigation.onnavigate = t.step_func(e => {
assert_equals(e.navigationType, "push");
assert_true(e.cancelable);
assert_true(e.canIntercept);
assert_false(e.userInitiated);
assert_true(e.hashChange);
assert_equals(e.downloadRequest, null);
assert_equals(e.formData, null);
assert_equals(new URL(e.destination.url).hash, "#1");
assert_true(e.destination.sameDocument);
assert_equals(e.destination.key, "");
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.sourceElement, document.getElementById("a"));
e.preventDefault();
t.step_timeout(t.step_func_done(() => assert_equals(location.hash, "")), 0);
});
document.getElementById("a").dispatchEvent(new MouseEvent('click'));
}, "<svg:a> click fires navigate event");
</script>
1 change: 1 addition & 0 deletions navigation-api/navigate-event/navigate-to-srcdoc.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
e.preventDefault();

// Make sure it doesn't navigate anyway.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
e.preventDefault();
});
window.onload = t.step_func(() => window.open("#1", "_self"));
Expand Down
1 change: 1 addition & 0 deletions navigation-api/navigate-event/navigate-window-open.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
assert_equals(e.destination.id, "");
assert_equals(e.destination.index, -1);
assert_equals(e.formData, null);
assert_equals(e.sourceElement, null);
e.preventDefault();
});

Expand Down

0 comments on commit 57649bf

Please sign in to comment.