Skip to content

Commit

Permalink
Bug 1558674 - Allow chrome privileged documents to enable/disable cop…
Browse files Browse the repository at this point in the history
…y/paste. r=Ehsan,bgrins

For content HTML/XHTML copy/paste should always be enabled, but for chrome
docs we can support enabling/disabling copy/paste.

Also, restores tests to how they were before copy/paste was always enabled.

Differential Revision: https://phabricator.services.mozilla.com/D34805
  • Loading branch information
Brendan Dahl committed Jun 18, 2019
1 parent 605e357 commit 5d76027
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ add_task(async function checkMenuEntryStates() {
is(cmdUndo.getAttribute("disabled"), "true", "cmdUndo is disabled");
is(cmdDelete.getAttribute("disabled"), "true", "cmdDelete is disabled");
is(cmdSelectAll.getAttribute("disabled"), "true", "cmdSelectAll is disabled");
is(cmdCut.getAttribute("disabled"), "true", "cmdCut is disabled");
is(cmdCopy.getAttribute("disabled"), "true", "cmdCopy is disabled");

// Cut/Copy/Paste items are enabled in context menu even if there
// is no selection. See also Bug 1303033, and 1317322
is(cmdCut.getAttribute("disabled"), "", "cmdCut is enabled");
is(cmdCopy.getAttribute("disabled"), "", "cmdCopy is enabled");
is(cmdPaste.getAttribute("disabled"), "", "cmdPaste is enabled");
if (isWindows()) {
// emptyClipboard only works on Windows (666254), assert paste only for this OS.
is(cmdPaste.getAttribute("disabled"), "true", "cmdPaste is disabled");
}

const onContextMenuHidden = toolbox.once("menu-close");
EventUtils.sendKey("ESCAPE", toolbox.win);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ add_task(async function() {
is(cmdDelete.getAttribute("disabled"), "true", "cmdDelete is disabled");
is(cmdSelectAll.getAttribute("disabled"), "true", "cmdSelectAll is disabled");

// Cut/Copy/Paste items are enabled in context menu even if there is no
// selection. See also Bug 1303033, and 1317322
is(cmdCut.getAttribute("disabled"), "", "cmdCut is enabled");
is(cmdCopy.getAttribute("disabled"), "", "cmdCopy is enabled");
is(cmdPaste.getAttribute("disabled"), "", "cmdPaste is enabled");
is(cmdCut.getAttribute("disabled"), "true", "cmdCut is disabled");
is(cmdCopy.getAttribute("disabled"), "true", "cmdCopy is disabled");
if (isWindows()) {
// emptyClipboard only works on Windows (666254), assert paste only for this OS.
is(cmdPaste.getAttribute("disabled"), "true", "cmdPaste is disabled");
}

info("Closing context menu");
let onContextMenuClose = toolbox.once("menu-close");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ add_task(async function() {
is(cmdUndo.getAttribute("disabled"), "true", "cmdUndo is disabled");
is(cmdDelete.getAttribute("disabled"), "true", "cmdDelete is disabled");
is(cmdSelectAll.getAttribute("disabled"), "true", "cmdSelectAll is disabled");
is(cmdCut.getAttribute("disabled"), "true", "cmdCut is disabled");
is(cmdCopy.getAttribute("disabled"), "true", "cmdCopy is disabled");

// Cut/Copy/Paste items are enabled in context menu even if there is no
// selection. See also Bug 1303033, and 1317322
is(cmdCut.getAttribute("disabled"), "", "cmdCut is enabled");
is(cmdCopy.getAttribute("disabled"), "", "cmdCopy is enabled");
is(cmdPaste.getAttribute("disabled"), "", "cmdPaste is enabled");
if (isWindows()) {
// emptyClipboard only works on Windows (666254), assert paste only for this OS.
is(cmdPaste.getAttribute("disabled"), "true", "cmdPaste is disabled");
}

info("Closing context menu");
let onContextMenuClose = toolbox.once("menu-close");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ add_task(async function() {
is(cmdUndo.getAttribute("disabled"), "true", "cmdUndo is disabled");
is(cmdDelete.getAttribute("disabled"), "true", "cmdDelete is disabled");
is(cmdSelectAll.getAttribute("disabled"), "true", "cmdSelectAll is disabled");
is(cmdCut.getAttribute("disabled"), "true", "cmdCut is disabled");
is(cmdCopy.getAttribute("disabled"), "true", "cmdCopy is disabled");

// Cut/Copy items are enabled in context menu even if there
// is no selection. See also Bug 1303033, and 1317322
is(cmdCut.getAttribute("disabled"), "", "cmdCut is enabled");
is(cmdCopy.getAttribute("disabled"), "", "cmdCopy is enabled");
is(cmdPaste.getAttribute("disabled"), "", "cmdPaste is enabled");
if (isWindows()) {
// emptyClipboard only works on Windows (666254), assert paste only for this OS.
is(cmdPaste.getAttribute("disabled"), "true", "cmdPaste is disabled");
}

info("Closing context menu");
let onContextMenuClose = toolbox.once("menu-close");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function runTests() {
};

const firstShow = function() {
ok(!cutItem.hasAttribute("disabled"), "cut menuitem is enabled");
ok(cutItem.hasAttribute("disabled"), "cut menuitem is disabled");
closeMenu(firstHide);
};

Expand Down Expand Up @@ -143,7 +143,7 @@ function runTests() {
};

const showAfterCut = function() {
ok(!cutItem.hasAttribute("disabled"), "cut menuitem is enabled after cut");
ok(cutItem.hasAttribute("disabled"), "cut menuitem is disabled after cut");
ok(!pasteItem.hasAttribute("disabled"), "paste menuitem is enabled after cut");
closeMenu(hideAfterCut);
};
Expand All @@ -161,7 +161,7 @@ function runTests() {
};

const showAfterPaste = function() {
ok(!cutItem.hasAttribute("disabled"), "cut menuitem is enabled after paste");
ok(cutItem.hasAttribute("disabled"), "cut menuitem is disabled after paste");
ok(!pasteItem.hasAttribute("disabled"), "paste menuitem is enabled after paste");
closeMenu(hideAfterPaste);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,31 @@ async function performTests() {

menuPopup = await openContextMenu(hud, hud.jsterm.node || hud.jsterm.inputNode);

expectedContextMenu = [
"#editmenu-undo (editmenu-undo) [disabled]",
"#editmenu-cut (editmenu-cut)",
"#editmenu-copy (editmenu-copy)",
"#editmenu-paste (editmenu-paste)",
"#editmenu-delete (editmenu-delete) [disabled]",
"#editmenu-selectAll (editmenu-select-all) [disabled]",
];
is(getL10NContextMenu(menuPopup).join("\n"), expectedContextMenu.join("\n"),
"The context menu has the correct edit menu items");
let actualEntries = getL10NContextMenu(menuPopup);
is(actualEntries.length, 6, "The context menu has the right number of entries.");
is(actualEntries[0], "#editmenu-undo (editmenu-undo) [disabled]");
is(actualEntries[1], "#editmenu-cut (editmenu-cut) [disabled]");
is(actualEntries[2], "#editmenu-copy (editmenu-copy) [disabled]");
// Paste may or may not be enabled depending on what ran before this.
// If emptyClipboard is fixed (666254) we could assert if it's enabled/disabled.
ok(actualEntries[3].startsWith("#editmenu-paste (editmenu-paste)"));
is(actualEntries[4], "#editmenu-delete (editmenu-delete) [disabled]");
is(actualEntries[5], "#editmenu-selectAll (editmenu-select-all) [disabled]");

const node = hud.jsterm.inputNode || hud.jsterm.node;
const inputContainer = node.closest(".jsterm-input-container");
await openContextMenu(hud, inputContainer);

expectedContextMenu = [
"#editmenu-undo (editmenu-undo) [disabled]",
"#editmenu-cut (editmenu-cut)",
"#editmenu-copy (editmenu-copy)",
"#editmenu-paste (editmenu-paste)",
"#editmenu-delete (editmenu-delete) [disabled]",
"#editmenu-selectAll (editmenu-select-all) [disabled]",
];
is(getL10NContextMenu(menuPopup).join("\n"), expectedContextMenu.join("\n"),
"The context menu has the required elements");
actualEntries = getL10NContextMenu(menuPopup);
is(actualEntries.length, 6, "The context menu has the right number of entries.");
is(actualEntries[0], "#editmenu-undo (editmenu-undo) [disabled]");
is(actualEntries[1], "#editmenu-cut (editmenu-cut) [disabled]");
is(actualEntries[2], "#editmenu-copy (editmenu-copy) [disabled]");
// Paste may or may not be enabled depending on what ran before this.
// If emptyClipboard is fixed (666254) we could assert if it's enabled/disabled.
ok(actualEntries[3].startsWith("#editmenu-paste (editmenu-paste)"));
is(actualEntries[4], "#editmenu-delete (editmenu-delete) [disabled]");
is(actualEntries[5], "#editmenu-selectAll (editmenu-select-all) [disabled]");

await hideContextMenu(hud);
// Close the browser console.
Expand Down
4 changes: 2 additions & 2 deletions dom/base/nsGlobalWindowCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,9 @@ nsresult nsClipboardCommand::IsCommandEnabled(const char* aCommandName,
RefPtr<dom::Document> doc = window->GetExtantDoc();
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);

if (doc->IsHTMLOrXHTML()) {
if (doc->IsHTMLOrXHTML() && !nsContentUtils::IsChromeDoc(doc)) {
// In HTML and XHTML documents, we always want the cut, copy and paste
// commands to be enabled.
// commands to be enabled, but if the document is chrome, let it control it.
*outCmdEnabled = true;
} else {
// Cut isn't enabled in xul documents which use nsClipboardCommand
Expand Down
6 changes: 4 additions & 2 deletions editor/libeditor/HTMLEditorDataTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1642,9 +1642,11 @@ static const char* textHtmlEditorFlavors[] = {kUnicodeMime, kHTMLMime,
kPNGImageMime, kGIFImageMime};

bool HTMLEditor::CanPaste(int32_t aClipboardType) const {
// Always enable the paste command when inside of a HTML or XHTML document.
// Always enable the paste command when inside of a HTML or XHTML document,
// but if the document is chrome, let it control it.
Document* document = GetDocument();
if (document && document->IsHTMLOrXHTML()) {
if (document && document->IsHTMLOrXHTML() &&
!nsContentUtils::IsChromeDoc(document)) {
return true;
}

Expand Down
12 changes: 8 additions & 4 deletions editor/libeditor/TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,9 +1794,11 @@ bool TextEditor::CanCut() const {
return false;
}

// Cut is always enabled in HTML documents
// Cut is always enabled in HTML documents, but if the document is chrome,
// let it control it.
Document* document = GetDocument();
if (document && document->IsHTMLOrXHTML()) {
if (document && document->IsHTMLOrXHTML() &&
!nsContentUtils::IsChromeDoc(document)) {
return true;
}

Expand All @@ -1823,9 +1825,11 @@ bool TextEditor::CanCopy() const {
return false;
}

// Copy is always enabled in HTML documents
// Copy is always enabled in HTML documents, but if the document is chrome,
// let it control it.
Document* document = GetDocument();
if (document && document->IsHTMLOrXHTML()) {
if (document && document->IsHTMLOrXHTML() &&
!nsContentUtils::IsChromeDoc(document)) {
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions editor/libeditor/TextEditorDataTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,10 @@ nsresult TextEditor::PasteTransferableAsAction(nsITransferable* aTransferable,
}

bool TextEditor::CanPaste(int32_t aClipboardType) const {
// Always enable the paste command when inside of a HTML or XHTML document.
// Always enable the paste command when inside of a HTML or XHTML document,
// but if the document is chrome, let it control it.
RefPtr<Document> doc = GetDocument();
if (doc && doc->IsHTMLOrXHTML()) {
if (doc && doc->IsHTMLOrXHTML() && !nsContentUtils::IsChromeDoc(doc)) {
return true;
}

Expand Down

0 comments on commit 5d76027

Please sign in to comment.