Title: [282370] trunk
Revision
282370
Author
[email protected]
Date
2021-09-13 15:49:06 -0700 (Mon, 13 Sep 2021)

Log Message

[macOS] -toggleAutomaticSpellingCorrection: menu item validation shouldn't require post-layout editor state
https://bugs.webkit.org/show_bug.cgi?id=230205

Reviewed by Darin Adler.

Source/WebCore:

See WebKit ChangeLog for more details.

* editing/AlternativeTextController.cpp:
(WebCore::AlternativeTextController::canEnableAutomaticSpellingCorrection const):

Slightly improve the performance of this method in the case where we're focusing a text field, by only
consulting the host form control element, rather than both the form control and the contenteditable shadow root
element.

Source/WebKit:

In preparation for https://webkit.org/b/230204, refactor code on macOS that determines whether or not the
"Correct Spelling Automatically" menu bar item should be enabled, such that it doesn't require post-layout
editor state data. This is requisite to avoiding post-layout editor state computation altogether on macOS, when
the touch bar is either unavailable, or hasn't ever been shown in the context of text editing. See below for
more details.

We also take this opportunity to remove support for the unused _WKSelectionAttributeAtStartOfSentence SPI value
in _WKSelectionAttributes, which was originally intended for use in Mail compose in WebKit2 but seems to have
been obviated by the ability to swipe horizontally on the custom input accessory view in Mail compose to switch
between text candidates and the text formatting options.

* Shared/EditorState.cpp:
(WebKit::EditorState::encode const):
(WebKit::EditorState::decode):
(WebKit::EditorState::PostLayoutData::encode const):
(WebKit::EditorState::PostLayoutData::decode):
(WebKit::operator<<):

Remove the `atStartOfSentence` bit on EditorState's post-layout data that was only used to implement the
_WKSelectionAttributeAtStartOfSentence SPI flag value, and also move the `canEnableAutomaticSpellingCorrection`
bit out of post-layout data, and into the EditorState struct. Importantly, computing this bit does not require
or trigger layout, and this refactoring will allow us to completely forgo post-layout data on configurations
that do not require it (i.e. when the text touch bar is not active).

* Shared/EditorState.h:
* UIProcess/API/Cocoa/WKWebView.mm:
(selectionAttributes):
* UIProcess/API/Cocoa/WKWebViewPrivate.h:

Remove an unused value, _WKSelectionAttributeAtStartOfSentence, in the _WKSelectionAttributes SPI flag.

* UIProcess/Cocoa/WebViewImpl.mm:
(WebKit::WebViewImpl::validateUserInterfaceItem):
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::getPlatformEditorState const):
* WebProcess/WebPage/mac/WebPageMac.mm:
(WebKit::WebPage::getPlatformEditorState const):

Tools:

See WebKit ChangeLog for more details.

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

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (282369 => 282370)


--- trunk/Source/WebCore/ChangeLog	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Source/WebCore/ChangeLog	2021-09-13 22:49:06 UTC (rev 282370)
@@ -1,3 +1,19 @@
+2021-09-13  Wenson Hsieh  <[email protected]>
+
+        [macOS] -toggleAutomaticSpellingCorrection: menu item validation shouldn't require post-layout editor state
+        https://bugs.webkit.org/show_bug.cgi?id=230205
+
+        Reviewed by Darin Adler.
+
+        See WebKit ChangeLog for more details.
+
+        * editing/AlternativeTextController.cpp:
+        (WebCore::AlternativeTextController::canEnableAutomaticSpellingCorrection const):
+
+        Slightly improve the performance of this method in the case where we're focusing a text field, by only
+        consulting the host form control element, rather than both the form control and the contenteditable shadow root
+        element.
+
 2021-09-13  Andres Gonzalez  <[email protected]>
 
         AX: set insertion point to the end of a native text control does not work when passing a collapsed TextMarkerRange with both start and end equals to the end TextMarker for the element.

