Title: [235375] trunk
Revision
235375
Author
wenson_hs...@apple.com
Date
2018-08-27 09:45:24 -0700 (Mon, 27 Aug 2018)

Log Message

[Attachment Support] [WK2] Images copied from Mail message view paste with the wrong file name in compose
https://bugs.webkit.org/show_bug.cgi?id=188957
<rdar://problem/43737715>

Reviewed by Darin Adler.

Source/WebCore:

Allow the alt attribute of a pasted image element to determine the name of an image attachment, rather than
using the source URL's last path component first. This is because in some clients, such as Mail, the source of
the image element is some nondescript UUID, and the alt text contains the real name of the image.

Test: WKAttachmentTests.PasteWebArchiveContainingImages

* editing/cocoa/WebContentReaderCocoa.mm:
(WebCore::replaceRichContentWithAttachments):

Tools:

Add a new API test to verify that pasting a web archive containing several image elements with alt attributes
generates _WKAttachments whose names reflect those alt attributes.

* TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:
(testGIFFileURL):
(testGIFData):
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (235374 => 235375)


--- trunk/Source/WebCore/ChangeLog	2018-08-27 16:43:23 UTC (rev 235374)
+++ trunk/Source/WebCore/ChangeLog	2018-08-27 16:45:24 UTC (rev 235375)
@@ -1,3 +1,20 @@
+2018-08-27  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [Attachment Support] [WK2] Images copied from Mail message view paste with the wrong file name in compose
+        https://bugs.webkit.org/show_bug.cgi?id=188957
+        <rdar://problem/43737715>
+
+        Reviewed by Darin Adler.
+
+        Allow the alt attribute of a pasted image element to determine the name of an image attachment, rather than
+        using the source URL's last path component first. This is because in some clients, such as Mail, the source of
+        the image element is some nondescript UUID, and the alt text contains the real name of the image.
+
+        Test: WKAttachmentTests.PasteWebArchiveContainingImages
+
+        * editing/cocoa/WebContentReaderCocoa.mm:
+        (WebCore::replaceRichContentWithAttachments):
+
 2018-08-27  Alex Christensen  <achristen...@webkit.org>
 
         Fix IOSMAC build

Modified: trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm (235374 => 235375)


--- trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm	2018-08-27 16:43:23 UTC (rev 235374)
+++ trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm	2018-08-27 16:45:24 UTC (rev 235375)
@@ -286,8 +286,10 @@
         if (resource == urlToResourceMap.end())
             continue;
 
-        auto name = URLParser { resourceURLString }.result().lastPathComponent();
+        auto name = image.attributeWithoutSynchronization(HTMLNames::altAttr);
         if (name.isEmpty())
+            name = URLParser { resourceURLString }.result().lastPathComponent();
+        if (name.isEmpty())
             name = AtomicString("media");
 
         attachmentInsertionInfo.append({ name, resource->value->mimeType(), resource->value->data(), image });

Modified: trunk/Tools/ChangeLog (235374 => 235375)


--- trunk/Tools/ChangeLog	2018-08-27 16:43:23 UTC (rev 235374)
+++ trunk/Tools/ChangeLog	2018-08-27 16:45:24 UTC (rev 235375)
@@ -1,3 +1,19 @@
+2018-08-27  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [Attachment Support] [WK2] Images copied from Mail message view paste with the wrong file name in compose
+        https://bugs.webkit.org/show_bug.cgi?id=188957
+        <rdar://problem/43737715>
+
+        Reviewed by Darin Adler.
+
+        Add a new API test to verify that pasting a web archive containing several image elements with alt attributes
+        generates _WKAttachments whose names reflect those alt attributes.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:
+        (testGIFFileURL):
+        (testGIFData):
+        (TestWebKitAPI::TEST):
+
 2018-08-27  Alex Christensen  <achristen...@webkit.org>
 
         Unreviewed, rolling out r235367.

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm (235374 => 235375)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm	2018-08-27 16:43:23 UTC (rev 235374)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm	2018-08-27 16:45:24 UTC (rev 235375)
@@ -32,7 +32,7 @@
 #import "WKWebViewConfigurationExtras.h"
 #import <WebKit/WKPreferencesRefPrivate.h>
 #import <WebKit/WKWebViewPrivate.h>
-#import <WebKit/WebKit.h>
+#import <WebKit/WebArchive.h>
 #import <WebKit/WebKitPrivate.h>
 #import <wtf/RetainPtr.h>
 
@@ -193,6 +193,16 @@
     return [NSData dataWithContentsOfURL:testImageFileURL()];
 }
 
+static NSURL *testGIFFileURL()
+{
+    return [[NSBundle mainBundle] URLForResource:@"apple" withExtension:@"gif" subdirectory:@"TestWebKitAPI.resources"];
+}
+
+static NSData *testGIFData()
+{
+    return [NSData dataWithContentsOfURL:testGIFFileURL()];
+}
+
 static NSURL *testPDFFileURL()
 {
     return [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf" subdirectory:@"TestWebKitAPI.resources"];
@@ -1202,6 +1212,46 @@
     [simulator endDataTransfer];
 }
 
+TEST(WKAttachmentTests, PasteWebArchiveContainingImages)
+{
+    NSData *markupData = [@"<img src='' alt='foo'><div><br></div><img src='' alt='bar'>" dataUsingEncoding:NSUTF8StringEncoding];
+
+    auto mainResource = adoptNS([[WebResource alloc] initWithData:markupData URL:[NSURL URLWithString:@"foo.html"] MIMEType:@"text/html" textEncodingName:@"utf-8" frameName:nil]);
+    auto pngResource = adoptNS([[WebResource alloc] initWithData:testImageData() URL:[NSURL URLWithString:@"1.png"] MIMEType:@"image/png" textEncodingName:nil frameName:nil]);
+    auto gifResource = adoptNS([[WebResource alloc] initWithData:testGIFData() URL:[NSURL URLWithString:@"2.gif"] MIMEType:@"image/gif" textEncodingName:nil frameName:nil]);
+    auto archive = adoptNS([[WebArchive alloc] initWithMainResource:(__bridge WebResource *)mainResource.get() subresources:@[ (__bridge WebResource *)pngResource.get(), (__bridge WebResource *)gifResource.get() ] subframeArchives:@[ ]]);
+
+#if PLATFORM(MAC)
+    NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
+    [pasteboard declareTypes:@[WebArchivePboardType] owner:nil];
+    [pasteboard setData:[archive data] forType:WebArchivePboardType];
+#else
+    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
+    [pasteboard setData:[archive data] forPasteboardType:WebArchivePboardType];
+#endif
+
+    RetainPtr<_WKAttachment> gifAttachment;
+    RetainPtr<_WKAttachment> pngAttachment;
+    auto webView = webViewForTestingAttachments();
+
+    ObserveAttachmentUpdatesForScope observer((__bridge TestWKWebView *)webView.get());
+    [webView _synchronouslyExecuteEditCommand:@"Paste" argument:nil];
+    [webView expectElementCount:2 tagName:@"IMG"];
+
+    for (_WKAttachment *attachment in observer.observer().inserted) {
+        if ([attachment.info.contentType isEqualToString:@"image/png"])
+            pngAttachment = attachment;
+        else if ([attachment.info.contentType isEqualToString:@"image/gif"])
+            gifAttachment = attachment;
+    }
+
+    EXPECT_WK_STREQ("foo", [pngAttachment info].name);
+    EXPECT_WK_STREQ("bar", [gifAttachment info].name);
+    [pngAttachment expectRequestedDataToBe:testImageData()];
+    [gifAttachment expectRequestedDataToBe:testGIFData()];
+    observer.expectAttachmentUpdates(@[ ], @[ (__bridge _WKAttachment *)pngAttachment.get(), (__bridge _WKAttachment *)gifAttachment.get() ]);
+}
+
 #pragma mark - Platform-specific tests
 
 #if PLATFORM(MAC)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to