Skip to content

Commit

Permalink
HTML notes is showing gray underline for autocorrection
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=265050
rdar://118401826

Reviewed by Wenson Hsieh.

Before 266070@main, the caret color on iOS was almost always blue (specifically, always except for
when the caret color was explicitly set by the CSS author). The change was needed on macOS because
otherwise some sites would have overlapping carets. The commit changed both macOS and iOS to be more
consistent, and more compliant with the CSS spec. However, while this did technically improve web
compatibility in iOS, it made things worse than they were before:

- The caret on iOS is now almost always black (it is on macOS too, but it is worse on iOS because
the caret was always blue previously)

- It has caused several issues in apps with custom tint colors (like https://bugs.webkit.org/show_bug.cgi?id=263123
and several others)

This specific bug is due to the fact that the correct underlines directly use the caret color. The
reason the commit that addressed the color of the caret itself on iOS and not this color as well
is because the two colors come from different paths, and the fix only fixed the actual caret
(insertion point) color.

To fix, and prevent similar bugs, undo the part of 266070@main that changed the iOS behavior. This
restores the previous behavior, in addition to fixing this bug.

* Source/WebCore/editing/FrameSelection.cpp:
(WebCore::CaretBase::computeCaretColor):

Canonical link: https://commits.webkit.org/271278@main
  • Loading branch information
rr-codes committed Nov 29, 2023
1 parent df1c826 commit a20779d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Source/WebCore/editing/FrameSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1889,7 +1889,7 @@ Color CaretBase::computeCaretColor(const RenderStyle& elementStyle, const Node*
// On iOS, we want to fall back to the tintColor, and only override if CSS has explicitly specified a custom color.
#if PLATFORM(IOS_FAMILY) && !PLATFORM(MACCATALYST)
UNUSED_PARAM(node);
if (elementStyle.hasAutoCaretColor() && !elementStyle.hasExplicitlySetColor())
if (elementStyle.hasAutoCaretColor())
return { };
return elementStyle.colorResolvingCurrentColor(elementStyle.caretColor());
#elif HAVE(REDESIGNED_TEXT_CURSOR)
Expand Down
80 changes: 80 additions & 0 deletions Tools/TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
#import "TestWKWebView.h"
#import "UIKitSPIForTesting.h"
#import "UserInterfaceSwizzler.h"
#import "WKWebViewConfigurationExtras.h"
#import <WebKit/WKProcessPoolPrivate.h>
#import <WebKit/WKWebViewConfigurationPrivate.h>
#import <WebKit/WKWebViewPrivate.h>
#import <WebKit/WKWebViewPrivateForTesting.h>
#import <WebKit/WKWebViewPrivateForTestingIOS.h>
#import <WebKit/_WKProcessPoolConfiguration.h>
#import <WebKitLegacy/WebEvent.h>
#import <cmath>
Expand Down Expand Up @@ -1117,6 +1119,84 @@ static BOOL shouldSimulateKeyboardInputOnTextInsertionOverride(id, SEL)
EXPECT_GT(numberOfDifferentPixels, 0U);
}

#if HAVE(AUTOCORRECTION_ENHANCEMENTS)
TEST(KeyboardInputTests, AutocorrectionIndicatorColorNotAffectedByAuthorDefinedAncestorColorProperty)
{
auto frame = CGRectMake(0, 0, 320, 568);
auto window = adoptNS([[UIWindow alloc] initWithFrame:frame]);

auto createSnapshotForHTMLString = ^(NSString *HTMLString) {
auto configuration = [WKWebViewConfiguration _test_configurationWithTestPlugInClassName:@"WebProcessPlugInWithInternals" configureJSCForTesting:YES];

auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:frame configuration:configuration addToWindow:NO]);

[window addSubview:webView.get()];

auto inputDelegate = adoptNS([[TestInputDelegate alloc] init]);
[inputDelegate setFocusStartsInputSessionPolicyHandler:[] (WKWebView *, id<_WKFocusedElementInfo>) -> _WKFocusStartsInputSessionPolicy {
return _WKFocusStartsInputSessionPolicyAllow;
}];

[webView _setInputDelegate:inputDelegate.get()];
[webView synchronouslyLoadHTMLString:HTMLString];

[webView waitForNextPresentationUpdate];

[webView stringByEvaluatingJavaScript:@"document.getElementById('input').focus();"];

[webView waitForNextPresentationUpdate];

auto *contentView = [webView textInputContentView];
[contentView insertText:@"Is it diferent"];

[webView waitForNextPresentationUpdate];

NSString *hasCorrectionIndicatorMarkerJavaScript = @"internals.hasCorrectionIndicatorMarker(6, 9);";

__block bool done = false;

[[webView textInputContentView] applyAutocorrection:@"different" toString:@"diferent" shouldUnderline:YES withCompletionHandler:^(UIWKAutocorrectionRects *) {
NSString *hasCorrectionIndicatorMarker = [webView stringByEvaluatingJavaScript:hasCorrectionIndicatorMarkerJavaScript];
EXPECT_WK_STREQ("1", hasCorrectionIndicatorMarker);
done = true;
}];

TestWebKitAPI::Util::run(&done);

[webView waitForNextPresentationUpdate];

auto snapshotConfiguration = adoptNS([[WKSnapshotConfiguration alloc] init]);
[snapshotConfiguration setAfterScreenUpdates:YES];

__block RetainPtr<CGImage> result;
done = false;
[webView takeSnapshotWithConfiguration:snapshotConfiguration.get() completionHandler:^(UIImage *snapshot, NSError *error) {
EXPECT_NULL(error);
result = snapshot.CGImage;
done = true;
}];

Util::run(&done);

[webView removeFromSuperview];

return result;
};

auto expected = createSnapshotForHTMLString(@"<body><div id='input' contenteditable/></body>");

auto actual = createSnapshotForHTMLString(@"<body style='color: black'><div id='input' contenteditable/></body>");

CGImagePixelReader snapshotReaderExpected { expected.get() };
CGImagePixelReader snapshotReaderActual { actual.get() };

for (int x = 0; x < frame.size.width * 3; ++x) {
for (int y = 0; y < frame.size.height * 3; ++y)
EXPECT_EQ(snapshotReaderExpected.at(x, y), snapshotReaderActual.at(x, y));
}
}
#endif

#endif // HAVE(REDESIGNED_TEXT_CURSOR)

#if HAVE(ESIM_AUTOFILL_SYSTEM_SUPPORT)
Expand Down

0 comments on commit a20779d

Please sign in to comment.