From a61883ae88eb3fd4b35cc51db77a4c9405480fb5 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Tue, 26 Nov 2024 15:36:18 +0000 Subject: [PATCH] LibWeb: Use correct integer parsing rules in `HTMLLIElement::value()` --- Libraries/LibWeb/HTML/HTMLLIElement.cpp | 13 +++++++++++++ Libraries/LibWeb/HTML/HTMLLIElement.h | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Libraries/LibWeb/HTML/HTMLLIElement.cpp b/Libraries/LibWeb/HTML/HTMLLIElement.cpp index d06806fca36..c443d9623b2 100644 --- a/Libraries/LibWeb/HTML/HTMLLIElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLLIElement.cpp @@ -6,6 +6,7 @@ #include #include +#include #include namespace Web::HTML { @@ -25,4 +26,16 @@ void HTMLLIElement::initialize(JS::Realm& realm) WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLLIElement); } +// https://html.spec.whatwg.org/multipage/grouping-content.html#dom-li-value +WebIDL::Long HTMLLIElement::value() +{ + // The value IDL attribute must reflect the value of the value content attribute. + // NOTE: This is equivalent to the code that would be generated by the IDL generator if we used [Reflect] on the value attribute. + // We don't do that in this case, since this method is used elsewhere. + auto content_attribute_value = get_attribute(AttributeNames::value).value_or("0"_string); + if (auto maybe_number = HTML::parse_integer(content_attribute_value); maybe_number.has_value()) + return *maybe_number; + return 0; +} + } diff --git a/Libraries/LibWeb/HTML/HTMLLIElement.h b/Libraries/LibWeb/HTML/HTMLLIElement.h index 8c85e5d0110..d03e391104f 100644 --- a/Libraries/LibWeb/HTML/HTMLLIElement.h +++ b/Libraries/LibWeb/HTML/HTMLLIElement.h @@ -8,6 +8,7 @@ #include #include +#include namespace Web::HTML { @@ -21,8 +22,8 @@ public: // https://www.w3.org/TR/html-aria/#el-li virtual Optional default_role() const override { return ARIA::Role::listitem; } - i32 value() { return get_attribute(AttributeNames::value).value_or("0"_string).to_number().value_or(0); } - void set_value(i32 value) + WebIDL::Long value(); + void set_value(WebIDL::Long value) { set_attribute(AttributeNames::value, String::number(value)).release_value_but_fixme_should_propagate_errors(); }