Commit bbc2ac0d authored by Xiaocheng Hu's avatar Xiaocheng Hu Committed by Commit Bot

(Re)allow copying image documents directly

In https://crrev.com/c/924027, we blocked copy command if selection is
hidden, but also incidentally broke copying on image documents because:
- We don't have visible selection on image document
- Copying on image document is done via a fast path that doesn't check
  selection at all, but the CL blocked the fast path

This patch moves the fast path before checking selection visibility,
so that the original copy behavior on image documents is recovered.

Bug: 845957
Change-Id: I920198d13ab3aa387f550c5c213fcbadc42fd43e
Reviewed-on: https://chromium-review.googlesource.com/1072730
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561989}
parent 158fa75a
......@@ -223,6 +223,20 @@ bool ClipboardCommands::ExecuteCopy(LocalFrame& frame,
if (!frame.GetEditor().CanCopy())
return true;
Document* const document = frame.GetDocument();
// 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
// before we obtain the selection.
document->UpdateStyleAndLayoutIgnorePendingStylesheets();
if (HTMLImageElement* image_element =
ImageElementFromImageDocument(document)) {
WriteImageNodeToClipboard(*image_element, document->title());
return true;
}
// 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.
......@@ -230,24 +244,12 @@ bool ClipboardCommands::ExecuteCopy(LocalFrame& frame,
frame.Selection().IsHidden())
return true;
// 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
// before we obtain the selection.
frame.GetDocument()->UpdateStyleAndLayoutIgnorePendingStylesheets();
if (EnclosingTextControl(
frame.Selection().ComputeVisibleSelectionInDOMTree().Start())) {
SystemClipboard::GetInstance().WritePlainText(
frame.SelectedTextForClipboard(), GetSmartReplaceOption(frame));
return true;
}
const Document* const document = frame.GetDocument();
if (HTMLImageElement* image_element =
ImageElementFromImageDocument(document)) {
WriteImageNodeToClipboard(*image_element, document->title());
return true;
}
WriteSelectionToClipboard(frame);
return true;
}
......
......@@ -11005,6 +11005,34 @@ TEST_F(WebFrameTest, ImageDocumentLoadFinishTime) {
EXPECT_EQ(loader->GetTiming().ResponseEnd(), resource->LoadFinishTime());
}
TEST_F(WebFrameTest, CopyImageDocument) {
// After loading an image document, we should be able to copy it directly.
RegisterMockedHttpURLLoadWithMimeType("white-1x1.png", "image/png");
FrameTestHelpers::WebViewHelper web_view_helper;
web_view_helper.InitializeAndLoad(base_url_ + "white-1x1.png");
WebViewImpl* web_view = web_view_helper.GetWebView();
WebLocalFrameImpl* web_frame = web_view->MainFrameImpl();
Document* document = web_frame->GetFrame()->GetDocument();
ASSERT_TRUE(document);
EXPECT_TRUE(document->IsImageDocument());
EXPECT_TRUE(SystemClipboard::GetInstance().ReadAvailableTypes().IsEmpty());
bool result = web_frame->ExecuteCommand("Copy");
test::RunPendingTasks();
EXPECT_TRUE(result);
Vector<String> types = SystemClipboard::GetInstance().ReadAvailableTypes();
EXPECT_EQ(2u, types.size());
EXPECT_EQ("text/html", types[0]);
EXPECT_EQ("image/png", types[1]);
// Clear clipboard data
SystemClipboard::GetInstance().WritePlainText("");
}
class CallbackOrderingWebFrameClient
: public FrameTestHelpers::TestWebFrameClient {
public:
......
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