Title: [221063] trunk
Revision
221063
Author
[email protected]
Date
2017-08-22 19:57:21 -0700 (Tue, 22 Aug 2017)

Log Message

Consolidate the code to normalize MIME type in DataTransfer
https://bugs.webkit.org/show_bug.cgi?id=175810

Reviewed by Wenson Hsieh.

Source/WebCore:

Factored out the code to convert MIME type to lowercase after stripping whitespace,
and treat "text" as "text/plain" and "url" as "text/uri-list".

Specifications:
https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-getdata-2
https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-setdata-2
https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-cleardata-2

Stripping of whitespace only happens in WebKit/Blink but it's probably required for compatbility.
Spec bug: https://github.com/whatwg/html/issues/2946

Test: editing/pasteboard/datatransfer-getdata-plaintext.html

* dom/DataTransfer.cpp:
(WebCore::normalizeType):
(WebCore::DataTransfer::clearData):
(WebCore::DataTransfer::getData const):
(WebCore::DataTransfer::setData):
* platform/gtk/PasteboardGtk.cpp:
(WebCore::selectionDataTypeFromHTMLClipboardType):
* platform/ios/PasteboardIOS.mm:
(WebCore::cocoaTypeFromHTMLClipboardType):
* platform/mac/PasteboardMac.mm:
(WebCore::cocoaTypeFromHTMLClipboardType):
* platform/win/PasteboardWin.cpp:
(WebCore::clipboardTypeFromMIMEType):

LayoutTests:

Added a regression test. Some test cases were failing on some platforms.

* editing/pasteboard/datatransfer-getdata-plaintext-expected.txt: Added.
* editing/pasteboard/datatransfer-getdata-plaintext.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (221062 => 221063)


--- trunk/LayoutTests/ChangeLog	2017-08-23 01:12:09 UTC (rev 221062)
+++ trunk/LayoutTests/ChangeLog	2017-08-23 02:57:21 UTC (rev 221063)
@@ -1,3 +1,15 @@
+2017-08-21  Ryosuke Niwa  <[email protected]>
+
+        Consolidate the code to normalize MIME type in DataTransfer
+        https://bugs.webkit.org/show_bug.cgi?id=175810
+
+        Reviewed by Wenson Hsieh.
+
+        Added a regression test. Some test cases were failing on some platforms.
+
+        * editing/pasteboard/datatransfer-getdata-plaintext-expected.txt: Added.
+        * editing/pasteboard/datatransfer-getdata-plaintext.html: Added.
+
 2017-08-22  Matt Lewis  <[email protected]>
 
         Unreviewed, rolling out r221033.

Added: trunk/LayoutTests/editing/pasteboard/datatransfer-getdata-plaintext-expected.txt (0 => 221063)


--- trunk/LayoutTests/editing/pasteboard/datatransfer-getdata-plaintext-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/editing/pasteboard/datatransfer-getdata-plaintext-expected.txt	2017-08-23 02:57:21 UTC (rev 221063)
@@ -0,0 +1,12 @@
+This tests peeks plain text using dataTransfer.getDAta. To manually test, click on "Copy text".
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS clipboardData.setData(" text/PLAIN ", "hello"); clipboardData.getData("text/plain") is "hello"
+PASS clipboardData.setData("   tEXT/pLaIN", "world"); clipboardData.getData("text/plain") is "world"
+PASS clipboardData.setData("text/plain; charset=utf-8", "hello"); clipboardData.getData("text/plain; charset=Shift_JIS") is "hello"
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/editing/pasteboard/datatransfer-getdata-plaintext.html (0 => 221063)


