mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 20:29:18 +00:00
LibWeb: Implement “convert a string to a number” for type=time inputs
This commit is contained in:
parent
678f531fe5
commit
8b0f6cb876
Notes:
github-actions[bot]
2025-03-04 12:34:10 +00:00
Author: https://github.com/sideshowbarker
Commit: 8b0f6cb876
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3794
Reviewed-by: https://github.com/tcl3 ✅
4 changed files with 39 additions and 13 deletions
|
@ -2137,6 +2137,18 @@ static Optional<double> convert_date_string_to_number(StringView input)
|
|||
return date_time.milliseconds_since_epoch();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-value-string-number
|
||||
Optional<double> HTMLInputElement::convert_time_string_to_number(StringView input) const
|
||||
{
|
||||
// The algorithm to convert a string to a number, given a string input, is as follows: If parsing a time from input
|
||||
// results in an error, then return an error; otherwise, return the number of milliseconds elapsed from midnight to
|
||||
// the parsed time on a day with no time changes.
|
||||
auto maybe_time = parse_time_string(realm(), input);
|
||||
if (maybe_time.is_exception())
|
||||
return {};
|
||||
return maybe_time.value()->date_value();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/input.html#concept-input-value-string-number
|
||||
Optional<double> HTMLInputElement::convert_string_to_number(StringView input) const
|
||||
{
|
||||
|
@ -2157,6 +2169,9 @@ Optional<double> HTMLInputElement::convert_string_to_number(StringView input) co
|
|||
if (type_state() == TypeAttributeState::Date)
|
||||
return convert_date_string_to_number(input);
|
||||
|
||||
if (type_state() == TypeAttributeState::Time)
|
||||
return convert_time_string_to_number(input);
|
||||
|
||||
dbgln("HTMLInputElement::convert_string_to_number() not implemented for input type {}", type());
|
||||
return {};
|
||||
}
|
||||
|
@ -2341,6 +2356,10 @@ double HTMLInputElement::default_step() const
|
|||
if (type_state() == TypeAttributeState::Range)
|
||||
return 1;
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-step-default
|
||||
if (type_state() == TypeAttributeState::Time)
|
||||
return 60;
|
||||
|
||||
dbgln("HTMLInputElement::default_step() not implemented for input type {}", type());
|
||||
return 0;
|
||||
}
|
||||
|
@ -2356,6 +2375,10 @@ double HTMLInputElement::step_scale_factor() const
|
|||
if (type_state() == TypeAttributeState::Range)
|
||||
return 1;
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-step-scale
|
||||
if (type_state() == TypeAttributeState::Time)
|
||||
return 1000;
|
||||
|
||||
dbgln("HTMLInputElement::step_scale_factor() not implemented for input type {}", type());
|
||||
return 0;
|
||||
}
|
||||
|
@ -2398,8 +2421,10 @@ double HTMLInputElement::step_base() const
|
|||
|
||||
// 2. If the element has a value content attribute, and the result of applying the algorithm to convert a string to a number to the value of
|
||||
// the value content attribute is not an error, then return that result.
|
||||
if (auto value = convert_string_to_number(this->value()); value.has_value())
|
||||
return *value;
|
||||
// AD-HOC: https://github.com/whatwg/html/issues/11097 Skipping this step seems to be necessary in order to get the
|
||||
// behavior that's actually expected — and necessary in order get the tests to pass (they otherwise fail).
|
||||
// if (auto value = convert_string_to_number(this->value()); value.has_value())
|
||||
// return *value;
|
||||
|
||||
// 3. If a default step base is defined for this element given its type attribute's state, then return it.
|
||||
if (type_state() == TypeAttributeState::Week) {
|
||||
|
|
|
@ -278,6 +278,7 @@ private:
|
|||
virtual void initialize(JS::Realm&) override;
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
Optional<double> convert_time_string_to_number(StringView input) const;
|
||||
Optional<double> convert_string_to_number(StringView input) const;
|
||||
String convert_number_to_string(double input) const;
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@ Harness status: OK
|
|||
|
||||
Found 130 tests
|
||||
|
||||
108 Pass
|
||||
22 Fail
|
||||
112 Pass
|
||||
18 Fail
|
||||
Pass [INPUT in TEXT status] no constraint
|
||||
Pass [INPUT in TEXT status] no constraint (in a form)
|
||||
Pass [INPUT in TEXT status] not suffering from being too long
|
||||
|
@ -102,16 +102,16 @@ Fail [INPUT in TIME status] suffering from an overflow
|
|||
Fail [INPUT in TIME status] suffering from an overflow (in a form)
|
||||
Fail [INPUT in TIME status] suffering from an underflow
|
||||
Fail [INPUT in TIME status] suffering from an underflow (in a form)
|
||||
Fail [INPUT in TIME status] suffering from a step mismatch
|
||||
Fail [INPUT in TIME status] suffering from a step mismatch (in a form)
|
||||
Pass [INPUT in TIME status] suffering from a step mismatch
|
||||
Pass [INPUT in TIME status] suffering from a step mismatch (in a form)
|
||||
Pass [INPUT in TIME status] suffering from being missing
|
||||
Pass [INPUT in TIME status] suffering from being missing (in a form)
|
||||
Fail [INPUT in NUMBER status] suffering from an overflow
|
||||
Fail [INPUT in NUMBER status] suffering from an overflow (in a form)
|
||||
Fail [INPUT in NUMBER status] suffering from an underflow
|
||||
Fail [INPUT in NUMBER status] suffering from an underflow (in a form)
|
||||
Fail [INPUT in NUMBER status] suffering from a step mismatch
|
||||
Fail [INPUT in NUMBER status] suffering from a step mismatch (in a form)
|
||||
Pass [INPUT in NUMBER status] suffering from a step mismatch
|
||||
Pass [INPUT in NUMBER status] suffering from a step mismatch (in a form)
|
||||
Pass [INPUT in NUMBER status] suffering from being missing
|
||||
Pass [INPUT in NUMBER status] suffering from being missing (in a form)
|
||||
Pass [INPUT in CHECKBOX status] no constraint
|
||||
|
|
|
@ -2,8 +2,8 @@ Harness status: OK
|
|||
|
||||
Found 60 tests
|
||||
|
||||
48 Pass
|
||||
12 Fail
|
||||
51 Pass
|
||||
9 Fail
|
||||
Pass valueAsNumber getter on type date (actual value: , expected valueAsNumber: NaN)
|
||||
Pass valueAsNumber getter on type date (actual value: 0000-12-10, expected valueAsNumber: NaN)
|
||||
Pass valueAsNumber getter on type date (actual value: 2019-00-12, expected valueAsNumber: NaN)
|
||||
|
@ -34,9 +34,9 @@ Pass valueAsNumber setter on type week (actual valueAsNumber: -20217600000, expe
|
|||
Pass valueAsNumber getter on type time (actual value: , expected valueAsNumber: NaN)
|
||||
Pass valueAsNumber getter on type time (actual value: 24:00, expected valueAsNumber: NaN)
|
||||
Pass valueAsNumber getter on type time (actual value: 00:60, expected valueAsNumber: NaN)
|
||||
Fail valueAsNumber getter on type time (actual value: 00:00, expected valueAsNumber: 0)
|
||||
Fail valueAsNumber getter on type time (actual value: 12:00, expected valueAsNumber: 43200000)
|
||||
Fail valueAsNumber getter on type time (actual value: 23:59, expected valueAsNumber: 86340000)
|
||||
Pass valueAsNumber getter on type time (actual value: 00:00, expected valueAsNumber: 0)
|
||||
Pass valueAsNumber getter on type time (actual value: 12:00, expected valueAsNumber: 43200000)
|
||||
Pass valueAsNumber getter on type time (actual value: 23:59, expected valueAsNumber: 86340000)
|
||||
Fail valueAsNumber setter on type time (actual valueAsNumber: 0, expected value: 00:00)
|
||||
Fail valueAsNumber setter on type time (actual valueAsNumber: 43200000, expected value: 12:00)
|
||||
Fail valueAsNumber setter on type time (actual valueAsNumber: 86340000, expected value: 23:59)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue