Changeset 241614 in webkit


Ignore:
Timestamp:
Feb 15, 2019 1:35:52 PM (5 years ago)
Author:
Wenson Hsieh
Message:

[iOS] WKWebView callout bar is missing Change Writing Direction item
https://bugs.webkit.org/show_bug.cgi?id=190015
<rdar://problem/44810366>

Reviewed by Tim Horton.

Source/WebKit:

Support -makeTextWritingDirectionLeftToRight: and -makeTextWritingDirectionRightToLeft: in WKWebView on iOS.
To match behavior in native UITextViews on iOS, we implement these methods by changing the *base* writing
direction, rather than the text writing direction (this is in contrast to macOS, which has different
NSResponder methods for changing the base writing direction as opposed to the text writing direction).

Additionally fixes the implementation of -makeTextWritingDirectionNatural:, which currently attempts to change
the text writing direction instead of the base writing direction.

  • Platform/spi/ios/UIKitSPI.h:

Add a forward declaration for keyboards SPI to determine whether the user has an active RTL keyboard.

  • Shared/EditorState.cpp:

(WebKit::EditorState::PostLayoutData::encode const):
(WebKit::EditorState::PostLayoutData::decode):
(WebKit::operator<<):

  • Shared/EditorState.h:

Plumb the base writing direction to the UI process through EditorState.

  • UIProcess/ios/WKContentViewInteraction.h:
  • UIProcess/ios/WKContentViewInteraction.mm:

(-[WKContentView makeTextWritingDirectionNaturalForWebView:]):
(-[WKContentView makeTextWritingDirectionLeftToRightForWebView:]):
(-[WKContentView makeTextWritingDirectionRightToLeftForWebView:]):

Implement the new SPI (see above for more details).

(-[WKContentView canPerformActionForWebView:withSender:]):

Implement -canPerformAction: for LTR and RTL actions. To match existing UIWebView behavior, we only enable
these actions if either the base writing direction is RTL, or the user has an active RTL keyboard. This means,
for instance, that in the case where a user with only an English keyboard is editing LTR content, we would never
show an option to convert to RTL.

  • WebProcess/WebPage/WebPage.cpp:

(WebKit::WebPage::editorState const):

Tools:

Make an existing API test that exercises platform SPI to change the inline text writing direction run only on
macOS, and add a new API test that uses similarly named SPI on iOS to change the base writing direction.

  • TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEditActions.mm:

(TestWebKitAPI::TEST):

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r241611 r241614  
     12019-02-15  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [iOS] WKWebView callout bar is missing Change Writing Direction item
     4        https://bugs.webkit.org/show_bug.cgi?id=190015
     5        <rdar://problem/44810366>
     6
     7        Reviewed by Tim Horton.
     8
     9        Support -makeTextWritingDirectionLeftToRight: and -makeTextWritingDirectionRightToLeft: in WKWebView on iOS.
     10        To match behavior in native UITextViews on iOS, we implement these methods by changing the *base* writing
     11        direction, rather than the text writing direction (this is in contrast to macOS, which has different
     12        NSResponder methods for changing the base writing direction as opposed to the text writing direction).
     13
     14        Additionally fixes the implementation of -makeTextWritingDirectionNatural:, which currently attempts to change
     15        the text writing direction instead of the base writing direction.
     16
     17        * Platform/spi/ios/UIKitSPI.h:
     18
     19        Add a forward declaration for keyboards SPI to determine whether the user has an active RTL keyboard.
     20
     21        * Shared/EditorState.cpp:
     22        (WebKit::EditorState::PostLayoutData::encode const):
     23        (WebKit::EditorState::PostLayoutData::decode):
     24        (WebKit::operator<<):
     25        * Shared/EditorState.h:
     26
     27        Plumb the base writing direction to the UI process through EditorState.
     28
     29        * UIProcess/ios/WKContentViewInteraction.h:
     30        * UIProcess/ios/WKContentViewInteraction.mm:
     31        (-[WKContentView makeTextWritingDirectionNaturalForWebView:]):
     32        (-[WKContentView makeTextWritingDirectionLeftToRightForWebView:]):
     33        (-[WKContentView makeTextWritingDirectionRightToLeftForWebView:]):
     34
     35        Implement the new SPI (see above for more details).
     36
     37        (-[WKContentView canPerformActionForWebView:withSender:]):
     38
     39        Implement -canPerformAction: for LTR and RTL actions. To match existing UIWebView behavior, we only enable
     40        these actions if either the base writing direction is RTL, or the user has an active RTL keyboard. This means,
     41        for instance, that in the case where a user with only an English keyboard is editing LTR content, we would never
     42        show an option to convert to RTL.
     43
     44        * WebProcess/WebPage/WebPage.cpp:
     45        (WebKit::WebPage::editorState const):
     46
    1472019-02-15  Alex Christensen  <achristensen@webkit.org>
    248
  • trunk/Source/WebKit/Platform/spi/ios/UIKitSPI.h

    r241435 r241614  
    11441144BOOL UIKeyboardEnabledInputModesAllowOneToManyShortcuts(void);
    11451145BOOL UIKeyboardEnabledInputModesAllowChineseTransliterationForText(NSString *);
     1146BOOL UIKeyboardIsRightToLeftInputModeActive(void);
    11461147
    11471148extern const float UITableCellDefaultFontSize;
  • trunk/Source/WebKit/Shared/EditorState.cpp

    r239931 r241614  
    112112    encoder << caretRectAtStart;
    113113#endif
    114 #if PLATFORM(IOS_FAMILY) || PLATFORM(MAC)
     114#if PLATFORM(COCOA)
    115115    encoder << focusedElementRect;
    116116    encoder << selectedTextLength;
     
    118118    encoder << textColor;
    119119    encoder << enclosingListType;
     120    encoder << baseWritingDirection;
    120121#endif
    121122#if PLATFORM(IOS_FAMILY)
     
    154155        return false;
    155156#endif
    156 #if PLATFORM(IOS_FAMILY) || PLATFORM(MAC)
     157#if PLATFORM(COCOA)
    157158    if (!decoder.decode(result.focusedElementRect))
    158159        return false;
     
    164165        return false;
    165166    if (!decoder.decode(result.enclosingListType))
     167        return false;
     168    if (!decoder.decode(result.baseWritingDirection))
    166169        return false;
    167170#endif
     
    265268        ts.dumpProperty("caretRectAtStart", editorState.postLayoutData().caretRectAtStart);
    266269#endif
    267 #if PLATFORM(IOS_FAMILY) || PLATFORM(MAC)
     270#if PLATFORM(COCOA)
    268271    if (editorState.postLayoutData().focusedElementRect != IntRect())
    269272        ts.dumpProperty("focusedElementRect", editorState.postLayoutData().focusedElementRect);
     
    276279    if (editorState.postLayoutData().enclosingListType != NoList)
    277280        ts.dumpProperty("enclosingListType", editorState.postLayoutData().enclosingListType);
    278 #endif
     281    if (editorState.postLayoutData().baseWritingDirection != WebCore::WritingDirection::Natural)
     282        ts.dumpProperty("baseWritingDirection", static_cast<uint8_t>(editorState.postLayoutData().baseWritingDirection));
     283#endif // PLATFORM(COCOA)
    279284#if PLATFORM(IOS_FAMILY)
    280285    if (editorState.postLayoutData().caretRectAtEnd != IntRect())
  • trunk/Source/WebKit/Shared/EditorState.h

    r239931 r241614  
    3030#include <WebCore/FontAttributes.h>
    3131#include <WebCore/IntRect.h>
     32#include <WebCore/WritingDirection.h>
    3233#include <wtf/text/WTFString.h>
    3334
     
    8990        WebCore::IntRect caretRectAtStart;
    9091#endif
    91 #if PLATFORM(IOS_FAMILY) || PLATFORM(MAC)
     92#if PLATFORM(COCOA)
    9293        WebCore::IntRect focusedElementRect;
    9394        uint64_t selectedTextLength { 0 };
     
    9596        WebCore::Color textColor { WebCore::Color::black };
    9697        uint32_t enclosingListType { NoList };
     98        WebCore::WritingDirection baseWritingDirection { WebCore::WritingDirection::Natural };
    9799#endif
    98100#if PLATFORM(IOS_FAMILY)
  • trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h

    r241320 r241614  
    132132    M(decreaseSize) \
    133133    M(pasteAndMatchStyle) \
    134     M(makeTextWritingDirectionNatural)
     134    M(makeTextWritingDirectionNatural) \
     135    M(makeTextWritingDirectionLeftToRight) \
     136    M(makeTextWritingDirectionRightToLeft)
    135137
    136138#define FOR_EACH_PRIVATE_WKCONTENTVIEW_ACTION(M) \
  • trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm

    r241435 r241614  
    24722472- (void)makeTextWritingDirectionNaturalForWebView:(id)sender
    24732473{
    2474     _page->executeEditCommand("makeTextWritingDirectionNatural"_s);
     2474    // Match platform behavior on iOS as well as legacy WebKit behavior by modifying the
     2475    // base (paragraph) writing direction rather than the inline direction.
     2476    _page->setBaseWritingDirection(WebCore::WritingDirection::Natural);
     2477}
     2478
     2479- (void)makeTextWritingDirectionLeftToRightForWebView:(id)sender
     2480{
     2481    _page->setBaseWritingDirection(WebCore::WritingDirection::LeftToRight);
     2482}
     2483
     2484- (void)makeTextWritingDirectionRightToLeftForWebView:(id)sender
     2485{
     2486    _page->setBaseWritingDirection(WebCore::WritingDirection::RightToLeft);
    24752487}
    24762488
     
    27982810    if (action == @selector(replace:))
    27992811        return editorState.isContentEditable && !editorState.isInPasswordField;
     2812
     2813    if (action == @selector(makeTextWritingDirectionLeftToRight:) || action == @selector(makeTextWritingDirectionRightToLeft:)) {
     2814        if (!editorState.isContentEditable)
     2815            return NO;
     2816
     2817        auto baseWritingDirection = editorState.postLayoutData().baseWritingDirection;
     2818        if (baseWritingDirection == WebCore::WritingDirection::LeftToRight && !UIKeyboardIsRightToLeftInputModeActive()) {
     2819            // A keyboard is considered "active" if it is available for the user to switch to. As such, this check prevents
     2820            // text direction actions from showing up in the case where a user has only added left-to-right keyboards, and
     2821            // is also not editing right-to-left content.
     2822            return NO;
     2823        }
     2824
     2825        if (action == @selector(makeTextWritingDirectionLeftToRight:))
     2826            return baseWritingDirection != WebCore::WritingDirection::LeftToRight;
     2827
     2828        return baseWritingDirection != WebCore::WritingDirection::RightToLeft;
     2829    }
    28002830
    28012831    return [super canPerformAction:action withSender:sender];
  • trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp

    r241574 r241614  
    10231023                    ASSERT_NOT_REACHED();
    10241024            }
     1025
     1026            postLayoutData.baseWritingDirection = editor.baseWritingDirectionForSelectionStart();
    10251027        }
    10261028#endif
  • trunk/Tools/ChangeLog

    r241606 r241614  
     12019-02-15  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [iOS] WKWebView callout bar is missing Change Writing Direction item
     4        https://bugs.webkit.org/show_bug.cgi?id=190015
     5        <rdar://problem/44810366>
     6
     7        Reviewed by Tim Horton.
     8
     9        Make an existing API test that exercises platform SPI to change the inline text writing direction run only on
     10        macOS, and add a new API test that uses similarly named SPI on iOS to change the base writing direction.
     11
     12        * TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEditActions.mm:
     13        (TestWebKitAPI::TEST):
     14
    1152019-02-15  Chris Dumez  <cdumez@apple.com>
    216
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEditActions.mm

    r238728 r241614  
    259259}
    260260
    261 TEST(WKWebViewEditActions, ModifyTextWritingDirection)
    262 {
    263     auto webView = webViewForEditActionTesting(@"<div id='text' style='direction: rtl; unicode-bidi: bidi-override;'>WebKit</div>");
    264     [webView selectAll:nil];
     261#if PLATFORM(IOS_FAMILY)
     262
     263TEST(WKWebViewEditActions, ModifyBaseWritingDirection)
     264{
     265    auto webView = webViewForEditActionTesting(@"<meta charset='utf8'><p id='english'>Hello world</p><p id='hebrew'>מקור השם עברית</p>");
     266
     267    [webView evaluateJavaScript:@"getSelection().setPosition(english)" completionHandler:nil];
     268    [webView makeTextWritingDirectionRightToLeft:nil];
     269    [webView waitForNextPresentationUpdate];
     270    EXPECT_WK_STREQ("rtl", [webView stringByEvaluatingJavaScript:@"getComputedStyle(english).direction"]);
     271    EXPECT_FALSE([webView canPerformAction:@selector(makeTextWritingDirectionRightToLeft:) withSender:nil]);
     272    EXPECT_TRUE([webView canPerformAction:@selector(makeTextWritingDirectionLeftToRight:) withSender:nil]);
     273
     274    [webView evaluateJavaScript:@"getSelection().setPosition(hebrew)" completionHandler:nil];
     275    [webView makeTextWritingDirectionLeftToRight:nil];
     276    [webView waitForNextPresentationUpdate];
     277    EXPECT_WK_STREQ("ltr", [webView stringByEvaluatingJavaScript:@"getComputedStyle(hebrew).direction"]);
     278    EXPECT_FALSE([webView canPerformAction:@selector(makeTextWritingDirectionLeftToRight:) withSender:nil]);
     279
     280    [webView evaluateJavaScript:@"getSelection().setPosition(english)" completionHandler:nil];
    265281    [webView makeTextWritingDirectionNatural:nil];
    266     EXPECT_WK_STREQ("normal", [webView stringByEvaluatingJavaScript:@"getComputedStyle(text).unicodeBidi"]);
    267 }
    268 
    269 #if PLATFORM(IOS_FAMILY)
     282    [webView waitForNextPresentationUpdate];
     283    EXPECT_WK_STREQ("ltr", [webView stringByEvaluatingJavaScript:@"getComputedStyle(english).direction"]);
     284    EXPECT_FALSE([webView canPerformAction:@selector(makeTextWritingDirectionLeftToRight:) withSender:nil]);
     285}
    270286
    271287TEST(WKWebViewEditActions, ChangeFontSize)
     
    329345}
    330346
    331 #endif // PLATFORM(IOS_FAMILY)
     347#else
     348
     349TEST(WKWebViewEditActions, ModifyTextWritingDirection)
     350{
     351    auto webView = webViewForEditActionTesting(@"<div id='text' style='direction: rtl; unicode-bidi: bidi-override;'>WebKit</div>");
     352    [webView selectAll:nil];
     353    [webView makeTextWritingDirectionNatural:nil];
     354    EXPECT_WK_STREQ("normal", [webView stringByEvaluatingJavaScript:@"getComputedStyle(text).unicodeBidi"]);
     355}
     356
     357#endif
    332358
    333359} // namespace TestWebKitAPI
Note: See TracChangeset for help on using the changeset viewer.