Skip to content
Permalink
Browse files
Source/WebCore:
[Mac] Write WebArchive to the pasteboard when copying image in WebKit
https://bugs.webkit.org/show_bug.cgi?id=163100

Reviewed by Darin Adler.

Write WebArchive to the pasteboard when copying image in WebKit. This
fixes pasting such images to a content editable field in WebKit because
Web archives take priority over RTFD when reading from the pasteboard
in WebKit.

Using RTFD when pasting the image in WebKit was causing issues because:
1. The pasted image would not be displayed because our RTFD import code
   is buggy.
2. The pasted image URL was a webkit-fake-url://
3. Formatting associated to the image (e.g. inline style) would be lost

No new tests, unskipped existing test on WebKit2.

* editing/Editor.h:
* editing/mac/EditorMac.mm:
(WebCore::Editor::imageInWebArchiveFormat):
(WebCore::Editor::writeImageToPasteboard):
* platform/Pasteboard.h:
* platform/mac/PasteboardMac.mm:
(WebCore::writableTypesForImage):
(WebCore::Pasteboard::write):
(WebCore::Pasteboard::Pasteboard): Deleted.
(WebCore::Pasteboard::createForCopyAndPaste): Deleted.

LayoutTests:
[Mac] Write HTML to the pasteboard when copying image in WebKit
https://bugs.webkit.org/show_bug.cgi?id=163100

Reviewed by Darin Adler.

Convert editing/pasteboard/copy-standalone-image.html into a ref test
and unskip on WebKit2 now that it passes there as well. Before this
patch, the test would fail on WebKit2 (the pasted image would not be
displayed).

* editing/pasteboard/copy-standalone-image-expected.html: Added.
* editing/pasteboard/copy-standalone-image.html:
* platform/gtk/editing/pasteboard/copy-standalone-image-expected.png: Removed.
* platform/gtk/editing/pasteboard/copy-standalone-image-expected.txt: Removed.
* platform/ios-simulator-wk2/editing/pasteboard/copy-standalone-image-expected.png: Removed.
* platform/ios-simulator/editing/pasteboard/copy-standalone-image-expected.txt: Removed.
* platform/mac/editing/pasteboard/copy-standalone-image-expected.png: Removed.
* platform/mac/editing/pasteboard/copy-standalone-image-expected.txt: Removed.
* platform/win/editing/pasteboard/copy-standalone-image-expected.txt: Removed.
* platform/wk2/TestExpectations:


Canonical link: https://commits.webkit.org/181013@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@206965 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
cdumez committed Oct 9, 2016
1 parent 12d2ab1 commit 3861f7c0f708eb6ab05f4e04ff787126ee5854b9
@@ -1,3 +1,26 @@
2016-10-08 Chris Dumez <cdumez@apple.com>

[Mac] Write HTML to the pasteboard when copying image in WebKit
https://bugs.webkit.org/show_bug.cgi?id=163100

Reviewed by Darin Adler.

Convert editing/pasteboard/copy-standalone-image.html into a ref test
and unskip on WebKit2 now that it passes there as well. Before this
patch, the test would fail on WebKit2 (the pasted image would not be
displayed).

* editing/pasteboard/copy-standalone-image-expected.html: Added.
* editing/pasteboard/copy-standalone-image.html:
* platform/gtk/editing/pasteboard/copy-standalone-image-expected.png: Removed.
* platform/gtk/editing/pasteboard/copy-standalone-image-expected.txt: Removed.
* platform/ios-simulator-wk2/editing/pasteboard/copy-standalone-image-expected.png: Removed.
* platform/ios-simulator/editing/pasteboard/copy-standalone-image-expected.txt: Removed.
* platform/mac/editing/pasteboard/copy-standalone-image-expected.png: Removed.
* platform/mac/editing/pasteboard/copy-standalone-image-expected.txt: Removed.
* platform/win/editing/pasteboard/copy-standalone-image-expected.txt: Removed.
* platform/wk2/TestExpectations:

2016-10-08 Chris Dumez <cdumez@apple.com>

Update CustomEvent to stop using legacy [ConstructorTemplate=Event]
@@ -0,0 +1,15 @@
<html>
<body>
<p>This is an automatic test of copying an image document.</p>
<p>To perform this test manually, click once in the image frame, choose Edit -> Copy
then click in the red box and paste the image. If the image pastes successfully the
test is passed.</p>

<iframe name="imageframe" src="../resources/abe.png"></iframe>

<div id="dest" contenteditable="true"><img src="../resources/abe.png" style="-webkit-user-select:none;"></div>
<script>
frames['imageframe'].focus();
</script>
</body>
</html>
@@ -1,41 +1,25 @@
<html>
<html>
<head>
<script>
if (window.testRunner)
testRunner.dumpEditingCallbacks();

function editingTest() {
frames['imageframe'].document.execCommand("Copy");

var s = window.getSelection();
var e = document.getElementById("dest");
s.setPosition(e, 0);
document.execCommand("Paste");
frames['imageframe'].focus();
}
</script>
<style>
.editable {
border: 2px solid red;
padding: 12px;
font-size: 24px;
}

iframe {
border: 2px solid blue;
}
</style>
</head>
<body>

<p>This is an automatic test of copying an image document.</p>
<p>To perform this test manually, click once in the image frame, choose Edit -> Copy
then click in the red box and paste the image. If the image pastes successfully the
test is passed.</p>

<iframe name="imageframe" src="../resources/abe.png" onload="editingTest()">
</iframe>

<div id="dest" class="editable" contenteditable="true"> </div>
<iframe name="imageframe" src="../resources/abe.png" onload="editingTest()"></iframe>

<div id="dest" contenteditable="true"></div>
</body>
</html>
Deleted file not rendered

This file was deleted.

This file was deleted.

Deleted file not rendered

This file was deleted.

This file was deleted.

@@ -511,9 +511,6 @@ transitions/text-indent-transition.html
# These storage tests fail in WebKit2 on the bot, but not running locally
storage/websql/multiple-databases-garbage-collection.html

# Crashes the WebProcess
editing/pasteboard/copy-standalone-image.html

# Mysterious failure after enabling paste tests
editing/mac/pasteboard/text-precomposed.html

@@ -1,3 +1,34 @@
2016-10-08 Chris Dumez <cdumez@apple.com>

[Mac] Write WebArchive to the pasteboard when copying image in WebKit
https://bugs.webkit.org/show_bug.cgi?id=163100

Reviewed by Darin Adler.

Write WebArchive to the pasteboard when copying image in WebKit. This
fixes pasting such images to a content editable field in WebKit because
Web archives take priority over RTFD when reading from the pasteboard
in WebKit.

Using RTFD when pasting the image in WebKit was causing issues because:
1. The pasted image would not be displayed because our RTFD import code
is buggy.
2. The pasted image URL was a webkit-fake-url://
3. Formatting associated to the image (e.g. inline style) would be lost

No new tests, unskipped existing test on WebKit2.

* editing/Editor.h:
* editing/mac/EditorMac.mm:
(WebCore::Editor::imageInWebArchiveFormat):
(WebCore::Editor::writeImageToPasteboard):
* platform/Pasteboard.h:
* platform/mac/PasteboardMac.mm:
(WebCore::writableTypesForImage):
(WebCore::Pasteboard::write):
(WebCore::Pasteboard::Pasteboard): Deleted.
(WebCore::Pasteboard::createForCopyAndPaste): Deleted.

2016-10-08 Chris Dumez <cdumez@apple.com>

