Skip to content

Commit

Permalink
Bug 1067255 - Disallow cut and copy in password fields. r=ehsan
Browse files Browse the repository at this point in the history
  • Loading branch information
janjongboom committed Oct 8, 2014
1 parent f19ff98 commit 87c112f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 37 deletions.
6 changes: 3 additions & 3 deletions browser/base/content/test/general/test_contextmenu.html
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,10 @@
// Context menu for selected text in input[type="password"]
checkContextMenu(["context-undo", false,
"---", null,
"context-cut", true,
"context-copy", true,
"context-cut", false,
"context-copy", false,
"context-paste", null, // ignore clipboard state
"context-delete", true,
"context-delete", false,
"---", null,
"context-selectall", true,
"---", null,
Expand Down
44 changes: 10 additions & 34 deletions dom/browser-element/mochitest/browserElement_CopyPaste.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,7 @@ function dispatchTest(e) {
stateMeaning = " (test: <input type=text>)";
focusScript = "var elt=content.document.getElementById('text');elt.focus();elt.select();";
break;
case 2: // test for input password
defaultData = "Test for selection change event";
pasteData = "from parent ";
iframe.src = "data:text/html,<html><body>" +
"<input type='password' id='text' value='" + defaultData + "'>" +
"</body>" +
"</html>";
stateMeaning = " (test: <input type=password>)";
focusScript = "var elt=content.document.getElementById('text');elt.focus();elt.select();";
break;
case 3: // test for input number
case 2: // test for input number
defaultData = "12345";
pasteData = "67890";
iframe.src = "data:text/html,<html><body>" +
Expand All @@ -112,7 +102,7 @@ function dispatchTest(e) {
stateMeaning = " (test: <input type=number>)";
focusScript = "var elt=content.document.getElementById('text');elt.focus();elt.select();";
break;
case 4: // test for div contenteditable
case 3: // test for div contenteditable
defaultData = "Test for selection change event";
pasteData = "from parent ";
iframe.src = "data:text/html,<html><body>" +
Expand All @@ -122,7 +112,7 @@ function dispatchTest(e) {
stateMeaning = " (test: content editable div)";
focusScript = "var elt=content.document.getElementById('text');elt.focus();";
break;
case 5: // test for normal div
case 4: // test for normal div
SimpleTest.finish();
return;
defaultData = "Test for selection change event";
Expand All @@ -134,7 +124,7 @@ function dispatchTest(e) {
stateMeaning = " (test: normal div)";
focusScript = "var elt=content.document.getElementById('text');elt.focus();";
break;
case 6: // test for normal div with designMode:on
case 5: // test for normal div with designMode:on
defaultData = "Test for selection change event";
pasteData = "from parent ";
iframe.src = "data:text/html,<html><body id='text'>" +
Expand Down Expand Up @@ -192,14 +182,6 @@ function testCopy1(e) {
}

let compareData = defaultData;
if (state == 2) {
// In password case, we just check length of text at clipboard is equal
// to length of defaultData
compareData = function(clipboardText) {
return clipboardText.length == defaultData.length;
};
}

SimpleTest.waitForClipboard(compareData, setup, success, fail);
}

Expand All @@ -215,10 +197,10 @@ function testPaste1(e) {
function testPaste2(e) {
mm.addMessageListener('content-text', function messageforpaste(msg) {
mm.removeMessageListener('content-text', messageforpaste);
if (state == 5) {
if (state == 4) {
// normal div cannot paste, so the content remain unchange
ok(SpecialPowers.wrap(msg).json === defaultData, "paste command works" + stateMeaning);
} else if (state == 4 && browserElementTestHelpers.getOOPByDefaultPref()) {
} else if (state == 3 && browserElementTestHelpers.getOOPByDefaultPref()) {
// Something weird when we doCommand with content editable element in OOP. Mark this case as todo
todo(false, "paste command works" + stateMeaning);
} else {
Expand All @@ -239,7 +221,7 @@ function testCut1(e) {
};

let nextTest = function(success) {
if (state == 4 && browserElementTestHelpers.getOOPByDefaultPref()) {
if (state == 3 && browserElementTestHelpers.getOOPByDefaultPref()) {
// Something weird when we doCommand with content editable element in OOP.
todo(false, "cut function works" + stateMeaning);
} else {
Expand All @@ -257,13 +239,7 @@ function testCut1(e) {
}

let compareData = pasteData;
if (state == 2) {
// In password case, we just check length of text at clipboard is equal
// to length of pasteData
compareData = function(clipboardText) {
return clipboardText.length == pasteData.length;
};
} else if (state == 4 && browserElementTestHelpers.getOOPByDefaultPref()) {
if (state == 3 && browserElementTestHelpers.getOOPByDefaultPref()) {
// Something weird when we doCommand with content editable element in OOP.
// Always true in this case
compareData = function() { return true; }
Expand All @@ -276,9 +252,9 @@ function testCut2(e) {
mm.addMessageListener('content-text', function messageforcut(msg) {
mm.removeMessageListener('content-text', messageforcut);
// normal div cannot cut
if (state == 5) {
if (state == 4) {
ok(SpecialPowers.wrap(msg).json !== "", "cut command works" + stateMeaning);
} else if (state == 4 && browserElementTestHelpers.getOOPByDefaultPref()) {
} else if (state == 3 && browserElementTestHelpers.getOOPByDefaultPref()) {
// Something weird when we doCommand with content editable element in OOP. Mark this case as todo
todo(false, "cut command works" + stateMeaning);
} else {
Expand Down
3 changes: 3 additions & 0 deletions editor/libeditor/nsPlaintextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,9 @@ nsPlaintextEditor::CanCutOrCopy()
if (NS_FAILED(GetSelection(getter_AddRefs(selection))))
return false;

if (IsPasswordEditor())
return false;

return !selection->Collapsed();
}

Expand Down
1 change: 1 addition & 0 deletions editor/libeditor/tests/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ skip-if = os != "win"
skip-if = os != "win"
[test_bug998188.html]
[test_bug1026397.html]
[test_bug1067255.html]
skip-if = e10s
[test_CF_HTML_clipboard.html]
[test_contenteditable_focus.html]
Expand Down
52 changes: 52 additions & 0 deletions editor/libeditor/tests/test_bug1067255.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE HTML>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1067255
-->

<head>
<title>Test for Bug 1067255</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
</head>

<body onload="doTest();">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1067255">Mozilla Bug 1067255</a>

<pre id="test">
<script type="application/javascript">
/** Test for Bug 1067255 **/
SimpleTest.waitForExplicitFinish();

function doTest() {
var text = $("text-field");
var password = $("password-field");

var editor1 = SpecialPowers.wrap(text).editor;
var editor2 = SpecialPowers.wrap(password).editor;

text.focus();
text.select();

ok(editor1.canCopy(), "can copy, text");
ok(editor1.canCut(), "can cut, text");

password.focus();
password.select();

ok(!editor2.canCopy(), "can copy, password");
ok(!editor2.canCut(), "can cut, password");

SimpleTest.finish();
}
</script>
</pre>

<input type="text" value="Gonzo says hi" id="text-field" />
<input type="password" value="Jan also" id="password-field" />
</body>
</html>

0 comments on commit 87c112f

Please sign in to comment.