WK1 and WK2 should share more candidate request code
authorbdakin@apple.com <bdakin@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 15 Jan 2016 00:07:36 +0000 (00:07 +0000)
committerbdakin@apple.com <bdakin@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 15 Jan 2016 00:07:36 +0000 (00:07 +0000)
https://bugs.webkit.org/show_bug.cgi?id=153108

Reviewed by Simon Fraser.

requestCandidatesForSelection() does not need to be exposed as an
EditorClient function. WK1 can just call invoke this code from the existing
respondToChangedSelection EditorClient function, which is what WK2 does.
Source/WebCore:

* editing/Editor.cpp:
(WebCore::Editor::respondToChangedSelection):
* loader/EmptyClients.h:
* page/EditorClient.h:
(WebCore::EditorClient::supportsGlobalSelection):

Source/WebKit/mac:

* WebCoreSupport/WebEditorClient.h:
* WebCoreSupport/WebEditorClient.mm:
(WebEditorClient::respondToChangedSelection):

Cleanup — use some code that was moved to WebCore::Editor.
(WebEditorClient::requestCandidatesForSelection):
(WebEditorClient::handleRequestedCandidates):
(textCheckingResultFromNSTextCheckingResult):
(WebEditorClient::handleAcceptedCandidate):
(candidateRangeForSelection): Deleted.
(candidateWouldReplaceText): Deleted.

Source/WebKit2:

* WebProcess/WebCoreSupport/WebEditorClient.h:

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

Source/WebCore/ChangeLog
Source/WebCore/editing/Editor.cpp
Source/WebCore/loader/EmptyClients.h
Source/WebCore/page/EditorClient.h
Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebCoreSupport/WebEditorClient.h
Source/WebKit/mac/WebCoreSupport/WebEditorClient.mm
Source/WebKit2/ChangeLog
Source/WebKit2/WebProcess/WebCoreSupport/WebEditorClient.h

index e5a5481..62e4e01 100644 (file)
@@ -1,5 +1,21 @@
 2016-01-14  Beth Dakin  <bdakin@apple.com>
 
+        WK1 and WK2 should share more candidate request code
+        https://bugs.webkit.org/show_bug.cgi?id=153108
+
+        Reviewed by Simon Fraser.
+
+        requestCandidatesForSelection() does not need to be exposed as an 
+        EditorClient function. WK1 can just call invoke this code from the existing 
+        respondToChangedSelection EditorClient function, which is what WK2 does.
+        * editing/Editor.cpp:
+        (WebCore::Editor::respondToChangedSelection):
+        * loader/EmptyClients.h:
+        * page/EditorClient.h:
+        (WebCore::EditorClient::supportsGlobalSelection):
+
+2016-01-14  Beth Dakin  <bdakin@apple.com>
+
         WK2: Request completion candidates when needed
         https://bugs.webkit.org/show_bug.cgi?id=153040
         -and corresponding-
index bf2cbdc..fe477cb 100644 (file)
@@ -3264,11 +3264,6 @@ void Editor::respondToChangedSelection(const VisibleSelection&, FrameSelection::
     if (client())
         client()->respondToChangedSelection(&m_frame);
 
-#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
-    if (client() && canEdit())
-        client()->requestCandidatesForSelection(m_frame.selection().selection());
-#endif
-
 #if ENABLE(TELEPHONE_NUMBER_DETECTION) && !PLATFORM(IOS)
     if (shouldDetectTelephoneNumbers())
         m_telephoneNumberDetectionUpdateTimer.startOneShot(0);
index 1a40b6d..abd96d8 100644 (file)
@@ -537,10 +537,6 @@ public:
     virtual void willSetInputMethodState() override { }
     virtual void setInputMethodState(bool) override { }
 
-#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
-    virtual void requestCandidatesForSelection(const VisibleSelection&) override { }
-#endif
-
 private:
     EmptyTextCheckerClient m_textCheckerClient;
 };
index eea62e2..3c53a53 100644 (file)
@@ -186,10 +186,6 @@ public:
     // Support for global selections, used on platforms like the X Window System that treat
     // selection as a type of clipboard.
     virtual bool supportsGlobalSelection() { return false; }
-
-#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
-    virtual void requestCandidatesForSelection(const VisibleSelection&) = 0;
-#endif
 };
 
 }
index 09e3762..82657df 100644 (file)
@@ -1,3 +1,25 @@
+2016-01-14  Beth Dakin  <bdakin@apple.com>
+
+        WK1 and WK2 should share more candidate request code
+        https://bugs.webkit.org/show_bug.cgi?id=153108
+
+        Reviewed by Simon Fraser.
+
+        requestCandidatesForSelection() does not need to be exposed as an 
+        EditorClient function. WK1 can just call invoke this code from the existing 
+        respondToChangedSelection EditorClient function, which is what WK2 does.
+        * WebCoreSupport/WebEditorClient.h:
+        * WebCoreSupport/WebEditorClient.mm:
+        (WebEditorClient::respondToChangedSelection):
+
+        Cleanup — use some code that was moved to WebCore::Editor.
+        (WebEditorClient::requestCandidatesForSelection):
+        (WebEditorClient::handleRequestedCandidates):
+        (textCheckingResultFromNSTextCheckingResult):
+        (WebEditorClient::handleAcceptedCandidate):
+        (candidateRangeForSelection): Deleted.
+        (candidateWouldReplaceText): Deleted.
+
 2016-01-13  Chris Dumez  <cdumez@apple.com>
 
         Unreviewed, rolling out r194900.
index 921f79b..c70cb97 100644 (file)
@@ -170,7 +170,7 @@ private:
     virtual void requestCheckingOfString(PassRefPtr<WebCore::TextCheckingRequest>) override;
 
 #if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
-    virtual void requestCandidatesForSelection(const WebCore::VisibleSelection&) override;
+    void requestCandidatesForSelection(const WebCore::VisibleSelection&);
     void handleRequestedCandidates(NSInteger, NSArray<NSTextCheckingResult *> *);
     void handleAcceptedCandidate(NSTextCheckingResult *);
 #endif
index fff0c06..dcb06f5 100644 (file)
@@ -367,6 +367,11 @@ void WebEditorClient::respondToChangedSelection(Frame* frame)
     if (![m_webView _isClosing])
         WebThreadPostNotification(WebViewDidChangeSelectionNotification, m_webView, nil);
 #endif
+
+#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
+    if (frame->editor().canEdit())
+        requestCandidatesForSelection(frame->selection().selection());
+#endif
 }
 
 void WebEditorClient::discardedComposition(Frame*)
@@ -1124,15 +1129,12 @@ void WebEditorClient::requestCandidatesForSelection(const VisibleSelection& sele
 
     VisiblePosition selectionStart = selection.visibleStart();
     VisiblePosition selectionEnd = selection.visibleEnd();
-
-    // Use the surrounding paragraphs of text as context.
     VisiblePosition paragraphStart = startOfParagraph(selectionStart);
     VisiblePosition paragraphEnd = endOfParagraph(selectionEnd);
 
     int lengthToSelectionStart = TextIterator::rangeLength(makeRange(paragraphStart, selectionStart).get());
     int lengthToSelectionEnd = TextIterator::rangeLength(makeRange(paragraphStart, selectionEnd).get());
     NSRange rangeForCandidates = NSMakeRange(lengthToSelectionStart, lengthToSelectionEnd - lengthToSelectionStart);
-
     String fullPlainTextStringOfParagraph = plainText(makeRange(paragraphStart, paragraphEnd).get());
 
     NSTextCheckingTypes checkingTypes = NSTextCheckingTypeSpelling | NSTextCheckingTypeReplacement | NSTextCheckingTypeCorrection;
@@ -1146,20 +1148,6 @@ void WebEditorClient::requestCandidatesForSelection(const VisibleSelection& sele
     }];
 }
 
