diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index 9f9d142b19..f931e078fb 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -1137,6 +1137,39 @@ i32 HTMLInputElement::default_tab_index_value() const return 0; } +// https://html.spec.whatwg.org/multipage/input.html#dom-input-valueasnumber +WebIDL::ExceptionOr HTMLInputElement::value_as_number() const +{ + // On getting, if the valueAsNumber attribute does not apply, as defined for the input element's type attribute's current state, then return a Not-a-Number (NaN) value. + if (type_state() != TypeAttributeState::Date || type_state() != TypeAttributeState::Month || type_state() != TypeAttributeState::Week || type_state() != TypeAttributeState::Time || type_state() != TypeAttributeState::LocalDateAndTime || type_state() != TypeAttributeState::Number || type_state() != TypeAttributeState::Range) + return NAN; + + // Otherwise, run the algorithm to convert a string to a number defined for that state to the element's value; if the algorithm returned a number, then return it, otherwise, return a Not-a-Number (NaN) value. + return value().to_double().value_or(NAN); +} + +// https://html.spec.whatwg.org/multipage/input.html#dom-input-valueasnumber +WebIDL::ExceptionOr HTMLInputElement::set_value_as_number(double value) +{ + // On setting, if the new value is infinite, then throw a TypeError exception. + if (!isfinite(value)) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "valueAsNumber: Value is infinite"sv }; + + // Otherwise, if the valueAsNumber attribute does not apply, as defined for the input element's type attribute's current state, then throw an "InvalidStateError" DOMException. + if (type_state() != TypeAttributeState::Date || type_state() != TypeAttributeState::Month || type_state() != TypeAttributeState::Week || type_state() != TypeAttributeState::Time || type_state() != TypeAttributeState::LocalDateAndTime || type_state() != TypeAttributeState::Number || type_state() != TypeAttributeState::Range) + return WebIDL::InvalidStateError::create(realm(), "valueAsNumber: Invalid input type used"_fly_string); + + // Otherwise, if the new value is a Not-a-Number (NaN) value, then set the value of the element to the empty string. + if (value == NAN) { + m_value = ""; + return {}; + } + + // Otherwise, run the algorithm to convert a number to a string, as defined for that state, on the new value, and set the value of the element to the resulting string. + m_value = DeprecatedString::number(value); + return {}; +} + // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-checkvalidity WebIDL::ExceptionOr HTMLInputElement::check_validity() { diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h index 927ce0b5ce..29a9784bda 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -94,6 +94,9 @@ public: // https://html.spec.whatwg.org/multipage/input.html#update-the-file-selection void update_the_file_selection(JS::NonnullGCPtr); + WebIDL::ExceptionOr value_as_number() const; + WebIDL::ExceptionOr set_value_as_number(double value); + WebIDL::ExceptionOr check_validity(); WebIDL::ExceptionOr report_validity(); void set_custom_validity(String const&); diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl index 99c43e9609..cdc90655cc 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.idl @@ -19,7 +19,7 @@ interface HTMLInputElement : HTMLElement { // FIXME: [CEReactions] attribute USVString formAction; // FIXME: [CEReactions] attribute DOMString formEnctype; // FIXME: [CEReactions] attribute DOMString formMethod; - [CEReactions, Reflect=formnovalidate] attribute boolean formNoValidate; + [CEReactions, Reflect=formnovalidate] attribute boolean formNoValidate; [CEReactions, Reflect=formtarget] attribute DOMString formTarget; // FIXME: [CEReactions] attribute unsigned long height; attribute boolean indeterminate; @@ -41,7 +41,7 @@ interface HTMLInputElement : HTMLElement { [CEReactions, Reflect=value] attribute DOMString defaultValue; [CEReactions, LegacyNullToEmptyString] attribute DOMString value; // FIXME: attribute object? valueAsDate; - // FIXME: attribute unrestricted double valueAsNumber; + attribute unrestricted double valueAsNumber; // FIXME: [CEReactions] attribute unsigned long width; // FIXME: undefined stepUp(optional long n = 1);