Skip to content

Commit

Permalink
Bug 1857303 - Fire copy event when pressing Ctrl+C on contenteditable…
Browse files Browse the repository at this point in the history
… without selection. r=sefeng,masayuki

Differential Revision: https://phabricator.services.mozilla.com/D190402
  • Loading branch information
agrawal-d committed Oct 27, 2023
1 parent e4533f2 commit 13347e5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion editor/libeditor/EditorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,11 @@ bool EditorBase::CheckForClipboardCommandListener(
return false;
}

RefPtr<EventTarget> et = GetDOMEventTarget();
RefPtr<EventTarget> et = IsHTMLEditor()
? AsHTMLEditor()->ComputeEditingHost(
HTMLEditor::LimitInBodyElement::No)
: GetDOMEventTarget();

while (et) {
EventListenerManager* elm = et->GetExistingListenerManager();
if (elm && elm->HasListenersFor(aCommand)) {
Expand Down
2 changes: 2 additions & 0 deletions editor/libeditor/tests/mochitest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ skip-if = ["os == 'android'"] #Bug 1575739

["test_composition_with_highlight_in_texteditor.html"]

["test_contenteditable_copy_empty_selection.html"]

["test_contenteditable_focus.html"]

["test_cut_copy_delete_command_enabled.html"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1857303
-->
<head>
<meta charset="utf-8">
<title>Test for contenteditable copy event fired even when selection is empty</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<div contenteditable=true id="elem">Copy</div>
<script>
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(() => {
let copyEventFired = false;
const editableDiv = document.getElementById("elem");

editableDiv.addEventListener("copy", function () {
copyEventFired = true;
});

editableDiv.focus();
synthesizeKey("c", { accelKey: true });
ok(copyEventFired, "Copy event should be fired");

SimpleTest.finish();
});
</script>
</body>
</html>

0 comments on commit 13347e5

Please sign in to comment.