dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / html_input_element_6.lucb
#==============================================================================================
#
# html_input_element_6 - HTML::HTMLInputElement: numbers, dates and stepping
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLInputElement.cpp (lines 2439-3116): the algorithms to convert
# a string to a number and a number to a string for the number, range, month, week,
# date, time and local date and time states, the conversions to and from Date objects
# (JS::Date is phase 3: making or reading one traps until then), the minimum, maximum,
# default step, step scale factor, allowed value step and step base, the valueAsDate and
# valueAsNumber IDL attributes, and stepUp()/stepDown().
#
#==============================================================================================
# mark: Converting strings to numbers ===========================================================
## Ported from HTMLInputElement.cpp:2440 Web::HTML::convert_month_string_to_number.
# https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):concept-input-value-string-number
func html_convert_month_string_to_number(input: ak.StringView) -> f64?:
# The algorithm to convert a string to a number, given a string input, is as follows: If parsing a month from input
# results in an error, then return an error; otherwise, return the number of months between January 1970 and the
# parsed month.
let maybe_year_and_month = html_parse_a_month_string(input) else return none
return f64(html_number_of_months_since_unix_epoch(maybe_year_and_month))
## Ported from HTMLInputElement.cpp:2452 Web::HTML::convert_week_string_to_number.
# https://html.spec.whatwg.org/multipage/input.html#week-state-(type=week):concept-input-value-string-number
func html_convert_week_string_to_number(input: ak.StringView) -> f64?:
# The algorithm to convert a string to a number, given a string input, is as follows: If parsing a week
# string from input results in an error, then return an error; otherwise, return the number of
# milliseconds elapsed from midnight UTC on the morning of 1970-01-01 (the time represented by the value
# "1970-01-01T00:00:00.0Z") to midnight UTC on the morning of the Monday of the parsed week, ignoring
# leap seconds.
let parsed_week = html_parse_a_week_string(input) else return none
let date_time = ak.unix_date_time_from_iso8601_week(parsed_week.week_year, parsed_week.week)
return f64(ak.unix_date_time_milliseconds_since_epoch(&date_time))
## Ported from HTMLInputElement.cpp:2466 Web::HTML::convert_date_string_to_number.
# https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):concept-input-value-number-string
func html_convert_date_string_to_number(input: ak.StringView) -> f64?:
# The algorithm to convert a string to a number, given a string input, is as follows: If parsing a date
# from input results in an error, then return an error; otherwise, return the number of milliseconds
# elapsed from midnight UTC on the morning of 1970-01-01 (the time represented by the value
# "1970-01-01T00:00:00.0Z") to midnight UTC on the morning of the parsed date, ignoring leap seconds.
let date = html_parse_a_date_string(input) else return none
let date_time = ak.unix_date_time_from_unix_time_parts((i32)date.year, (u8)date.month, (u8)date.day, 0, 0, 0, 0)
return f64(ak.unix_date_time_milliseconds_since_epoch(&date_time))
## Ported from HTMLInputElement.cpp:2482 Web::HTML::convert_local_date_and_time_string_to_number.
# https://html.spec.whatwg.org/multipage/input.html#local-date-and-time-state-(type=datetime-local):parse-a-local-date-and-time-string-2
func html_convert_local_date_and_time_string_to_number(input: ak.StringView) -> f64?:
# The algorithm to convert a string to a number, given a string input, is as follows: If parsing a date and time
# from input results in an error, then return an error; otherwise, return the number of milliseconds elapsed from
# midnight on the morning of 1970-01-01 (the time represented by the value "1970-01-01T00:00:00.0") to the parsed
# local date and time, ignoring leap seconds.
let date_and_time = html_parse_a_local_date_and_time_string(input) else return none
let date = date_and_time.date
let time = date_and_time.time
let date_time = ak.unix_date_time_from_unix_time_parts((i32)date.year, (u8)date.month, (u8)date.day, (u8)time.hour, (u8)time.minute, (u8)time.second, (u16)((i32)(time.second * 1000.0) % 1000))
return f64(ak.unix_date_time_milliseconds_since_epoch(&date_time))
## Ported from HTMLInputElement.cpp:2500 Web::HTML::HTMLInputElement::convert_time_string_to_number.
# https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-value-string-number
func html_input_element_convert_time_string_to_number(this: HtmlInputElement*, input: ak.StringView) -> f64?:
# 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.
let maybe_time = html_parse_time_string(html_input_element_realm(this), input) catch:
_ = idl_take_pending_exception()
return none
return js_date_date_value(maybe_time)
## Ported from HTMLInputElement.cpp:2512 Web::HTML::HTMLInputElement::convert_string_to_number.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-value-string-number
func html_input_element_convert_string_to_number_string_view(this: HtmlInputElement*, input: ak.StringView) -> f64?:
# https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number):concept-input-value-string-number
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.number:
return html_parse_floating_point_number_string_view(input)
# https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-value-string-number
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.range:
return html_parse_floating_point_number_string_view(input)
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.month:
return html_convert_month_string_to_number(input)
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.week:
return html_convert_week_string_to_number(input)
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.date:
return html_convert_date_string_to_number(input)
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.time:
return html_input_element_convert_time_string_to_number(this, input)
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.local_date_and_time:
return html_convert_local_date_and_time_string_to_number(input)
return none
## Ported from HTMLInputElement.cpp:2541 Web::HTML::HTMLInputElement::convert_string_to_number.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-value-string-number
func html_input_element_convert_string_to_number_utf16_string(this: HtmlInputElement*, input: ak.Utf16String) -> f64?:
# FIXME: Implement a UTF-16 GenericLexer.
if not ak.utf16_string_base_has_ascii_storage(&input.utf16_string_base):
return none
return html_input_element_convert_string_to_number_string_view(this, ak.utf16_string_base_ascii_view(&input.utf16_string_base))
# mark: Converting numbers to strings ===========================================================
## Ported from HTMLInputElement.cpp:2550 Web::HTML::convert_number_to_month_string.
# https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):concept-input-value-number-string
func html_convert_number_to_month_string(input: f64) -> ak.Utf16String:
# The algorithm to convert a number to a string, given a number input, is as follows: Return a valid month
# string that represents the month that has input months between it and January 1970.
let months = js_modulo(input, 12.0)
let year = 1970.0 + (input - months) / 12.0
return html_utf16_formatted("{:04d}-{:02d}", [ak.arg_i64((i64)(i32)year), ak.arg_i64((i64)((i32)months + 1))])
## Ported from HTMLInputElement.cpp:2561 Web::HTML::convert_date_to_month_string.
# https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):concept-input-value-date-string
func html_convert_date_to_month_string(input: f64) -> ak.Utf16String:
# The algorithm to convert a number to a string, given a number input, is as follows: Return a valid month
# string that represents the month that has input months between it and January 1970.
let date = ak.unix_date_time_from_milliseconds_since_epoch((i64)input)
return ak.unix_date_time_to_utf16_string(&date, ak.sv("%Y-%m"), ak.UnixDateTimeLocalTime.no)
## `constexpr Array normalYearDays` of convert_number_to_week_string.
let html_convert_number_to_week_string_normal_year_days: i32[12] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
## `constexpr Array leapYearDays` of convert_number_to_week_string.
let html_convert_number_to_week_string_leap_year_days: i32[12] = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]
## Ported from HTMLInputElement.cpp:2570 Web::HTML::convert_number_to_week_string.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-value-number-string
func html_convert_number_to_week_string(input: f64) -> ak.Utf16String:
# The algorithm to convert a number to a string, given a number input, is as follows: Return a valid week string
# that represents the week that, in UTC, is current input milliseconds after midnight UTC on the morning of
# 1970-01-01 (the time represented by the value "1970-01-01T00:00:00.0Z").
# AD-HOC: Values outside the valid ECMSScript time value range cannot produce valid date strings.
if not math.is_finite(input) or math.abs(input) > js_max_time_value:
return ak.utf16_string_new()
var year = js_year_from_time(input)
let month = (i32)js_month_from_time(input) + 1 # Adjust for zero-based month
let day = js_date_from_time(input)
let normal_year_days = html_convert_number_to_week_string_normal_year_days
let leap_year_days = html_convert_number_to_week_string_leap_year_days
let ordinal_day = (leap_year_days[(usize)(month - 1)] if ak.is_leap_year(year) else normal_year_days[(usize)(month - 1)]) + (i32)day
var week = (u32)((10 + ordinal_day - 1) // 7) # -1 because the day of the week is always monday
if week > ak.iso8061_weeks_in_year(year):
year += 1
week = 1
return html_utf16_formatted("{:04d}-W{:02d}", [ak.arg_i64(year), ak.arg_u64(week)])
## Ported from HTMLInputElement.cpp:2598 Web::HTML::convert_number_to_date_string.
# https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):concept-input-value-number-string
func html_convert_number_to_date_string(input: f64) -> ak.Utf16String:
# The algorithm to convert a number to a string, given a number input, is as follows: Return a valid
# date string that represents the date that, in UTC, is current input milliseconds after midnight UTC
# on the morning of 1970-01-01 (the time represented by the value "1970-01-01T00:00:00.0Z").
let date = ak.unix_date_time_from_milliseconds_since_epoch((i64)input)
return ak.unix_date_time_to_utf16_string(&date, ak.sv("%Y-%m-%d"), ak.UnixDateTimeLocalTime.no)
## Ported from HTMLInputElement.cpp:2608 Web::HTML::convert_number_to_time_string.
# https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-value-number-string
func html_convert_number_to_time_string(input: f64) -> ak.Utf16String:
# The algorithm to convert a number to a string, given a number input, is as follows: Return a valid time
# string that represents the time that is input milliseconds after midnight on a day with no time changes.
# NB: We don't use JS::*_from_time helpers here because they lose sub-day precision when |input| is much larger
# than 2^53.
var time_of_day = math.mod(input, js_ms_per_day)
if time_of_day < 0.0:
time_of_day += js_ms_per_day
let hours = (u32)(time_of_day / js_ms_per_hour)
let minutes = (u32)(math.mod(time_of_day, js_ms_per_hour) / js_ms_per_minute)
let seconds = (u32)(math.mod(time_of_day, js_ms_per_minute) / js_ms_per_second)
let milliseconds = (u32)math.mod(time_of_day, js_ms_per_second)
if milliseconds > 0:
return html_utf16_formatted("{:02d}:{:02d}:{:02d}.{:03d}", [ak.arg_u64(hours), ak.arg_u64(minutes), ak.arg_u64(seconds), ak.arg_u64(milliseconds)])
if seconds > 0:
return html_utf16_formatted("{:02d}:{:02d}:{:02d}", [ak.arg_u64(hours), ak.arg_u64(minutes), ak.arg_u64(seconds)])
return html_utf16_formatted("{:02d}:{:02d}", [ak.arg_u64(hours), ak.arg_u64(minutes)])
## Ported from HTMLInputElement.cpp:2632 Web::HTML::convert_number_to_local_date_and_time_string.
# https://html.spec.whatwg.org/multipage/input.html#local-date-and-time-state-(type=datetime-local):concept-input-value-number-string
func html_convert_number_to_local_date_and_time_string(input: f64) -> ak.Utf16String:
# The algorithm to convert a number to a string, given a number input, is as follows: Return a valid
# normalized local date and time string that represents the date and time that is input milliseconds
# after midnight on the morning of 1970-01-01 (the time represented by the value "1970-01-01T00:00:00.0").
# AD-HOC: Values outside the valid ECMSScript time value range cannot produce valid date strings.
if not math.is_finite(input) or math.abs(input) > js_max_time_value:
return ak.utf16_string_new()
let year = js_year_from_time(input)
let month = (i32)js_month_from_time(input) + 1 # Adjust for zero-based month
let day = js_date_from_time(input)
let hour = js_hour_from_time(input)
let minutes = js_min_from_time(input)
let seconds = js_sec_from_time(input)
let milliseconds = js_ms_from_time(input)
if milliseconds > 0:
let formatted = html_utf16_formatted("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}.{:03d}", [ak.arg_i64(year), ak.arg_i64(month), ak.arg_u64(day), ak.arg_u64(hour), ak.arg_u64(minutes), ak.arg_u64(seconds), ak.arg_u64(milliseconds)])
return ak.utf16_string_trim(&formatted, ak.utf16_view_from_ascii("0".bytes), ak.TrimMode.right)
if seconds > 0:
return html_utf16_formatted("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}", [ak.arg_i64(year), ak.arg_i64(month), ak.arg_u64(day), ak.arg_u64(hour), ak.arg_u64(minutes), ak.arg_u64(seconds)])
return html_utf16_formatted("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}", [ak.arg_i64(year), ak.arg_i64(month), ak.arg_u64(day), ak.arg_u64(hour), ak.arg_u64(minutes)])
## Ported from HTMLInputElement.cpp:2658 Web::HTML::HTMLInputElement::convert_number_to_string.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-value-string-number
func html_input_element_convert_number_to_string(this: HtmlInputElement*, input: f64) -> ak.Utf16String:
# https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number):concept-input-value-number-string
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.number:
return html_utf16_formatted("{}", [ak.arg_f64(input)])
# https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-value-number-string
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.range:
return html_utf16_formatted("{}", [ak.arg_f64(input)])
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.month:
return html_convert_number_to_month_string(input)
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.week:
return html_convert_number_to_week_string(input)
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.date:
return html_convert_number_to_date_string(input)
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.time:
return html_convert_number_to_time_string(input)
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.local_date_and_time:
return html_convert_number_to_local_date_and_time_string(input)
ak.dbgln(f"HTMLInputElement::convert_number_to_string() not implemented for input type {ak.string_view_text(html_input_element_type(this))}")
return ak.utf16_string_new()
# mark: Converting strings and Date objects =====================================================
## Ported from HTMLInputElement.cpp:2688 Web::HTML::HTMLInputElement::convert_string_to_date.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-value-string-date
func html_input_element_convert_string_to_date_string_view(this: HtmlInputElement*, input: ak.StringView) -> JsDate*?!:
let realm = html_input_element_realm(this)
# https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):concept-input-value-string-date
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.date:
# If parsing a date from input results in an error, then return an error;
let maybe_date = html_parse_a_date_string(input)
if maybe_date == none:
try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("Can't parse date string")))
let date = maybe_date else trap("unreachable")
# otherwise, return a new Date object representing midnight UTC on the morning of the parsed date.
return js_date_create(realm, js_make_date(js_make_day(f64(date.year), f64(date.month - 1), f64(date.day)), 0.0))
# https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):concept-input-value-string-date
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.month:
# If parsing a month from input results in an error, then return an error;
let maybe_year_and_month = html_parse_a_month_string(input)
if maybe_year_and_month == none:
try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("Can't parse month string")))
let year_and_month = maybe_year_and_month else trap("unreachable")
# otherwise, return a new Date object representing midnight UTC on the morning of the first day of the parsed month.
return js_date_create(realm, js_make_date(js_make_day(f64(year_and_month.year), f64(year_and_month.month - 1), 1.0), 0.0))
# https://html.spec.whatwg.org/multipage/input.html#week-state-(type=week):concept-input-value-string-date
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.week:
# If parsing a week from input results in an error, then return an error;
let maybe_week = html_parse_a_week_string(input)
if maybe_week == none:
try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("Can't parse week string")))
let week = maybe_week else trap("unreachable")
# otherwise, return a new Date object representing midnight UTC on the morning of the Monday of the parsed week.
let datetime = ak.unix_date_time_from_iso8601_week(week.week_year, week.week)
return js_date_create(realm, f64(ak.unix_date_time_milliseconds_since_epoch(&datetime)))
# https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-value-string-date
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.time:
# If parsing a time from input results in an error, then return an error;
# otherwise, return a new Date object representing the parsed time in UTC on 1970-01-01.
return try html_parse_time_string(realm, input)
ak.dbgln(f"HTMLInputElement::convert_string_to_date() not implemented for input type {ak.string_view_text(html_input_element_type(this))}")
return none
## Ported from HTMLInputElement.cpp:2743 Web::HTML::HTMLInputElement::convert_string_to_date.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-value-string-date
func html_input_element_convert_string_to_date_utf16_string(this: HtmlInputElement*, input: ak.Utf16String) -> JsDate*?!:
# FIXME: Implement a UTF-16 GenericLexer.
if not ak.utf16_string_base_has_ascii_storage(&input.utf16_string_base):
return none
return try html_input_element_convert_string_to_date_string_view(this, ak.utf16_string_base_ascii_view(&input.utf16_string_base))
## Ported from HTMLInputElement.cpp:2752 Web::HTML::HTMLInputElement::convert_date_to_string.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-value-date-string
func html_input_element_convert_date_to_string(this: HtmlInputElement*, input: JsDate*) -> ak.Utf16String:
# https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):concept-input-value-date-string
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.date:
# Return a valid date string that represents the date current at the time represented by input in the UTC time zone.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
return html_convert_number_to_date_string(js_date_date_value(input))
# https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):concept-input-value-date-string
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.month:
# Return a valid month string that represents the month current at the time represented by input in the UTC time zone.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-month-string
return html_convert_date_to_month_string(js_date_date_value(input))
# https://html.spec.whatwg.org/multipage/input.html#week-state-(type=week):concept-input-value-date-string
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.week:
# Return a valid week string that represents the week current at the time represented by input in the UTC time zone.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-week-string
return html_convert_number_to_week_string(js_date_date_value(input))
# https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-value-string-date
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.time:
# Return a valid time string that represents the UTC time component that is represented by input.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-time-string
return html_convert_number_to_time_string(js_date_date_value(input))
ak.dbgln(f"HTMLInputElement::convert_date_to_string() not implemented for input type {ak.string_view_text(html_input_element_type(this))}")
return ak.utf16_string_new()
# mark: Minimum, maximum and steps ==============================================================
## Ported from HTMLInputElement.cpp:2787 Web::HTML::HTMLInputElement::min.
# https://html.spec.whatwg.org/multipage/input.html#attr-input-min
func html_input_element_min(this: HtmlInputElement*) -> f64?:
# If the element has a min attribute, and the result of applying the algorithm to convert a string to a number to
# the value of the min attribute is a number, then that number is the element's minimum; otherwise, if the type
# attribute's current state defines a default minimum, then that is the minimum; otherwise, the element has no minimum.
if let min_string = html_input_element_get_attribute(this, html_attribute_names_min):
if let min = html_input_element_convert_string_to_number_string_view(this, ak.string_bytes_as_string_view(&min_string)):
return min
# https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-min-default
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.range:
return 0.0
return none
## Ported from HTMLInputElement.cpp:2805 Web::HTML::HTMLInputElement::max.
# https://html.spec.whatwg.org/multipage/input.html#attr-input-max
func html_input_element_max(this: HtmlInputElement*) -> f64?:
# If the element has a max attribute, and the result of applying the algorithm to convert a string to a number to the
# value of the max attribute is a number, then that number is the element's maximum; otherwise, if the type attribute's
# current state defines a default maximum, then that is the maximum; otherwise, the element has no maximum.
if let max_string = html_input_element_get_attribute(this, html_attribute_names_max):
if let max = html_input_element_convert_string_to_number_string_view(this, ak.string_bytes_as_string_view(&max_string)):
return max
# https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-max-default
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.range:
return 100.0
return none
## Ported from HTMLInputElement.cpp:2823 Web::HTML::HTMLInputElement::default_step.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-step-default
func html_input_element_default_step(this: HtmlInputElement*) -> f64:
# https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number):concept-input-step-default
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.number:
return 1.0
# https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-step-default
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.range:
return 1.0
# https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-step-default
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.time:
return 60.0
# https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):concept-input-step-default
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.date:
return 1.0
# https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):concept-input-step-default
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.month:
return 1.0
# https://html.spec.whatwg.org/multipage/input.html#week-state-(type=week):concept-input-step-default
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.week:
return 1.0
# https://html.spec.whatwg.org/multipage/input.html#local-date-and-time-state-(type=datetime-local):concept-input-step-default
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.local_date_and_time:
return 60.0
trap("VERIFY_NOT_REACHED")
## Ported from HTMLInputElement.cpp:2857 Web::HTML::HTMLInputElement::step_scale_factor.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-step-scale
func html_input_element_step_scale_factor(this: HtmlInputElement*) -> f64:
# https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number):concept-input-step-scale
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.number:
return 1.0
# https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-step-scale
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.range:
return 1.0
# https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-step-scale
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.time:
return 1000.0
# https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):concept-input-step-scale
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.date:
return 86400000.0
# https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):concept-input-step-scale
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.month:
return 1.0
# https://html.spec.whatwg.org/multipage/input.html#week-state-(type=week):concept-input-step-scale
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.week:
return 604800000.0
# https://html.spec.whatwg.org/multipage/input.html#local-date-and-time-state-(type=datetime-local):concept-input-step-scale
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.local_date_and_time:
return 1000.0
trap("VERIFY_NOT_REACHED")
## Ported from HTMLInputElement.cpp:2891 Web::HTML::HTMLInputElement::allowed_value_step.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-step
func html_input_element_allowed_value_step(this: HtmlInputElement*) -> f64?:
# 1. If the attribute does not apply, then there is no allowed value step.
if not html_input_element_step_applies(this):
return none
# 2. Otherwise, if the attribute is absent, then the allowed value step is the default step multiplied by the step scale factor.
let step_string = html_input_element_get_attribute(this, html_attribute_names_step) else return html_input_element_default_step(this) * html_input_element_step_scale_factor(this)
# 3. Otherwise, if the attribute's value is an ASCII case-insensitive match for the string "any", then there is no allowed value step.
if ak.string_equals_ignoring_ascii_case_string_view(&step_string, ak.sv("any")):
return none
# 4. Otherwise, if the rules for parsing floating-point number values, when they are applied to the attribute's value, return an error,
# zero, or a number less than zero, then the allowed value step is the default step multiplied by the step scale factor.
let maybe_step = html_parse_floating_point_number_string_view(ak.string_bytes_as_string_view(&step_string))
if maybe_step == none or (maybe_step else 0.0) == 0.0 or (maybe_step else 0.0) < 0.0:
return html_input_element_default_step(this) * html_input_element_step_scale_factor(this)
# 5. Otherwise, the allowed value step is the number returned by the rules for parsing floating-point number values when they are applied
# to the attribute's value, multiplied by the step scale factor.
return (maybe_step else trap("unreachable")) * html_input_element_step_scale_factor(this)
## Ported from HTMLInputElement.cpp:2919 Web::HTML::HTMLInputElement::step_base.
# https://html.spec.whatwg.org/multipage/input.html#concept-input-min-zero
func html_input_element_step_base(this: HtmlInputElement*) -> f64:
# 1. If the element has a min content attribute, and the result of applying the algorithm to convert a string to a number to the value of
# the min content attribute is not an error, then return that result.
if let min = html_input_element_min(this):
return min
# 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 let value = html_input_element_get_attribute(this, html_attribute_names_value):
if let value_as_number = html_input_element_convert_string_to_number_string_view(this, ak.string_bytes_as_string_view(&value)):
return value_as_number
# 3. If a default step base is defined for this element given its type attribute's state, then return it.
if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.week:
# The default step base is −259,200,000 (the start of week 1970-W01).
return -259200000.0
# 4. Return zero.
return 0.0
# mark: valueAsDate and valueAsNumber ===========================================================
## Ported from HTMLInputElement.cpp:2944 Web::HTML::HTMLInputElement::value_as_date.
# https://html.spec.whatwg.org/multipage/input.html#dom-input-valueasdate
pub func html_input_element_value_as_date(this: HtmlInputElement*) -> JsObject*?:
# On getting, if the valueAsDate attribute does not apply, as defined for the input element's type attribute's current state, then return null.
if not html_input_element_value_as_date_applies(this):
return none
# Otherwise, run the algorithm to convert a string to a Date object defined for that state to the element's value;
# if the algorithm returned a Date object, then return it, otherwise, return null.
let maybe_date = html_input_element_convert_string_to_date_utf16_string(this, html_input_element_value(this)) catch:
_ = idl_take_pending_exception()
return none
return (JsObject*?)maybe_date
## Ported from HTMLInputElement.cpp:2959 Web::HTML::HTMLInputElement::set_value_as_date.
# https://html.spec.whatwg.org/multipage/input.html#dom-input-valueasdate
pub func html_input_element_set_value_as_date(this: HtmlInputElement*, value: JsObject*?) -> !:
# On setting, if the valueAsDate attribute does not apply, as defined for the input element's type attribute's current state, then throw an "InvalidStateError" DOMException;
if not html_input_element_value_as_date_applies(this):
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(html_input_element_realm(this), ak.utf16_string_from_str("valueAsDate: Invalid input type used"))))
# otherwise, if the new value is not null and not a Date object throw a TypeError exception;
if value != none and not is_js_date(value else trap("unreachable")):
try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("valueAsDate: input is not a Date")))
# otherwise if the new value is null or a Date object representing the NaN time value, then set the value of the element to the empty string;
if value == none:
try html_input_element_set_value(this, ak.utf16_string_new())
return
let date = (JsDate*)(value else trap("unreachable"))
if not math.is_finite(js_date_date_value(date)):
try html_input_element_set_value(this, ak.utf16_string_new())
return
# otherwise, run the algorithm to convert a Date object to a string, as defined for that state, on the new value, and set the value of the element to the resulting string.
try html_input_element_set_value(this, html_input_element_convert_date_to_string(this, date))
## Ported from HTMLInputElement.cpp:2986 Web::HTML::HTMLInputElement::value_as_number.
# https://html.spec.whatwg.org/multipage/input.html#dom-input-valueasnumber
pub func html_input_element_value_as_number(this: HtmlInputElement*) -> f64:
# 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 not html_input_element_value_as_number_applies(this):
return math.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 html_input_element_convert_string_to_number_utf16_string(this, html_input_element_value(this)) else math.nan
## Ported from HTMLInputElement.cpp:2998 Web::HTML::HTMLInputElement::set_value_as_number.
# https://html.spec.whatwg.org/multipage/input.html#dom-input-valueasnumber
pub func html_input_element_set_value_as_number(this: HtmlInputElement*, value: f64) -> !:
# On setting, if the new value is infinite, then throw a TypeError exception.
if not math.is_finite(value):
try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("valueAsNumber: Value is infinite")))
# 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 not html_input_element_value_as_number_applies(this):
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(html_input_element_realm(this), ak.utf16_string_from_str("valueAsNumber: Invalid input type used"))))
# Otherwise, if the new value is a Not-a-Number (NaN) value, then set the value of the element to the empty string.
# (Donor quirk kept: `value == NAN` is never true; a NaN is not finite and threw above.)
if value == math.nan:
try html_input_element_set_value(this, ak.utf16_string_new())
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.
try html_input_element_set_value(this, html_input_element_convert_number_to_string(this, value))
# mark: stepUp() and stepDown() =================================================================
## Ported from HTMLInputElement.cpp:3020 Web::HTML::HTMLInputElement::step_up.
# https://html.spec.whatwg.org/multipage/input.html#dom-input-stepup
pub func html_input_element_step_up(this: HtmlInputElement*, n: i32 = 1) -> !:
try html_input_element_step_up_or_down(this, false, n)
## Ported from HTMLInputElement.cpp:3026 Web::HTML::HTMLInputElement::step_down.
# https://html.spec.whatwg.org/multipage/input.html#dom-input-stepdown
pub func html_input_element_step_down(this: HtmlInputElement*, n: i32 = 1) -> !:
try html_input_element_step_up_or_down(this, true, n)
## Ported from HTMLInputElement.cpp:3032 Web::HTML::HTMLInputElement::step_up_or_down.
# https://html.spec.whatwg.org/multipage/input.html#dom-input-stepup
func html_input_element_step_up_or_down(this: HtmlInputElement*, is_down: bool, n: i32) -> !:
# 1. If the stepDown() and stepUp() methods do not apply, as defined for the input element's type attribute's current state, then throw an "InvalidStateError" DOMException.
if not html_input_element_step_up_or_down_applies(this):
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(html_input_element_realm(this), html_utf16_formatted("{}: Invalid input type used", [ak.arg_str("stepDown()" if is_down else "stepUp()")]))))
# 2. If the element has no allowed value step, then throw an "InvalidStateError" DOMException.
let maybe_allowed_value_step = html_input_element_allowed_value_step(this)
if maybe_allowed_value_step == none:
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(html_input_element_realm(this), ak.utf16_string_from_str("element has no allowed value step"))))
let allowed_value_step = maybe_allowed_value_step else trap("unreachable")
# 3. If the element has a minimum and a maximum and the minimum is greater than the maximum, then return.
let maybe_minimum = html_input_element_min(this)
let maybe_maximum = html_input_element_max(this)
if maybe_minimum != none and maybe_maximum != none and (maybe_minimum else 0.0) > (maybe_maximum else 0.0):
return
# 4. If the element has a minimum and a maximum and there is no value greater than or equal to the element's minimum and less than
# or equal to the element's maximum that, when subtracted from the step base, is an integral multiple of the allowed value step, then return.
if maybe_minimum != none and maybe_maximum != none and math.ceil(((maybe_minimum else 0.0) - html_input_element_step_base(this)) / allowed_value_step) > math.floor(((maybe_maximum else 0.0) - html_input_element_step_base(this)) / allowed_value_step):
return
# 5. If applying the algorithm to convert a string to a number to the string given by the element's value does not result in an error,
# then let value be the result of that algorithm. Otherwise, let value be zero.
var value = html_input_element_convert_string_to_number_utf16_string(this, html_input_element_value(this)) else 0.0
# 6. Let valueBeforeStepping be value.
let value_before_stepping = value
# 7. If value subtracted from the step base is not an integral multiple of the allowed value step, then set value to the nearest value that,
# when subtracted from the step base, is an integral multiple of the allowed value step, and that is less than value if the method invoked was the stepDown() method, and more than value otherwise.
if not is_integral_multiple(html_input_element_step_base(this) - value, allowed_value_step):
if is_down:
value = html_input_element_step_base(this) + math.floor((value - html_input_element_step_base(this)) / allowed_value_step) * allowed_value_step
else:
value = html_input_element_step_base(this) + math.ceil((value - html_input_element_step_base(this)) / allowed_value_step) * allowed_value_step
# Otherwise (value subtracted from the step base is an integral multiple of the allowed value step):
else:
# 1. Let n be the argument.
# 2. Let delta be the allowed value step multiplied by n.
var delta = allowed_value_step * f64(n)
# 3. If the method invoked was the stepDown() method, negate delta.
if is_down:
delta = -delta
# 4. Let value be the result of adding delta to value.
value += delta
# 8. If the element has a minimum, and value is less than that minimum, then set value to the smallest value that,
# when subtracted from the step base, is an integral multiple of the allowed value step, and that is more than
# or equal to that minimum.
if let minimum = maybe_minimum:
if value < minimum:
value = html_input_element_step_base(this) + math.ceil((minimum - html_input_element_step_base(this)) / allowed_value_step) * allowed_value_step
# 9. If the element has a maximum, and value is greater than that maximum, then set value to the largest value that,
# when subtracted from the step base, is an integral multiple of the allowed value step, and that is less than
# or equal to that maximum.
if let maximum = maybe_maximum:
if value > maximum:
value = html_input_element_step_base(this) + math.floor((maximum - html_input_element_step_base(this)) / allowed_value_step) * allowed_value_step
# 10. If either the method invoked was the stepDown() method and value is greater than valueBeforeStepping,
# or the method invoked was the stepUp() method and value is less than valueBeforeStepping, then return.
if is_down:
if value > value_before_stepping:
return
else:
if value < value_before_stepping:
return
# 11. Let value as string be the result of running the algorithm to convert a number to a string,
# as defined for the input element's type attribute's current state, on value.
let value_as_string = html_input_element_convert_number_to_string(this, value)
# 12. Set the value of the element to value as string.
try html_input_element_set_value(this, value_as_string)