Title: [245637] trunk
Revision
245637
Author
[email protected]
Date
2019-05-22 12:13:34 -0700 (Wed, 22 May 2019)

Log Message

[Paste] Add support for preferred presentation size when pasting an image
https://bugs.webkit.org/show_bug.cgi?id=198132
<rdar://problem/50886917>

Reviewed by Wenson Hsieh.

Source/WebCore:

Set the pasted <img> width/height attributes when the preferred presentation size is available.

* editing/WebContentReader.h:
* editing/cocoa/WebContentReaderCocoa.mm:
(WebCore::createFragmentForImageAttachment):
(WebCore::WebContentReader::readImage):
* editing/markup.cpp:
(WebCore::createFragmentForImageAndURL):
* editing/markup.h:
* platform/Pasteboard.h:
(WebCore::PasteboardWebContentReader::readImage):
* platform/PasteboardItemInfo.h:
(WebCore::PasteboardItemInfo::encode const):
(WebCore::PasteboardItemInfo::decode):
* platform/ios/PasteboardIOS.mm:
(WebCore::Pasteboard::readPasteboardWebContentDataForType):
(WebCore::Pasteboard::read):
(WebCore::Pasteboard::readRespectingUTIFidelities):
* platform/ios/PlatformPasteboardIOS.mm:
(WebCore::PlatformPasteboard::informationForItemAtIndex):

Source/WebKit:

* UIProcess/API/Cocoa/_WKElementAction.mm:
(addToReadingList):
* UIProcess/ios/WKActionSheetAssistant.mm:
(-[WKActionSheetAssistant defaultActionsForLinkSheet:]):
(-[WKActionSheetAssistant defaultActionsForImageSheet:]):

Tools:

* TestWebKitAPI/Tests/ios/UIPasteboardTests.mm:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (245636 => 245637)


--- trunk/Source/WebCore/ChangeLog	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebCore/ChangeLog	2019-05-22 19:13:34 UTC (rev 245637)
@@ -1,3 +1,32 @@
+2019-05-22  Zalan Bujtas  <[email protected]>
+
+        [Paste] Add support for preferred presentation size when pasting an image
+        https://bugs.webkit.org/show_bug.cgi?id=198132
+        <rdar://problem/50886917>
+
+        Reviewed by Wenson Hsieh.
+
+        Set the pasted <img> width/height attributes when the preferred presentation size is available.
+
+        * editing/WebContentReader.h:
+        * editing/cocoa/WebContentReaderCocoa.mm:
+        (WebCore::createFragmentForImageAttachment):
+        (WebCore::WebContentReader::readImage):
+        * editing/markup.cpp:
+        (WebCore::createFragmentForImageAndURL):
+        * editing/markup.h:
+        * platform/Pasteboard.h:
+        (WebCore::PasteboardWebContentReader::readImage):
+        * platform/PasteboardItemInfo.h:
+        (WebCore::PasteboardItemInfo::encode const):
+        (WebCore::PasteboardItemInfo::decode):
+        * platform/ios/PasteboardIOS.mm:
+        (WebCore::Pasteboard::readPasteboardWebContentDataForType):
+        (WebCore::Pasteboard::read):
+        (WebCore::Pasteboard::readRespectingUTIFidelities):
+        * platform/ios/PlatformPasteboardIOS.mm:
+        (WebCore::PlatformPasteboard::informationForItemAtIndex):
+
 2019-05-22  Jer Noble  <[email protected]>
 
         Hide MediaCapabilities.encodingInfo() when the platform does not support it.

Modified: trunk/Source/WebCore/editing/WebContentReader.h (245636 => 245637)


--- trunk/Source/WebCore/editing/WebContentReader.h	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebCore/editing/WebContentReader.h	2019-05-22 19:13:34 UTC (rev 245637)
@@ -75,7 +75,7 @@
     bool readHTML(const String&) override;
     bool readRTFD(SharedBuffer&) override;
     bool readRTF(SharedBuffer&) override;
-    bool readImage(Ref<SharedBuffer>&&, const String& type) override;
+    bool readImage(Ref<SharedBuffer>&&, const String& type, Optional<FloatSize> preferredPresentationSize = { }) override;
     bool readURL(const URL&, const String& title) override;
     bool readDataBuffer(SharedBuffer&, const String& type, const String& name) override;
 #endif
@@ -98,7 +98,7 @@
     bool readHTML(const String&) override;
     bool readRTFD(SharedBuffer&) override;
     bool readRTF(SharedBuffer&) override;
