Commit 9be931cb authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by Commit Bot

[WPT/common/security-features] Refactor navigable tests

For further refactoring: removing createHelperIframe() and
unifying requestViaForm() with other navigables.

This can enable referrer-policy tests for <form>,
while it is not spec'ed yet though:
https://github.com/whatwg/html/issues/4320.

Bug: 906850
Change-Id: I96c43ec8c8b39f794f8bf8291f2af798f1a30c55
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1562120
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711559}
parent dc920535
...@@ -314,19 +314,6 @@ function createRequestViaElement(tagName, attrs, parentNode) { ...@@ -314,19 +314,6 @@ function createRequestViaElement(tagName, attrs, parentNode) {
return createElement(tagName, attrs, parentNode, true).eventPromise; return createElement(tagName, attrs, parentNode, true).eventPromise;
} }
/**
* Creates a new empty iframe and appends it to {@code document.body} .
* @param {string} name The name and ID of the new iframe.
* @param {boolean} doBindEvents Whether to bind load and error events.
* @return {DOMElement} The newly created iframe.
*/
function createHelperIframe(name, doBindEvents) {
return createElement("iframe",
{"name": name, "id": name},
document.body,
doBindEvents);
}
function wrapResult(server_data) { function wrapResult(server_data) {
if (typeof(server_data) === "string") { if (typeof(server_data) === "string") {
throw server_data; throw server_data;
...@@ -394,7 +381,7 @@ function wrapResult(server_data) { ...@@ -394,7 +381,7 @@ function wrapResult(server_data) {
requestViaAudio 3 - Y - requestViaAudio 3 - Y -
requestViaDedicatedWorker 2 Y Y Y requestViaDedicatedWorker 2 Y Y Y
requestViaFetch 2 Y Y - requestViaFetch 2 Y Y -
requestViaForm 3 - Y - requestViaForm 2 - Y -
requestViaIframe 1 Y Y - requestViaIframe 1 Y Y -
requestViaImage 2 Y Y - requestViaImage 2 Y Y -
requestViaLinkPrefetch 3 - Y - requestViaLinkPrefetch 3 - Y -
...@@ -583,18 +570,29 @@ function requestViaWorklet(type, url) { ...@@ -583,18 +570,29 @@ function requestViaWorklet(type, url) {
} }
/** /**
* Sets the href attribute on a navigable DOM element and performs a navigation * Creates a navigable element with the name `navigableElementName`
* by clicking it. To avoid navigating away from the current execution * (<a>, <area>, or <form>) under `parentNode`, and
* context, a target attribute is set to point to a new helper iframe. * performs a navigation by `trigger()` (e.g. clicking <a>).
* @param {DOMElement} navigableElement The navigable DOMElement * To avoid navigating away from the current execution context,
* @param {string} url The href for the navigable element. * a target attribute is set to point to a new helper iframe.
* @param {string} navigableElementName
* @param {object} additionalAttributes The attributes of the navigable element.
* @param {DOMElement} parentNode
* @param {function(DOMElement} trigger A callback called after the navigable
* element is inserted and should trigger navigation using the element.
* @return {Promise} The promise for success/error events. * @return {Promise} The promise for success/error events.
*/ */
function requestViaNavigable(navigableElement, url) { function requestViaNavigable(navigableElementName, additionalAttributes,
var iframe = createHelperIframe(guid(), false); parentNode, trigger) {
setAttributes(navigableElement, const name = guid();
{"href": url,
"target": iframe.name}); const iframe =
createElement("iframe", {"name": name, "id": name}, parentNode, false);
const navigable = createElement(
navigableElementName,
Object.assign({"target": name}, additionalAttributes),
parentNode, false);
const promise = const promise =
bindEvents2(window, "message", iframe, "error", window, "error") bindEvents2(window, "message", iframe, "error", window, "error")
...@@ -603,7 +601,7 @@ function requestViaNavigable(navigableElement, url) { ...@@ -603,7 +601,7 @@ function requestViaNavigable(navigableElement, url) {
return Promise.reject(new Error('Unexpected event.source')); return Promise.reject(new Error('Unexpected event.source'));
return event.data; return event.data;
}); });
navigableElement.click(); trigger(navigable);
return promise; return promise;
} }
...@@ -614,12 +612,11 @@ function requestViaNavigable(navigableElement, url) { ...@@ -614,12 +612,11 @@ function requestViaNavigable(navigableElement, url) {
* @return {Promise} The promise for success/error events. * @return {Promise} The promise for success/error events.
*/ */
function requestViaAnchor(url, additionalAttributes) { function requestViaAnchor(url, additionalAttributes) {
var a = createElement( return requestViaNavigable(
"a", "a",
Object.assign({"innerHTML": "Link to resource"}, additionalAttributes), Object.assign({"href": url, "innerHTML": "Link to resource"},
document.body); additionalAttributes),
document.body, a => a.click());
return requestViaNavigable(a, url);
} }
/** /**
...@@ -629,13 +626,11 @@ function requestViaAnchor(url, additionalAttributes) { ...@@ -629,13 +626,11 @@ function requestViaAnchor(url, additionalAttributes) {
* @return {Promise} The promise for success/error events. * @return {Promise} The promise for success/error events.
*/ */
function requestViaArea(url, additionalAttributes) { function requestViaArea(url, additionalAttributes) {
var area = createElement(
"area",
Object.assign({}, additionalAttributes),
document.body);
// TODO(kristijanburnik): Append to map and add image. // TODO(kristijanburnik): Append to map and add image.
return requestViaNavigable(area, url); return requestViaNavigable(
"area",
Object.assign({"href": url}, additionalAttributes),
document.body, area => area.click());
} }
/** /**
...@@ -661,17 +656,11 @@ function requestViaScript(url, additionalAttributes) { ...@@ -661,17 +656,11 @@ function requestViaScript(url, additionalAttributes) {
* @param {string} url The URL to submit to. * @param {string} url The URL to submit to.
* @return {Promise} The promise for success/error events. * @return {Promise} The promise for success/error events.
*/ */
function requestViaForm(url) { function requestViaForm(url, additionalAttributes) {
var iframe = createHelperIframe(guid()); return requestViaNavigable(
var form = createElement("form", "form",
{"action": url, Object.assign({"action": url, "method": "POST"}, additionalAttributes),
"method": "POST", document.body, form => form.submit());
"target": iframe.name},
document.body);
bindEvents(iframe);
form.submit();
return iframe.eventPromise;
} }
/** /**
...@@ -866,7 +855,7 @@ const subresourceMap = { ...@@ -866,7 +855,7 @@ const subresourceMap = {
invoker: requestViaFetch, invoker: requestViaFetch,
}, },
"form-tag": { "form-tag": {
path: "/common/security-features/subresource/empty.py", path: "/common/security-features/subresource/document.py",
invoker: requestViaForm, invoker: requestViaForm,
}, },
"iframe-tag": { "iframe-tag": {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment