LibWeb/HTML: Ensure the year of month and week values is greater than 0

The validation algorithm for month and week inputs now also checks if
the year is greater than zero.
This commit is contained in:
Glenn Skrzypczak 2025-08-10 17:55:41 +02:00 committed by Tim Flynn
commit 57376f8701
Notes: github-actions[bot] 2025-08-14 15:07:09 +00:00
2 changed files with 9 additions and 16 deletions

View file

@ -51,9 +51,9 @@ bool is_valid_week_string(Utf16View const& value)
if (parts[1].length_in_code_units() != 3)
return false;
for (auto digit : parts[0])
if (!is_ascii_digit(digit))
return false;
auto year = parts[0].to_number<u64>();
if (!year.has_value() || *year == 0)
return false;
if (!parts[1].starts_with('W'))
return false;
@ -62,14 +62,8 @@ bool is_valid_week_string(Utf16View const& value)
if (!is_ascii_digit(parts[1].code_unit_at(2)))
return false;
u64 year = 0;
for (auto d : parts[0]) {
year *= 10;
year += parse_ascii_digit(d);
}
auto week = (parse_ascii_digit(parts[1].code_unit_at(1)) * 10) + parse_ascii_digit(parts[1].code_unit_at(2));
return week >= 1 && week <= week_number_of_the_last_day(year);
return week >= 1 && week <= week_number_of_the_last_day(*year);
}
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-month-string
@ -90,9 +84,9 @@ bool is_valid_month_string(Utf16View const& value)
if (parts[1].length_in_code_units() != 2)
return false;
for (auto digit : parts[0])
if (!is_ascii_digit(digit))
return false;
auto year = parts[0].to_number<u64>();
if (!year.has_value() || *year == 0)
return false;
if (!is_ascii_digit(parts[1].code_unit_at(0)))
return false;

View file

@ -2,8 +2,7 @@ Harness status: OK
Found 15 tests
14 Pass
1 Fail
15 Pass
Pass year can be more than four digits
Pass valid value test
Pass year can contain prefixes of zero, as long as there are at least four digits
@ -15,7 +14,7 @@ Pass When value is set with invalid value, the value must return empty string.
Pass When step attribute is given invalid value, it must ignore the invalid value and use defaul value instead.
Pass Month should be <= 13: If the value of the element is not a valid month string, then set it to the empty string instead.
Pass Month should be > 0: If the value of the element is not a valid month string, then set it to the empty string instead.>
Fail Year should be > 0: If the value of the element is not a valid year string, then set it to the empty string instead.>
Pass Year should be > 0: If the value of the element is not a valid year string, then set it to the empty string instead.>
Pass Month should be two digits: If the value of the element is not a valid month string, then set it to the empty string instead.>
Pass Month should be two digits not characters: If the value of the element is not a valid month string, then set it to the empty string instead.>
Pass Value should be two parts: If the value of the element is not a valid month string, then set it to the empty string instead.>