LibWeb/HTML: Correctly normalize local datetime values

Now trailing zeros in the second and millisecond part of local datetime
values are removed during normalization.
This commit is contained in:
Glenn Skrzypczak 2025-08-10 17:46:41 +02:00 committed by Tim Flynn
commit 544a6b21fc
Notes: github-actions[bot] 2025-08-14 15:07:16 +00:00
3 changed files with 29 additions and 8 deletions

View file

@ -152,13 +152,35 @@ bool is_valid_local_date_and_time_string(Utf16View const& value)
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-normalised-local-date-and-time-string
Utf16String normalize_local_date_and_time_string(Utf16String const& value)
{
// A string is a valid normalized local date and time string representing a date and time if it consists of the following components in the given order:
// 1. A valid date string representing the date
// 2. A U+0054 LATIN CAPITAL LETTER T character (T)
// 3. A valid time string representing the time, expressed as the shortest possible string for the given time (e.g. omitting the seconds component entirely if the given time is zero seconds past the minute)
auto value_with_normalized_t = value;
if (auto spaces = value.count(" "sv); spaces > 0) {
VERIFY(spaces == 1);
return value.replace(" "sv, "T"sv, ReplaceMode::FirstOnly);
value_with_normalized_t = value.replace(" "sv, "T"sv, ReplaceMode::FirstOnly);
}
VERIFY(value.count("T"sv) == 1);
return value;
auto parts = value_with_normalized_t.split_view('T', SplitBehavior::KeepEmpty);
VERIFY(parts.size() == 2);
auto normalized_length = parts[1].length_in_code_points();
while (normalized_length > 9) {
if (parts[1].code_point_at(normalized_length - 1) != '0') {
return Utf16String::formatted("{}T{}", parts[0], parts[1].unicode_substring_view(0, normalized_length));
}
normalized_length--;
}
if (normalized_length > 5) {
auto time_without_milliseconds = parts[1].unicode_substring_view(0, 8);
return Utf16String::formatted("{}T{}", parts[0], parts[1].unicode_substring_view(0, time_without_milliseconds.ends_with(":00"sv) ? 5 : 8));
}
return value_with_normalized_t;
}
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-time-string