Changeset 245775 in webkit


Ignore:
Timestamp:
May 25, 2019 11:59:22 AM (5 years ago)
Author:
Wenson Hsieh
Message:

[iOS] Respect -[NSItemProvider preferredPresentationSize] when dropping images
https://bugs.webkit.org/show_bug.cgi?id=198242

Reviewed by Beth Dakin.

Source/WebCore:

Teach the web content reader to respect the -preferredPresentationSize when creating attachment-backed image
elements. See below for more details.

Test: WKAttachmentTestsIOS.InsertDroppedImageWithPreferredPresentationSize

  • editing/WebContentReader.h:
  • editing/cocoa/WebContentReaderCocoa.mm:

(WebCore::attachmentForFilePath):
(WebCore::attachmentForData):

When creating attachment-backed image elements, additionally set width and height attributes from the preferred
presentation size, if specified.

(WebCore::WebContentReader::readFilePath):

Add a version of readFilePath that takes a single file path, and an optional preferred presentation size.

(WebCore::WebContentReader::readFilePaths):

Reimplement readFilePaths in terms of readFilePath.

(WebCore::WebContentReader::readDataBuffer):

Add more plumbing for preferredPresentationSize.

  • platform/Pasteboard.h:

(WebCore::PasteboardWebContentReader::readFilePath):
(WebCore::PasteboardWebContentReader::readDataBuffer):

  • platform/ios/PasteboardIOS.mm:

(WebCore::Pasteboard::read):
(WebCore::Pasteboard::readRespectingUTIFidelities):

Forward PasteboardItemInfo's preferredPresentationSize to the web content reader.

Tools:

Add a new test that simulates dropping an image with a preferred presentation size into an editable element
with attachment elements enabled.

  • TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:

(TestWebKitAPI::TEST):

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r245774 r245775  
     12019-05-25  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [iOS] Respect -[NSItemProvider preferredPresentationSize] when dropping images
     4        https://bugs.webkit.org/show_bug.cgi?id=198242
     5
     6        Reviewed by Beth Dakin.
     7
     8        Teach the web content reader to respect the -preferredPresentationSize when creating attachment-backed image
     9        elements. See below for more details.
     10
     11        Test: WKAttachmentTestsIOS.InsertDroppedImageWithPreferredPresentationSize
     12
     13        * editing/WebContentReader.h:
     14        * editing/cocoa/WebContentReaderCocoa.mm:
     15        (WebCore::attachmentForFilePath):
     16        (WebCore::attachmentForData):
     17
     18        When creating attachment-backed image elements, additionally set width and height attributes from the preferred
     19        presentation size, if specified.
     20
     21        (WebCore::WebContentReader::readFilePath):
     22
     23        Add a version of readFilePath that takes a single file path, and an optional preferred presentation size.
     24
     25        (WebCore::WebContentReader::readFilePaths):
     26
     27        Reimplement readFilePaths in terms of readFilePath.
     28
     29        (WebCore::WebContentReader::readDataBuffer):
     30
     31        Add more plumbing for preferredPresentationSize.
     32
     33        * platform/Pasteboard.h:
     34        (WebCore::PasteboardWebContentReader::readFilePath):
     35        (WebCore::PasteboardWebContentReader::readDataBuffer):
     36        * platform/ios/PasteboardIOS.mm:
     37        (WebCore::Pasteboard::read):
     38        (WebCore::Pasteboard::readRespectingUTIFidelities):
     39
     40        Forward PasteboardItemInfo's preferredPresentationSize to the web content reader.
     41
    1422019-05-25  Antoine Quint  <graouts@apple.com>
    243
  • trunk/Source/WebCore/editing/WebContentReader.h

    r245637 r245775  
    7272#if PLATFORM(COCOA)
    7373    bool readWebArchive(SharedBuffer&) override;
     74    bool readFilePath(const String&, Optional<FloatSize> preferredPresentationSize = { }) override;
    7475    bool readFilePaths(const Vector<String>&) override;
    7576    bool readHTML(const String&) override;
     
    7879    bool readImage(Ref<SharedBuffer>&&, const String& type, Optional<FloatSize> preferredPresentationSize = { }) override;
    7980    bool readURL(const URL&, const String& title) override;
    80     bool readDataBuffer(SharedBuffer&, const String& type, const String& name) override;
     81    bool readDataBuffer(SharedBuffer&, const String& type, const String& name, Optional<FloatSize> preferredPresentationSize = { }) override;
    8182#endif
    8283    bool readPlainText(const String&) override;
     
    9596#if PLATFORM(COCOA)
    9697    bool readWebArchive(SharedBuffer&) override;
     98    bool readFilePath(const String&, Optional<FloatSize> = { }) override { return false; }
    9799    bool readFilePaths(const Vector<String>&) override { return false; }
    98100    bool readHTML(const String&) override;
     
    101103    bool readImage(Ref<SharedBuffer>&&, const String&, Optional<FloatSize> = { }) override { return false; }
    102104    bool readURL(const URL&, const String&) override { return false; }
    103     bool readDataBuffer(SharedBuffer&, const String&, const String&) override { return false; }
     105    bool readDataBuffer(SharedBuffer&, const String&, const String&, Optional<FloatSize> = { }) override { return false; }
    104106#endif
    105107    bool readPlainText(const String&) override { return false; }
  • trunk/Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm

    r245637 r245775  
    696696#if ENABLE(ATTACHMENT_ELEMENT)
    697697
    698 static Ref<HTMLElement> attachmentForFilePath(Frame& frame, const String& path)
     698static Ref<HTMLElement> attachmentForFilePath(Frame& frame, const String& path, Optional<FloatSize> preferredSize)
    699699{
    700700    auto document = makeRef(*frame.document());
     
    724724        image->setAttributeWithoutSynchronization(HTMLNames::srcAttr, DOMURL::createObjectURL(document, File::create(path)));
    725725        image->setAttachmentElement(WTFMove(attachment));
     726        if (preferredSize) {
     727            image->setAttributeWithoutSynchronization(HTMLNames::widthAttr, AtomicString::number(preferredSize->width()));
     728            image->setAttributeWithoutSynchronization(HTMLNames::heightAttr, AtomicString::number(preferredSize->height()));
     729        }
    726730        return image;
    727731    }
     
    731735}
    732736
    733 static Ref<HTMLElement> attachmentForData(Frame& frame, SharedBuffer& buffer, const String& contentType, const String& name)
     737static Ref<HTMLElement> attachmentForData(Frame& frame, SharedBuffer& buffer, const String& contentType, const String& name, Optional<FloatSize> preferredSize)
    734738{
    735739    auto document = makeRef(*frame.document());
     
    758762        image->setAttributeWithoutSynchronization(HTMLNames::srcAttr, DOMURL::createObjectURL(document, File::create(Blob::create(buffer, WTFMove(typeForAttachmentElement)), WTFMove(fileName))));
    759763        image->setAttachmentElement(WTFMove(attachment));
     764        if (preferredSize) {
     765            image->setAttributeWithoutSynchronization(HTMLNames::widthAttr, AtomicString::number(preferredSize->width()));
     766            image->setAttributeWithoutSynchronization(HTMLNames::heightAttr, AtomicString::number(preferredSize->height()));
     767        }
    760768        return image;
    761769    }
     
    767775#endif // ENABLE(ATTACHMENT_ELEMENT)
    768776
    769 bool WebContentReader::readFilePaths(const Vector<String>& paths)
    770 {
    771     if (paths.isEmpty() || !frame.document())
     777bool WebContentReader::readFilePath(const String& path, Optional<FloatSize> preferredPresentationSize)
     778{
     779    if (path.isEmpty() || !frame.document())
    772780        return false;
    773781
     
    777785
    778786#if ENABLE(ATTACHMENT_ELEMENT)
    779     if (RuntimeEnabledFeatures::sharedFeatures().attachmentElementEnabled()) {
    780         for (auto& path : paths)
    781             fragment->appendChild(attachmentForFilePath(frame, path));
    782     }
    783 #endif
     787    if (RuntimeEnabledFeatures::sharedFeatures().attachmentElementEnabled())
     788        fragment->appendChild(attachmentForFilePath(frame, path, preferredPresentationSize));
     789#endif
     790
     791    return true;
     792}
     793
     794bool WebContentReader::readFilePaths(const Vector<String>& paths)
     795{
     796    if (paths.isEmpty() || !frame.document())
     797        return false;
     798
     799    for (auto& path : paths)
     800        readFilePath(path);
    784801
    785802    return true;
     
    817834}
    818835
    819 bool WebContentReader::readDataBuffer(SharedBuffer& buffer, const String& type, const String& name)
     836bool WebContentReader::readDataBuffer(SharedBuffer& buffer, const String& type, const String& name, Optional<FloatSize> preferredPresentationSize)
    820837{
    821838    if (buffer.isEmpty())
     
    833850
    834851#if ENABLE(ATTACHMENT_ELEMENT)
    835     fragment->appendChild(attachmentForData(frame, buffer, type, name));
     852    fragment->appendChild(attachmentForData(frame, buffer, type, name, preferredPresentationSize));
    836853#else
    837854    UNUSED_PARAM(type);
  • trunk/Source/WebCore/platform/Pasteboard.h

    r245637 r245775  
    137137#if PLATFORM(COCOA)
    138138    virtual bool readWebArchive(SharedBuffer&) = 0;
     139    virtual bool readFilePath(const String&, Optional<FloatSize> preferredPresentationSize = { }) = 0;
    139140    virtual bool readFilePaths(const Vector<String>&) = 0;
    140141    virtual bool readHTML(const String&) = 0;
     
    143144    virtual bool readImage(Ref<SharedBuffer>&&, const String& type, Optional<FloatSize> preferredPresentationSize = { }) = 0;
    144145    virtual bool readURL(const URL&, const String& title) = 0;
    145     virtual bool readDataBuffer(SharedBuffer&, const String& type, const String& name) = 0;
     146    virtual bool readDataBuffer(SharedBuffer&, const String& type, const String& name, Optional<FloatSize> preferredPresentationSize = { }) = 0;
    146147#endif
    147148    virtual bool readPlainText(const String&) = 0;
  • trunk/Source/WebCore/platform/ios/PasteboardIOS.mm

    r245637 r245775  
    308308            if (auto buffer = strategy.readBufferFromPasteboard(i, typeForFileUpload, m_pasteboardName)) {
    309309                readURLAlongsideAttachmentIfNecessary(reader, strategy, typeForFileUpload, m_pasteboardName, i);
    310                 reader.readDataBuffer(*buffer, typeForFileUpload, info.suggestedFileName);
     310                reader.readDataBuffer(*buffer, typeForFileUpload, info.suggestedFileName, info.preferredPresentationSize);
    311311                continue;
    312312            }
     
    348348        if (canReadAttachment && prefersAttachmentRepresentation(info)) {
    349349            readURLAlongsideAttachmentIfNecessary(reader, strategy, info.contentTypeForHighestFidelityItem(), m_pasteboardName, index);
    350             reader.readFilePaths({ WTFMove(attachmentFilePath) });
     350            reader.readFilePath(WTFMove(attachmentFilePath), info.preferredPresentationSize);
    351351            continue;
    352352        }
     
    367367#if ENABLE(ATTACHMENT_ELEMENT)
    368368        if (canReadAttachment && result == ReaderResult::DidNotReadType)
    369             reader.readFilePaths({ WTFMove(attachmentFilePath) });
     369            reader.readFilePath(WTFMove(attachmentFilePath), info.preferredPresentationSize);
    370370#endif
    371371    }
  • trunk/Tools/ChangeLog

    r245744 r245775  
     12019-05-25  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [iOS] Respect -[NSItemProvider preferredPresentationSize] when dropping images
     4        https://bugs.webkit.org/show_bug.cgi?id=198242
     5
     6        Reviewed by Beth Dakin.
     7
     8        Add a new test that simulates dropping an image with a preferred presentation size into an editable element
     9        with attachment elements enabled.
     10
     11        * TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:
     12        (TestWebKitAPI::TEST):
     13
    1142019-05-24  Yusuke Suzuki  <ysuzuki@apple.com>
    215
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm

    r244251 r245775  
    16321632}
    16331633
     1634TEST(WKAttachmentTestsIOS, InsertDroppedImageWithPreferredPresentationSize)
     1635{
     1636    auto webView = webViewForTestingAttachments();
     1637    auto dragAndDropSimulator = adoptNS([[DragAndDropSimulator alloc] initWithWebView:webView.get()]);
     1638    auto item = adoptNS([[NSItemProvider alloc] init]);
     1639    [item registerData:testImageData() type:(__bridge NSString *)kUTTypePNG];
     1640    [item setPreferredPresentationSize:CGSizeMake(200, 100)];
     1641    [dragAndDropSimulator setExternalItemProviders:@[ item.get() ]];
     1642    [dragAndDropSimulator runFrom:CGPointZero to:CGPointMake(50, 50)];
     1643
     1644    CGSize imageElementSize = [webView imageElementSize];
     1645    EXPECT_EQ(200, imageElementSize.width);
     1646    EXPECT_EQ(100, imageElementSize.height);
     1647}
     1648
    16341649TEST(WKAttachmentTestsIOS, InsertDroppedAttributedStringContainingAttachment)
    16351650{
Note: See TracChangeset for help on using the changeset viewer.