Explorar el Código

LibWeb: Add input element valueAsNumber

Bastiaan van der Plaat hace 1 año
padre
commit
d5ca8209bf

+ 33 - 0
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<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)
+        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<void> 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<bool> HTMLInputElement::check_validity()
 {

+ 3 - 0
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<FileAPI::FileList>);
 
+    WebIDL::ExceptionOr<double> value_as_number() const;
+    WebIDL::ExceptionOr<void> set_value_as_number(double value);
+
     WebIDL::ExceptionOr<bool> check_validity();
     WebIDL::ExceptionOr<bool> report_validity();
     void set_custom_validity(String const&);

+ 2 - 2
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);