Title: [289673] trunk/Source
Revision
289673
Author
[email protected]
Date
2022-02-11 15:03:20 -0800 (Fri, 11 Feb 2022)

Log Message

Enable grammar checking on Mac Catalyst.
https://bugs.webkit.org/show_bug.cgi?id=236479

Reviewed by Devin Rousso.

Source/WebCore:

* editing/Editor.cpp:
(WebCore::Editor::markMisspellingsAfterTypingToWord):
* platform/graphics/cocoa/GraphicsContextCocoa.mm:
(WebCore::grammarColor):
(WebCore::colorForMarkerLineStyle):

Source/WebKit:

* Platform/spi/ios/UIKitSPI.h:
* UIProcess/ios/TextCheckerIOS.mm:
(WebKit::mutableState):
(WebKit::TextChecker::setGrammarCheckingEnabled):
(WebKit::TextChecker::checkTextOfParagraph):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (289672 => 289673)


--- trunk/Source/WebCore/ChangeLog	2022-02-11 22:57:33 UTC (rev 289672)
+++ trunk/Source/WebCore/ChangeLog	2022-02-11 23:03:20 UTC (rev 289673)
@@ -1,3 +1,16 @@
+2022-02-11  Megan Gardner  <[email protected]>
+
+        Enable grammar checking on Mac Catalyst.
+        https://bugs.webkit.org/show_bug.cgi?id=236479
+
+        Reviewed by Devin Rousso.
+
+        * editing/Editor.cpp:
+        (WebCore::Editor::markMisspellingsAfterTypingToWord):
+        * platform/graphics/cocoa/GraphicsContextCocoa.mm:
+        (WebCore::grammarColor):
+        (WebCore::colorForMarkerLineStyle):
+
 2022-02-11  Chris Dumez  <[email protected]>
 
         Fix MIME type check for classic worker script fetches

Modified: trunk/Source/WebCore/editing/Editor.cpp (289672 => 289673)


--- trunk/Source/WebCore/editing/Editor.cpp	2022-02-11 22:57:33 UTC (rev 289672)
+++ trunk/Source/WebCore/editing/Editor.cpp	2022-02-11 23:03:20 UTC (rev 289673)
@@ -2522,7 +2522,23 @@
 
     auto adjacentWords = VisibleSelection(startOfWord(wordStart, LeftWordIfOnBoundary), endOfWord(wordStart, RightWordIfOnBoundary));
     auto adjacentWordRange = adjacentWords.toNormalizedRange();
-    markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWordRange, adjacentWordRange, adjacentWordRange);
+
+#if ENABLE(MAC_CATALYST_GRAMMAR_CHECKING)
+    if (isGrammarCheckingEnabled()) {
+        textCheckingOptions.add(TextCheckingType::Grammar);
+        auto sentenceStart = startOfSentence(wordStart);
+        auto sentenceEnd = endOfSentence(wordStart);
+        VisibleSelection fullSentence(sentenceStart, sentenceEnd);
+        auto fullSentenceRange = fullSentence.toNormalizedRange();
+        if (!fullSentenceRange)
+            return;
+
+        markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWordRange, adjacentWordRange, fullSentenceRange);
+    } else
+#endif
+    {
+        markAllMisspellingsAndBadGrammarInRanges(textCheckingOptions, adjacentWordRange, adjacentWordRange, adjacentWordRange);
+    }
 #else
 #if !USE(AUTOMATIC_TEXT_REPLACEMENT)
     UNUSED_PARAM(doReplacement);

Modified: trunk/Source/WebCore/platform/graphics/cocoa/GraphicsContextCocoa.mm (289672 => 289673)


--- trunk/Source/WebCore/platform/graphics/cocoa/GraphicsContextCocoa.mm	2022-02-11 22:57:33 UTC (rev 289672)
+++ trunk/Source/WebCore/platform/graphics/cocoa/GraphicsContextCocoa.mm	2022-02-11 23:03:20 UTC (rev 289673)
@@ -60,6 +60,15 @@
 // NSColor, NSBezierPath, and NSGraphicsContext calls do not raise exceptions
 // so we don't block exceptions.
 
