Supported mime types for encoding should be supported mime types for loading
https://bugs.webkit.org/show_bug.cgi?id=206239

Patch by Said Abou-Hallawa <sabouhallawa@apple.com> on 2020-01-14
Reviewed by Simon Fraser.

Source/WebCore:

Consult isSupportedImageType() for the system supported UTI before
considering its mime type is allowed for the image DataURL encoding.

Test: fast/canvas/toDataURL-unsupportedTypes.html

* platform/MIMETypeRegistry.cpp:
(WebCore::MIMETypeRegistry::createMIMETypeRegistryThreadGlobalData):

LayoutTests:

* fast/canvas/toDataURL-unsupportedTypes-expected.txt: Added.
* fast/canvas/toDataURL-unsupportedTypes.html: Added.
Unsupported mime-types for encoding should fall back to 'image/png'.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@254541 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index dd926cc..f6db8db 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,14 @@
+2020-01-14  Said Abou-Hallawa  <sabouhallawa@apple.com>
+
+        Supported mime types for encoding should be supported mime types for loading
+        https://bugs.webkit.org/show_bug.cgi?id=206239
+
+        Reviewed by Simon Fraser.
+
+        * fast/canvas/toDataURL-unsupportedTypes-expected.txt: Added.
+        * fast/canvas/toDataURL-unsupportedTypes.html: Added.
+        Unsupported mime-types for encoding should fall back to 'image/png'.
+
 2020-01-14  Myles C. Maxfield  <mmaxfield@apple.com>
 
         [Cocoa] Glyph lookup should be language-sensitive (specifically between Yiddish and Hebrew)
diff --git a/LayoutTests/fast/canvas/toDataURL-unsupportedTypes-expected.txt b/LayoutTests/fast/canvas/toDataURL-unsupportedTypes-expected.txt
new file mode 100644
index 0000000..d993ea3
--- /dev/null
+++ b/LayoutTests/fast/canvas/toDataURL-unsupportedTypes-expected.txt
@@ -0,0 +1,14 @@
+This is a test of the unsupported mime-types for canvas.toDataURL().
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS canvas.toDataURL('image/heic') is canvas.toDataURL('image/png')
+PASS canvas.toDataURL('image/heic-sequence') is canvas.toDataURL('image/png')
+PASS canvas.toDataURL('image/vnd.adobe.photoshop') is canvas.toDataURL('image/png')
+PASS canvas.toDataURL('application/pdf') is canvas.toDataURL('image/png')
+PASS canvas.toDataURL('image/targa') is canvas.toDataURL('image/png')
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/canvas/toDataURL-unsupportedTypes.html b/LayoutTests/fast/canvas/toDataURL-unsupportedTypes.html
new file mode 100644
index 0000000..cb08b49
--- /dev/null
+++ b/LayoutTests/fast/canvas/toDataURL-unsupportedTypes.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<head>
+    <script src="../../resources/js-test-pre.js"></script>
+</head>
+<body>
+    <canvas width="8" height="8"></canvas>
+    <script>
+        var canvas = document.querySelector("canvas");
+        canvas.width = 8;
+        canvas.height = 8;
+
+        description("This is a test of the unsupported mime-types for canvas.toDataURL().");
+        shouldBe("canvas.toDataURL('image/heic')", "canvas.toDataURL('image/png')");
+        shouldBe("canvas.toDataURL('image/heic-sequence')", "canvas.toDataURL('image/png')");
+        shouldBe("canvas.toDataURL('image/vnd.adobe.photoshop')", "canvas.toDataURL('image/png')");
+        shouldBe("canvas.toDataURL('application/pdf')", "canvas.toDataURL('image/png')");
+        shouldBe("canvas.toDataURL('image/targa')", "canvas.toDataURL('image/png')");
+    </script>
+    <script src="../../resources/js-test-post.js"></script>
+</body>
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 0141be0..d360931 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,18 @@
+2020-01-14  Said Abou-Hallawa  <sabouhallawa@apple.com>
+
+        Supported mime types for encoding should be supported mime types for loading
+        https://bugs.webkit.org/show_bug.cgi?id=206239
+
+        Reviewed by Simon Fraser.
+
+        Consult isSupportedImageType() for the system supported UTI before
+        considering its mime type is allowed for the image DataURL encoding.
+
+        Test: fast/canvas/toDataURL-unsupportedTypes.html
+
+        * platform/MIMETypeRegistry.cpp:
+        (WebCore::MIMETypeRegistry::createMIMETypeRegistryThreadGlobalData):
+
 2020-01-14  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r254533 and r254535.
diff --git a/Source/WebCore/platform/MIMETypeRegistry.cpp b/Source/WebCore/platform/MIMETypeRegistry.cpp
index d81c3d6..3fa4c26 100644
--- a/Source/WebCore/platform/MIMETypeRegistry.cpp
+++ b/Source/WebCore/platform/MIMETypeRegistry.cpp
@@ -446,9 +446,10 @@
     CFIndex count = CFArrayGetCount(supportedTypes.get());
     for (CFIndex i = 0; i < count; i++) {
         CFStringRef supportedType = reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(supportedTypes.get(), i));
-        String mimeType = MIMETypeForImageType(supportedType);
-        if (!mimeType.isEmpty())
+        if (isSupportedImageType(supportedType)) {
+            String mimeType = MIMETypeForImageType(supportedType);
             supportedImageMIMETypesForEncoding.add(mimeType);
+        }
     }
 #else
     HashSet<String, ASCIICaseInsensitiveHash> supportedImageMIMETypesForEncoding = std::initializer_list<String> {