Modified: trunk/Source/WebCore/editing/AlternativeTextController.cpp (282369 => 282370)


--- trunk/Source/WebCore/editing/AlternativeTextController.cpp	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Source/WebCore/editing/AlternativeTextController.cpp	2021-09-13 22:49:06 UTC (rev 282370)
@@ -339,15 +339,11 @@
 {
 #if ENABLE(AUTOCORRECT)
     auto position = m_document.selection().selection().start();
-    if (auto editableRoot = position.rootEditableElement()) {
-        if (is<HTMLElement>(editableRoot) && !downcast<HTMLElement>(*editableRoot).shouldAutocorrect())
-            return false;
-    }
-
-    if (auto control = enclosingTextFormControl(position)) {
+    if (RefPtr control = enclosingTextFormControl(position)) {
         if (!control->shouldAutocorrect())
             return false;
-    }
+    } else if (RefPtr editableRoot = position.rootEditableElement(); is<HTMLElement>(editableRoot) && !downcast<HTMLElement>(*editableRoot).shouldAutocorrect())
+        return false;
 #endif
 
     return true;

Modified: trunk/Source/WebKit/ChangeLog (282369 => 282370)


--- trunk/Source/WebKit/ChangeLog	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Source/WebKit/ChangeLog	2021-09-13 22:49:06 UTC (rev 282370)
@@ -1,3 +1,48 @@
+2021-09-13  Wenson Hsieh  <[email protected]>
+
+        [macOS] -toggleAutomaticSpellingCorrection: menu item validation shouldn't require post-layout editor state
+        https://bugs.webkit.org/show_bug.cgi?id=230205
+
+        Reviewed by Darin Adler.
+
+        In preparation for https://webkit.org/b/230204, refactor code on macOS that determines whether or not the
+        "Correct Spelling Automatically" menu bar item should be enabled, such that it doesn't require post-layout
+        editor state data. This is requisite to avoiding post-layout editor state computation altogether on macOS, when
+        the touch bar is either unavailable, or hasn't ever been shown in the context of text editing. See below for
+        more details.
+
+        We also take this opportunity to remove support for the unused _WKSelectionAttributeAtStartOfSentence SPI value
+        in _WKSelectionAttributes, which was originally intended for use in Mail compose in WebKit2 but seems to have
+        been obviated by the ability to swipe horizontally on the custom input accessory view in Mail compose to switch
+        between text candidates and the text formatting options.
+
+        * Shared/EditorState.cpp:
+        (WebKit::EditorState::encode const):
+        (WebKit::EditorState::decode):
+        (WebKit::EditorState::PostLayoutData::encode const):
+        (WebKit::EditorState::PostLayoutData::decode):
+        (WebKit::operator<<):
+
+        Remove the `atStartOfSentence` bit on EditorState's post-layout data that was only used to implement the
+        _WKSelectionAttributeAtStartOfSentence SPI flag value, and also move the `canEnableAutomaticSpellingCorrection`
+        bit out of post-layout data, and into the EditorState struct. Importantly, computing this bit does not require
+        or trigger layout, and this refactoring will allow us to completely forgo post-layout data on configurations
+        that do not require it (i.e. when the text touch bar is not active).
+
+        * Shared/EditorState.h:
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (selectionAttributes):
+        * UIProcess/API/Cocoa/WKWebViewPrivate.h:
+
+        Remove an unused value, _WKSelectionAttributeAtStartOfSentence, in the _WKSelectionAttributes SPI flag.
+
+        * UIProcess/Cocoa/WebViewImpl.mm:
+        (WebKit::WebViewImpl::validateUserInterfaceItem):
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::getPlatformEditorState const):
+        * WebProcess/WebPage/mac/WebPageMac.mm:
+        (WebKit::WebPage::getPlatformEditorState const):
+
 2021-09-13  Alex Christensen  <[email protected]>
 
         Add unit test that uses PCM daemon

