Setting the file wrapper and content type of an attachment to a PDF should update...
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 21 Dec 2018 19:40:41 +0000 (19:40 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 21 Dec 2018 19:40:41 +0000 (19:40 +0000)
https://bugs.webkit.org/show_bug.cgi?id=192984
<rdar://problem/46798028>

Reviewed by Tim Horton.

Source/WebCore:

Allow PDF data to be used to update enclosing image elements when setting the file wrapper for an attachment.
Covered by a new API test: WKAttachmentTests.SetFileWrapperForPDFImageAttachment.

* html/HTMLAttachmentElement.cpp:
(WebCore::mimeTypeIsSuitableForInlineImageAttachment):
(WebCore::HTMLAttachmentElement::updateEnclosingImageWithData):

Tools:

Add an API test to verify that setting the file wrapper for a _WKAttachment with an enclosing image element with
PDF data correctly updates the image element.

* TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@239511 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/html/HTMLAttachmentElement.cpp
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm

index d799083..81bf3fc 100644 (file)
@@ -1,3 +1,18 @@
+2018-12-21  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Setting the file wrapper and content type of an attachment to a PDF should update its image
+        https://bugs.webkit.org/show_bug.cgi?id=192984
+        <rdar://problem/46798028>
+
+        Reviewed by Tim Horton.
+
+        Allow PDF data to be used to update enclosing image elements when setting the file wrapper for an attachment.
+        Covered by a new API test: WKAttachmentTests.SetFileWrapperForPDFImageAttachment.
+
+        * html/HTMLAttachmentElement.cpp:
+        (WebCore::mimeTypeIsSuitableForInlineImageAttachment):
+        (WebCore::HTMLAttachmentElement::updateEnclosingImageWithData):
+
 2018-12-21  Justin Michaud  <justin_michaud@apple.com>
 
         Repeated background images with zero size should display the background color
index 7111cf6..c040c19 100644 (file)
@@ -226,6 +226,11 @@ void HTMLAttachmentElement::updateAttributes(Optional<uint64_t>&& newFileSize, c
         renderer->invalidate();
 }
 
+static bool mimeTypeIsSuitableForInlineImageAttachment(const String& mimeType)
+{
+    return MIMETypeRegistry::isSupportedImageMIMEType(mimeType) || MIMETypeRegistry::isPDFMIMEType(mimeType);
+}
+
 void HTMLAttachmentElement::updateEnclosingImageWithData(const String& contentType, Ref<SharedBuffer>&& data)
 {
     auto* hostElement = shadowHost();
@@ -238,7 +243,7 @@ void HTMLAttachmentElement::updateEnclosingImageWithData(const String& contentTy
         mimeType = MIMETypeFromUTI(contentType);
 #endif
 
-    if (!MIMETypeRegistry::isSupportedImageMIMEType(mimeType))
+    if (!mimeTypeIsSuitableForInlineImageAttachment(mimeType))
         return;
 
     hostElement->setAttributeWithoutSynchronization(HTMLNames::srcAttr, DOMURL::createObjectURL(document(), Blob::create(WTFMove(data), mimeType)));
index 1fb10f3..2ae467f 100644 (file)
@@ -1,3 +1,16 @@
+2018-12-21  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Setting the file wrapper and content type of an attachment to a PDF should update its image
+        https://bugs.webkit.org/show_bug.cgi?id=192984
+        <rdar://problem/46798028>
+
+        Reviewed by Tim Horton.
+
+        Add an API test to verify that setting the file wrapper for a _WKAttachment with an enclosing image element with
+        PDF data correctly updates the image element.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:
+
 2018-12-21  Michael Catanzaro  <mcatanzaro@igalia.com>
 
         Add JSCOnly to bot dashboard
index 83b0660..722b68c 100644 (file)
@@ -1505,6 +1505,18 @@ TEST(WKAttachmentTests, AttachmentIdentifierOfClonedAttachment)
     EXPECT_WK_STREQ([attachment uniqueIdentifier], [webView stringByEvaluatingJavaScript:@"document.body.cloneNode(true).querySelector('attachment').uniqueIdentifier"]);
 }
 
+TEST(WKAttachmentTests, SetFileWrapperForPDFImageAttachment)
+{
+    auto webView = webViewForTestingAttachments();
+    [webView evaluateJavaScript:@"document.body.appendChild()" completionHandler:nil];
+    NSString *identifier = [webView stringByEvaluatingJavaScript:@"const i = document.createElement('img'); document.body.appendChild(i); HTMLAttachmentElement.getAttachmentIdentifier(i)"];
+    _WKAttachment *attachment = [webView _attachmentForIdentifier:identifier];
+
+    auto pdfFile = adoptNS([[NSFileWrapper alloc] initRegularFileWithContents:testPDFData()]);
+    [attachment setFileWrapper:pdfFile.get() contentType:(__bridge NSString *)kUTTypePDF completion:nil];
+    [webView waitForImageElementSizeToBecome:CGSizeMake(130, 29)];
+}
+
 #pragma mark - Platform-specific tests
 
 #if PLATFORM(MAC)