⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 245637 in webkit


Ignore:
Timestamp:
May 22, 2019, 12:13:34 PM (7 years ago)
Author:
Alan Bujtas
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):

Location:
trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r245636 r245637  
     12019-05-22  Zalan Bujtas  <zalan@apple.com>
     2
     3        [Paste] Add support for preferred presentation size when pasting an image
     4        https://bugs.webkit.org/show_bug.cgi?id=198132
     5        <rdar://problem/50886917>
     6
     7        Reviewed by Wenson Hsieh.
     8
     9        Set the pasted <img> width/height attributes when the preferred presentation size is available.
     10
     11        * editing/WebContentReader.h:
     12        * editing/cocoa/WebContentReaderCocoa.mm:
     13        (WebCore::createFragmentForImageAttachment):
     14        (WebCore::WebContentReader::readImage):
     15        * editing/markup.cpp:
     16        (WebCore::createFragmentForImageAndURL):
     17        * editing/markup.h:
     18        * platform/Pasteboard.h:
     19        (WebCore::PasteboardWebContentReader::readImage):
     20        * platform/PasteboardItemInfo.h:
     21        (WebCore::PasteboardItemInfo::encode const):
     22        (WebCore::PasteboardItemInfo::decode):
     23        * platform/ios/PasteboardIOS.mm:
     24        (WebCore::Pasteboard::readPasteboardWebContentDataForType):
     25        (WebCore::Pasteboard::read):
     26        (WebCore::Pasteboard::readRespectingUTIFidelities):
     27        * platform/ios/PlatformPasteboardIOS.mm:
     28        (WebCore::PlatformPasteboard::informationForItemAtIndex):
     29
    1302019-05-22  Jer Noble  <jer.noble@apple.com>
    231
  • trunk/Source/WebCore/editing/WebContentReader.h

    r243695 r245637  
    7676    bool readRTFD(SharedBuffer&) override;
    7777    bool readRTF(SharedBuffer&) override;
    78     bool readImage(Ref<SharedBuffer>&&, const String& type) override;
     78    bool readImage(Ref<SharedBuffer>&&, const String& type, Optional<FloatSize> preferredPresentationSize = { }) override;
    7979    bool readURL(const URL&, const String& title) override;
    8080    bool readDataBuffer(SharedBuffer&, const String& type, const String& name) override;
    … …  
    9999    bool readRTFD(SharedBuffer&) override;
    100100    bool readRTF(SharedBuffer&) override;
    101     bool readImage(Ref<SharedBuffer>&&, const String&) override { return false; }
     101    bool readImage(Ref<SharedBuffer>&&, const String&, Optional<FloatSize> = { }) override { return false; }
    102102    bool readURL(const URL&, const String&) override { return false; }
    103103    bool readDataBuffer(SharedBuffer&, const String&, const String&) override { return false; }
  • trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm

    r245508 r245637  
    248248#endif
    249249
    250 static Ref<DocumentFragment> createFragmentForImageAttachment(Frame& frame, Document& document, Ref<SharedBuffer>&& buffer, const String& contentType)
     250static Ref<DocumentFragment> createFragmentForImageAttachment(Frame& frame, Document& document, Ref<SharedBuffer>&& buffer, const String& contentType, Optional<FloatSize> preferredSize)
    251251{
    252252#if ENABLE(ATTACHMENT_ELEMENT)
    … …  
    262262            image->setAttributeWithoutSynchronization(HTMLNames::srcAttr, DOMURL::createObjectURL(document, Blob::create(buffer.get(), contentType)));
    263263            image->setAttachmentElement(WTFMove(attachment));
     264            if (preferredSize) {
     265                image->setAttributeWithoutSynchronization(HTMLNames::widthAttr, AtomicString::number(preferredSize->width()));
     266                image->setAttributeWithoutSynchronization(HTMLNames::heightAttr, AtomicString::number(preferredSize->height()));
     267            }
    264268            fragment->appendChild(WTFMove(image));
    265269        } else {
    … …  
    678682}
    679683
    680 bool WebContentReader::readImage(Ref<SharedBuffer>&& buffer, const String& type)
     684bool WebContentReader::readImage(Ref<SharedBuffer>&& buffer, const String& type, Optional<FloatSize> preferredPresentationSize)
    681685{
    682686    ASSERT(frame.document());
    683687    auto& document = *frame.document();
    684688    if (shouldReplaceRichContentWithAttachments())
    685         addFragment(createFragmentForImageAttachment(frame, document, WTFMove(buffer), type));
     689        addFragment(createFragmentForImageAttachment(frame, document, WTFMove(buffer), type, preferredPresentationSize));
    686690    else
    687         addFragment(createFragmentForImageAndURL(document, DOMURL::createObjectURL(document, Blob::create(buffer.get(), type))));
     691        addFragment(createFragmentForImageAndURL(document, DOMURL::createObjectURL(document, Blob::create(buffer.get(), type)), preferredPresentationSize));
    688692
    689693    return fragment;
  • trunk/Source/WebCore/editing/gtk/EditorGtk.cpp

    r236785 r245637  
    7070            if (!frame.document())
    7171                return nullptr;
    72             return createFragmentForImageAndURL(*frame.document(), DOMURL::createObjectURL(*frame.document(), blob));
     72            return createFragmentForImageAndURL(*frame.document(), DOMURL::createObjectURL(*frame.document(), blob), { });
    7373        }
    7474    }
  • trunk/Source/WebCore/editing/markup.cpp

    r245508 r245637  
    12281228}
    12291229
    1230 Ref<DocumentFragment> createFragmentForImageAndURL(Document& document, const String& url)
     1230Ref<DocumentFragment> createFragmentForImageAndURL(Document& document, const String& url, Optional<FloatSize> preferredSize)
    12311231{
    12321232    auto imageElement = HTMLImageElement::create(document);
    12331233    imageElement->setAttributeWithoutSynchronization(HTMLNames::srcAttr, url);
     1234    if (preferredSize) {
     1235        imageElement->setAttributeWithoutSynchronization(HTMLNames::widthAttr, AtomicString::number(preferredSize->width()));
     1236        imageElement->setAttributeWithoutSynchronization(HTMLNames::heightAttr, AtomicString::number(preferredSize->height()));
     1237    }
    12341238
    12351239    auto fragment = document.createDocumentFragment();
  • trunk/Source/WebCore/editing/markup.h

    r239427 r245637  
    6060ExceptionOr<Ref<DocumentFragment>> createFragmentForInnerOuterHTML(Element&, const String& markup, ParserContentPolicy);
    6161RefPtr<DocumentFragment> createFragmentForTransformToFragment(Document&, const String& sourceString, const String& sourceMIMEType);
    62 Ref<DocumentFragment> createFragmentForImageAndURL(Document&, const String&);
     62Ref<DocumentFragment> createFragmentForImageAndURL(Document&, const String&, Optional<FloatSize> preferredSize);
    6363ExceptionOr<Ref<DocumentFragment>> createContextualFragment(Element&, const String& markup, ParserContentPolicy);
    6464
  • trunk/Source/WebCore/platform/Pasteboard.h

    r243695 r245637  
    141141    virtual bool readRTFD(SharedBuffer&) = 0;
    142142    virtual bool readRTF(SharedBuffer&) = 0;
    143     virtual bool readImage(Ref<SharedBuffer>&&, const String& type) = 0;
     143    virtual bool readImage(Ref<SharedBuffer>&&, const String& type, Optional<FloatSize> preferredPresentationSize = { }) = 0;
    144144    virtual bool readURL(const URL&, const String& title) = 0;
    145145    virtual bool readDataBuffer(SharedBuffer&, const String& type, const String& name) = 0;
    … …  
    289289        PasteboardWasChangedExternally
    290290    };
    291     ReaderResult readPasteboardWebContentDataForType(PasteboardWebContentReader&, PasteboardStrategy&, NSString *type, int itemIndex);
     291    ReaderResult readPasteboardWebContentDataForType(PasteboardWebContentReader&, PasteboardStrategy&, NSString *type, const PasteboardItemInfo&, int itemIndex);
    292292#endif
    293293
  • trunk/Source/WebCore/platform/PasteboardItemInfo.h

    r243713 r245637  
    4343    Vector<String> contentTypesByFidelity;
    4444    String suggestedFileName;
     45    Optional<FloatSize> preferredPresentationSize;
    4546    bool isNonTextType { false };
    4647    bool containsFileURLAndFileUploadContent { false };
    … …  
    9899void PasteboardItemInfo::encode(Encoder& encoder) const
    99100{
    100     encoder << pathsForFileUpload << contentTypesForFileUpload << contentTypesByFidelity << suggestedFileName << isNonTextType << containsFileURLAndFileUploadContent;
     101    encoder << pathsForFileUpload << contentTypesForFileUpload << contentTypesByFidelity << suggestedFileName << preferredPresentationSize << isNonTextType << containsFileURLAndFileUploadContent;
    101102    encoder.encodeEnum(preferredPresentationStyle);
    102103}
    … …  
    116117
    117118    if (!decoder.decode(result.suggestedFileName))
     119        return WTF::nullopt;
     120
     121    if (!decoder.decode(result.preferredPresentationSize))
    118122        return WTF::nullopt;
    119123
  • trunk/Source/WebCore/platform/ios/PasteboardIOS.mm

    r243713 r245637  
    182182}
    183183
    184 Pasteboard::ReaderResult Pasteboard::readPasteboardWebContentDataForType(PasteboardWebContentReader& reader, PasteboardStrategy& strategy, NSString *type, int itemIndex)
     184Pasteboard::ReaderResult Pasteboard::readPasteboardWebContentDataForType(PasteboardWebContentReader& reader, PasteboardStrategy& strategy, NSString *type, const PasteboardItemInfo& itemInfo, int itemIndex)
    185185{
    186186    if ([type isEqualToString:WebArchivePboardType] || [type isEqualToString:(__bridge NSString *)kUTTypeWebArchive]) {
    … …  
    226226        if (m_changeCount != changeCount())
    227227            return ReaderResult::PasteboardWasChangedExternally;
    228         return buffer && reader.readImage(buffer.releaseNonNull(), type) ? ReaderResult::ReadType : ReaderResult::DidNotReadType;
     228        return buffer && reader.readImage(buffer.releaseNonNull(), type, itemInfo.preferredPresentationSize) ? ReaderResult::ReadType : ReaderResult::DidNotReadType;
    229229    }
    230230
    … …  
    319319                continue;
    320320
    321             auto itemResult = readPasteboardWebContentDataForType(reader, strategy, type, i);
     321            auto itemResult = readPasteboardWebContentDataForType(reader, strategy, type, info, i);
    322322            if (itemResult == ReaderResult::PasteboardWasChangedExternally)
    323323                return;
    … …  
    359359                continue;
    360360
    361             result = readPasteboardWebContentDataForType(reader, strategy, type, index);
     361            result = readPasteboardWebContentDataForType(reader, strategy, type, info, index);
    362362            if (result == ReaderResult::PasteboardWasChangedExternally)
    363363                return;
  • trunk/Source/WebCore/platform/ios/PlatformPasteboardIOS.mm

    r244953 r245637  
    156156    info.preferredPresentationStyle = pasteboardItemPresentationStyle(itemProvider.preferredPresentationStyle);
    157157#endif
     158    if (!CGSizeEqualToSize(itemProvider.preferredPresentationSize, CGSizeZero))
     159        info.preferredPresentationSize = FloatSize { itemProvider.preferredPresentationSize };
    158160    info.containsFileURLAndFileUploadContent = itemProvider.web_containsFileURLAndFileUploadContent;
    159161    info.suggestedFileName = itemProvider.suggestedName;
  • trunk/Source/WebKit/ChangeLog

    r245625 r245637  
     12019-05-22  Zalan Bujtas  <zalan@apple.com>
     2
     3        [Paste] Add support for preferred presentation size when pasting an image
     4        https://bugs.webkit.org/show_bug.cgi?id=198132
     5        <rdar://problem/50886917>
     6
     7        Reviewed by Wenson Hsieh.
     8
     9        * UIProcess/API/Cocoa/_WKElementAction.mm:
     10        (addToReadingList):
     11        * UIProcess/ios/WKActionSheetAssistant.mm:
     12        (-[WKActionSheetAssistant defaultActionsForLinkSheet:]):
     13        (-[WKActionSheetAssistant defaultActionsForImageSheet:]):
     14
    1152019-05-22  Youenn Fablet  <youenn@apple.com>
    216
  • trunk/Tools/ChangeLog

    r245635 r245637  
     12019-05-22  Zalan Bujtas  <zalan@apple.com>
     2
     3        [Paste] Add support for preferred presentation size when pasting an image
     4        https://bugs.webkit.org/show_bug.cgi?id=198132
     5        <rdar://problem/50886917>
     6
     7        Reviewed by Wenson Hsieh.
     8
     9        * TestWebKitAPI/Tests/ios/UIPasteboardTests.mm:
     10        (TestWebKitAPI::TEST):
     11
    1122019-05-22  Keith Rollin  <krollin@apple.com>
    213
  • trunk/Tools/TestWebKitAPI/Tests/ios/UIPasteboardTests.mm

    r244955 r245637  
    303303}
    304304
     305TEST(UIPasteboardTests, ValidPreferredPresentationSizeForImage)
     306{
     307    auto webView = setUpWebViewForPasteboardTests(@"autofocus-contenteditable");
     308    auto itemProvider = adoptNS([[NSItemProvider alloc] init]);
     309    [itemProvider setPreferredPresentationSize:CGSizeMake(10, 20)];
     310    [itemProvider registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypePNG visibility:NSItemProviderRepresentationVisibilityAll loadHandler:[] (DataLoadCompletionBlock completionHandler) -> NSProgress * {
     311        completionHandler([NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"icon" withExtension:@"png" subdirectory:@"TestWebKitAPI.resources"]], nil);
     312        return nil;
     313    }];
     314    [UIPasteboard generalPasteboard].itemProviders = @[ itemProvider.get() ];
     315    [webView paste:nil];
     316
     317    EXPECT_WK_STREQ("10", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').width"]);
     318    EXPECT_WK_STREQ("20", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').height"]);
     319}
     320
     321TEST(UIPasteboardTests, InvalidPreferredPresentationSizeForImage)
     322{
     323    auto webView = setUpWebViewForPasteboardTests(@"autofocus-contenteditable");
     324    auto itemProvider = adoptNS([[NSItemProvider alloc] init]);
     325    [itemProvider setPreferredPresentationSize:CGSizeMake(-10, -20)];
     326    [itemProvider registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypePNG visibility:NSItemProviderRepresentationVisibilityAll loadHandler:[] (DataLoadCompletionBlock completionHandler) -> NSProgress * {
     327        completionHandler([NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"icon" withExtension:@"png" subdirectory:@"TestWebKitAPI.resources"]], nil);
     328        return nil;
     329    }];
     330    [UIPasteboard generalPasteboard].itemProviders = @[ itemProvider.get() ];
     331    [webView paste:nil];
     332
     333    EXPECT_WK_STREQ("0", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').width"]);
     334    EXPECT_WK_STREQ("174", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').height"]);
     335}
     336
     337TEST(UIPasteboardTests, MissingPreferredPresentationSizeForImage)
     338{
     339    auto webView = setUpWebViewForPasteboardTests(@"autofocus-contenteditable");
     340    auto itemProvider = adoptNS([[NSItemProvider alloc] init]);
     341    [itemProvider registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypePNG visibility:NSItemProviderRepresentationVisibilityAll loadHandler:[] (DataLoadCompletionBlock completionHandler) -> NSProgress * {
     342        completionHandler([NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"icon" withExtension:@"png" subdirectory:@"TestWebKitAPI.resources"]], nil);
     343        return nil;
     344    }];
     345    [UIPasteboard generalPasteboard].itemProviders = @[ itemProvider.get() ];
     346    [webView paste:nil];
     347
     348    EXPECT_WK_STREQ("0", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').width"]);
     349    EXPECT_WK_STREQ("174", [webView stringByEvaluatingJavaScript:@"document.querySelector('img').height"]);
     350}
    305351#endif // PLATFORM(IOS)
    306352
Note: See TracChangeset for help on using the changeset viewer.