Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / dates.lucb

#==============================================================================================
#
#   dates - HTML::Dates: the date and time microsyntaxes
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/Dates.cpp (lines 1-542): the week number of a year's last day,
#       the valid week / month / date / local date and time / time string checks, the
#       normalized local date and time string, and the parsers for month, week, date, time and
#       local date and time strings (with their month, date and time components). The
#       components are C++ `static` functions and are private here.
#
#==============================================================================================

# mark: Valid strings ===========================================================================

## Ported from Dates.cpp:18 Web::HTML::week_number_of_the_last_day.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#week-number-of-the-last-day
pub func html_week_number_of_the_last_day(year: u64) -> u32:
    # https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#weeks
    # NOTE: A year is considered to have 53 weeks if either of the following conditions are satisfied:
    # - January 1 of that year is a Thursday.
    # - January 1 of that year is a Wednesday and the year is divisible by 400, or divisible by 4, but not 100.

    # Note: Gauss's algorithm for determining the day of the week with D = 1, and M = 0
    # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Gauss's_algorithm
    let previous_year = year -% 1
    let day_of_week = (u8)((1 + 5 * (previous_year % 4) + 4 * (previous_year % 100) + 6 * (previous_year % 400)) % 7)

    if day_of_week == 4 or (day_of_week == 3 and (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0))):
        return 53

    return 52

## The ASCII digit at `index` of `view`, as AK's parse_ascii_digit reads a code unit.
func html_dates_digit_at(view: ak.Utf16View, index: usize) -> u32:
    return ak.parse_ascii_digit(ak.utf16_view_code_unit_at(&view, index))

## `is_ascii_digit(view.code_unit_at(index))`.
func html_dates_is_digit_at(view: ak.Utf16View, index: usize) -> bool:
    return ak.is_ascii_digit(ak.utf16_view_code_unit_at(&view, index))

## Ported from Dates.cpp:36 Web::HTML::is_valid_week_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-week-string
pub func html_is_valid_week_string(value: ak.Utf16View) -> bool:
    # A string is a valid week string representing a week-year year and week week if it consists of the following components in the given order:

    # 1. Four or more ASCII digits, representing year, where year > 0
    # 2. A U+002D HYPHEN-MINUS character (-)
    # 3. A U+0057 LATIN CAPITAL LETTER W character (W)
    # 4. Two ASCII digits, representing the week week, in the range 1 ≤ week ≤ maxweek, where maxweek is the week number
    #    of the last day of week-year year
    var parts = ak.utf16_view_split_view_u16(&value, (u16)'-', ak.SplitBehavior.keep_empty)
    if ak.vector_size(&parts) != 2:
        return false

    let part_0 = *ak.vector_at(&parts, 0)
    let part_1 = *ak.vector_at(&parts, 1)
    if ak.utf16_view_length_in_code_units(&part_0) < 4:
        return false
    if ak.utf16_view_length_in_code_units(&part_1) != 3:
        return false

    let year = ak.utf16_view_to_number_u64(&part_0) else return false
    if year == 0:
        return false

    if not ak.utf16_view_starts_with_u16(&part_1, (u16)'W'):
        return false
    if not html_dates_is_digit_at(part_1, 1):
        return false
    if not html_dates_is_digit_at(part_1, 2):
        return false

    let week = (html_dates_digit_at(part_1, 1) * 10) + html_dates_digit_at(part_1, 2)
    return week >= 1 and week <= html_week_number_of_the_last_day(year)

