Skip to content

Commit

Permalink
Support title attribute for pattern validation errors
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=258301
rdar://111394402

Reviewed by Aditya Keerthi.

The HTML Standard suggests that the title attribute can be used to
augment the message presented to the end user. As this is not trusted
UI space this seems reasonable (JavaScript has full control over this
UI) and also matches other browsers.

* LayoutTests/fast/forms/validation-messages-expected.txt:
* LayoutTests/fast/forms/validation-messages.html:
* LayoutTests/platform/ios-wk2/fast/forms/validation-messages-expected.txt:
* Source/WebCore/en.lproj/Localizable.strings:
* Source/WebCore/html/InputType.cpp:
(WebCore::InputType::validationMessage const):
* Source/WebCore/platform/LocalizedStrings.cpp:
(WebCore::validationMessagePatternMismatchText):
* Source/WebCore/platform/LocalizedStrings.h:

Canonical link: https://commits.webkit.org/266311@main
  • Loading branch information
annevk committed Jul 26, 2023
1 parent 933d6a6 commit 2d36f43
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions LayoutTests/fast/forms/validation-messages-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Required file input:
Required email input:
Required url input:
Required input with pattern:
Required input with pattern and title:
Required input with minlength=100:
Required range with min=5:
Required range with max=5:
Expand All @@ -21,6 +22,7 @@ PASS message is "Select a file"
PASS message is "Enter an email address"
PASS message is "Enter a URL"
PASS message is "Match the requested format"
PASS message is "Match the requested format: UI testing is awesome"
PASS message is "Use at least 100 characters"
PASS message is "Value must be greater than or equal to 5"
PASS message is "Value must be less than or equal to 5"
Expand Down
11 changes: 11 additions & 0 deletions LayoutTests/fast/forms/validation-messages.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
<form>
Required input with pattern: <input type="text" value="1" pattern="[a-z]" required><input id="input_with_pattern_submit" type="submit">
</form>
<form>
Required input with pattern and title: <input type="text" value="1" pattern="[a-z]" required title="
UI
testing
is
awesome "><input id="input_with_pattern_title_submit" type="submit">
</form>
<form>
Required input with minlength=100: <input type="text" minlength=100 id="field_with_minlength" required><input id="input_with_minlength_submit" type="submit">
</form>
Expand All @@ -52,6 +62,7 @@
['required_email_submit', 'Enter an email address'],
['required_url_submit', 'Enter a URL'],
['input_with_pattern_submit', 'Match the requested format'],
['input_with_pattern_title_submit', 'Match the requested format: UI testing is awesome'],
['input_with_minlength_submit', 'Use at least 100 characters'],
['range_with_min_submit', 'Value must be greater than or equal to 5'],
['range_with_max_submit', 'Value must be less than or equal to 5'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Required file input:
Required email input:
Required url input:
Required input with pattern:
Required input with pattern and title:
Required input with minlength=100:
Required range with min=5:
Required range with max=5:
Expand All @@ -21,6 +22,7 @@ PASS message is "Select a file"
PASS message is "Enter an email address"
PASS message is "Enter a URL"
PASS message is "Match the requested format"
PASS message is "Match the requested format: UI testing is awesome"
FAIL message should be Use at least 100 characters. Was Fill out this field.
PASS message is "Value must be greater than or equal to 5"
PASS message is "Value must be less than or equal to 5"
Expand Down
6 changes: 6 additions & 0 deletions Source/WebCore/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,12 @@
/* Validation message for input form controls requiring a constrained value according to pattern */
"Match the requested format" = "Match the requested format";

/* Validation message for input form controls requiring a constrained value according to pattern followed by a website-provided description of the pattern */
"Match the requested format: %@" = "Match the requested format: %@";

/* Validation message for input form controls requiring a constrained value according to pattern followed by a website-provided description of the pattern */
"Match the requested format: %s" = "Match the requested format: %s";

/* Malware confirmation dialog */
"Merely visiting a site is sufficient for malware to install itself and harm your computer." = "Merely visiting a site is sufficient for malware to install itself and harm your computer.";

Expand Down
8 changes: 6 additions & 2 deletions Source/WebCore/html/InputType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,12 @@ String InputType::validationMessage() const
if (typeMismatch())
return typeMismatchText();

if (patternMismatch(value))
return validationMessagePatternMismatchText();
if (patternMismatch(value)) {
auto title = element()->attributeWithoutSynchronization(HTMLNames::titleAttr).string().trim(isASCIIWhitespace).simplifyWhiteSpace(isASCIIWhitespace);
if (title.isEmpty())
return validationMessagePatternMismatchText();
return validationMessagePatternMismatchText(title);
}

if (element()->tooShort())
return validationMessageTooShortText(value.length(), element()->minLength());
Expand Down
12 changes: 12 additions & 0 deletions Source/WebCore/platform/LocalizedStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,18 @@ String validationMessagePatternMismatchText()
return WEB_UI_STRING("Match the requested format", "Validation message for input form controls requiring a constrained value according to pattern");
}

String validationMessagePatternMismatchText(const String& title)
{
#if PLATFORM(COCOA)
return WEB_UI_FORMAT_CFSTRING("Match the requested format: %@", "Validation message for input form controls requiring a constrained value according to pattern followed by a website-provided description of the pattern", title.createCFString().get());
#elif USE(GLIB)
return WEB_UI_FORMAT_STRING("Match the requested format: %s", "Validation message for input form controls requiring a constrained value according to pattern followed by a website-provided description of the pattern", title.utf8().data());
#else
UNUSED_PARAM(title);
return validationMessagePatternMismatchText();
#endif
}

#if !PLATFORM(GTK)
String validationMessageTooShortText(int, int minLength)
{
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/platform/LocalizedStrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ namespace WebCore {
String validationMessageTypeMismatchForMultipleEmailText();
String validationMessageTypeMismatchForURLText();
String validationMessagePatternMismatchText();
String validationMessagePatternMismatchText(const String& title);
String validationMessageTooShortText(int valueLength, int minLength);
String validationMessageTooLongText(int valueLength, int maxLength);
String validationMessageRangeUnderflowText(const String& minimum);
Expand Down

0 comments on commit 2d36f43

Please sign in to comment.