-    bool readImage(Ref<SharedBuffer>&&, const String&) override { return false; }
+    bool readImage(Ref<SharedBuffer>&&, const String&, Optional<FloatSize> = { }) override { return false; }
     bool readURL(const URL&, const String&) override { return false; }
     bool readDataBuffer(SharedBuffer&, const String&, const String&) override { return false; }
 #endif

Modified: trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm (245636 => 245637)


--- trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm	2019-05-22 19:13:34 UTC (rev 245637)
@@ -247,7 +247,7 @@
 
 #endif
 
-static Ref<DocumentFragment> createFragmentForImageAttachment(Frame& frame, Document& document, Ref<SharedBuffer>&& buffer, const String& contentType)
+static Ref<DocumentFragment> createFragmentForImageAttachment(Frame& frame, Document& document, Ref<SharedBuffer>&& buffer, const String& contentType, Optional<FloatSize> preferredSize)
 {
 #if ENABLE(ATTACHMENT_ELEMENT)
     auto attachment = HTMLAttachmentElement::create(HTMLNames::attachmentTag, document);
@@ -261,6 +261,10 @@
             auto image = HTMLImageElement::create(document);
             image->setAttributeWithoutSynchronization(HTMLNames::srcAttr, DOMURL::createObjectURL(document, Blob::create(buffer.get(), contentType)));
             image->setAttachmentElement(WTFMove(attachment));
+            if (preferredSize) {
+                image->setAttributeWithoutSynchronization(HTMLNames::widthAttr, AtomicString::number(preferredSize->width()));
+                image->setAttributeWithoutSynchronization(HTMLNames::heightAttr, AtomicString::number(preferredSize->height()));
+            }
             fragment->appendChild(WTFMove(image));
         } else {
             attachment->updateAttributes(buffer->size(), contentType, defaultImageAttachmentName);
@@ -677,14 +681,14 @@
     return true;
 }
 
-bool WebContentReader::readImage(Ref<SharedBuffer>&& buffer, const String& type)
+bool WebContentReader::readImage(Ref<SharedBuffer>&& buffer, const String& type, Optional<FloatSize> preferredPresentationSize)
 {
     ASSERT(frame.document());
     auto& document = *frame.document();
     if (shouldReplaceRichContentWithAttachments())
-        addFragment(createFragmentForImageAttachment(frame, document, WTFMove(buffer), type));
+        addFragment(createFragmentForImageAttachment(frame, document, WTFMove(buffer), type, preferredPresentationSize));
     else
-        addFragment(createFragmentForImageAndURL(document, DOMURL::createObjectURL(document, Blob::create(buffer.get(), type))));
+        addFragment(createFragmentForImageAndURL(document, DOMURL::createObjectURL(document, Blob::create(buffer.get(), type)), preferredPresentationSize));
 
     return fragment;
 }

Modified: trunk/Source/WebCore/editing/gtk/EditorGtk.cpp (245636 => 245637)


--- trunk/Source/WebCore/editing/gtk/EditorGtk.cpp	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebCore/editing/gtk/EditorGtk.cpp	2019-05-22 19:13:34 UTC (rev 245637)
@@ -69,7 +69,7 @@
             auto blob = Blob::create(WTFMove(buffer), "image/png");
             if (!frame.document())
                 return nullptr;
-            return createFragmentForImageAndURL(*frame.document(), DOMURL::createObjectURL(*frame.document(), blob));
+            return createFragmentForImageAndURL(*frame.document(), DOMURL::createObjectURL(*frame.document(), blob), { });
         }
     }
 

Modified: trunk/Source/WebCore/editing/markup.cpp (245636 => 245637)


--- trunk/Source/WebCore/editing/markup.cpp	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebCore/editing/markup.cpp	2019-05-22 19:13:34 UTC (rev 245637)
@@ -1227,10 +1227,14 @@
     return fragment;
 }
 