Modified: trunk/Source/WebKit/Shared/EditorState.cpp (282369 => 282370)


--- trunk/Source/WebKit/Shared/EditorState.cpp	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Source/WebKit/Shared/EditorState.cpp	2021-09-13 22:49:06 UTC (rev 282370)
@@ -46,6 +46,9 @@
     encoder << isInPlugin;
     encoder << hasComposition;
     encoder << triggeredByAccessibilitySelectionChange;
+#if PLATFORM(MAC)
+    encoder << canEnableAutomaticSpellingCorrection;
+#endif
     encoder << isMissingPostLayoutData;
     if (!isMissingPostLayoutData)
         m_postLayoutData.encode(encoder);
@@ -89,6 +92,11 @@
     if (!decoder.decode(result.triggeredByAccessibilitySelectionChange))
         return false;
 
+#if PLATFORM(MAC)
+    if (!decoder.decode(result.canEnableAutomaticSpellingCorrection))
+        return false;
+#endif
+
     if (!decoder.decode(result.isMissingPostLayoutData))
         return false;
 
@@ -135,7 +143,6 @@
     encoder << hasPlainText;
     encoder << editableRootIsTransparentOrFullyClipped;
     encoder << caretColor;
-    encoder << atStartOfSentence;
     encoder << selectionStartIsAtParagraphBoundary;
     encoder << selectionEndIsAtParagraphBoundary;
 #endif
@@ -144,7 +151,6 @@
     encoder << candidateRequestStartPosition;
     encoder << paragraphContextForCandidateRequest;
     encoder << stringForCandidateRequest;
-    encoder << canEnableAutomaticSpellingCorrection;
 #endif
 #if PLATFORM(GTK) || PLATFORM(WPE)
     encoder << surroundingContext;
@@ -218,8 +224,6 @@
         return false;
     if (!decoder.decode(result.caretColor))
         return false;
-    if (!decoder.decode(result.atStartOfSentence))
-        return false;
     if (!decoder.decode(result.selectionStartIsAtParagraphBoundary))
         return false;
     if (!decoder.decode(result.selectionEndIsAtParagraphBoundary))
@@ -237,9 +241,6 @@
 
     if (!decoder.decode(result.stringForCandidateRequest))
         return false;
-
-    if (!decoder.decode(result.canEnableAutomaticSpellingCorrection))
-        return false;
 #endif
 #if PLATFORM(GTK) || PLATFORM(WPE)
     if (!decoder.decode(result.surroundingContext))
@@ -287,6 +288,10 @@
         ts.dumpProperty("hasComposition", editorState.hasComposition);
     if (editorState.triggeredByAccessibilitySelectionChange)
         ts.dumpProperty("triggeredByAccessibilitySelectionChange", editorState.triggeredByAccessibilitySelectionChange);
+#if PLATFORM(MAC)
+    if (!editorState.canEnableAutomaticSpellingCorrection)
+        ts.dumpProperty("canEnableAutomaticSpellingCorrection", editorState.canEnableAutomaticSpellingCorrection);
+#endif
     if (editorState.isMissingPostLayoutData)
         ts.dumpProperty("isMissingPostLayoutData", editorState.isMissingPostLayoutData);
 
@@ -356,8 +361,6 @@
         ts.dumpProperty("paragraphContextForCandidateRequest", editorState.postLayoutData().paragraphContextForCandidateRequest);
     if (editorState.postLayoutData().stringForCandidateRequest.length())
         ts.dumpProperty("stringForCandidateRequest", editorState.postLayoutData().stringForCandidateRequest);
-    if (editorState.postLayoutData().canEnableAutomaticSpellingCorrection)
-        ts.dumpProperty("canEnableAutomaticSpellingCorrection", editorState.postLayoutData().canEnableAutomaticSpellingCorrection);
 #endif
 
     if (editorState.postLayoutData().canCut)

Modified: trunk/Source/WebKit/Shared/EditorState.h (282369 => 282370)


