Skip to content

Commit

Permalink
[iOS] Unable to autofill username-only pages in WKWebView
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=273538
rdar://126970887

Reviewed by Wenson Hsieh and Abrar Rahman Protyasha.

As a result of UIKit / AutoFillUI changes in iOS 17.4, username autofill is now
displayed as a suggestion in the keyboard candidates bar on pages that separate
out their username and password fields, like yahoo.com. Previously, this
autofill flow was only available to pages that contained both a username and
password field together.

However, the introduction of standalone username support was a consequence of
other feature work, and never explicitly tested in WKWebView. Due to the way
autofill text suggestions are set up in WebKit, attempting to autofill a
standalone username field, simply drops the content.

Specifically, `AutofillElements::computeAutofillElements` returns `std::nullopt`
if no password field is present. This means that when a username is autofilled
using `-[WKContentView insertTextSuggestion:]` and
`WebPageProxy::autofillLoginCredentials`, the data is simply dropped by an
early return in `WebPage::autofillLoginCredentials`.

Fix by updating `AutofillElements::computeAutofillElements` to handle the
username-only case.

* Source/WebCore/editing/cocoa/AutofillElements.cpp:
(WebCore::AutofillElements::computeAutofillElements):

Support for autofilling specific fields is already present in
`AutofillElements::autofill`, which null checks each individual autofill
element, including the username field.

However, this method currently returns no elements in the case where only a
username field is present. Update to return the username field in the case
where the `autocomplete` value is "username". This restriction corresponds to
the specification of `UITextContentTypeUsername` on the UI process side.

* Tools/TestWebKitAPI/Tests/ios/WKWebViewAutofillTests.mm:
(TestWebKitAPI::TEST(WKWebViewAutoFillTests, StandaloneUsernameField)):

Canonical link: https://commits.webkit.org/278215@main
  • Loading branch information
pxlcoder committed May 1, 2024
1 parent 40274a8 commit 926918e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Source/WebCore/editing/cocoa/AutofillElements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ std::optional<AutofillElements> AutofillElements::computeAutofillElements(Ref<HT
}
}

// Handle the case where a username field appears separately from a password field.
auto autofillData = start->autofillData();
if (toAutofillFieldName(autofillData.fieldName) == AutofillFieldName::Username)
return {{ WTFMove(start), nullptr, nullptr }};

return std::nullopt;
}

Expand Down
16 changes: 16 additions & 0 deletions Tools/TestWebKitAPI/Tests/ios/WKWebViewAutofillTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ - (BOOL)acceptsAutoFillLoginCredentials
EXPECT_FALSE([webView acceptsAutoFillLoginCredentials]);
}

TEST(WKWebViewAutoFillTests, StandaloneUsernameField)
{
auto webView = adoptNS([[AutoFillTestView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
[webView synchronouslyLoadHTMLString:@"<input id='username' autocomplete='username'>"];
[webView evaluateJavaScriptAndWaitForInputSessionToChange:@"username.focus()"];
EXPECT_TRUE([webView acceptsAutoFillLoginCredentials]);

auto credentialSuggestion = [UITextAutofillSuggestion autofillSuggestionWithUsername:@"frederik" password:@"famos"];
[[webView _autofillInputView] insertTextSuggestion:credentialSuggestion];

EXPECT_WK_STREQ("frederik", [webView stringByEvaluatingJavaScript:@"username.value"]);

[webView evaluateJavaScriptAndWaitForInputSessionToChange:@"document.activeElement.blur()"];
EXPECT_FALSE([webView acceptsAutoFillLoginCredentials]);
}

TEST(WKWebViewAutoFillTests, StandaloneTextField)
{
auto webView = adoptNS([[AutoFillTestView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
Expand Down

0 comments on commit 926918e

Please sign in to comment.