Consolidate WebContentReaderIOS and WebContentReaderMac into WebContentReaderCocoa
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 9 May 2018 00:37:55 +0000 (00:37 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 9 May 2018 00:37:55 +0000 (00:37 +0000)
https://bugs.webkit.org/show_bug.cgi?id=185340

Reviewed by Tim Horton.

Source/WebCore:

WebContentReader::readURL is currently the only method implemented separately in iOS and macOS platform
WebContentReader files. The implementation across macOS and iOS is nearly identical (with some exceptions with
the way iOS handles file URLs and plain text editing), so we can merge these into a single method
WebContentReaderCocoa and delete WebContentReaderIOS and WebContentReaderMac.

This also has the added bonus of fixing a latent bug in WebContentReaderMac, wherein URLs written to the
pasteboard using -[NSPasteboard writeObjects:] are currently pasted as empty anchor elements. In this case, the
link title isn't made explicit, so the `title` passed in to WebContentReader::readURL is empty. On iOS, we have
code to fall back to pasting the absolute string of the URL if the title is empty, but on macOS, we'll just use
this empty string as the title of the anchor.

Test: PasteMixedContent.PasteURLWrittenToPasteboardUsingWriteObjects

* SourcesCocoa.txt:
* WebCore.xcodeproj/project.pbxproj:
* editing/cocoa/WebContentReaderCocoa.mm:
(WebCore::WebContentReader::readURL):
* editing/ios/WebContentReaderIOS.mm: Removed.
* editing/mac/WebContentReaderMac.mm: Removed.

Tools:

Adds a new API test to verify that URLs written using -[NSPasteboard writeObjects:] are pasted as anchors with
non-empty text in a contenteditable.

* TestWebKitAPI/Tests/WebKitCocoa/PasteMixedContent.mm:
(TestWebKitAPI::TEST):

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

Source/WebCore/ChangeLog
Source/WebCore/SourcesCocoa.txt
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm
Source/WebCore/editing/ios/WebContentReaderIOS.mm [deleted file]
Source/WebCore/editing/mac/WebContentReaderMac.mm [deleted file]
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/PasteMixedContent.mm

index fd87e4e..01db9a0 100644 (file)
@@ -1,3 +1,30 @@
+2018-05-08  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Consolidate WebContentReaderIOS and WebContentReaderMac into WebContentReaderCocoa
+        https://bugs.webkit.org/show_bug.cgi?id=185340
+
+        Reviewed by Tim Horton.
+
+        WebContentReader::readURL is currently the only method implemented separately in iOS and macOS platform
+        WebContentReader files. The implementation across macOS and iOS is nearly identical (with some exceptions with
+        the way iOS handles file URLs and plain text editing), so we can merge these into a single method
+        WebContentReaderCocoa and delete WebContentReaderIOS and WebContentReaderMac.
+
+        This also has the added bonus of fixing a latent bug in WebContentReaderMac, wherein URLs written to the
+        pasteboard using -[NSPasteboard writeObjects:] are currently pasted as empty anchor elements. In this case, the
+        link title isn't made explicit, so the `title` passed in to WebContentReader::readURL is empty. On iOS, we have
+        code to fall back to pasting the absolute string of the URL if the title is empty, but on macOS, we'll just use
+        this empty string as the title of the anchor.
+
+        Test: PasteMixedContent.PasteURLWrittenToPasteboardUsingWriteObjects
+
+        * SourcesCocoa.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        * editing/cocoa/WebContentReaderCocoa.mm:
+        (WebCore::WebContentReader::readURL):
+        * editing/ios/WebContentReaderIOS.mm: Removed.
+        * editing/mac/WebContentReaderMac.mm: Removed.
+
 2018-05-08  Zalan Bujtas  <zalan@apple.com>
 
         [Simple line layout] Cache run resolver.
index 26fb2f0..d1d831d 100644 (file)
@@ -84,7 +84,6 @@ editing/cocoa/WebContentReaderCocoa.mm
 
 editing/ios/DictationCommandIOS.cpp
 editing/ios/EditorIOS.mm
-editing/ios/WebContentReaderIOS.mm
 
 editing/mac/AlternativeTextUIController.mm
 editing/mac/DictionaryLookup.mm
@@ -92,7 +91,6 @@ editing/mac/EditorMac.mm
 editing/mac/FrameSelectionMac.mm
 editing/mac/TextAlternativeWithRange.mm
 editing/mac/TextUndoInsertionMarkupMac.mm
-editing/mac/WebContentReaderMac.mm
 
 editing/ios/AutofillElements.cpp
 
index 0108e78..f17294b 100644 (file)
                9B03D8061BB3110D00B764DA /* JSDOMBindingInternalsBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSDOMBindingInternalsBuiltins.h; sourceTree = "<group>"; };
                9B03D8061BB3110D00B764E8 /* WritableStreamBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WritableStreamBuiltins.h; sourceTree = "<group>"; };
                9B03D8061BB3110D00B764E9 /* WritableStreamInternalsBuiltins.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WritableStreamInternalsBuiltins.h; sourceTree = "<group>"; };
-               9B0811231F67CDC00074BDE2 /* WebContentReaderIOS.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WebContentReaderIOS.mm; sourceTree = "<group>"; };
                9B098BD51F3D6033002DD562 /* DataTransferItem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DataTransferItem.cpp; sourceTree = "<group>"; };
                9B098BDD1F3D673D002DD562 /* JSDataTransferItem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSDataTransferItem.cpp; sourceTree = "<group>"; };
                9B098BDE1F3D673D002DD562 /* JSDataTransferItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSDataTransferItem.h; sourceTree = "<group>"; };
                9B6C41521344949000085B62 /* StringWithDirection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringWithDirection.h; sourceTree = "<group>"; };
                9B714E1E1C91166900AC0E92 /* EventPath.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EventPath.cpp; sourceTree = "<group>"; };
                9B714E1F1C91166900AC0E92 /* EventPath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EventPath.h; sourceTree = "<group>"; };
-               9B9299AD1F67865B006723C2 /* WebContentReaderMac.mm */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; path = WebContentReaderMac.mm; sourceTree = "<group>"; };
                9B9299B01F6796A4006723C2 /* WebContentReaderCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebContentReaderCocoa.mm; sourceTree = "<group>"; };
                9BA273F3172206BB0097CE47 /* LogicalSelectionOffsetCaches.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LogicalSelectionOffsetCaches.h; sourceTree = "<group>"; };
                9BA827781F06156500F71E75 /* NavigationDisabler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NavigationDisabler.h; sourceTree = "<group>"; };
                                D0BD4F5A1408850F006839B6 /* DictationCommandIOS.cpp */,
                                D0BD4F5B1408850F006839B6 /* DictationCommandIOS.h */,
                                FED13D390CEA934600D89466 /* EditorIOS.mm */,
