Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test has a WPT meta file that expects 16 subtest issues.
- This WPT test may be referenced by the following Test IDs:
- /html/semantics/the-button-element/command-and-commandfor/on-dialog-behavior-request-close.tentative.html - WPT Dashboard Interop Dashboard
<!doctype html>
<meta charset="utf-8" />
<meta name="author" title="Keith Cirkel" href="mailto:wpt@keithcirkel.co.uk" />
<meta name="author" title="Luke Warlow" href="mailto:lwarlow@igalia.com" />
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/invoker-utils.js"></script>
<!-- Merge the following tests into the main test file when the feature is stable -->
<dialog id="invokee">
<button id="containedinvoker" commandfor="invokee" command="request-close"></button>
</dialog>
<button id="invokerbutton" commandfor="invokee" command="show-modal"></button>
<script>
function resetState() {
invokee.close();
try { invokee.hidePopover(); } catch {}
invokee.removeAttribute("popover");
invokee.returnValue = '';
invokerbutton.setAttribute("command", "show-modal");
containedinvoker.setAttribute("command", "request-close");
containedinvoker.removeAttribute("value");
}
// request to close an already open dialog
["request-close", /* test case sensitivity */ "reQuEst-Close"].forEach((command) => {
["property", "attribute"].forEach((setType) => {
promise_test(
async function (t) {
t.add_cleanup(resetState);
invokee.show();
assert_true(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
if (setType === "property") {
containedinvoker.command = command;
} else {
containedinvoker.setAttribute("command", command);
}
await clickOn(containedinvoker);
assert_equals(invokee.returnValue, "");
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
},
`invoking to request-close (with command ${setType} as ${command}) open dialog closes`,
);
promise_test(
async function (t) {
t.add_cleanup(resetState);
invokee.show();
assert_true(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
if (setType === "property") {
containedinvoker.command = command;
} else {
containedinvoker.setAttribute("command", command);
}
containedinvoker.setAttribute("value", "foo");
await clickOn(containedinvoker);
assert_equals(invokee.returnValue, "foo");
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
},
`invoking to request-close with value (with command ${setType} as ${command}) open dialog closes and sets returnValue`,
);
promise_test(
async function (t) {
t.add_cleanup(resetState);
invokee.show();
assert_true(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
if (typeof command === "string") {
if (setType === "property") {
containedinvoker.command = command;
} else {
containedinvoker.setAttribute("command", command);
}
}
invokee.addEventListener("command", (e) => e.preventDefault(), {
once: true,
});
await clickOn(containedinvoker);
assert_true(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
},
`invoking to request-close (with command ${setType} as ${command}) open dialog with preventDefault is no-op`,
);
promise_test(
async function (t) {
t.add_cleanup(resetState);
invokee.showModal();
assert_true(invokee.open, "invokee.open");
assert_true(invokee.matches(":modal"), "invokee :modal");
if (setType === "property") {
containedinvoker.command = command;
} else {
containedinvoker.setAttribute("command", command);
}
invokee.addEventListener("command", (e) => e.preventDefault(), {
once: true,
});
await clickOn(containedinvoker);
assert_true(invokee.open, "invokee.open");
assert_true(invokee.matches(":modal"), "invokee :modal");
},
`invoking to request-close (with command ${setType} as ${command}) open modal dialog with preventDefault is no-op`,
);
promise_test(
async function (t) {
t.add_cleanup(resetState);
invokee.show();
assert_true(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
if (setType === "property") {
containedinvoker.command = command;
} else {
containedinvoker.setAttribute("command", command);
}
invokee.addEventListener(
"command",
(e) => {
containedinvoker.setAttribute("command", "show");
},
{ once: true },
);
await clickOn(containedinvoker);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
},
`invoking to request-close (with command ${setType} as ${command}) open dialog while changing command still closes`,
);
promise_test(
async function (t) {
t.add_cleanup(resetState);
invokee.showModal();
assert_true(invokee.open, "invokee.open");
assert_true(invokee.matches(":modal"), "invokee :modal");
if (setType === "property") {
containedinvoker.command = command;
} else {
containedinvoker.setAttribute("command", command);
}
invokee.addEventListener(
"command",
(e) => {
containedinvoker.setAttribute("command", "show");
},
{ once: true },
);
await clickOn(containedinvoker);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
},
`invoking to request-close (with command ${setType} as ${command}) open modal dialog while changing command still closes`,
);
});
});
// request-close explicit behaviours
promise_test(async function (t) {
t.add_cleanup(resetState);
invokerbutton.setAttribute("command", "request-close");
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
await clickOn(containedinvoker);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
}, "invoking (as request-close) already closed dialog is noop");
// Open Popovers using Dialog actions
["request-close"].forEach((command) => {
["manual", "auto"].forEach((popoverState) => {
promise_test(
async function (t) {
t.add_cleanup(resetState);
invokee.setAttribute("popover", popoverState);
invokee.showPopover();
containedinvoker.setAttribute("command", command);
assert_true(
invokee.matches(":popover-open"),
"invokee :popover-open",
);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
invokee.addEventListener("command", (e) => e.preventDefault(), {
once: true,
});
await clickOn(containedinvoker);
assert_true(
invokee.matches(":popover-open"),
"invokee :popover-open",
);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
},
`invoking (as ${command}) dialog as open popover=${popoverState} is noop`,
);
});
});
// Elements being disconnected during invoke steps
["request-close"].forEach((command) => {
promise_test(
async function (t) {
t.add_cleanup(() => {
document.body.prepend(invokee);
resetState();
});
const invokee = document.querySelector("#invokee");
invokerbutton.setAttribute("command", command);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
invokee.addEventListener(
"command",
(e) => {
invokee.remove();
},
{
once: true,
},
);
await clickOn(invokerbutton);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
},
`invoking (as ${command}) dialog that is removed is noop`,
);
promise_test(
async function (t) {
const invokerbutton = document.createElement("button");
invokerbutton.commandForElement = invokee;
invokerbutton.setAttribute("command", command);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
await clickOn(invokerbutton);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
},
`invoking (as ${command}) dialog from a detached invoker`,
);
promise_test(
async function (t) {
const invokerbutton = document.createElement("button");
const invokee = document.createElement("dialog");
invokerbutton.commandForElementt = invokee;
invokerbutton.setAttribute("command", command);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
await clickOn(invokerbutton);
assert_false(invokee.open, "invokee.open");
assert_false(invokee.matches(":modal"), "invokee :modal");
},
`invoking (as ${command}) detached dialog from a detached invoker`,
);
});
</script>