summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp
diff options
context:
space:
mode:
authorKyle Lanmon <kyle.lanmon@gmail.com>2022-12-03 23:10:45 -0600
committerLinus Groh <mail@linusgroh.de>2022-12-15 09:43:41 +0000
commit6fa34a4ee3419bfb8dc4d33223451148352fe26a (patch)
tree77f32f0640e96d0198c3fbd49602a30f090460ad /Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp
parenta2cf026b306331f5e64d6658d34dfa474d412ae0 (diff)
downloadserenity-6fa34a4ee3419bfb8dc4d33223451148352fe26a.zip
LibWeb: Implement input color type sanitation algorithm
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp')
-rw-r--r--Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index aa8d45a434..b5369da429 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -466,6 +466,23 @@ void HTMLInputElement::set_type(DeprecatedString const& type)
MUST(set_attribute(HTML::AttributeNames::type, type));
}
+// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-simple-colour
+static bool is_valid_simple_color(DeprecatedString const& value)
+{
+ // if it is exactly seven characters long,
+ if (value.length() != 7)
+ return false;
+ // and the first character is a U+0023 NUMBER SIGN character (#),
+ if (!value.starts_with('#'))
+ return false;
+ // and the remaining six characters are all ASCII hex digits
+ for (size_t i = 1; i < value.length(); i++)
+ if (!is_ascii_hex_digit(value[i]))
+ return false;
+
+ return true;
+}
+
// https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
DeprecatedString HTMLInputElement::value_sanitization_algorithm(DeprecatedString value) const
{
@@ -496,6 +513,13 @@ DeprecatedString HTMLInputElement::value_sanitization_algorithm(DeprecatedString
auto maybe_double = value.to_double(TrimWhitespace::Yes);
if (!maybe_double.has_value() || !isfinite(maybe_double.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;
+ if (is_valid_simple_color(value))
+ return value.to_lowercase();
+ // otherwise, set it to the string "#000000".
+ return "#000000";
}
// FIXME: Implement remaining value sanitation algorithms