--- trunk/LayoutTests/editing/pasteboard/datatransfer-getdata-plaintext.html	                        (rev 0)
+++ trunk/LayoutTests/editing/pasteboard/datatransfer-getdata-plaintext.html	2017-08-23 02:57:21 UTC (rev 221063)
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<html>
+<body>
+<script src=""
+<script>
+
+description('This tests peeks plain text using dataTransfer.getDAta. To manually test, click on "Copy text".');
+
+function copyText()
+{
+    document.getElementById('source').focus();
+    document.execCommand('SelectAll', false, null);
+    document.execCommand('Copy', false, null);
+}
+
+function copy(event)
+{
+    clipboardData = event.clipboardData;
+    shouldBeEqualToString('clipboardData.setData(" text/PLAIN ", "hello"); clipboardData.getData("text/plain")', 'hello');
+    shouldBeEqualToString('clipboardData.setData("   tEXT/pLaIN", "world"); clipboardData.getData("text/plain")', 'world');
+    shouldBeEqualToString('clipboardData.setData("text/plain; charset=utf-8", "hello"); clipboardData.getData("text/plain; charset=Shift_JIS")', 'hello');
+    document.getElementById('container').style.display = 'none';
+    finishJSTest();
+}
+
+if (window.testRunner)
+    window._onload_ = copyText;
+jsTestIsAsync = true;
+successfullyParsed = true;
+
+</script>
+<div id="container">
+    <button _onclick_="copyText();">Copy text</button>
+    <div id="source" _oncopy_="copy(event)" contenteditable="true">hello, world</div>
+</div>
+<script src=""
+</body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (221062 => 221063)


--- trunk/Source/WebCore/ChangeLog	2017-08-23 01:12:09 UTC (rev 221062)
+++ trunk/Source/WebCore/ChangeLog	2017-08-23 02:57:21 UTC (rev 221063)
@@ -1,3 +1,37 @@
+2017-08-21  Ryosuke Niwa  <[email protected]>
+
+        Consolidate the code to normalize MIME type in DataTransfer
+        https://bugs.webkit.org/show_bug.cgi?id=175810
+
+        Reviewed by Wenson Hsieh.
+
+        Factored out the code to convert MIME type to lowercase after stripping whitespace,
+        and treat "text" as "text/plain" and "url" as "text/uri-list".
+
+        Specifications:
+        https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-getdata-2
+        https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-setdata-2
+        https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-cleardata-2
+
+        Stripping of whitespace only happens in WebKit/Blink but it's probably required for compatbility.
+        Spec bug: https://github.com/whatwg/html/issues/2946
+
+        Test: editing/pasteboard/datatransfer-getdata-plaintext.html
+
+        * dom/DataTransfer.cpp:
+        (WebCore::normalizeType):
+        (WebCore::DataTransfer::clearData):
+        (WebCore::DataTransfer::getData const):
+        (WebCore::DataTransfer::setData):
+        * platform/gtk/PasteboardGtk.cpp:
+        (WebCore::selectionDataTypeFromHTMLClipboardType):
+        * platform/ios/PasteboardIOS.mm:
+        (WebCore::cocoaTypeFromHTMLClipboardType):
+        * platform/mac/PasteboardMac.mm:
+        (WebCore::cocoaTypeFromHTMLClipboardType):
+        * platform/win/PasteboardWin.cpp:
+        (WebCore::clipboardTypeFromMIMEType):
+
 2017-08-22  Matt Lewis  <[email protected]>
 
         Unreviewed, rolling out r221033.

Modified: trunk/Source/WebCore/dom/DataTransfer.cpp (221062 => 221063)


--- trunk/Source/WebCore/dom/DataTransfer.cpp	2017-08-23 01:12:09 UTC (rev 221062)
+++ trunk/Source/WebCore/dom/DataTransfer.cpp	2017-08-23 02:57:21 UTC (rev 221063)
@@ -101,11 +101,28 @@
     return m_storeMode == StoreMode::ReadWrite;
 }
 