-Ref<DocumentFragment> createFragmentForImageAndURL(Document& document, const String& url)
+Ref<DocumentFragment> createFragmentForImageAndURL(Document& document, const String& url, Optional<FloatSize> preferredSize)
 {
     auto imageElement = HTMLImageElement::create(document);
     imageElement->setAttributeWithoutSynchronization(HTMLNames::srcAttr, url);
+    if (preferredSize) {
+        imageElement->setAttributeWithoutSynchronization(HTMLNames::widthAttr, AtomicString::number(preferredSize->width()));
+        imageElement->setAttributeWithoutSynchronization(HTMLNames::heightAttr, AtomicString::number(preferredSize->height()));
+    }
 
     auto fragment = document.createDocumentFragment();
     fragment->appendChild(imageElement);

Modified: trunk/Source/WebCore/editing/markup.h (245636 => 245637)


--- trunk/Source/WebCore/editing/markup.h	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebCore/editing/markup.h	2019-05-22 19:13:34 UTC (rev 245637)
@@ -59,7 +59,7 @@
 WEBCORE_EXPORT Ref<DocumentFragment> createFragmentFromMarkup(Document&, const String& markup, const String& baseURL, ParserContentPolicy = AllowScriptingContent);
 ExceptionOr<Ref<DocumentFragment>> createFragmentForInnerOuterHTML(Element&, const String& markup, ParserContentPolicy);
 RefPtr<DocumentFragment> createFragmentForTransformToFragment(Document&, const String& sourceString, const String& sourceMIMEType);
-Ref<DocumentFragment> createFragmentForImageAndURL(Document&, const String&);
+Ref<DocumentFragment> createFragmentForImageAndURL(Document&, const String&, Optional<FloatSize> preferredSize);
 ExceptionOr<Ref<DocumentFragment>> createContextualFragment(Element&, const String& markup, ParserContentPolicy);
 
 bool isPlainTextMarkup(Node*);

Modified: trunk/Source/WebCore/platform/Pasteboard.h (245636 => 245637)


--- trunk/Source/WebCore/platform/Pasteboard.h	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebCore/platform/Pasteboard.h	2019-05-22 19:13:34 UTC (rev 245637)
@@ -140,7 +140,7 @@
     virtual bool readHTML(const String&) = 0;
     virtual bool readRTFD(SharedBuffer&) = 0;
     virtual bool readRTF(SharedBuffer&) = 0;
-    virtual bool readImage(Ref<SharedBuffer>&&, const String& type) = 0;
+    virtual bool readImage(Ref<SharedBuffer>&&, const String& type, Optional<FloatSize> preferredPresentationSize = { }) = 0;
     virtual bool readURL(const URL&, const String& title) = 0;
     virtual bool readDataBuffer(SharedBuffer&, const String& type, const String& name) = 0;
 #endif
@@ -288,7 +288,7 @@
         DidNotReadType,
         PasteboardWasChangedExternally
     };
-    ReaderResult readPasteboardWebContentDataForType(PasteboardWebContentReader&, PasteboardStrategy&, NSString *type, int itemIndex);
+    ReaderResult readPasteboardWebContentDataForType(PasteboardWebContentReader&, PasteboardStrategy&, NSString *type, const PasteboardItemInfo&, int itemIndex);
 #endif
 
 #if PLATFORM(WIN)

Modified: trunk/Source/WebCore/platform/PasteboardItemInfo.h (245636 => 245637)


--- trunk/Source/WebCore/platform/PasteboardItemInfo.h	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebCore/platform/PasteboardItemInfo.h	2019-05-22 19:13:34 UTC (rev 245637)
@@ -42,6 +42,7 @@
     Vector<String> contentTypesForFileUpload;
     Vector<String> contentTypesByFidelity;
     String suggestedFileName;
+    Optional<FloatSize> preferredPresentationSize;
     bool isNonTextType { false };
     bool containsFileURLAndFileUploadContent { false };
     PasteboardItemPresentationStyle preferredPresentationStyle { PasteboardItemPresentationStyle::Unspecified };
@@ -97,7 +98,7 @@
 template<class Encoder>
 void PasteboardItemInfo::encode(Encoder& encoder) const
 {
-    encoder << pathsForFileUpload << contentTypesForFileUpload << contentTypesByFidelity << suggestedFileName << isNonTextType << containsFileURLAndFileUploadContent;
+    encoder << pathsForFileUpload << contentTypesForFileUpload << contentTypesByFidelity << suggestedFileName << preferredPresentationSize << isNonTextType << containsFileURLAndFileUploadContent;
     encoder.encodeEnum(preferredPresentationStyle);
 }
 
@@ -117,6 +118,9 @@
     if (!decoder.decode(result.suggestedFileName))
         return WTF::nullopt;
 
+    if (!decoder.decode(result.preferredPresentationSize))
+        return WTF::nullopt;
+
     if (!decoder.decode(result.isNonTextType))
         return WTF::nullopt;
 

Modified: trunk/Source/WebCore/platform/ios/PasteboardIOS.mm (245636 => 245637)


