Commit 0a9dcf3d authored by Hugo Holgersson's avatar Hugo Holgersson Committed by Commit Bot

Don't copy hidden selections

Chrome sometimes hides selections*. CTRL+C should not copy
such selections because users expect that CTRL+C has no
effect when their browser doesn't display a selection.

What about cut and paste?
Cut and paste are read/write-operations so they only operate
on _focused_ selections. A focused selection is always visible:

  bool FrameSelection::IsHidden() const {
    if (SelectionHasFocus())
      return false;
    ...

=> Cut and paste already follow the principle of visibility
so we only need to fix "copy". This CL does that.

* See the explainer in crrev.com/525029 for more information.

Bug: 813416
Change-Id: Ibb5f3868e7a5fc37c7522bb753a41ae9568d98e2
Reviewed-on: https://chromium-review.googlesource.com/924027
Commit-Queue: Hugo Holgersson <hugoh@vewd.com>
Reviewed-by: default avatarXiaocheng Hu <xiaochengh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537633}
parent 172bcb68
......@@ -950,6 +950,12 @@ void Editor::Copy(EditorCommandSource source) {
if (!CanCopy())
return;
// Since copy is a read-only operation it succeeds anytime a selection
// is *visible*. In contrast to cut or paste, the selection does not
// need to be focused - being visible is enough.
if (source == kCommandFromMenuOrKeyBinding && GetFrameSelection().IsHidden())
return;
// TODO(editing-dev): The use of UpdateStyleAndLayoutIgnorePendingStylesheets
// needs to be audited. See http://crbug.com/590369 for more details.
// A 'copy' event handler might have dirtied the layout so we need to update
......
......@@ -4,6 +4,7 @@
#include "core/editing/Editor.h"
#include "core/clipboard/Pasteboard.h"
#include "core/editing/testing/EditingTestBase.h"
#include "core/html/forms/HTMLInputElement.h"
......@@ -34,4 +35,25 @@ TEST_F(EditorTest, copyGeneratedPassword) {
EXPECT_TRUE(editor.CanCopy());
}
TEST_F(EditorTest, DontCopyHiddenSelections) {
const char* body_content =
"<input type=checkbox id=checkbox>"
"<input id=hiding value=HEY></input>";
SetBodyContent(body_content);
HTMLInputElement& text_control =
ToHTMLInputElement(*GetDocument().getElementById("hiding"));
text_control.select();
HTMLInputElement& checkbox =
ToHTMLInputElement(*GetDocument().getElementById("checkbox"));
checkbox.focus();
Editor& editor = GetDocument().GetFrame()->GetEditor();
editor.Copy(kCommandFromMenuOrKeyBinding);
const String copied = Pasteboard::GeneralPasteboard()->PlainText();
EXPECT_TRUE(copied.IsEmpty()) << copied << " was copied.";
}
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment