Title: [291261] trunk
Revision
291261
Author
[email protected]
Date
2022-03-14 18:55:13 -0700 (Mon, 14 Mar 2022)

Log Message

[Mail compose] Allow copied resources with remote (HTTP/HTTPS) URLs to be pasted as attachments
https://bugs.webkit.org/show_bug.cgi?id=237810
rdar://90119983

Reviewed by Devin Rousso.

Source/WebCore:

When creating attachments by pasting subresources with remote URLs, Mail compose in shipping macOS inserts such
subresources as attachments, which are sent as MIME parts alongside the rest of the Mail message. However, when
using the WebKit2 client-side attachment API, we currently leave remote (i.e. HTTP-family) URLs as-is, which
means they don't end up being sent as attachments.

I implemented this current behavior in r226340; back then, we opted to preserve the HTTP/HTTPS URL in pasted
resources over falling back to blob URL conversion. However, in order to match legacy Mail compose behavior, we
should've handled it by propagating the data as attachments to the client layer instead.

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

Tools:

Add an API test to exercise copying remote images and pasting as attachment-backed images. This API test first
loads a web page (simulating page load via HTTPS, along with multiple image subresources); it then pastes into
an attachment-enabled WKWebView-based editor and verifies that the _WKAttachment UI delegate hooks are invoked
with attachments corresponding to each of the remote images.

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

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (291260 => 291261)


--- trunk/Source/WebCore/ChangeLog	2022-03-15 01:48:15 UTC (rev 291260)
+++ trunk/Source/WebCore/ChangeLog	2022-03-15 01:55:13 UTC (rev 291261)
@@ -1,3 +1,23 @@
+2022-03-14  Wenson Hsieh  <[email protected]>
+
+        [Mail compose] Allow copied resources with remote (HTTP/HTTPS) URLs to be pasted as attachments
+        https://bugs.webkit.org/show_bug.cgi?id=237810
+        rdar://90119983
+
+        Reviewed by Devin Rousso.
+
+        When creating attachments by pasting subresources with remote URLs, Mail compose in shipping macOS inserts such
+        subresources as attachments, which are sent as MIME parts alongside the rest of the Mail message. However, when
+        using the WebKit2 client-side attachment API, we currently leave remote (i.e. HTTP-family) URLs as-is, which
+        means they don't end up being sent as attachments.
+
+        I implemented this current behavior in r226340; back then, we opted to preserve the HTTP/HTTPS URL in pasted
+        resources over falling back to blob URL conversion. However, in order to match legacy Mail compose behavior, we
+        should've handled it by propagating the data as attachments to the client layer instead.
+
+        * editing/cocoa/WebContentReaderCocoa.mm:
+        (WebCore::shouldReplaceSubresourceURL):
+
 2022-03-14  Oriol Brufau  <[email protected]>
 
         [css-cascade] Fix 'revert' on low-priority properties

Modified: trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm (291260 => 291261)


--- trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm	2022-03-15 01:48:15 UTC (rev 291260)
+++ trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm	2022-03-15 01:55:13 UTC (rev 291261)
@@ -200,10 +200,16 @@
     bool m_didDisableImage { false };
 };
 
+enum class ReplacementMethod : bool { BlobURL, Attachment };
+static bool shouldReplaceSubresourceURL(const URL& url, ReplacementMethod replacementType = ReplacementMethod::BlobURL)
+{
+    if (url.protocolIsData())
+        return false;
 
-static bool shouldReplaceSubresourceURL(const URL& url)
-{
-    return !(url.protocolIsInHTTPFamily() || url.protocolIsData());
+    if (url.protocolIsInHTTPFamily())
+        return replacementType == ReplacementMethod::Attachment;
+
+    return true;
 }
 
 static bool shouldReplaceRichContentWithAttachments()
@@ -297,7 +303,7 @@
     HashMap<AtomString, Ref<ArchiveResource>> urlToResourceMap;
     for (auto& subresource : subresources) {
         auto& url = ""
-        if (shouldReplaceSubresourceURL(url))
+        if (shouldReplaceSubresourceURL(url, ReplacementMethod::Attachment))
             urlToResourceMap.set(url.string(), subresource.copyRef());
     }
 

Modified: trunk/Tools/ChangeLog (291260 => 291261)


--- trunk/Tools/ChangeLog	2022-03-15 01:48:15 UTC (rev 291260)
+++ trunk/Tools/ChangeLog	2022-03-15 01:55:13 UTC (rev 291261)
@@ -1,5 +1,21 @@
 2022-03-14  Wenson Hsieh  <[email protected]>
 
+        [Mail compose] Allow copied resources with remote (HTTP/HTTPS) URLs to be pasted as attachments
+        https://bugs.webkit.org/show_bug.cgi?id=237810
+        rdar://90119983
+
+        Reviewed by Devin Rousso.
+
+        Add an API test to exercise copying remote images and pasting as attachment-backed images. This API test first
+        loads a web page (simulating page load via HTTPS, along with multiple image subresources); it then pastes into
+        an attachment-enabled WKWebView-based editor and verifies that the _WKAttachment UI delegate hooks are invoked
+        with attachments corresponding to each of the remote images.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:
+        (TestWebKitAPI::TEST):
+
+2022-03-14  Wenson Hsieh  <[email protected]>
+
         [Mail Compose] Spelling and grammar context menu items need custom identifiers
         https://bugs.webkit.org/show_bug.cgi?id=237804
         rdar://90124049

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm (291260 => 291261)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm	2022-03-15 01:48:15 UTC (rev 291260)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm	2022-03-15 01:55:13 UTC (rev 291261)
@@ -31,6 +31,7 @@
 #import "NSItemProviderAdditions.h"
 #import "PlatformUtilities.h"
 #import "TestNavigationDelegate.h"
+#import "TestProtocol.h"
 #import "TestWKWebView.h"
 #import "WKWebViewConfigurationExtras.h"
 #import <Contacts/Contacts.h>
@@ -70,6 +71,12 @@
 
 #define USES_MODERN_ATTRIBUTED_STRING_CONVERSION (!PLATFORM(WATCHOS) && !PLATFORM(APPLETV))
 
+#if USE(APPKIT)
+using CocoaPasteboard = NSPasteboard;
+#else
+using CocoaPasteboard = UIPasteboard;
+#endif
+
 @interface AttachmentUpdateObserver : NSObject <WKUIDelegatePrivate>
 @property (nonatomic, readonly) NSArray *inserted;
 @property (nonatomic, readonly) NSArray *removed;
@@ -1670,6 +1677,58 @@
     }
 }
 
+TEST(WKAttachmentTests, CopyAndPasteRemoteImages)
+{
+    {
+        [TestProtocol registerWithScheme:@"https"];
+
+        auto navigationDelegate = adoptNS([[TestNavigationDelegate alloc] init]);
+        auto sourceView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
+        [sourceView setNavigationDelegate:navigationDelegate.get()];
+        [sourceView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://bundle-file/multiple-images.html"]]];
+        [navigationDelegate waitForDidFinishNavigation];
+
+        auto pasteboard = [CocoaPasteboard generalPasteboard];
+        auto changeCountBeforeCopying = pasteboard.changeCount;
+
+        [sourceView selectAll:nil];
+        [sourceView _synchronouslyExecuteEditCommand:@"Copy" argument:nil];
+
+        Util::waitForConditionWithLogging([changeCountBeforeCopying, pasteboard] {
+            return changeCountBeforeCopying != pasteboard.changeCount;
+        }, 2, @"Expected %@ to change.", pasteboard);
+    }
+    {
+        auto webView = webViewForTestingAttachments();
+        ObserveAttachmentUpdatesForScope observer(webView.get());
+        [webView _synchronouslyExecuteEditCommand:@"Paste" argument:nil];
+        [webView expectElementCount:5 querySelector:@"IMG"];
+        EXPECT_EQ(5U, observer.observer().inserted.count);
+        Vector attachmentInfo { {
+            RetainPtr { [observer.observer().inserted[0] info] },
+            RetainPtr { [observer.observer().inserted[1] info] },
+            RetainPtr { [observer.observer().inserted[2] info] },
+            RetainPtr { [observer.observer().inserted[3] info] },
+            RetainPtr { [observer.observer().inserted[4] info] },
+        } };
+
+        std::sort(attachmentInfo.begin(), attachmentInfo.end(), [] (auto& a, auto& b) {
+            return [[a name] compare:[b name]] == NSOrderedAscending;
+        });
+
+        EXPECT_WK_STREQ("400x400-green.png", [attachmentInfo[0] name]);
+        EXPECT_WK_STREQ("image/png", [attachmentInfo[0] contentType]);
+        EXPECT_WK_STREQ("large-red-square.png", [attachmentInfo[1] name]);
+        EXPECT_WK_STREQ("image/png", [attachmentInfo[1] contentType]);
+        EXPECT_WK_STREQ("sunset-in-cupertino-100px.tiff", [attachmentInfo[2] name]);
+        EXPECT_WK_STREQ("image/tiff", [attachmentInfo[2] contentType]);
+        EXPECT_WK_STREQ("sunset-in-cupertino-200px.png", [attachmentInfo[3] name]);
+        EXPECT_WK_STREQ("image/png", [attachmentInfo[3] contentType]);
+        EXPECT_WK_STREQ("test.jpg", [attachmentInfo[4] name]);
+        EXPECT_WK_STREQ("image/jpeg", [attachmentInfo[4] contentType]);
+    }
+}
+
 #pragma mark - Platform-specific tests
 
 #if PLATFORM(MAC)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to