diff options
author | Kyle Lanmon <kyle.lanmon@gmail.com> | 2022-12-04 00:22:00 -0600 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-15 09:43:41 +0000 |
commit | d249a69150ce0c6fdea52dac9d6943679fda1533 (patch) | |
tree | a40b04c81183ff1cfb385a05c4ee2c9459f1541e /Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp | |
parent | 8197b7a06332d9513cab5efbbc31a459f546adf3 (diff) | |
download | serenity-d249a69150ce0c6fdea52dac9d6943679fda1533.zip |
LibWeb: Implement input week type sanitation algorithm
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp')
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index 09e2147624..fe9060328c 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -483,6 +483,51 @@ static bool is_valid_simple_color(DeprecatedString const& value) return true; } +// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#week-number-of-the-last-day +static u32 week_number_of_the_last_day(u64) +{ + // FIXME: sometimes return 53 (!) + // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#weeks + return 52; +} + +// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-week-string +static bool is_valid_week_string(DeprecatedString const& value) +{ + // A string is a valid week string representing a week-year year and week week if it consists of the following components in the given order: + + // 1. Four or more ASCII digits, representing year, where year > 0 + // 2. A U+002D HYPHEN-MINUS character (-) + // 3. A U+0057 LATIN CAPITAL LETTER W character (W) + // 4. Two ASCII digits, representing the week week, in the range 1 ≤ week ≤ maxweek, where maxweek is the week number of the last day of week-year year + auto parts = value.split('-'); + if (parts.size() != 2) + return false; + if (parts[0].length() < 4) + return false; + for (auto digit : parts[0]) + if (!is_ascii_digit(digit)) + return false; + if (parts[1].length() != 3) + return false; + + if (!parts[1].starts_with('W')) + return false; + if (!is_ascii_digit(parts[1][1])) + return false; + if (!is_ascii_digit(parts[1][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][1]) * 10) + parse_ascii_digit(parts[1][2]); + + return week >= 1 && week <= week_number_of_the_last_day(year); +} + // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-month-string static bool is_valid_month_string(DeprecatedString const& value) { @@ -593,6 +638,10 @@ DeprecatedString HTMLInputElement::value_sanitization_algorithm(DeprecatedString // https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):value-sanitization-algorithm if (!is_valid_month_string(value)) return ""; + } else if (type_state() == HTMLInputElement::TypeAttributeState::Week) { + // https://html.spec.whatwg.org/multipage/input.html#week-state-(type=week):value-sanitization-algorithm + if (!is_valid_week_string(value)) + return ""; } else if (type_state() == HTMLInputElement::TypeAttributeState::Color) { // https://html.spec.whatwg.org/multipage/input.html#color-state-(type=color):value-sanitization-algorithm // If the value of the element is a valid simple color, then set it to the value of the element converted to ASCII lowercase; |