Bug 1170531 - Disable clipboard menu commands correctly in non-(X)HTML documents; r=ehsan

This commit is contained in:
Michael Layzell 2015-06-22 08:13:26 -04:00 committed by Ehsan Akhgari
parent 6274739387
commit d4b27c154d
6 changed files with 57 additions and 4 deletions

View File

@ -486,14 +486,23 @@ nsClipboardCommand::IsCommandEnabled(const char* aCommandName, nsISupports *aCon
*outCmdEnabled = false;
if (strcmp(aCommandName, "cmd_copy") &&
strcmp(aCommandName, "cmd_copyAndCollapseToEnd"))
strcmp(aCommandName, "cmd_copyAndCollapseToEnd") &&
strcmp(aCommandName, "cmd_cut"))
return NS_OK;
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
*outCmdEnabled = nsCopySupport::CanCopy(doc);
if (doc->IsHTMLOrXHTML()) {
// In HTML and XHTML documents, we always want cut and copy commands to be enabled.
*outCmdEnabled = true;
} else {
// Cut isn't enabled in xul documents which use nsClipboardCommand
if (strcmp(aCommandName, "cmd_cut")) {
*outCmdEnabled = nsCopySupport::CanCopy(doc);
}
}
return NS_OK;
}

View File

@ -1155,6 +1155,12 @@ nsPlaintextEditor::Redo(uint32_t aCount)
bool
nsPlaintextEditor::CanCutOrCopy(PasswordFieldAllowed aPasswordFieldAllowed)
{
nsCOMPtr<nsIDocument> doc = GetDocument();
if (doc && doc->IsHTMLOrXHTML()) {
// In HTML and XHTML documents, we always want cut and copy commands to be enabled.
return true;
}
nsRefPtr<Selection> selection = GetSelection();
if (!selection) {
return false;

View File

@ -17,7 +17,8 @@ function goUpdateGlobalEditMenuItems()
goUpdateCommand("cmd_undo");
goUpdateCommand("cmd_redo");
// don't update the cmd_cut or cmd_copy items - as we want them to always be enabled
goUpdateCommand("cmd_cut");
goUpdateCommand("cmd_copy");
goUpdateCommand("cmd_paste");
goUpdateCommand("cmd_selectAll");
goUpdateCommand("cmd_delete");

View File

@ -93,7 +93,7 @@ function goDoCommand(aCommand)
try {
var controller = top.document.commandDispatcher
.getControllerForCommand(aCommand);
if (controller)
if (controller && controller.isCommandEnabled(aCommand))
controller.doCommand(aCommand);
}
catch (e) {

View File

@ -36,3 +36,4 @@ skip-if = !e10s || !crashreporter
support-files =
file_redirect.html
file_redirect_to.html
[browser_bug1170531.js]

View File

@ -0,0 +1,36 @@
// Test for bug 1170531
// https://bugzilla.mozilla.org/show_bug.cgi?id=1170531
add_task(function* () {
yield BrowserTestUtils.withNewTab({ gBrowser: gBrowser, url: "about:blank" }, function* (browser) {
let menu_EditPopup = document.getElementById("menu_EditPopup");
let menu_cut_disabled, menu_copy_disabled;
yield BrowserTestUtils.loadURI(browser, "data:text/html,<div>hello!</div>");
browser.focus();
yield new Promise(resolve => waitForFocus(resolve, window));
goUpdateGlobalEditMenuItems();
menu_cut_disabled = menu_EditPopup.querySelector("#menu_cut").getAttribute('disabled') == "true";
is(menu_cut_disabled, false, "menu_cut should be enabled");
menu_copy_disabled = menu_EditPopup.querySelector("#menu_copy").getAttribute('disabled') == "true";
is(menu_copy_disabled, false, "menu_copy should be enabled");
yield BrowserTestUtils.loadURI(browser, "data:text/html,<div contentEditable='true'>hello!</div>");
browser.focus();
yield new Promise(resolve => waitForFocus(resolve, window));
goUpdateGlobalEditMenuItems();
menu_cut_disabled = menu_EditPopup.querySelector("#menu_cut").getAttribute('disabled') == "true";
is(menu_cut_disabled, false, "menu_cut should be enabled");
menu_copy_disabled = menu_EditPopup.querySelector("#menu_copy").getAttribute('disabled') == "true";
is(menu_copy_disabled, false, "menu_copy should be enabled");
yield BrowserTestUtils.loadURI(browser, "about:preferences");
browser.focus();
yield new Promise(resolve => waitForFocus(resolve, window));
goUpdateGlobalEditMenuItems();
menu_cut_disabled = menu_EditPopup.querySelector("#menu_cut").getAttribute('disabled') == "true";
is(menu_cut_disabled, true, "menu_cut should be disabled");
menu_copy_disabled = menu_EditPopup.querySelector("#menu_copy").getAttribute('disabled') == "true";
is(menu_copy_disabled, true, "menu_copy should be disabled");
});
});