Need to be able to specify MIME type for <attachment> without filename or handle
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 27 Jan 2016 19:11:22 +0000 (19:11 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 27 Jan 2016 19:11:22 +0000 (19:11 +0000)
https://bugs.webkit.org/show_bug.cgi?id=153552
<rdar://problem/20145857>

Reviewed by Anders Carlsson.

Tests: fast/attachment/attachment-default-icon.html
       fast/attachment/attachment-type-attribute.html

* html/HTMLAttachmentElement.cpp:
(WebCore::HTMLAttachmentElement::parseAttribute):
Invalidate attachment when 'type' attribute changes.

(WebCore::HTMLAttachmentElement::attachmentType):
* html/HTMLAttachmentElement.h:
* platform/graphics/Icon.h:
* platform/graphics/mac/IconMac.mm:
(WebCore::Icon::createIconForUTI):
(WebCore::Icon::createIconForMIMEType):
Add Icon class methods to retrieve an icon given a UTI or MIME type.

* rendering/RenderThemeMac.mm:
(WebCore::paintAttachmentIcon):
Use the 'type' attribute (a MIME type) if we have one. Otherwise,
use the filename. Lastly fall back to a plain file icon (using the root
file UTI, public.data).

* fast/attachment/attachment-default-icon-expected.html: Added.
* fast/attachment/attachment-default-icon.html: Added.
* fast/attachment/attachment-type-attribute-expected.html: Added.
* fast/attachment/attachment-type-attribute.html: Added.
Add some tests.

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

LayoutTests/ChangeLog
LayoutTests/fast/attachment/attachment-default-icon-expected.html [new file with mode: 0644]
LayoutTests/fast/attachment/attachment-default-icon.html [new file with mode: 0644]
LayoutTests/fast/attachment/attachment-type-attribute-expected.html [new file with mode: 0644]
LayoutTests/fast/attachment/attachment-type-attribute.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/html/HTMLAttachmentElement.cpp
Source/WebCore/html/HTMLAttachmentElement.h
Source/WebCore/platform/graphics/Icon.h
Source/WebCore/platform/graphics/mac/IconMac.mm
Source/WebCore/rendering/RenderThemeMac.mm

index f11ebbc..9447f31 100644 (file)
@@ -1,3 +1,17 @@
+2016-01-27  Tim Horton  <timothy_horton@apple.com>
+
+        Need to be able to specify MIME type for <attachment> without filename or handle
+        https://bugs.webkit.org/show_bug.cgi?id=153552
+        <rdar://problem/20145857>
+
+        Reviewed by Anders Carlsson.
+
+        * fast/attachment/attachment-default-icon-expected.html: Added.
+        * fast/attachment/attachment-default-icon.html: Added.
+        * fast/attachment/attachment-type-attribute-expected.html: Added.
+        * fast/attachment/attachment-type-attribute.html: Added.
+        Add some tests.
+
 2016-01-27  Xabier Rodriguez Calvar  <calvaris@igalia.com> and Youenn Fablet  <youenn.fablet@crf.canon.fr>
 
         [GTK] Problem running promises code in workers
diff --git a/LayoutTests/fast/attachment/attachment-default-icon-expected.html b/LayoutTests/fast/attachment/attachment-default-icon-expected.html
new file mode 100644 (file)
index 0000000..122a8dd
--- /dev/null
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+<body>
+<attachment id="attachment" title="  "></attachment>
+<script>
+var file;
+if (window.internals)
+    file = window.internals.createFile("resources/test-file.dat");
+
+document.getElementById("attachment").file = file;
+</script>
+</body>
+</html>
diff --git a/LayoutTests/fast/attachment/attachment-default-icon.html b/LayoutTests/fast/attachment/attachment-default-icon.html
new file mode 100644 (file)
index 0000000..c686259
--- /dev/null
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<html>
+<body>
+<attachment></attachment>
+</body>
+</html>
diff --git a/LayoutTests/fast/attachment/attachment-type-attribute-expected.html b/LayoutTests/fast/attachment/attachment-type-attribute-expected.html
new file mode 100644 (file)
index 0000000..b4d4cc7
--- /dev/null
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+<body>
+<attachment id="attachment" title="  "></attachment>
+<script>
+var file;
+if (window.internals)
+    file = window.internals.createFile("resources/test-file.txt");
+
+document.getElementById("attachment").file = file;
+</script>
+</body>
+</html>
diff --git a/LayoutTests/fast/attachment/attachment-type-attribute.html b/LayoutTests/fast/attachment/attachment-type-attribute.html
new file mode 100644 (file)
index 0000000..19f929b
--- /dev/null
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<html>
+<body>
+<attachment type="text/plain"></attachment>
+</body>
+</html>
index c4b23c0..03c0297 100644 (file)
@@ -1,3 +1,32 @@
+2016-01-27  Tim Horton  <timothy_horton@apple.com>
+
+        Need to be able to specify MIME type for <attachment> without filename or handle
+        https://bugs.webkit.org/show_bug.cgi?id=153552
+        <rdar://problem/20145857>
+
+        Reviewed by Anders Carlsson.
+
+        Tests: fast/attachment/attachment-default-icon.html
+               fast/attachment/attachment-type-attribute.html
+
+        * html/HTMLAttachmentElement.cpp:
+        (WebCore::HTMLAttachmentElement::parseAttribute):
+        Invalidate attachment when 'type' attribute changes.
+
+        (WebCore::HTMLAttachmentElement::attachmentType):
+        * html/HTMLAttachmentElement.h:
+        * platform/graphics/Icon.h:
+        * platform/graphics/mac/IconMac.mm:
+        (WebCore::Icon::createIconForUTI):
+        (WebCore::Icon::createIconForMIMEType):
+        Add Icon class methods to retrieve an icon given a UTI or MIME type.
+
+        * rendering/RenderThemeMac.mm:
+        (WebCore::paintAttachmentIcon):
+        Use the 'type' attribute (a MIME type) if we have one. Otherwise,
+        use the filename. Lastly fall back to a plain file icon (using the root
+        file UTI, public.data).
+
 2016-01-27  Alexey Proskuryakov  <ap@apple.com>
 
         Remove ENABLE_CURRENTSRC
index fc7b52b..d0fbf02 100644 (file)
@@ -74,7 +74,7 @@ void HTMLAttachmentElement::setFile(File* file)
 
 void HTMLAttachmentElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
 {
-    if ((name == progressAttr || name == titleAttr || name == subtitleAttr) && is<RenderAttachment>(renderer())) {
+    if ((name == progressAttr || name == titleAttr || name == subtitleAttr || name == typeAttr) && is<RenderAttachment>(renderer())) {
         downcast<RenderAttachment>(*renderer()).invalidate();
         return;
     }
@@ -90,6 +90,11 @@ String HTMLAttachmentElement::attachmentTitle() const
     return m_file ? m_file->name() : String();
 }
 
+String HTMLAttachmentElement::attachmentType() const
+{
+    return fastGetAttribute(typeAttr);
+}
+
 } // namespace WebCore
 
 #endif // ENABLE(ATTACHMENT_ELEMENT)
index 46a34ed..e54dc89 100644 (file)
@@ -41,6 +41,7 @@ public:
     void setFile(File*);
 
     String attachmentTitle() const;
+    String attachmentType() const;
 
 private:
     HTMLAttachmentElement(const QualifiedName&, Document&);
index aa395c5..e785982 100644 (file)
@@ -60,6 +60,11 @@ public:
     WEBCORE_EXPORT static PassRefPtr<Icon> createIconForImage(NativeImagePtr);
 #endif
 
+#if PLATFORM(MAC)
+    static RefPtr<Icon> createIconForUTI(const String&);
+    static RefPtr<Icon> createIconForMIMEType(const String&);
+#endif
+
 private:
 #if PLATFORM(IOS)
     Icon(CGImageRef);
index f41027f..f2781b1 100644 (file)
@@ -26,6 +26,7 @@
 #import "GraphicsContext.h"
 #import "IntRect.h"
 #import "LocalCurrentGraphicsContext.h"
+#import "UTIUtilities.h"
 #import <wtf/PassRefPtr.h>
 #include <wtf/text/WTFString.h>
 
@@ -72,6 +73,20 @@ PassRefPtr<Icon> Icon::createIconForFiles(const Vector<String>& filenames)
     return adoptRef(new Icon(image));
 }
 