## Ported from Dates.cpp:70 Web::HTML::is_valid_month_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-month-string
pub func html_is_valid_month_string(value: ak.Utf16View) -> bool:
    # A string is a valid month string representing a year year and month month if it consists of the following components in the given order:

    # 1. Four or more ASCII digits, representing year, where year > 0
    # 2. A U+002D HYPHEN-MINUS character (-)
    # 3. Two ASCII digits, representing the month month, in the range 1 ≤ month ≤ 12

    var parts = ak.utf16_view_split_view_u16(&value, (u16)'-', ak.SplitBehavior.keep_empty)
    if ak.vector_size(&parts) != 2:
        return false

    let part_0 = *ak.vector_at(&parts, 0)
    let part_1 = *ak.vector_at(&parts, 1)
    if ak.utf16_view_length_in_code_units(&part_0) < 4:
        return false
    if ak.utf16_view_length_in_code_units(&part_1) != 2:
        return false

    let year = ak.utf16_view_to_number_u64(&part_0) else return false
    if year == 0:
        return false

    if not html_dates_is_digit_at(part_1, 0):
        return false
    if not html_dates_is_digit_at(part_1, 1):
        return false

    let month = (html_dates_digit_at(part_1, 0) * 10) + html_dates_digit_at(part_1, 1)
    return month >= 1 and month <= 12

## Ported from Dates.cpp:101 Web::HTML::is_valid_date_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
pub func html_is_valid_date_string(value: ak.Utf16View) -> bool:
    # A string is a valid date string representing a year year, month month, and day day if it consists of the following components in the given order:

    # 1. A valid month string, representing year and month
    # 2. A U+002D HYPHEN-MINUS character (-)
    # 3. Two ASCII digits, representing day, in the range 1 ≤ day ≤ maxday where maxday is the number of days in the
    #    month month and year year
    var parts = ak.utf16_view_split_view_u16(&value, (u16)'-', ak.SplitBehavior.keep_empty)
    if ak.vector_size(&parts) != 3:
        return false

    let part_0 = *ak.vector_at(&parts, 0)
    let part_1 = *ak.vector_at(&parts, 1)
    let part_2 = *ak.vector_at(&parts, 2)
    let month_string = ak.utf16_view_substring_view_usize(&value, 0, ak.utf16_view_length_in_code_units(&part_0) + 1 + ak.utf16_view_length_in_code_units(&part_1))
    if not html_is_valid_month_string(month_string):
        return false

    if ak.utf16_view_length_in_code_units(&part_2) != 2:
        return false

    # C++ accumulates in an i64 (`for (auto d : parts[0])` over the code points).
    var year: i64 = 0
    var it = ak.utf16_view_begin(&part_0)
    while not ak.utf16_code_point_iterator_done(&it):
        year = year *% 10
        year = year +% (i64)ak.parse_ascii_digit(ak.utf16_code_point_iterator_deref(&it))
        _ = ak.utf16_code_point_iterator_inc(&it)

    let month = (html_dates_digit_at(part_1, 0) * 10) + html_dates_digit_at(part_1, 1)
    let day = (i64)((html_dates_digit_at(part_2, 0) * 10) + html_dates_digit_at(part_2, 1))

    # AK::days_in_month takes an int year.
    return day >= 1 and day <= (i64)ak.days_in_month((i32)year, month)

## Ported from Dates.cpp:133 Web::HTML::is_valid_local_date_and_time_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-local-date-and-time-string
pub func html_is_valid_local_date_and_time_string(value: ak.Utf16View) -> bool:
    var parts_split_by_t = ak.utf16_view_split_view_u16(&value, (u16)'T', ak.SplitBehavior.keep_empty)
    if ak.vector_size(&parts_split_by_t) == 2:
        return html_is_valid_date_string(*ak.vector_at(&parts_split_by_t, 0)) and html_is_valid_time_string(*ak.vector_at(&parts_split_by_t, 1))

    var parts_split_by_space = ak.utf16_view_split_view_u16(&value, (u16)' ', ak.SplitBehavior.keep_empty)
    if ak.vector_size(&parts_split_by_space) == 2:
        return html_is_valid_date_string(*ak.vector_at(&parts_split_by_space, 0)) and html_is_valid_time_string(*ak.vector_at(&parts_split_by_space, 1))

    return false

## `Utf16String::formatted("{}T{}", date, time)`.
func html_dates_join_with_t(date: ak.Utf16View, time: ak.Utf16View) -> ak.Utf16String:
    var builder = ak.string_builder_new_mode(ak.StringBuilderMode.utf16)
    ak.string_builder_append_utf16_view(&builder, date)
    ak.string_builder_append_code_unit(&builder, (u16)'T')
    ak.string_builder_append_utf16_view(&builder, time)
    return ak.string_builder_to_utf16_string(&builder)