--- trunk/Source/WebKit/Shared/EditorState.h	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Source/WebKit/Shared/EditorState.h	2021-09-13 22:49:06 UTC (rev 282370)
@@ -82,6 +82,9 @@
     bool isInPlugin { false };
     bool hasComposition { false };
     bool triggeredByAccessibilitySelectionChange { false };
+#if PLATFORM(MAC)
+    bool canEnableAutomaticSpellingCorrection { true };
+#endif
     bool isMissingPostLayoutData { true };
 
     struct PostLayoutData {
@@ -127,7 +130,6 @@
         uint64_t candidateRequestStartPosition { 0 };
         String paragraphContextForCandidateRequest;
         String stringForCandidateRequest;
-        bool canEnableAutomaticSpellingCorrection { true };
 #endif
 #if PLATFORM(GTK) || PLATFORM(WPE)
         String surroundingContext;

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm (282369 => 282370)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm	2021-09-13 22:49:06 UTC (rev 282370)
@@ -1703,14 +1703,6 @@
     else
         attributes |= _WKSelectionAttributeIsCaret;
 
-    if (!editorState.isMissingPostLayoutData) {
-#if PLATFORM(IOS_FAMILY)
-        if (editorState.postLayoutData().atStartOfSentence)
-            attributes |= _WKSelectionAttributeAtStartOfSentence;
-#endif
-    } else if (previousAttributes & _WKSelectionAttributeAtStartOfSentence)
-        attributes |= _WKSelectionAttributeAtStartOfSentence;
-
     return attributes;
 }
 

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h (282369 => 282370)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h	2021-09-13 22:49:06 UTC (rev 282370)
@@ -65,7 +65,6 @@
     _WKSelectionAttributeNoSelection = 0,
     _WKSelectionAttributeIsCaret = 1 << 0,
     _WKSelectionAttributeIsRange = 1 << 1,
-    _WKSelectionAttributeAtStartOfSentence = 1 << 2,
 } WK_API_AVAILABLE(macos(10.15), ios(13.0));
 
 typedef NS_ENUM(NSInteger, _WKShouldOpenExternalURLsPolicy) {

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm (282369 => 282370)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm	2021-09-13 22:49:06 UTC (rev 282370)
@@ -3242,8 +3242,7 @@
     }
 
     if (action == @selector(toggleAutomaticSpellingCorrection:)) {
-        auto& editorState = m_page->editorState();
-        bool enable = editorState.isContentEditable && (editorState.isMissingPostLayoutData || editorState.postLayoutData().canEnableAutomaticSpellingCorrection);
+        bool enable = m_page->editorState().canEnableAutomaticSpellingCorrection;
         menuItem(item).state = TextChecker::state().isAutomaticSpellingCorrectionEnabled && enable ? NSControlStateValueOn : NSControlStateValueOff;
         return enable;
     }

Modified: trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm (282369 => 282370)


--- trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm	2021-09-13 22:49:06 UTC (rev 282370)
@@ -333,7 +333,6 @@
     }
 #endif
 