+RefPtr<Icon> Icon::createIconForUTI(const String& UTI)
+{
+    NSImage *image = [[NSWorkspace sharedWorkspace] iconForFileType:UTI];
+    if (!image)
+        return nullptr;
+
+    return adoptRef(new Icon(image));
+}
+
+RefPtr<Icon> Icon::createIconForMIMEType(const String& MIMEType)
+{
+    return createIconForUTI(UTIFromMIMEType(MIMEType.createCFString().get()).get());
+}
+
 void Icon::paint(GraphicsContext& context, const FloatRect& rect)
 {
     if (context.paintingDisabled())
index ccdbd29..dea9269 100644 (file)
@@ -2339,10 +2339,20 @@ static void paintAttachmentIconBackground(const RenderAttachment&, GraphicsConte
 
 static void paintAttachmentIcon(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
 {
-    Vector<String> filenames;
-    if (File* file = attachment.attachmentElement().file())
-        filenames.append(file->path());
-    RefPtr<Icon> icon = Icon::createIconForFiles(filenames);
+    RefPtr<Icon> icon;
+    String type = attachment.attachmentElement().attachmentType();
+    if (!type.isEmpty())
+        icon = Icon::createIconForMIMEType(type);
+    else {
+        Vector<String> filenames;
+        if (File* file = attachment.attachmentElement().file())
+            filenames.append(file->path());
+        icon = Icon::createIconForFiles(filenames);
+    }
+
+    if (!icon)
+        icon = Icon::createIconForUTI("public.data");
+
     if (!icon)
         return;
     icon->paint(context, layout.iconRect);