Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / time.lucb

#==============================================================================================
#
#   time - Time: a <time> value and its unit
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Time.cpp (lines 1-88) and the inline members of Time.h. C++'s
#       operator== and operator<=> are css_time_eq and css_time_cmp.
#
#       Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/CSS/Time.cpp:15 Web::CSS::Time::Time.
pub func css_time_construct(this: CssTime*, value: f64, unit_: css_data.TimeUnit):
    this.m_unit = unit_
    this.m_value = value

## `Time { value, unit }` as a value.
pub func css_time_new(value: f64, unit_: css_data.TimeUnit) -> CssTime:
    var time: CssTime = ---
    css_time_construct(&time, value, unit_)
    return time

## Ported from Libraries/LibWeb/CSS/Time.cpp:21 Web::CSS::Time::from_style_value.
pub func css_time_from_style_value(style_value: CssStyleValue*, percentage_basis: CssTime?) -> CssTime:
    if css_style_value_is_time(style_value):
        return css_time_style_value_time(css_style_value_as_time(style_value))

    if css_style_value_is_calculated(style_value):
        var resolved_percentage_basis = CssCalculationResolutionContextPercentageBasis.empty

        if let basis = percentage_basis:
            resolved_percentage_basis = .css_time(basis)

        return css_calculated_style_value_resolve_time(css_style_value_as_calculated(style_value), CssCalculationResolutionContext(percentage_basis = resolved_percentage_basis)) else trap("VERIFY: a resolved time")

    if css_style_value_is_percentage(style_value):
        let basis = percentage_basis else trap("VERIFY: a percentage basis")

        return css_time_percentage_of(&basis, css_percentage_style_value_percentage(css_style_value_as_percentage(style_value)))

    trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/CSS/Time.cpp:45 Web::CSS::Time::make_seconds.
pub func css_time_make_seconds(value: f64) -> CssTime:
    return css_time_new(value, css_data.TimeUnit.s)

## Ported from Libraries/LibWeb/CSS/Time.cpp:50 Web::CSS::Time::percentage_of.
pub func css_time_percentage_of(this: const CssTime*, percentage: CssPercentage) -> CssTime:
    return css_time_new(css_percentage_as_fraction(&percentage) * this.m_value, this.m_unit)

## Ported from Libraries/LibWeb/CSS/Time.cpp:55 Web::CSS::Time::serialize.
pub func css_time_serialize(this: const CssTime*, builder: ak.StringBuilder*, serialization_mode: css_syntax.SerializationMode = css_syntax.SerializationMode.normal):
    # https://drafts.csswg.org/cssom/#serialize-a-css-value
    # -> <time>
    # The time in seconds serialized as per <number> followed by the literal string "s".
    # AD-HOC: WPT expects us to serialize using the actual unit, like for other dimensions.
    #         https://github.com/w3c/csswg-drafts/issues/12616
    if serialization_mode == css_syntax.SerializationMode.resolved_value:
        css_syntax.serialize_a_number_string_builder(builder, css_time_to_seconds(this))
        ak.string_builder_append(builder, "s")
        return
    css_syntax.serialize_a_number_string_builder(builder, css_time_raw_value(this))
    let unit_name = css_time_unit_name(this)
    ak.string_builder_append_string_view(builder, ak.fly_string_bytes_as_string_view(&unit_name))

## Ported from Libraries/LibWeb/CSS/Time.cpp:71 Web::CSS::Time::to_string.
pub func css_time_to_string(this: const CssTime*, serialization_mode: css_syntax.SerializationMode = css_syntax.SerializationMode.normal) -> ak.String:
    var builder = ak.string_builder_new()
    css_time_serialize(this, &builder, serialization_mode)
    return ak.string_builder_to_string_without_validation(&builder)

## Ported from Libraries/LibWeb/CSS/Time.cpp:78 Web::CSS::Time::to_seconds.
pub func css_time_to_seconds(this: const CssTime*) -> f64:
    return css_data.ratio_between_units_time_unit(this.m_unit, css_data.TimeUnit.s) * this.m_value

## Ported from Libraries/LibWeb/CSS/Time.cpp:83 Web::CSS::Time::to_milliseconds.
pub func css_time_to_milliseconds(this: const CssTime*) -> f64:
    return css_data.ratio_between_units_time_unit(this.m_unit, css_data.TimeUnit.ms) * this.m_value

## Ported from Libraries/LibWeb/CSS/Time.h:28 Web::CSS::Time::raw_value.
pub func css_time_raw_value(this: const CssTime*) -> f64:
    return this.m_value

## Ported from Libraries/LibWeb/CSS/Time.h:29 Web::CSS::Time::unit.
pub func css_time_unit(this: const CssTime*) -> css_data.TimeUnit:
    return this.m_unit

## Ported from Libraries/LibWeb/CSS/Time.h:30 Web::CSS::Time::unit_name.
pub func css_time_unit_name(this: const CssTime*) -> ak.FlyString:
    return css_data.to_string_time_unit(this.m_unit)

## Ported from Libraries/LibWeb/CSS/Time.h:32 Web::CSS::Time::operator==.
pub func css_time_eq(this: const CssTime*, other: CssTime) -> bool:
    return this.m_unit == other.m_unit and this.m_value == other.m_value

## Ported from Libraries/LibWeb/CSS/Time.h:37 Web::CSS::Time::operator<=>.
pub func css_time_cmp(this: const CssTime*, other: CssTime) -> i32:
    let this_seconds = css_time_to_seconds(this)
    let other_seconds = css_time_to_seconds(&other)

    if this_seconds < other_seconds:
        return -1
    if this_seconds > other_seconds:
        return 1
    return 0