Changeset 244473 in webkit


Ignore:
Timestamp:
Apr 19, 2019 4:48:41 PM (5 years ago)
Author:
dbates@webkit.org
Message:

-[WKAutocorrectionContext emptyAutocorrectionContext:] generates invalid empty context
https://bugs.webkit.org/show_bug.cgi?id=197119

Reviewed by Wenson Hsieh.

Use the existing EditingRange type to represent the location and length of the marked text
range for an autocorrection instead of managing integers. This type avoid the need to handle
the special case for an empty range represented as NSMakeRange(NSNotFound, 0). Currently
WKAutocorrectionContext incorrectly represents the empty range as NSMakeRange(WTF::notFound, 0).

While I am here, simplify the existing WebAutocorrectionContext encoder/decoder code and rename
+[WKAutocorrectionContext autocorrectionContextWithContext:] to +autocorrectionContextWithWebContext
to better reflect the expected source of the conversion: a Web-type.

  • Shared/ios/WebAutocorrectionContext.h:

(WebKit::WebAutocorrectionContext::encode const): Reformat while I am here to make this logic easy
to amend without losing SVN history.
(WebKit::WebAutocorrectionContext::decode): Simplify the code while I am here.

  • UIProcess/ios/WKContentViewInteraction.mm:

(-[WKContentView _handleAutocorrectionContext:]): Update for renaming.
(+[WKAutocorrectionContext emptyAutocorrectionContext]): Update for renaming.
(+[WKAutocorrectionContext autocorrectionContextWithWebContext:]): Renamed; formerly named autocorrectionContextWithContext.
(+[WKAutocorrectionContext autocorrectionContextWithContext:]): Deleted.

  • WebProcess/WebPage/ios/WebPageIOS.mm:

(WebKit::WebPage::autocorrectionContext): Update to make use of EditingRange. Also instantiate
the struct and return it, initializing its fields individually instead of using the constructor to
make this code less error prone. It's easy to introduce an error with the constructor notation when
amending the the struct because so many of the arguments are of the same data type. Individually
initializing the struct fields makes it less likely for an ordering mistake to be introduced.

