[iOS] API test UIPasteboardTests.DataTransferGetDataWhenPastingPlatformRepresentation...
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 30 Jan 2018 23:05:57 +0000 (23:05 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 30 Jan 2018 23:05:57 +0000 (23:05 +0000)
https://bugs.webkit.org/show_bug.cgi?id=182307
<rdar://problem/36041485>

Reviewed by Ryosuke Niwa.

Adjusts a failing API test that we forgot to rebaseline after r223440, which ensures that the result of reading
"text/html" from the DataTransfer is sanitized markup. To do this, teach the test harness
(dump-datatransfer-types.html) to ignore inline styles in markup if a flag is set, and then set that flag in
UIPasteboardTests.DataTransferGetDataWhenPastingPlatformRepresentations.

* TestWebKitAPI/Tests/WebKitCocoa/dump-datatransfer-types.html:
* TestWebKitAPI/Tests/ios/UIPasteboardTests.mm:
(TestWebKitAPI::TEST):

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

Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/dump-datatransfer-types.html
Tools/TestWebKitAPI/Tests/ios/UIPasteboardTests.mm

index a4ccb1d..37307aa 100644 (file)
@@ -1,3 +1,20 @@
+2018-01-30  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        [iOS] API test UIPasteboardTests.DataTransferGetDataWhenPastingPlatformRepresentations fails after r223440
+        https://bugs.webkit.org/show_bug.cgi?id=182307
+        <rdar://problem/36041485>
+
+        Reviewed by Ryosuke Niwa.
+
+        Adjusts a failing API test that we forgot to rebaseline after r223440, which ensures that the result of reading
+        "text/html" from the DataTransfer is sanitized markup. To do this, teach the test harness
+        (dump-datatransfer-types.html) to ignore inline styles in markup if a flag is set, and then set that flag in
+        UIPasteboardTests.DataTransferGetDataWhenPastingPlatformRepresentations.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/dump-datatransfer-types.html:
+        * TestWebKitAPI/Tests/ios/UIPasteboardTests.mm:
+        (TestWebKitAPI::TEST):
+
 2018-01-30  Sergio Villar Senin  <svillar@igalia.com>
 
         [WebVR][GTK][WPE] Exclude OpenVR from tarballs
index b730a1d..d6e3542 100644 (file)
@@ -55,6 +55,9 @@ result = {};
 // Setting this flag to `true` allows the page to write custom MIME types via the DataTransfer when dragging or copying.
 writeCustomData = false;
 
+// Setting this flag to `true` strips out inline styles in markup generated when getting "text/html" from a DataTransfer.
+ignoreInlineStyles = false;
+
 // Entries in this dictionary will be supplied as custom data using DataTransfer.setData when dragging or copying.
 // This is prepopulated with sample data by default, but tests may override this as needed.
 customData = {
@@ -66,11 +69,24 @@ customData = {
     "baz/uri-list" : "https://www.webkit.org"
 };
 
+function stripInlineStylesFromPasteboardData(data)
+{
+    const parsedDocument = new DOMParser().parseFromString(data, "text/html");
+    const treeWalker = parsedDocument.createTreeWalker(parsedDocument.body, NodeFilter.SHOW_ELEMENT);
+    while (treeWalker.nextNode())
+        treeWalker.currentNode.removeAttribute("style");
+    return parsedDocument.body.innerHTML;
+}
+
 function updateResultWithEvent(event) {
     let pasteboard = event.dataTransfer || event.clipboardData;
     const eventData = {};
-    for (const type of pasteboard.types)
-        eventData[type] = pasteboard.getData(type);
+    for (const type of pasteboard.types) {
+        let pasteboardData = pasteboard.getData(type);
+        if (ignoreInlineStyles && type === "text/html")
+            pasteboardData = stripInlineStylesFromPasteboardData(pasteboardData);
+        eventData[type] = pasteboardData;
+    }
     result[event.type] = eventData;
     if (event.type === "paste" || event.type === "drop") {
         output.value = JSON.stringify(result, null, "    ");
index 0b0560c..2550562 100644 (file)
@@ -167,11 +167,12 @@ TEST(UIPasteboardTests, DataTransferGetDataWhenPastingURL)
 TEST(UIPasteboardTests, DataTransferGetDataWhenPastingPlatformRepresentations)
 {
     auto webView = setUpWebViewForPasteboardTests(@"dump-datatransfer-types");
+    [webView stringByEvaluatingJavaScript:@"ignoreInlineStyles = true"];
 
     // This simulates how a native app on iOS might write to the pasteboard when copying.
     RetainPtr<NSURL> testURL = [NSURL URLWithString:@"https://www.apple.com/"];
     RetainPtr<NSString> testPlainTextString = @"WebKit";
-    RetainPtr<NSString> testMarkupString = @"<a href='https://www.webkit.org/'>The WebKit Project</a>";
+    RetainPtr<NSString> testMarkupString = @"<a href=\"https://www.webkit.org/\">The WebKit Project</a>";
     auto itemProvider = adoptNS([[NSItemProvider alloc] init]);
     [itemProvider registerDataRepresentationForTypeIdentifier:(NSString *)kUTTypeHTML visibility:NSItemProviderRepresentationVisibilityAll loadHandler:^NSProgress *(DataLoadCompletionBlock completionHandler)
     {