-                               9B0811231F67CDC00074BDE2 /* WebContentReaderIOS.mm */,
                        );
                        path = ios;
                        sourceTree = "<group>";
                                CE7B2DB21586ABAD0098B3FA /* TextAlternativeWithRange.mm */,
                                29FAF4B5195AB08900A522DC /* TextUndoInsertionMarkupMac.h */,
                                29498681195341940072D2BD /* TextUndoInsertionMarkupMac.mm */,
-                               9B9299AD1F67865B006723C2 /* WebContentReaderMac.mm */,
                        );
                        path = mac;
                        sourceTree = "<group>";
index 27cd6c8..522ebb0 100644 (file)
@@ -39,6 +39,7 @@
 #import "FrameLoader.h"
 #import "FrameLoaderClient.h"
 #import "HTMLAttachmentElement.h"
+#import "HTMLBRElement.h"
 #import "HTMLBodyElement.h"
 #import "HTMLIFrameElement.h"
 #import "HTMLImageElement.h"
@@ -653,4 +654,35 @@ bool WebContentReader::readFilePaths(const Vector<String>& paths)
     return true;
 }
 
+bool WebContentReader::readURL(const URL& url, const String& title)
+{
+    if (url.isEmpty())
+        return false;
+
+#if PLATFORM(IOS)
+    // FIXME: This code shouldn't be accessing selection and changing the behavior.
+    if (!frame.editor().client()->hasRichlyEditableSelection()) {
+        if (readPlainText([(NSURL *)url absoluteString]))
+            return true;
+    }
+
+    if ([(NSURL *)url isFileURL])
+        return false;
+#endif // PLATFORM(IOS)
+
+    auto document = makeRef(*frame.document());
+    auto anchor = HTMLAnchorElement::create(document.get());
+    anchor->setAttributeWithoutSynchronization(HTMLNames::hrefAttr, url.string());
+
+    NSString *linkText = title.isEmpty() ? [(NSURL *)url absoluteString] : (NSString *)title;
+    anchor->appendChild(document->createTextNode([linkText precomposedStringWithCanonicalMapping]));
+
+    auto newFragment = document->createDocumentFragment();
+    if (fragment)
+        newFragment->appendChild(HTMLBRElement::create(document.get()));
+    newFragment->appendChild(anchor);
+    addFragment(WTFMove(newFragment));
+    return true;
+}
+
 }