+static String normalizeType(const String& type)
+{
+    if (type.isNull())
+        return type;
+
+    String lowercaseType = type.stripWhiteSpace().convertToASCIILowercase();
+    if (lowercaseType == "text" || lowercaseType.startsWithIgnoringASCIICase("text/plain;"))
+        return "text/plain";
+    if (lowercaseType == "url" || lowercaseType.startsWithIgnoringASCIICase("text/uri-list;"))
+        return "text/uri-list";
+    if (lowercaseType.startsWithIgnoringASCIICase("text/html;"))
+        return "text/html";
+
+    return lowercaseType;
+}
+
 void DataTransfer::clearData(const String& type)
 {
     if (!canWriteData())
         return;
 
+    String normalizedType = normalizeType(type);
     if (type.isNull())
         m_pasteboard->clear();
     else
@@ -124,7 +141,7 @@
         return String();
 #endif
 
-    return m_pasteboard->readString(type);
+    return m_pasteboard->readString(normalizeType(type));
 }
 
 void DataTransfer::setData(const String& type, const String& data)
@@ -137,9 +154,10 @@
         return;
 #endif
 
-    m_pasteboard->writeString(type, data);
+    String normalizedType = normalizeType(type);
+    m_pasteboard->writeString(normalizedType, data);
     if (m_itemList)
-        m_itemList->didSetStringData(type);
+        m_itemList->didSetStringData(normalizedType);
 }
 
 DataTransferItemList& DataTransfer::items()

Modified: trunk/Source/WebCore/platform/gtk/PasteboardGtk.cpp (221062 => 221063)


--- trunk/Source/WebCore/platform/gtk/PasteboardGtk.cpp	2017-08-23 01:12:09 UTC (rev 221062)
+++ trunk/Source/WebCore/platform/gtk/PasteboardGtk.cpp	2017-08-23 02:57:21 UTC (rev 221063)
@@ -102,23 +102,15 @@
     return m_selectionData.get();
 }
 
