Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!doctype html>
<html>
<head>
<title>Test for cut/copy in password field</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<input type="password">
<script>
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
let input = document.getElementsByTagName("input")[0];
let editor = SpecialPowers.wrap(input).editor;
const kMask = editor.passwordMask;
async function copyToClipboard(aExpectedValue) {
try {
await SimpleTest.promiseClipboardChange(
aExpectedValue, () => { SpecialPowers.doCommand(window, "cmd_copy"); },
undefined, undefined, aExpectedValue === null);
} catch (e) {
console.error(e);
}
}
async function cutToClipboard(aExpectedValue) {
try {
await SimpleTest.promiseClipboardChange(
aExpectedValue, () => { SpecialPowers.doCommand(window, "cmd_cut"); },
undefined, undefined, aExpectedValue === null);
} catch (e) {
console.error(e);
}
}
input.value = "abcdef";
input.focus();
input.setSelectionRange(0, 6);
ok(true, "Trying to copy masked password...");
await copyToClipboard(null);
isnot(SpecialPowers.getClipboardData("text/plain"), "abcdef",
"Copying masked password shouldn't copy raw value into the clipboard");
isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}${kMask}${kMask}${kMask}${kMask}`,
"Copying masked password shouldn't copy masked value into the clipboard");
ok(true, "Trying to cut masked password...");
await cutToClipboard(null);
isnot(SpecialPowers.getClipboardData("text/plain"), "abcdef",
"Cutting masked password shouldn't copy raw value into the clipboard");
isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}${kMask}${kMask}${kMask}${kMask}`,
"Cutting masked password shouldn't copy masked value into the clipboard");
is(input.value, "abcdef",
"Cutting masked password shouldn't modify the value");
editor.unmask(2, 4);
input.setSelectionRange(0, 6);
ok(true, "Trying to copy partially masked password...");
await copyToClipboard(null);
isnot(SpecialPowers.getClipboardData("text/plain"), "abcdef",
"Copying partially masked password shouldn't copy raw value into the clipboard");
isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}cd${kMask}${kMask}`,
"Copying partially masked password shouldn't copy partially masked value into the clipboard");
isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}${kMask}${kMask}${kMask}${kMask}`,
"Copying partially masked password shouldn't copy masked value into the clipboard");
ok(true, "Trying to cut partially masked password...");
await cutToClipboard(null);
isnot(SpecialPowers.getClipboardData("text/plain"), "abcdef",
"Cutting partially masked password shouldn't copy raw value into the clipboard");
isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}cd${kMask}${kMask}`,
"Cutting partially masked password shouldn't copy partially masked value into the clipboard");
isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}${kMask}${kMask}${kMask}${kMask}`,
"Cutting partially masked password shouldn't copy masked value into the clipboard");
is(input.value, "abcdef",
"Cutting partially masked password shouldn't modify the value");
input.setSelectionRange(2, 4);
ok(true, "Trying to copy unmasked password...");
await copyToClipboard("cd");
is(input.value, "abcdef",
"Copying unmasked password shouldn't modify the value");
input.value = "012345";
editor.unmask(2, 4);
input.setSelectionRange(2, 4);
ok(true, "Trying to cut unmasked password...");
await cutToClipboard("23");
is(input.value, "0145",
"Cutting unmasked password should modify the value");
SimpleTest.finish();
});
</script>
</body>
</html>