LibWeb: Move logic to check if valueAsNumber applies to its own function

This commit is contained in:
Bastiaan van der Plaat 2023-12-07 19:31:42 +01:00 committed by Tim Flynn
parent c0751b2a49
commit 1a63639518
Notes: sideshowbarker 2024-07-17 06:40:35 +09:00
2 changed files with 20 additions and 2 deletions

View file

@ -1206,7 +1206,7 @@ String HTMLInputElement::covert_number_to_string(double input) const
WebIDL::ExceptionOr<double> 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)
if (!value_as_number_applies())
return NAN;
// Otherwise, run the algorithm to convert a string to a number defined for that state to the element's value;
@ -1222,7 +1222,7 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_value_as_number(double 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)
if (!value_as_number_applies())
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.
@ -1411,4 +1411,21 @@ bool HTMLInputElement::change_event_applies() const
}
}
// https://html.spec.whatwg.org/multipage/input.html#the-input-element:dom-input-valueasnumber-3
bool HTMLInputElement::value_as_number_applies() const
{
switch (type_state()) {
case TypeAttributeState::Date:
case TypeAttributeState::Month:
case TypeAttributeState::Week:
case TypeAttributeState::Time:
case TypeAttributeState::LocalDateAndTime:
case TypeAttributeState::Number:
case TypeAttributeState::Range:
return true;
default:
return false;
}
}
}

View file

@ -160,6 +160,7 @@ public:
bool has_input_activation_behavior() const;
bool change_event_applies() const;
bool value_as_number_applies() const;
private:
HTMLInputElement(DOM::Document&, DOM::QualifiedName);