Location:
trunk/Source/WebKit
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r244472 r244473  
     12019-04-19  Daniel Bates  <dabates@apple.com>
     2
     3        -[WKAutocorrectionContext emptyAutocorrectionContext:] generates invalid empty context
     4        https://bugs.webkit.org/show_bug.cgi?id=197119
     5
     6        Reviewed by Wenson Hsieh.
     7
     8        Use the existing EditingRange type to represent the location and length of the marked text
     9        range for an autocorrection instead of managing integers. This type avoid the need to handle
     10        the special case for an empty range represented as NSMakeRange(NSNotFound, 0). Currently
     11        WKAutocorrectionContext incorrectly represents the empty range as NSMakeRange(WTF::notFound, 0).
     12
     13        While I am here, simplify the existing WebAutocorrectionContext encoder/decoder code and rename
     14        +[WKAutocorrectionContext autocorrectionContextWithContext:] to +autocorrectionContextWithWebContext
     15        to better reflect the expected source of the conversion: a Web-type.
     16
     17        * Shared/ios/WebAutocorrectionContext.h:
     18        (WebKit::WebAutocorrectionContext::encode const): Reformat while I am here to make this logic easy
     19        to amend without losing SVN history.
     20        (WebKit::WebAutocorrectionContext::decode): Simplify the code while I am here.
     21        * UIProcess/ios/WKContentViewInteraction.mm:
     22        (-[WKContentView _handleAutocorrectionContext:]): Update for renaming.
     23        (+[WKAutocorrectionContext emptyAutocorrectionContext]): Update for renaming.
     24        (+[WKAutocorrectionContext autocorrectionContextWithWebContext:]): Renamed; formerly named autocorrectionContextWithContext.
     25        (+[WKAutocorrectionContext autocorrectionContextWithContext:]): Deleted.
     26        * WebProcess/WebPage/ios/WebPageIOS.mm:
     27        (WebKit::WebPage::autocorrectionContext): Update to make use of EditingRange. Also instantiate
     28        the struct and return it, initializing its fields individually instead of using the constructor to
     29        make this code less error prone. It's easy to introduce an error with the constructor notation when
     30        amending the the struct because so many of the arguments are of the same data type. Individually
     31        initializing the struct fields makes it less likely for an ordering mistake to be introduced.
     32
    1332019-04-19  Dean Jackson  <dino@apple.com>
    234
  • trunk/Source/WebKit/Shared/ios/WebAutocorrectionContext.h

    r241146 r244473  
    2727
    2828#include "Decoder.h"
     29#include "EditingRange.h"
    2930#include "Encoder.h"
    30 #include <wtf/NotFound.h>
    3131#include <wtf/Optional.h>
    3232#include <wtf/text/WTFString.h>
     
    3939    String selectedText;
    4040    String contextAfter;
    41     uint64_t location { notFound };
    42     uint64_t length { 0 };
     41    EditingRange markedTextRange;
    4342
    4443    template<class Encoder> void encode(Encoder&) const;
     
    4645};
    4746
    48 template<class Encoder> inline void WebAutocorrectionContext::encode(Encoder& encoder) const
     47template<class Encoder> void WebAutocorrectionContext::encode(Encoder& encoder) const
    4948{
    50     encoder << contextBefore << markedText << selectedText << contextAfter << location << length;
     49    encoder << contextBefore;
     50    encoder << markedText;
     51    encoder << selectedText;
     52    encoder << contextAfter;
     53    encoder << markedTextRange;
    5154}
    5255
    53 template<class Decoder> inline Optional<WebAutocorrectionContext> WebAutocorrectionContext::decode(Decoder& decoder)
     56template<class Decoder> Optional<WebAutocorrectionContext> WebAutocorrectionContext::decode(Decoder& decoder)
    5457{
    55     Optional<String> contextBefore;
    56     decoder >> contextBefore;
    57     if (!contextBefore)
     58    WebAutocorrectionContext correction;
     59    if (!decoder.decode(correction.contextBefore))
    5860        return WTF::nullopt;
    59 
    60     Optional<String> markedText;
    61     decoder >> markedText;
    62     if (!markedText)
     61    if (!decoder.decode(correction.markedText))
    6362        return WTF::nullopt;
    64 
    65     Optional<String> selectedText;
    66     decoder >> selectedText;
    67     if (!selectedText)
     63    if (!decoder.decode(correction.selectedText))
    6864        return WTF::nullopt;
    69 
    70     Optional<String> contextAfter;
    71     decoder >> contextAfter;
    72     if (!contextAfter)
     65    if (!decoder.decode(correction.contextAfter))
    7366        return WTF::nullopt;
    74 
    75     Optional<uint64_t> location;
    76     decoder >> location;
    77     if (!location)
     67    if (!decoder.decode(correction.markedTextRange))
    7868        return WTF::nullopt;
    79 
    80     Optional<uint64_t> length;
    81     decoder >> length;
    82     if (!length)
    83         return WTF::nullopt;
    84 
    85     return {{ WTFMove(*contextBefore), WTFMove(*markedText), WTFMove(*selectedText), WTFMove(*contextAfter), WTFMove(*location), WTFMove(*length) }};
     69    return correction;
    8670}
    8771
  • trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm

    r244471 r244473  
    291291@interface WKAutocorrectionContext : UIWKAutocorrectionContext
    292292+ (WKAutocorrectionContext *)emptyAutocorrectionContext;
    293 + (WKAutocorrectionContext *)autocorrectionContextWithContext:(const WebKit::WebAutocorrectionContext&)context;
     293+ (WKAutocorrectionContext *)autocorrectionContextWithWebContext:(const WebKit::WebAutocorrectionContext&)context;
    294294@end
    295295
     
    37193719- (void)_handleAutocorrectionContext:(const WebKit::WebAutocorrectionContext&)context
    37203720{
    3721     [self _invokePendingAutocorrectionContextHandler:[WKAutocorrectionContext autocorrectionContextWithContext:context]];
     3721    [self _invokePendingAutocorrectionContextHandler:[WKAutocorrectionContext autocorrectionContextWithWebContext:context]];
    37223722}
    37233723
     
    76877687+ (WKAutocorrectionContext *)emptyAutocorrectionContext
    76887688{
    7689     return [self autocorrectionContextWithContext:WebKit::WebAutocorrectionContext { }];
    7690 }
    7691 
    7692 + (WKAutocorrectionContext *)autocorrectionContextWithContext:(const WebKit::WebAutocorrectionContext&)webContext
    7693 {
    7694     WKAutocorrectionContext *context = [[WKAutocorrectionContext alloc] init];
    7695 
    7696     if (!webContext.contextBefore.isEmpty())
    7697         context.contextBeforeSelection = webContext.contextBefore;
    7698     if (!webContext.selectedText.isEmpty())
    7699         context.selectedText = webContext.selectedText;
    7700     if (!webContext.markedText.isEmpty())
    7701         context.markedText = webContext.markedText;
    7702     if (!webContext.contextAfter.isEmpty())
    7703         context.contextAfterSelection = webContext.contextAfter;
    7704     context.rangeInMarkedText = NSMakeRange(webContext.location, webContext.length);
    7705     return [context autorelease];
     7689    return [self autocorrectionContextWithWebContext:WebKit::WebAutocorrectionContext { }];
     7690}
     7691
     7692+ (WKAutocorrectionContext *)autocorrectionContextWithWebContext:(const WebKit::WebAutocorrectionContext&)webCorrection
     7693{
     7694    auto correction = adoptNS([[WKAutocorrectionContext alloc] init]);
     7695    [correction setContextBeforeSelection:nsStringNilIfEmpty(webCorrection.contextBefore)];
     7696    [correction setSelectedText:nsStringNilIfEmpty(webCorrection.selectedText)];
     7697    [correction setMarkedText:nsStringNilIfEmpty(webCorrection.markedText)];
     7698    [correction setContextAfterSelection:nsStringNilIfEmpty(webCorrection.contextAfter)];
     7699    [correction setRangeInMarkedText:webCorrection.markedTextRange];
     7700    return correction.autorelease();
    77067701}
    77077702
  • trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm

    r244457 r244473  
    21812181    String selectedText;
    21822182    String contextAfter;
    2183     uint64_t location = NSNotFound;
    2184     uint64_t length = 0;
     2183    EditingRange markedTextRange;
    21852184
    21862185    auto& frame = m_page->focusController().focusedOrMainFrame();
     
    22062205        markedText = markedTextBefore + selectedText + markedTextAfter;
    22072206        if (!markedText.isEmpty()) {
    2208             location = markedTextBefore.length();
    2209             length = selectedText.length();
     2207            markedTextRange.location = markedTextBefore.length();
     2208            markedTextRange.length = selectedText.length();
    22102209        }
    22112210    } else {
     
    22412240        }
    22422241    }
    2243     return { WTFMove(contextBefore), WTFMove(markedText), WTFMove(selectedText), WTFMove(contextAfter), location, length };
     2242
     2243    WebAutocorrectionContext correction;
     2244    correction.contextBefore = WTFMove(contextBefore);
     2245    correction.markedText = WTFMove(markedText);
     2246    correction.selectedText = WTFMove(selectedText);
     2247    correction.contextAfter = WTFMove(contextAfter);
     2248    correction.markedTextRange = WTFMove(markedTextRange);
     2249    return correction;
    22442250}
    22452251
Note: See TracChangeset for help on using the changeset viewer.