-    postLayoutData.atStartOfSentence = frame.selection().selectionAtSentenceStart();
     postLayoutData.insideFixedPosition = startNodeIsInsideFixedPosition || endNodeIsInsideFixedPosition;
     if (!selection.isNone()) {
         if (selection.hasEditableStyle()) {

Modified: trunk/Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm (282369 => 282370)


--- trunk/Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm	2021-09-13 22:49:06 UTC (rev 282370)
@@ -144,6 +144,8 @@
 {
     getPlatformEditorStateCommon(frame, result);
 
+    result.canEnableAutomaticSpellingCorrection = result.isContentEditable && frame.editor().canEnableAutomaticSpellingCorrection();
+
     if (result.isMissingPostLayoutData)
         return;
 
@@ -167,7 +169,6 @@
     postLayoutData.selectedTextLength = characterCount({ *selectionStartBoundary, *selectionEnd });
     postLayoutData.paragraphContextForCandidateRequest = contextRangeForCandidateRequest ? plainText(*contextRangeForCandidateRequest) : String();
     postLayoutData.stringForCandidateRequest = frame.editor().stringForCandidateRequest();
-    postLayoutData.canEnableAutomaticSpellingCorrection = frame.editor().canEnableAutomaticSpellingCorrection();
 
     auto quads = RenderObject::absoluteTextQuads(*selectedRange);
     if (!quads.isEmpty())

Modified: trunk/Tools/ChangeLog (282369 => 282370)


--- trunk/Tools/ChangeLog	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Tools/ChangeLog	2021-09-13 22:49:06 UTC (rev 282370)
@@ -1,3 +1,15 @@
+2021-09-13  Wenson Hsieh  <[email protected]>
+
+        [macOS] -toggleAutomaticSpellingCorrection: menu item validation shouldn't require post-layout editor state
+        https://bugs.webkit.org/show_bug.cgi?id=230205
+
+        Reviewed by Darin Adler.
+
+        See WebKit ChangeLog for more details.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/EditorStateTests.mm:
+        (TestWebKitAPI::TEST):
+
 2021-09-13  Andres Gonzalez  <[email protected]>
 
         AX: set insertion point to the end of a native text control does not work when passing a collapsed TextMarkerRange with both start and end equals to the end TextMarker for the element.

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/EditorStateTests.mm (282369 => 282370)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/EditorStateTests.mm	2021-09-13 22:42:52 UTC (rev 282369)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/EditorStateTests.mm	2021-09-13 22:49:06 UTC (rev 282370)
@@ -372,28 +372,28 @@
 
     [webView evaluateJavaScript:@"document.body.focus()" completionHandler:nil];
     [webView waitForNextPresentationUpdate];
-    EXPECT_EQ(_WKSelectionAttributeIsCaret | _WKSelectionAttributeAtStartOfSentence, [observer currentSelectionAttributes]);
+    EXPECT_EQ(_WKSelectionAttributeIsCaret, [observer currentSelectionAttributes]);
 
     [editor insertText:@"Hello"];
     EXPECT_EQ(_WKSelectionAttributeIsCaret, [observer currentSelectionAttributes]);
 
     [editor insertText:@"."];
-    EXPECT_EQ(_WKSelectionAttributeIsCaret | _WKSelectionAttributeAtStartOfSentence, [observer currentSelectionAttributes]);
+    EXPECT_EQ(_WKSelectionAttributeIsCaret, [observer currentSelectionAttributes]);
 
     [editor moveBackward];
     EXPECT_EQ(_WKSelectionAttributeIsCaret, [observer currentSelectionAttributes]);
 
     [editor moveForward];
-    EXPECT_EQ(_WKSelectionAttributeIsCaret | _WKSelectionAttributeAtStartOfSentence, [observer currentSelectionAttributes]);
+    EXPECT_EQ(_WKSelectionAttributeIsCaret, [observer currentSelectionAttributes]);
 
     [editor deleteBackwards];
     EXPECT_EQ(_WKSelectionAttributeIsCaret, [observer currentSelectionAttributes]);
 
     [editor insertParagraph];
-    EXPECT_EQ(_WKSelectionAttributeIsCaret | _WKSelectionAttributeAtStartOfSentence, [observer currentSelectionAttributes]);
+    EXPECT_EQ(_WKSelectionAttributeIsCaret, [observer currentSelectionAttributes]);
 
     [editor selectAll];
-    EXPECT_EQ(_WKSelectionAttributeIsRange | _WKSelectionAttributeAtStartOfSentence, [observer currentSelectionAttributes]);
+    EXPECT_EQ(_WKSelectionAttributeIsRange, [observer currentSelectionAttributes]);
 
     [webView evaluateJavaScript:@"getSelection().removeAllRanges()" completionHandler:nil];
     [webView waitForNextPresentationUpdate];
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to