Update CustomEvent to stop using legacy [ConstructorTemplate=Event]
@@ -508,6 +508,7 @@ class Editor {

#if PLATFORM(COCOA)
RefPtr<SharedBuffer> selectionInWebArchiveFormat();
RefPtr<SharedBuffer> imageInWebArchiveFormat(Element&);
RefPtr<Range> adjustedSelectionRange();
RefPtr<DocumentFragment> createFragmentForImageResourceAndAddResource(RefPtr<ArchiveResource>&&);
RefPtr<DocumentFragment> createFragmentAndAddResources(NSAttributedString *);
@@ -310,6 +310,14 @@ static void maybeCopyNodeAttributesToFragment(const Node& node, DocumentFragment
return SharedBuffer::wrapCFData(archive->rawDataRepresentation().get());
}

RefPtr<SharedBuffer> Editor::imageInWebArchiveFormat(Element& imageElement)
{
RefPtr<LegacyWebArchive> archive = LegacyWebArchive::create(imageElement);
if (!archive)
return nullptr;
return SharedBuffer::wrapCFData(archive->rawDataRepresentation().get());
}

RefPtr<Range> Editor::adjustedSelectionRange()
{
// FIXME: Why do we need to adjust the selection to include the anchor tag it's in?
@@ -436,6 +444,7 @@ static void getImage(Element& imageElement, RefPtr<Image>& image, CachedImage*&
return;
ASSERT(cachedImage);

pasteboardImage.dataInWebArchiveFormat = imageInWebArchiveFormat(imageElement);
pasteboardImage.url.url = url;
pasteboardImage.url.title = title;
pasteboardImage.url.userVisibleForm = client()->userVisibleString(pasteboardImage.url.url);
@@ -96,6 +96,9 @@ struct PasteboardImage {
WEBCORE_EXPORT PasteboardImage();
WEBCORE_EXPORT ~PasteboardImage();
RefPtr<Image> image;
#if PLATFORM(MAC)
RefPtr<SharedBuffer> dataInWebArchiveFormat;
#endif
#if !(PLATFORM(EFL) || PLATFORM(WIN))
PasteboardURL url;
#endif
@@ -98,23 +98,15 @@
return types;
}

static inline Vector<String> createWritableTypesForImage()
static Vector<String> writableTypesForImage()
{
Vector<String> types;

types.append(String(NSTIFFPboardType));
types.appendVector(writableTypesForURL());
types.append(String(NSRTFDPboardType));
return types;
}

static Vector<String> writableTypesForImage()
{
Vector<String> types;
types.appendVector(createWritableTypesForImage());
return types;
}

Pasteboard::Pasteboard(const String& pasteboardName)
: m_pasteboardName(pasteboardName)
, m_changeCount(platformStrategies()->pasteboardStrategy()->changeCount(m_pasteboardName))
@@ -262,8 +254,14 @@ static void writeFileWrapperAsRTFDAttachment(NSFileWrapper *wrapper, const Strin
// FIXME: Why can we assert this? It doesn't seem like it's guaranteed.
ASSERT(MIMETypeRegistry::isSupportedImageResourceMIMEType(pasteboardImage.resourceMIMEType));

m_changeCount = writeURLForTypes(writableTypesForImage(), m_pasteboardName, pasteboardImage.url);
auto types = writableTypesForImage();
if (pasteboardImage.dataInWebArchiveFormat)
types.append(WebArchivePboardType);

m_changeCount = writeURLForTypes(types, m_pasteboardName, pasteboardImage.url);
m_changeCount = platformStrategies()->pasteboardStrategy()->setBufferForType(SharedBuffer::wrapCFData(imageData).ptr(), NSTIFFPboardType, m_pasteboardName);
if (pasteboardImage.dataInWebArchiveFormat)
m_changeCount = platformStrategies()->pasteboardStrategy()->setBufferForType(pasteboardImage.dataInWebArchiveFormat.get(), WebArchivePboardType, m_pasteboardName);
writeFileWrapperAsRTFDAttachment(fileWrapper(pasteboardImage), m_pasteboardName, m_changeCount);
}

0 comments on commit 3861f7c

Please sign in to comment.