+#if USE(APPLE_INTERNAL_SDK)
+#include <WebKitAdditions/GraphicsContextCocoa.mm>
+#else
+static RetainPtr<CGColorRef> grammarColor(bool useDarkMode)
+{
+    return cachedCGColor(useDarkMode ? SRGBA<uint8_t> { 50, 215, 75, 217 } : SRGBA<uint8_t> { 25, 175, 50, 191 });
+}
+#endif
+
 static bool drawFocusRingAtTime(CGContextRef context, NSTimeInterval timeOffset, const Color& color)
 {
 #if USE(APPKIT)
@@ -170,9 +179,8 @@
     case DocumentMarkerLineStyle::Mode::TextCheckingDictationPhraseWithAlternatives:
     case DocumentMarkerLineStyle::Mode::AutocorrectionReplacement:
         return cachedCGColor(useDarkMode ? SRGBA<uint8_t> { 40, 145, 255, 217 } : SRGBA<uint8_t> { 0, 122, 255, 191 });
-    // Green
     case DocumentMarkerLineStyle::Mode::Grammar:
-        return cachedCGColor(useDarkMode ? SRGBA<uint8_t> { 50, 215, 75, 217 } : SRGBA<uint8_t> { 25, 175, 50, 191 });
+        return grammarColor(useDarkMode);
     }
 }
 

Modified: trunk/Source/WebKit/ChangeLog (289672 => 289673)


--- trunk/Source/WebKit/ChangeLog	2022-02-11 22:57:33 UTC (rev 289672)
+++ trunk/Source/WebKit/ChangeLog	2022-02-11 23:03:20 UTC (rev 289673)
@@ -1,3 +1,16 @@
+2022-02-11  Megan Gardner  <[email protected]>
+
+        Enable grammar checking on Mac Catalyst.
+        https://bugs.webkit.org/show_bug.cgi?id=236479
+
+        Reviewed by Devin Rousso.
+
+        * Platform/spi/ios/UIKitSPI.h:
+        * UIProcess/ios/TextCheckerIOS.mm:
+        (WebKit::mutableState):
+        (WebKit::TextChecker::setGrammarCheckingEnabled):
+        (WebKit::TextChecker::checkTextOfParagraph):
+
 2022-02-11  Chris Dumez  <[email protected]>
 
         Fix MIME type check for classic worker script fetches

Modified: trunk/Source/WebKit/Platform/spi/ios/UIKitSPI.h (289672 => 289673)


--- trunk/Source/WebKit/Platform/spi/ios/UIKitSPI.h	2022-02-11 22:57:33 UTC (rev 289672)
+++ trunk/Source/WebKit/Platform/spi/ios/UIKitSPI.h	2022-02-11 23:03:20 UTC (rev 289673)
@@ -1496,6 +1496,13 @@
 @end
 #endif
 
+#if ENABLE(MAC_CATALYST_GRAMMAR_CHECKING)
+@interface UITextChecker ()
++ (BOOL)grammarCheckingEnabled;
+- (NSArray<NSTextCheckingResult *> *)checkString:(NSString *)stringToCheck range:(NSRange)range types:(NSTextCheckingTypes)checkingTypes languages:(NSArray<NSString *> *)languagesArray options:(NSDictionary<NSString *, id> *)options;
+@end
+#endif
+
 @protocol UITextInputInternal <UITextInputPrivate>
 @optional
 @property (nonatomic, readonly) CGRect _selectionClipRect;

Modified: trunk/Source/WebKit/UIProcess/ios/TextCheckerIOS.mm (289672 => 289673)


--- trunk/Source/WebKit/UIProcess/ios/TextCheckerIOS.mm	2022-02-11 22:57:33 UTC (rev 289672)
+++ trunk/Source/WebKit/UIProcess/ios/TextCheckerIOS.mm	2022-02-11 23:03:20 UTC (rev 289673)
@@ -44,7 +44,11 @@
     static NeverDestroyed state = [] {
         TextCheckerState initialState;
         initialState.isContinuousSpellCheckingEnabled = TextChecker::isContinuousSpellCheckingAllowed();
+#if ENABLE(MAC_CATALYST_GRAMMAR_CHECKING)
+        initialState.isGrammarCheckingEnabled = [UITextChecker respondsToSelector:@selector(grammarCheckingEnabled)] && [UITextChecker grammarCheckingEnabled];
+#else
         initialState.isGrammarCheckingEnabled = false;
+#endif
         return initialState;
     }();
     return state;
@@ -73,9 +77,16 @@
     return true;
 }
 
-void TextChecker::setGrammarCheckingEnabled(bool)
+void TextChecker::setGrammarCheckingEnabled(bool isGrammarCheckingEnabled)
 {
-    notImplemented();
+#if ENABLE(MAC_CATALYST_GRAMMAR_CHECKING)
+    if (state().isGrammarCheckingEnabled == isGrammarCheckingEnabled)
+        return;
+
+    mutableState().isGrammarCheckingEnabled = isGrammarCheckingEnabled;
+#else
+    UNUSED_PARAM(isGrammarCheckingEnabled);
+#endif
 }
 
 void TextChecker::setAutomaticSpellingCorrectionEnabled(bool)
@@ -189,7 +200,7 @@
     return addResult.iterator->value;
 }
 