## Ported from Dates.cpp:147 Web::HTML::normalize_local_date_and_time_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-normalised-local-date-and-time-string
pub func html_normalize_local_date_and_time_string(value: ak.Utf16String) -> ak.Utf16String:
    # A string is a valid normalized local date and time string representing a date and time if it consists of the following components in the given order:

    # 1. A valid date string representing the date
    # 2. A U+0054 LATIN CAPITAL LETTER T character (T)
    # 3. A valid time string representing the time, expressed as the shortest possible string for the given time (e.g. omitting the seconds component entirely if the given time is zero seconds past the minute)

    var value_with_normalized_t = value
    let spaces = ak.utf16_string_base_count(&value.utf16_string_base, ak.utf16_sv(" "))
    if spaces > 0:
        assert(spaces == 1)
        value_with_normalized_t = ak.utf16_string_replace_utf16_view(&value, ak.utf16_sv(" "), ak.utf16_sv("T"), ak.ReplaceMode.first_only)

    var parts = ak.utf16_string_base_split_view_u16(&value_with_normalized_t.utf16_string_base, (u16)'T', ak.SplitBehavior.keep_empty)
    assert(ak.vector_size(&parts) == 2)
    let part_0 = *ak.vector_at(&parts, 0)
    let part_1 = *ak.vector_at(&parts, 1)

    var normalized_length = ak.utf16_view_length_in_code_points(&part_1)
    while normalized_length > 9:
        if ak.utf16_view_code_point_at(&part_1, normalized_length - 1) != (u32)'0':
            return html_dates_join_with_t(part_0, ak.utf16_view_unicode_substring_view_usize(&part_1, 0, normalized_length))
        normalized_length -= 1

    if normalized_length > 5:
        let time_without_milliseconds = ak.utf16_view_unicode_substring_view_usize(&part_1, 0, 8)
        let length: usize = 5 if ak.utf16_view_ends_with_utf16_view(&time_without_milliseconds, ak.utf16_sv(":00")) else 8
        return html_dates_join_with_t(part_0, ak.utf16_view_unicode_substring_view_usize(&part_1, 0, length))

    return value_with_normalized_t

## Ported from Dates.cpp:181 Web::HTML::is_valid_time_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-time-string
pub func html_is_valid_time_string(value: ak.Utf16View) -> bool:
    # A string is a valid time string representing an hour hour, a minute minute, and a second second if it consists of the following components in the given order:

    # 1. Two ASCII digits, representing hour, in the range 0 ≤ hour ≤ 23
    # 2. A U+003A COLON character (:)
    # 3. Two ASCII digits, representing minute, in the range 0 ≤ minute ≤ 59
    # 4. If second is nonzero, or optionally if second is zero:
    #     1. A U+003A COLON character (:)
    #     2. Two ASCII digits, representing the integer part of second, in the range 0 ≤ s ≤ 59
    #     3. If second is not an integer, or optionally if second is an integer:
    #         1. A U+002E FULL STOP character (.)
    #         2. One, two, or three ASCII digits, representing the fractional part of second
    var parts = ak.utf16_view_split_view_u16(&value, (u16)':', ak.SplitBehavior.keep_empty)
    if ak.vector_size(&parts) != 2 and ak.vector_size(&parts) != 3:
        return false

    let part_0 = *ak.vector_at(&parts, 0)
    let part_1 = *ak.vector_at(&parts, 1)
    if ak.utf16_view_length_in_code_units(&part_0) != 2:
        return false
    if ak.utf16_view_length_in_code_units(&part_1) != 2:
        return false

    if not html_dates_is_digit_at(part_0, 0) or not html_dates_is_digit_at(part_0, 1):
        return false

    let hour = (html_dates_digit_at(part_0, 0) * 10) + html_dates_digit_at(part_0, 1)
    if hour > 23:
        return false

    if not html_dates_is_digit_at(part_1, 0) or not html_dates_is_digit_at(part_1, 1):
        return false

    let minute = (html_dates_digit_at(part_1, 0) * 10) + html_dates_digit_at(part_1, 1)
    if minute > 59:
        return false

    if ak.vector_size(&parts) == 3:
        let part_2 = *ak.vector_at(&parts, 2)
        if ak.utf16_view_length_in_code_units(&part_2) < 2:
            return false

        if not html_dates_is_digit_at(part_2, 0) or not html_dates_is_digit_at(part_2, 1):
            return false

        let second = (html_dates_digit_at(part_2, 0) * 10) + html_dates_digit_at(part_2, 1)
        if second > 59:
            return false

        if ak.utf16_view_length_in_code_units(&part_2) > 2:
            var fractional = ak.utf16_view_split_view_u16(&part_2, (u16)'.', ak.SplitBehavior.keep_empty)
            if ak.vector_size(&fractional) != 2:
                return false

            let fraction = *ak.vector_at(&fractional, 1)
            if ak.utf16_view_length_in_code_units(&fraction) < 1 or ak.utf16_view_length_in_code_units(&fraction) > 3:
                return false

            var it = ak.utf16_view_begin(&fraction)
            while not ak.utf16_code_point_iterator_done(&it):
                if not ak.is_ascii_digit(ak.utf16_code_point_iterator_deref(&it)):
                    return false
                _ = ak.utf16_code_point_iterator_inc(&it)

    return true

# mark: Parsing =================================================================================

## `[](auto ch) { return is_ascii_digit(ch) || ch == '.'; }` of parse_a_time_component.
struct HtmlParseATimeComponentIsDigitOrFullStop: ak.CharPredicate:
    var unused: u8

    func matches(ch: u8) -> bool:
        return ak.is_ascii_digit(ch) or ch == '.'

## Ported from Dates.cpp:246 Web::HTML::parse_a_month_component.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-month-component
func html_parse_a_month_component(input: ak.GenericLexer[u8]*) -> HtmlYearAndMonth?:
    let is_ascii_digit = ak.CharPredicateFunction(function = ak.is_ascii_digit)

    # 1. Collect a sequence of code points that are ASCII digits from input given position. If the collected sequence is
    #    not at least four characters long, then fail. Otherwise, interpret the resulting sequence as a base-ten integer.
    #    Let year be that number.
    let year_string = ak.generic_lexer_consume_while(input, &is_ascii_digit)
    if ak.string_view_length(&year_string) < 4:
        return none
    let year = ak.string_view_to_number_u32(&year_string) else return none

    # 2. If year is not a number greater than zero, then fail.
    if year < 1:
        return none

    # 3. If position is beyond the end of input or if the character at position is not a U+002D HYPHEN-MINUS character, then
    #    fail. Otherwise, move position forwards one character.
    if not ak.generic_lexer_consume_specific_t_char_type(input, '-'):
        return none

    # 4. Collect a sequence of code points that are ASCII digits from input given position. If the collected sequence is not
    #    exactly two characters long, then fail. Otherwise, interpret the resulting sequence as a base-ten integer. Let month
    #    be that number.
    let month_string = ak.generic_lexer_consume_while(input, &is_ascii_digit)
    if ak.string_view_length(&month_string) != 2:
        return none
    let month = ak.string_view_to_number_u32(&month_string) else trap("VERIFY: Optional::value() of two ASCII digits")

    # 5. If month is not a number in the range 1 ≤ month ≤ 12, then fail.
    if month < 1 or month > 12:
        return none

    # 6. Return year and month.
    return HtmlYearAndMonth(year = year, month = month)