diff --git a/Source/WebCore/editing/ios/WebContentReaderIOS.mm b/Source/WebCore/editing/ios/WebContentReaderIOS.mm
deleted file mode 100644 (file)
index 8de5c15..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (C) 2006-2017 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#import "config.h"
-#import "WebContentReader.h"
-
-#if PLATFORM(IOS)
-
-#import "Document.h"
-#import "DocumentFragment.h"
-#import "Editor.h"
-#import "EditorClient.h"
-#import "HTMLAnchorElement.h"
-#import "HTMLBRElement.h"
-#import "HTMLNames.h"
-#import "Text.h"
-
-namespace WebCore {
-
-bool WebContentReader::readURL(const URL& url, const String& title)
-{
-    if (url.isEmpty())
-        return false;
-
-    // FIXME: This code shoudln't be accessing selection and changing the behavior.
-    if (!frame.editor().client()->hasRichlyEditableSelection()) {
-        if (readPlainText([(NSURL *)url absoluteString]))
-            return true;
-    }
-
-    if ([(NSURL *)url isFileURL])
-        return false;
-
-    auto document = makeRef(*frame.document());
-    auto anchor = HTMLAnchorElement::create(document.get());
-    anchor->setAttributeWithoutSynchronization(HTMLNames::hrefAttr, url.string());
-
-    String linkText = title.length() ? title : String([[(NSURL *)url absoluteString] precomposedStringWithCanonicalMapping]);
-    anchor->appendChild(document->createTextNode(linkText));
-
-    auto newFragment = document->createDocumentFragment();
-    if (fragment)
-        newFragment->appendChild(HTMLBRElement::create(document.get()));
-    newFragment->appendChild(anchor);
-    addFragment(WTFMove(newFragment));
-    return true;
-}
-
-}
-
-#endif // PLATFORM(IOS)
diff --git a/Source/WebCore/editing/mac/WebContentReaderMac.mm b/Source/WebCore/editing/mac/WebContentReaderMac.mm
deleted file mode 100644 (file)
index eef0672..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright (C) 2006-2017 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#import "config.h"
-#import "WebContentReader.h"
-
-#if PLATFORM(MAC)
-
-#import "ArchiveResource.h"
-#import "Document.h"
-#import "DocumentFragment.h"
-#import "DocumentLoader.h"
-#import "Editing.h"
-#import "Editor.h"
-#import "EditorClient.h"
-#import "File.h"
-#import "FragmentScriptingPermission.h"
-#import "FrameLoader.h"
-#import "FrameLoaderClient.h"
-#import "HTMLAnchorElement.h"
-#import "HTMLNames.h"
-#import "LegacyWebArchive.h"
-#import "Settings.h"
-#import "Text.h"
-#import "WebCoreNSURLExtras.h"
-#import "markup.h"
-
-namespace WebCore {
-
-bool WebContentReader::readURL(const URL& url, const String& title)
-{
-    if (url.string().isEmpty())
-        return false;
-
-    auto anchor = HTMLAnchorElement::create(*frame.document());
-    anchor->setAttributeWithoutSynchronization(HTMLNames::hrefAttr, url.string());
-    anchor->appendChild(frame.document()->createTextNode([title precomposedStringWithCanonicalMapping]));
-
-    fragment = frame.document()->createDocumentFragment();
-    fragment->appendChild(anchor);
-    return true;
-}
-
-}
-
-#endif // PLATFORM(MAC)
index 0611b28..2f5e347 100644 (file)
@@ -1,3 +1,16 @@
+2018-05-08  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Consolidate WebContentReaderIOS and WebContentReaderMac into WebContentReaderCocoa
+        https://bugs.webkit.org/show_bug.cgi?id=185340
+
+        Reviewed by Tim Horton.
+
+        Adds a new API test to verify that URLs written using -[NSPasteboard writeObjects:] are pasted as anchors with
+        non-empty text in a contenteditable.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/PasteMixedContent.mm:
+        (TestWebKitAPI::TEST):
+
 2018-05-08  Valerie R Young  <valerie@bocoup.com>
 
         test262/Runner.pm: create results dir for results of test262 run
index 04114ab..9f4ae5f 100644 (file)
@@ -237,6 +237,20 @@ TEST(PasteMixedContent, ImageDataAndPlainTextAndURLAndHTML)
     EXPECT_FALSE([[webView stringByEvaluatingJavaScript:@"rawHTMLData.textContent"] containsString:@"script"]);
 }
 
+TEST(PasteMixedContent, PasteURLWrittenToPasteboardUsingWriteObjects)
+{
+    NSString *urlToCopy = @"https://www.webkit.org/";
+
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400)]);
+    [webView synchronouslyLoadHTMLString:@"<body contenteditable></body><script>document.body.focus()</script>"];
+    [[NSPasteboard generalPasteboard] clearContents];
+    [[NSPasteboard generalPasteboard] writeObjects:@[ [NSURL URLWithString:urlToCopy] ]];
+    [webView paste:nil];
+
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"document.querySelector('a').href"], urlToCopy);
+    EXPECT_WK_STREQ([webView stringByEvaluatingJavaScript:@"document.querySelector('a').textContent"], urlToCopy);
+}
+
 } // namespace TestWebKitAPI
 
 #endif // PLATFORM(MAC) && WK_API_ENABLED