--- trunk/Source/WebCore/platform/ios/PasteboardIOS.mm	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebCore/platform/ios/PasteboardIOS.mm	2019-05-22 19:13:34 UTC (rev 245637)
@@ -181,7 +181,7 @@
         || [type isEqualToString:(__bridge NSString *)kUTTypeFlatRTFD];
 }
 
-Pasteboard::ReaderResult Pasteboard::readPasteboardWebContentDataForType(PasteboardWebContentReader& reader, PasteboardStrategy& strategy, NSString *type, int itemIndex)
+Pasteboard::ReaderResult Pasteboard::readPasteboardWebContentDataForType(PasteboardWebContentReader& reader, PasteboardStrategy& strategy, NSString *type, const PasteboardItemInfo& itemInfo, int itemIndex)
 {
     if ([type isEqualToString:WebArchivePboardType] || [type isEqualToString:(__bridge NSString *)kUTTypeWebArchive]) {
         auto buffer = strategy.readBufferFromPasteboard(itemIndex, type, m_pasteboardName);
@@ -225,7 +225,7 @@
         RefPtr<SharedBuffer> buffer = strategy.readBufferFromPasteboard(itemIndex, type, m_pasteboardName);
         if (m_changeCount != changeCount())
             return ReaderResult::PasteboardWasChangedExternally;
-        return buffer && reader.readImage(buffer.releaseNonNull(), type) ? ReaderResult::ReadType : ReaderResult::DidNotReadType;
+        return buffer && reader.readImage(buffer.releaseNonNull(), type, itemInfo.preferredPresentationSize) ? ReaderResult::ReadType : ReaderResult::DidNotReadType;
     }
 
     if ([type isEqualToString:(__bridge NSString *)kUTTypeURL]) {
@@ -318,7 +318,7 @@
             if (!isTypeAllowedByReadingPolicy(type, policy))
                 continue;
 
-            auto itemResult = readPasteboardWebContentDataForType(reader, strategy, type, i);
+            auto itemResult = readPasteboardWebContentDataForType(reader, strategy, type, info, i);
             if (itemResult == ReaderResult::PasteboardWasChangedExternally)
                 return;
 
@@ -358,7 +358,7 @@
             if (!isTypeAllowedByReadingPolicy(type, policy))
                 continue;
 
-            result = readPasteboardWebContentDataForType(reader, strategy, type, index);
+            result = readPasteboardWebContentDataForType(reader, strategy, type, info, index);
             if (result == ReaderResult::PasteboardWasChangedExternally)
                 return;
             if (result == ReaderResult::ReadType)

Modified: trunk/Source/WebCore/platform/ios/PlatformPasteboardIOS.mm (245636 => 245637)


--- trunk/Source/WebCore/platform/ios/PlatformPasteboardIOS.mm	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebCore/platform/ios/PlatformPasteboardIOS.mm	2019-05-22 19:13:34 UTC (rev 245637)
@@ -155,6 +155,8 @@
 #if PASTEBOARD_SUPPORTS_PRESENTATION_STYLE_AND_TEAM_DATA
     info.preferredPresentationStyle = pasteboardItemPresentationStyle(itemProvider.preferredPresentationStyle);
 #endif
+    if (!CGSizeEqualToSize(itemProvider.preferredPresentationSize, CGSizeZero))
+        info.preferredPresentationSize = FloatSize { itemProvider.preferredPresentationSize };
     info.containsFileURLAndFileUploadContent = itemProvider.web_containsFileURLAndFileUploadContent;
     info.suggestedFileName = itemProvider.suggestedName;
     NSArray<NSString *> *registeredTypeIdentifiers = itemProvider.registeredTypeIdentifiers;

Modified: trunk/Source/WebKit/ChangeLog (245636 => 245637)


--- trunk/Source/WebKit/ChangeLog	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Source/WebKit/ChangeLog	2019-05-22 19:13:34 UTC (rev 245637)
@@ -1,3 +1,17 @@
+2019-05-22  Zalan Bujtas  <[email protected]>
+
+        [Paste] Add support for preferred presentation size when pasting an image
+        https://bugs.webkit.org/show_bug.cgi?id=198132
+        <rdar://problem/50886917>
+
+        Reviewed by Wenson Hsieh.
+
+        * UIProcess/API/Cocoa/_WKElementAction.mm:
+        (addToReadingList):
+        * UIProcess/ios/WKActionSheetAssistant.mm:
+        (-[WKActionSheetAssistant defaultActionsForLinkSheet:]):
+        (-[WKActionSheetAssistant defaultActionsForImageSheet:]):
+
 2019-05-22  Youenn Fablet  <[email protected]>
 
         Implement Feature policy self/none/* parsing

Modified: trunk/Tools/ChangeLog (245636 => 245637)


--- trunk/Tools/ChangeLog	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Tools/ChangeLog	2019-05-22 19:13:34 UTC (rev 245637)
@@ -1,3 +1,14 @@
+2019-05-22  Zalan Bujtas  <[email protected]>
+
+        [Paste] Add support for preferred presentation size when pasting an image
+        https://bugs.webkit.org/show_bug.cgi?id=198132
+        <rdar://problem/50886917>
+
+        Reviewed by Wenson Hsieh.
+
+        * TestWebKitAPI/Tests/ios/UIPasteboardTests.mm:
+        (TestWebKitAPI::TEST):
+
 2019-05-22  Keith Rollin  <[email protected]>
 
         Use a different variable when testing for Xcode context in generate-xcfilelists

Modified: trunk/Tools/TestWebKitAPI/Tests/ios/UIPasteboardTests.mm (245636 => 245637)


--- trunk/Tools/TestWebKitAPI/Tests/ios/UIPasteboardTests.mm	2019-05-22 19:10:05 UTC (rev 245636)
+++ trunk/Tools/TestWebKitAPI/Tests/ios/UIPasteboardTests.mm	2019-05-22 19:13:34 UTC (rev 245637)
@@ -302,6 +302,52 @@
     EXPECT_WK_STREQ("https://www.apple.com/", [webView stringByEvaluatingJavaScript:@"textData.textContent"]);
 }
 
+TEST(UIPasteboardTests, ValidPreferredPresentationSizeForImage)
+{
+    auto webView = setUpWebViewForPasteboardTests(@"autofocus-contenteditable");
+    auto itemProvider = adoptNS([[NSItemProvider alloc] init]);
+    [itemProvider setPreferredPresentationSize:CGSizeMake(10, 20)];
+    [itemProvider registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypePNG visibility:NSItemProviderRepresentationVisibilityAll loadHandler:[] (DataLoadCompletionBlock completionHandler) -> NSProgress * {
+        completionHandler([NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"icon" withExtension:@"png" subdirectory:@"TestWebKitAPI.resources"]], nil);
+        return nil;
+    }];
+    [UIPasteboard generalPasteboard].itemProviders = @[ itemProvider.get() ];
+    [webView paste:nil];
+
+    EXPECT_WK_STREQ("10", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').width"]);
+    EXPECT_WK_STREQ("20", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').height"]);
+}
+
+TEST(UIPasteboardTests, InvalidPreferredPresentationSizeForImage)
+{
+    auto webView = setUpWebViewForPasteboardTests(@"autofocus-contenteditable");
+    auto itemProvider = adoptNS([[NSItemProvider alloc] init]);
+    [itemProvider setPreferredPresentationSize:CGSizeMake(-10, -20)];
+    [itemProvider registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypePNG visibility:NSItemProviderRepresentationVisibilityAll loadHandler:[] (DataLoadCompletionBlock completionHandler) -> NSProgress * {
+        completionHandler([NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"icon" withExtension:@"png" subdirectory:@"TestWebKitAPI.resources"]], nil);
+        return nil;
+    }];
+    [UIPasteboard generalPasteboard].itemProviders = @[ itemProvider.get() ];
+    [webView paste:nil];
+
+    EXPECT_WK_STREQ("0", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').width"]);
+    EXPECT_WK_STREQ("174", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').height"]);
+}
+
+TEST(UIPasteboardTests, MissingPreferredPresentationSizeForImage)
+{
+    auto webView = setUpWebViewForPasteboardTests(@"autofocus-contenteditable");
+    auto itemProvider = adoptNS([[NSItemProvider alloc] init]);
+    [itemProvider registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypePNG visibility:NSItemProviderRepresentationVisibilityAll loadHandler:[] (DataLoadCompletionBlock completionHandler) -> NSProgress * {
+        completionHandler([NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"icon" withExtension:@"png" subdirectory:@"TestWebKitAPI.resources"]], nil);
+        return nil;
+    }];
+    [UIPasteboard generalPasteboard].itemProviders = @[ itemProvider.get() ];
+    [webView paste:nil];
+
+    EXPECT_WK_STREQ("0", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').width"]);
+    EXPECT_WK_STREQ("174", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').height"]);
+}
 #endif // PLATFORM(IOS)
 
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to