## Ported from Dates.cpp:285 Web::HTML::parse_a_month_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-month-string
pub func html_parse_a_month_string(input_view: ak.StringView) -> HtmlYearAndMonth?:
    # 1. Let input be the string being parsed.
    # 2. Let position be a pointer into input, initially pointing at the start of the string.
    var input = ak.generic_lexer_new(input_view)

    # 3. Parse a month component to obtain year and month. If this returns nothing, then fail.
    let year_and_month = html_parse_a_month_component(&input) else return none

    # 4. If position is not beyond the end of input, then fail.
    if not ak.generic_lexer_is_eof(&input):
        return none

    # 5. Return year and month.
    return year_and_month

## Ported from Dates.cpp:304 Web::HTML::number_of_months_since_unix_epoch.
pub func html_number_of_months_since_unix_epoch(year_and_month: HtmlYearAndMonth) -> i32:
    # C++ computes in u32 (wrapping below 1970) and converts the result to i32.
    return (i32)((year_and_month.year -% 1970) *% 12 +% year_and_month.month -% 1)

## Ported from Dates.cpp:310 Web::HTML::parse_a_week_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-week-string
pub func html_parse_a_week_string(input_view: ak.StringView) -> HtmlWeekYearAndWeek?:
    let is_ascii_digit = ak.CharPredicateFunction(function = ak.is_ascii_digit)

    # 1. Let input be the string being parsed.
    # 2. Let position be a pointer into input, initially pointing at the start of the string.
    var input = ak.generic_lexer_new(input_view)

    # 3. Collect a sequence of code points that are ASCII digits from input given position. If the collected sequence is
    #    not at least four characters long, then fail. Otherwise, interpret the resulting sequence as a base-ten integer.
    #    Let year be that number.
    let year_string = ak.generic_lexer_consume_while(&input, &is_ascii_digit)
    if ak.string_view_length(&year_string) < 4:
        return none
    let year = ak.string_view_to_number_u32(&year_string) else return none

    # 4. If year is not a number greater than zero, then fail.
    if year < 1:
        return none

    # 5. If position is beyond the end of input or if the character at position is not a U+002D HYPHEN-MINUS character, then
    #    fail. Otherwise, move position forwards one character.
    if not ak.generic_lexer_consume_specific_t_char_type(&input, '-'):
        return none

    # 6. If position is beyond the end of input or if the character at position is not a U+0057 LATIN CAPITAL LETTER W character
    #    (W), then fail. Otherwise, move position forwards one character.
    if not ak.generic_lexer_consume_specific_t_char_type(&input, 'W'):
        return none

    # 7. Collect a sequence of code points that are ASCII digits from input given position. If the collected sequence is not
    #    exactly two characters long, then fail. Otherwise, interpret the resulting sequence as a base-ten integer. Let week
    #    be that number.
    let week_string = ak.generic_lexer_consume_while(&input, &is_ascii_digit)
    if ak.string_view_length(&week_string) != 2:
        return none
    let week = ak.string_view_to_number_u32(&week_string) else trap("VERIFY: Optional::value() of two ASCII digits")

    # 8. Let maxweek be the week number of the last day of year year.
    let maxweek = html_week_number_of_the_last_day(year)

    # 9. If week is not a number in the range 1 ≤ week ≤ maxweek, then fail.
    if week < 1 or week > maxweek:
        return none

    # 10. If position is not beyond the end of input, then fail.
    if not ak.generic_lexer_is_eof(&input):
        return none

    # 11. Return the week-year number year and the week number week.
    return HtmlWeekYearAndWeek(week_year = year, week = week)