-static ClipboardDataType selectionDataTypeFromHTMLClipboardType(const String& rawType)
+static ClipboardDataType selectionDataTypeFromHTMLClipboardType(const String& type)
 {
-    String type(rawType.stripWhiteSpace());
-
-    // Two special cases for IE compatibility
-    if (type == "Text" || type == "text")
-        return ClipboardDataTypeText;
-    if (type == "URL")
-        return ClipboardDataTypeURL;
-
     // From the Mac port: Ignore any trailing charset - JS strings are
     // Unicode, which encapsulates the charset issue.
-    if (type == "text/plain" || type.startsWith("text/plain;"))
+    if (type == "text/plain")
         return ClipboardDataTypeText;
-    if (type == "text/html" || type.startsWith("text/html;"))
+    if (type == "text/html")
         return ClipboardDataTypeMarkup;
-    if (type == "Files" || type == "text/uri-list" || type.startsWith("text/uri-list;"))
+    if (type == "Files" || type == "text/uri-list")
         return ClipboardDataTypeURIList;
 
     // Not a known type, so just default to using the text portion.

Modified: trunk/Source/WebCore/platform/ios/PasteboardIOS.mm (221062 => 221063)


--- trunk/Source/WebCore/platform/ios/PasteboardIOS.mm	2017-08-23 01:12:09 UTC (rev 221062)
+++ trunk/Source/WebCore/platform/ios/PasteboardIOS.mm	2017-08-23 02:57:21 UTC (rev 221063)
@@ -324,27 +324,20 @@
 
 static RetainPtr<NSString> cocoaTypeFromHTMLClipboardType(const String& type)
 {
-    String strippedType = type.stripWhiteSpace();
-
-    if (strippedType == "Text")
-        return (NSString *)kUTTypeText;
-    if (strippedType == "URL")
-        return (NSString *)kUTTypeURL;
-
     // Ignore any trailing charset - JS strings are Unicode, which encapsulates the charset issue.
-    if (strippedType.startsWith("text/plain"))
+    if (type == "text/plain")
         return (NSString *)kUTTypeText;
 
     // Special case because UTI doesn't work with Cocoa's URL type.
-    if (strippedType == "text/uri-list")
+    if (type == "text/uri-list")
         return (NSString *)kUTTypeURL;
 
     // Try UTI now.
-    if (NSString *utiType = utiTypeFromCocoaType(strippedType))
+    if (NSString *utiType = utiTypeFromCocoaType(type))
         return utiType;
 
     // No mapping, just pass the whole string though.
-    return (NSString *)strippedType;
+    return (NSString *)type;
 }
 
 void Pasteboard::clear(const String& type)

Modified: trunk/Source/WebCore/platform/mac/PasteboardMac.mm (221062 => 221063)


--- trunk/Source/WebCore/platform/mac/PasteboardMac.mm	2017-08-23 01:12:09 UTC (rev 221062)
+++ trunk/Source/WebCore/platform/mac/PasteboardMac.mm	2017-08-23 02:57:21 UTC (rev 221063)
@@ -463,27 +463,19 @@
 
 static String cocoaTypeFromHTMLClipboardType(const String& type)
 {
-    // http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#dom-datatransfer-setdata
-    String lowercasedType = type.convertToASCIILowercase();
-
-    if (lowercasedType == "text")
-        lowercasedType = ASCIILiteral("text/plain");
-    if (lowercasedType == "url")
-        lowercasedType = ASCIILiteral("text/uri-list");
-
     // Ignore any trailing charset - strings are already UTF-16, and the charset issue has already been dealt with.
-    if (lowercasedType == "text/plain" || lowercasedType.startsWith("text/plain;"))
+    if (type == "text/plain")
         return NSStringPboardType;
-    if (lowercasedType == "text/uri-list") {
+    if (type == "text/uri-list") {
         // Special case because UTI doesn't work with Cocoa's URL type.
         return NSURLPboardType;
     }
 
     // Blacklist types that might contain subframe information.
-    if (lowercasedType == "text/rtf" || lowercasedType == "public.rtf" || lowercasedType == "com.apple.traditional-mac-plain-text")
+    if (type == "text/rtf" || type == "public.rtf" || type == "com.apple.traditional-mac-plain-text")
         return String();
 
-    auto utiType = UTIFromMIMEType(lowercasedType);
+    auto utiType = UTIFromMIMEType(type);
     if (!utiType.isEmpty()) {
         if (auto pbType = adoptCF(UTTypeCopyPreferredTagWithClass(utiType.createCFString().get(), kUTTagClassNSPboardType)))
             return pbType.get();
@@ -490,7 +482,7 @@
     }
 
     // No mapping, just pass the whole string though
-    return lowercasedType;
+    return type;
 }
 
 void Pasteboard::clear(const String& type)

Modified: trunk/Source/WebCore/platform/win/PasteboardWin.cpp (221062 => 221063)


--- trunk/Source/WebCore/platform/win/PasteboardWin.cpp	2017-08-23 01:12:09 UTC (rev 221062)
+++ trunk/Source/WebCore/platform/win/PasteboardWin.cpp	2017-08-23 02:57:21 UTC (rev 221063)
@@ -181,14 +181,12 @@
 
 static ClipboardDataType clipboardTypeFromMIMEType(const String& type)
 {
-    String strippedType = type.stripWhiteSpace();
-
     // two special cases for IE compatibility
-    if (equalLettersIgnoringASCIICase(strippedType, "text") || equalLettersIgnoringASCIICase(strippedType, "text/plain") || strippedType.startsWith("text/plain;", false))
+    if (equalLettersIgnoringASCIICase(type, "text/plain"))
         return ClipboardDataTypeText;
-    if (equalLettersIgnoringASCIICase(strippedType, "url") || equalLettersIgnoringASCIICase(strippedType, "text/uri-list"))
+    if (equalLettersIgnoringASCIICase(type, "text/uri-list"))
         return ClipboardDataTypeURL;
-    if (equalLettersIgnoringASCIICase(strippedType, "text/html"))
+    if (equalLettersIgnoringASCIICase(type, "text/html"))
         return ClipboardDataTypeTextHTML;
 
     return ClipboardDataTypeNone;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to