-Vector<TextCheckingResult> TextChecker::checkTextOfParagraph(SpellDocumentTag spellDocumentTag, StringView text, int32_t /* insertionPoint */, OptionSet<TextCheckingType> checkingTypes, bool /* initialCapitalizationEnabled */)
+Vector<TextCheckingResult> TextChecker::checkTextOfParagraph(SpellDocumentTag spellDocumentTag, StringView text, int32_t insertionPoint, OptionSet<TextCheckingType> checkingTypes, bool /* initialCapitalizationEnabled */)
 {
     Vector<TextCheckingResult> results;
     if (!checkingTypes.contains(TextCheckingType::Spelling))
@@ -208,19 +219,72 @@
 
     auto stringToCheck = text.createNSStringWithoutCopying();
     auto range = NSMakeRange(0, [stringToCheck length]);
-    NSUInteger offsetSoFar = 0;
-    do {
-        auto misspelledRange = [textChecker rangeOfMisspelledWordInString:stringToCheck.get() range:range startingAt:offsetSoFar wrap:NO languages:keyboardLanguages];
-        if (misspelledRange.location == NSNotFound)
-            break;
 
-        TextCheckingResult result;
-        result.type = TextCheckingType::Spelling;
-        result.range = misspelledRange;
-        results.append(WTFMove(result));
+#if ENABLE(MAC_CATALYST_GRAMMAR_CHECKING)
+    if ([textChecker respondsToSelector:@selector(checkString:range:types:languages:options:)]) {
+        NSTextCheckingTypes types = 0;
+        if (checkingTypes.contains(TextCheckingType::Spelling))
+            types |= NSTextCheckingTypeSpelling;
+        if (checkingTypes.contains(TextCheckingType::Grammar))
+            types |= NSTextCheckingTypeGrammar;
+        NSDictionary *options = @{
+            @"InsertionPoint" : @(insertionPoint)
+        };
+        NSArray *incomingResults = [textChecker checkString:stringToCheck.get() range:range types:types languages:keyboardLanguages options:options];
+        for (NSTextCheckingResult *incomingResult in incomingResults) {
+            NSTextCheckingType resultType = [incomingResult resultType];
+            ASSERT(incomingResult.range.location != NSNotFound);
+            ASSERT(incomingResult.range.length > 0);
+            auto resultRange = incomingResult.range;
+            if (resultType == NSTextCheckingTypeSpelling && checkingTypes.contains(TextCheckingType::Spelling)) {
+                TextCheckingResult result;
+                result.type = TextCheckingType::Spelling;
+                result.range = resultRange;
+                results.append(result);
+            } else if (resultType == NSTextCheckingTypeGrammar && checkingTypes.contains(TextCheckingType::Grammar)) {
+                TextCheckingResult result;
+                NSArray *details = [incomingResult grammarDetails];
+                result.type = TextCheckingType::Grammar;
+                result.range = resultRange;
+                for (NSDictionary *incomingDetail in details) {
+                    ASSERT(incomingDetail);
+                    GrammarDetail detail;
+                    
+                    NSValue *detailRangeAsNSValue = [incomingDetail objectForKey:@"NSGrammarRange"];
+                    ASSERT(detailRangeAsNSValue);
+                    
+                    NSRange detailNSRange = [detailRangeAsNSValue rangeValue];
+                    ASSERT(detailNSRange.location != NSNotFound);
+                    ASSERT(detailNSRange.length > 0);
+                    
+                    detail.range = detailNSRange;
+                    detail.userDescription = [incomingDetail objectForKey:@"NSGrammarUserDescription"];
+                    NSArray *guesses = [incomingDetail objectForKey:@"NSGrammarCorrections"];
+                    
+                    for (NSString *guess in guesses)
+                        detail.guesses.append(guess);
+                    result.details.append(detail);
+                }
+                results.append(result);
+            }
+        }
+    } else
+#endif
+    {
+        NSUInteger offsetSoFar = 0;
+        do {
+            auto misspelledRange = [textChecker rangeOfMisspelledWordInString:stringToCheck.get() range:range startingAt:offsetSoFar wrap:NO languages:keyboardLanguages];
+            if (misspelledRange.location == NSNotFound)
+                break;
 
-        offsetSoFar = misspelledRange.location + misspelledRange.length;
-    } while (offsetSoFar < [stringToCheck length]);
+            TextCheckingResult result;
+            result.type = TextCheckingType::Spelling;
+            result.range = misspelledRange;
+            results.append(WTFMove(result));
+
+            offsetSoFar = misspelledRange.location + misspelledRange.length;
+        } while (offsetSoFar < [stringToCheck length]);
+    }
     return results;
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to