## Ported from Dates.cpp:365 Web::HTML::parse_a_date_component.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-date-component
func html_parse_a_date_component(input: ak.GenericLexer[u8]*) -> HtmlYearMonthDay?:
    let is_ascii_digit = ak.CharPredicateFunction(function = ak.is_ascii_digit)

    # 1. Parse a month component to obtain year and month. If this returns nothing, then fail.
    let month_component = html_parse_a_month_component(input) else return none

    # 2. Let maxday be the number of days in month month of year year.
    let maxday = (u32)ak.days_in_month((i32)month_component.year, month_component.month)

    # 3. If position is beyond the end of input or if the character at position is not a U+002D HYPHEN-MINUS character, then fail.
    #    Otherwise, move position forwards one character.
    if not ak.generic_lexer_consume_specific_t_char_type(input, '-'):
        return none

    # 4. Collect a sequence of code points that are ASCII digits from input given position. If the collected sequence is not
    #    exactly two characters long, then fail. Otherwise, interpret the resulting sequence as a base-ten integer. Let day
    #    be that number.
    let day_string = ak.generic_lexer_consume_while(input, &is_ascii_digit)
    if ak.string_view_length(&day_string) != 2:
        return none
    let day = ak.string_view_to_number_u32(&day_string) else trap("VERIFY: Optional::value() of two ASCII digits")

    # 5. If day is not a number in the range 1 ≤ day ≤ maxday, then fail.
    if day < 1 or day > maxday:
        return none

    # 6. Return year, month, and day.
    return HtmlYearMonthDay(year = month_component.year, month = month_component.month, day = day)

## Ported from Dates.cpp:398 Web::HTML::parse_a_date_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-date-string
pub func html_parse_a_date_string(input_view: ak.StringView) -> HtmlYearMonthDay?:
    # 1. Let input be the string being parsed.
    # 2. Let position be a pointer into input, initially pointing at the start of the string.
    var input = ak.generic_lexer_new(input_view)

    # 3. Parse a date component to obtain year, month, and day. If this returns nothing, then fail.
    let year_month_day = html_parse_a_date_component(&input) else return none

    # 4. If position is not beyond the end of input, then fail.
    if not ak.generic_lexer_is_eof(&input):
        return none

    # 5. Let date be the date with year year, month month, and day day.
    # 6. Return date.
    return year_month_day

## Ported from Dates.cpp:419 Web::HTML::parse_a_time_component.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-time-component
func html_parse_a_time_component(input: ak.GenericLexer[u8]*) -> HtmlHourMinuteSecond?:
    let is_ascii_digit = ak.CharPredicateFunction(function = ak.is_ascii_digit)

    # 1. Collect a sequence of code points that are ASCII digits from input given position. If the collected sequence
    #    is not exactly two characters long, then fail.  Otherwise, interpret the resulting sequence as a base-ten
    #    integer. Let hour be that number.
    let hour_string = ak.generic_lexer_consume_while(input, &is_ascii_digit)
    if ak.string_view_length(&hour_string) != 2:
        return none
    let hour = ak.string_view_to_number_i32(&hour_string) else return none

    # 2. If hour is not a number in the range 0 ≤ hour ≤ 23, then fail.
    if hour < 0 or hour > 23:
        return none

    # 3. If position is beyond the end of input or if the character at position is not a U+003A COLON character, then
    #    fail. Otherwise, move position forwards one character.
    if not ak.generic_lexer_consume_specific_t_char_type(input, ':'):
        return none

    # 4. Collect a sequence of code points that are ASCII digits from input given position. If the collected sequence
    #    is not exactly two characters long, then fail. Otherwise, interpret the resulting sequence as a base-ten integer.
    #    Let minute be that number.
    let minute_string = ak.generic_lexer_consume_while(input, &is_ascii_digit)
    if ak.string_view_length(&minute_string) != 2:
        return none
    let minute = ak.string_view_to_number_i32(&minute_string) else return none

    # 5. If minute is not a number in the range 0 ≤ minute ≤ 59, then fail.
    # donor bug: the C++ tests `hour > 59` here (not minute), so minutes 60-99 are accepted.
    if minute < 0 or hour > 59:
        return none

    # 6. Let second be 0.
    var second: f32 = 0.0

    # 7. If position is not beyond the end of input and the character at position is U+003A (:), then:
    if ak.generic_lexer_consume_specific_t_char_type(input, ':'):
        # 1. Advance position to the next character in input.
        # 2. If position is beyond the end of input, or at the last character in input, or if the next two characters in
        #    input starting at position are not both ASCII digits, then fail.
        if ak.generic_lexer_is_eof(input) or ak.generic_lexer_tell_remaining(input) == 1 or (not ak.is_ascii_digit(ak.generic_lexer_peek(input)) and not ak.is_ascii_digit(ak.generic_lexer_peek(input, 1))):
            return none

        # 3. Collect a sequence of code points that are either ASCII digits or U+002E FULL STOP characters from input
        #    given position.
        let is_digit_or_full_stop = HtmlParseATimeComponentIsDigitOrFullStop(unused = 0)
        let second_string = ak.generic_lexer_consume_while(input, &is_digit_or_full_stop)
        # If the collected sequence is three characters long, or if it is longer than three characters long and the third
        # character is not a U+002E FULL STOP character, or if it has more than one U+002E FULL STOP character, then fail.
        if ak.string_view_length(&second_string) == 3:
            return none
        if ak.string_view_length(&second_string) > 3 and ak.string_view_at(&second_string, 2) != '.':
            return none
        var full_stops = ak.string_view_find_all(&second_string, ak.sv("."))
        if ak.vector_size(&full_stops) > 1:
            return none
        # Otherwise, interpret the resulting sequence as a base-ten number (possibly with a fractional part). Set second
        # to that number.
        second = ak.string_view_to_number_f32(&second_string) else return none

        # 4. If second is not a number in the range 0 ≤ second < 60, then fail.
        if second < 0.0 or second >= 60.0:
            return none

    # 8. Return hour, minute, and second.
    return HtmlHourMinuteSecond(hour = hour, minute = minute, second = second)