-static RefPtr<Range> candidateRangeForSelection(const VisibleSelection& selection, Frame* frame)
-{
-    return selection.isCaret() ? wordRangeFromPosition(selection.start()) : frame->selection().toNormalizedRange();
-}
-
-static bool candidateWouldReplaceText(const VisibleSelection& selection)
-{
-    // If the character behind the caret in the current selection is anything but a space or a newline then we should
-    // replace the whole current word with the candidate.
-    UChar32 characterAfterSelection, characterBeforeSelection, twoCharacterBeforeSelection = 0;
-    charactersAroundPosition(selection.visibleStart(), characterAfterSelection, characterBeforeSelection, twoCharacterBeforeSelection);
-    return !(characterBeforeSelection == '\0' || characterBeforeSelection == '\n' || characterBeforeSelection == ' ');
-}
-
 void WebEditorClient::handleRequestedCandidates(NSInteger sequenceNumber, NSArray<NSTextCheckingResult *> *candidates)
 {
     Frame* frame = core([m_webView _selectedOrMainFrame]);
@@ -1170,14 +1158,7 @@ void WebEditorClient::handleRequestedCandidates(NSInteger sequenceNumber, NSArra
     if (selection != m_lastSelectionForRequestedCandidates)
         return;
 
-    RefPtr<Range> rangeForCurrentlyTypedString = candidateRangeForSelection(selection, frame);
-    NSString *currentlyTypedString;
-    if (rangeForCurrentlyTypedString && candidateWouldReplaceText(selection))
-        currentlyTypedString = plainText(rangeForCurrentlyTypedString.get());
-    else
-        currentlyTypedString = @"";
-
-    RefPtr<Range> selectedRange = frame->selection().selection().toNormalizedRange();
+    RefPtr<Range> selectedRange = selection.toNormalizedRange();
     if (!selectedRange)
         return;
 
@@ -1188,7 +1169,7 @@ void WebEditorClient::handleRequestedCandidates(NSInteger sequenceNumber, NSArra
         rectForSelectionCandidates = frame->view()->contentsToWindow(quads[0].enclosingBoundingBox());
 
     auto weakEditor = m_weakPtrFactory.createWeakPtr();
-    [[NSSpellChecker sharedSpellChecker] showCandidates:candidates forString:currentlyTypedString inRect:rectForSelectionCandidates view:m_webView completionHandler:[weakEditor](NSTextCheckingResult *acceptedCandidate) {
+    [[NSSpellChecker sharedSpellChecker] showCandidates:candidates forString:frame->editor().stringForCandidateRequest() inRect:rectForSelectionCandidates view:m_webView completionHandler:[weakEditor](NSTextCheckingResult *acceptedCandidate) {
         dispatch_async(dispatch_get_main_queue(), ^{
             if (!weakEditor)
                 return;
@@ -1198,6 +1179,32 @@ void WebEditorClient::handleRequestedCandidates(NSInteger sequenceNumber, NSArra
 
 }
 
+static WebCore::TextCheckingResult textCheckingResultFromNSTextCheckingResult(NSTextCheckingResult *nsResult)
+{
+    WebCore::TextCheckingResult result;
+
+    switch ([nsResult resultType]) {
+    case NSTextCheckingTypeSpelling:
+        result.type = WebCore::TextCheckingTypeSpelling;
+        break;
+    case NSTextCheckingTypeReplacement:
+        result.type = WebCore::TextCheckingTypeReplacement;
+        break;
+    case NSTextCheckingTypeCorrection:
+        result.type = WebCore::TextCheckingTypeCorrection;
+        break;
+    default:
+        result.type = WebCore::TextCheckingTypeNone;
+    }
+
+    NSRange resultRange = [nsResult range];
+    result.location = resultRange.location;
+    result.length = resultRange.length;
+    result.replacement = [nsResult replacementString];
+
+    return result;
+}
+
 void WebEditorClient::handleAcceptedCandidate(NSTextCheckingResult *acceptedCandidate)
 {
     Frame* frame = core([m_webView _selectedOrMainFrame]);
@@ -1208,16 +1215,7 @@ void WebEditorClient::handleAcceptedCandidate(NSTextCheckingResult *acceptedCand
     if (selection != m_lastSelectionForRequestedCandidates)
         return;
 
-    RefPtr<Range> candidateRange = candidateRangeForSelection(selection, frame);
-
-    frame->editor().setIgnoreCompositionSelectionChange(true);
-
-    if (candidateWouldReplaceText(selection))
-        frame->selection().setSelectedRange(candidateRange.get(), UPSTREAM, true);
-
-    frame->editor().insertText(acceptedCandidate.replacementString, 0);
-    frame->editor().insertText(String(" "), 0);
-    frame->editor().setIgnoreCompositionSelectionChange(false);
+    frame->editor().handleAcceptedCandidate(textCheckingResultFromNSTextCheckingResult(acceptedCandidate));
 }
 #endif // PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
 
index eee8b80..2df065d 100644 (file)
@@ -1,5 +1,17 @@
 2016-01-14  Beth Dakin  <bdakin@apple.com>
 
+        WK1 and WK2 should share more candidate request code
+        https://bugs.webkit.org/show_bug.cgi?id=153108
+
+        Reviewed by Simon Fraser.
+
+        requestCandidatesForSelection() does not need to be exposed as an 
+        EditorClient function. WK1 can just call invoke this code from the existing 
+        respondToChangedSelection EditorClient function, which is what WK2 does.
+        * WebProcess/WebCoreSupport/WebEditorClient.h:
+
+2016-01-14  Beth Dakin  <bdakin@apple.com>
+
         WK2: Request completion candidates when needed
         https://bugs.webkit.org/show_bug.cgi?id=153040
         -and corresponding-
index 71c24f8..61b4327 100644 (file)
@@ -150,10 +150,6 @@ private:
     virtual void setInputMethodState(bool enabled) override;
     virtual void requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest>) override;
 
-#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
-    virtual void requestCandidatesForSelection(const WebCore::VisibleSelection&) override { }
-#endif
-
 #if PLATFORM(GTK)
     virtual bool shouldShowUnicodeMenu() override;
 #endif