## Ported from Dates.cpp:495 Web::HTML::parse_time_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-time-string
pub func html_parse_time_string(realm: Realm*, value: ak.StringView) -> JsDate*!:
    # 1. Let input be the string being parsed.
    # 2. Let position be a pointer into input, initially pointing at the start of the string.
    var input = ak.generic_lexer_new(value)

    # 3. Parse a time component to obtain hour, minute, and second. If this returns nothing, then fail.
    let parsed = html_parse_a_time_component(&input)
    if parsed == none:
        try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("Can't parse time string")))
    let hour_minute_second = parsed else trap("unreachable")

    # 4. If position is not beyond the end of input, then fail.
    if not ak.generic_lexer_is_eof(&input):
        try idl_throw_simple(IdlSimpleExceptionType.type_error, .string_view(ak.sv("Can't parse time string")))

    # 5. Let time be the time with hour hour, minute minute, and second second.
    # 6. Return time.
    let milliseconds = (i32)(hour_minute_second.second * 1000.0) % 1000
    return js_date_create(realm, js_make_time(f64(hour_minute_second.hour), f64(hour_minute_second.minute), f64(hour_minute_second.second), f64(milliseconds)))

## Ported from Dates.cpp:516 Web::HTML::parse_a_local_date_and_time_string.
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-local-date-and-time-string
pub func html_parse_a_local_date_and_time_string(input_view: ak.StringView) -> HtmlDateAndTime?:
    # 1. Let input be the string being parsed.
    # 2. Let position be a pointer into input, initially pointing at the start of the string.
    var input = ak.generic_lexer_new(input_view)
    # 3. Parse a date component to obtain year, month, and day. If this returns nothing, then fail.
    let year_month_day = html_parse_a_date_component(&input) else return none
    # 4. If position is beyond the end of input or if the character at position is neither a U+0054 LATIN CAPITAL
    #    LETTER T character (T) nor a U+0020 SPACE character, then fail. Otherwise, move position forwards one character.
    if not ak.generic_lexer_consume_specific_t_char_type(&input, 'T') and not ak.generic_lexer_consume_specific_t_char_type(&input, ' '):
        return none
    # 5. Parse a time component to obtain hour, minute, and second. If this returns nothing, then fail.
    let hour_minute_second = html_parse_a_time_component(&input) else return none
    # 6. If position is not beyond the end of input, then fail.
    if not ak.generic_lexer_is_eof(&input):
        return none
    # 7. Let date be the date with year year, month month, and day day.
    # 8. Let time be the time with hour hour, minute minute, and second second.
    # 9. Return date and time.
    return HtmlDateAndTime(date = year_month_day, time = hour_minute_second)