Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / style_values / calculated_style_value_6.lucb

#==============================================================================================
#
#   calculated_style_value_6 - CalculatedStyleValue: hypot(), log(), exp(), round() and mod()
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp (lines 2125-2604) and the inline
#       members of CalculatedStyleValue.h they belong with: the Hypot, Log, Exp, Round and Mod
#       nodes and run_mod_or_rem_operation_if_possible.
#
#       Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
#       Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: HypotCalculationNode ====================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2130 Web::CSS::HypotCalculationNode::create.
pub func css_hypot_calculation_node_create(values: ak.Vector[CssCalculationNode*]) -> CssHypotCalculationNode*:
    # https://drafts.csswg.org/css-values-4/#determine-the-type-of-a-calculation
    # The result of adding the types of its comma-separated calculations.
    let numeric_type = css_add_the_types_vector(&values)
    let this = &(new CssHypotCalculationNode[1] --- else trap("out of memory"))[0]
    css_hypot_calculation_node_construct(this, values, numeric_type)
    return this

## css_hypot_calculation_node_create as a CssCreateFromChildren.
func css_hypot_calculation_node_create_node(values: ak.Vector[CssCalculationNode*]) -> CssCalculationNode*:
    return css_hypot_calculation_node_create(values).css_calculation_node()

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2138 Web::CSS::HypotCalculationNode::HypotCalculationNode.
pub func css_hypot_calculation_node_construct(this: CssHypotCalculationNode*, values: ak.Vector[CssCalculationNode*], numeric_type: CssNumericType?):
    css_calculation_node_construct(this.css_calculation_node(), .hypot, numeric_type)
    css_calculation_node_set_class(this.css_calculation_node(), (const CssCalculationNodeVTable*)&css_hypot_calculation_node_vtable, &css_hypot_calculation_node_class)
    this.m_values = values

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2146 Web::CSS::HypotCalculationNode::contains_percentage (override).
pub func css_hypot_calculation_node_contains_percentage_impl(base: CssCalculationNode*) -> bool:
    let this = (CssHypotCalculationNode*)base
    for value in ak.vector_span_const(&this.m_values):
        if css_calculation_node_contains_percentage(value):
            return true

    return false

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2156 Web::CSS::HypotCalculationNode::with_simplified_children (override).
pub func css_hypot_calculation_node_with_simplified_children_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
    return css_simplify_children_vector(base, css_hypot_calculation_node_create_node, context, resolution_context)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2162 Web::CSS::HypotCalculationNode::run_operation_if_possible (override).
# https://drafts.csswg.org/css-values-4/#funcdef-hypot
pub func css_hypot_calculation_node_run_operation_if_possible_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculatedStyleValueCalculationResult?:
    let this = (CssHypotCalculationNode*)base
    # The hypot(A, …) function contains one or more comma-separated calculations, and returns the length of an
    # N-dimensional vector with components equal to each of the calculations. (That is, the square root of the sum of
    # the squares of its arguments.) The argument calculations can resolve to any <number>, <dimension>, or
    # <percentage>, but must have a consistent type or else the function is invalid; the result’s type will be the
    # consistent type.

    var consistent_type: CssNumericType? = none
    var value = 0.0

    for child in ak.vector_span_const(&this.m_values):
        let canonical_child = css_try_get_value_with_canonical_unit(child, context, resolution_context) else return none

        if let current = consistent_type:
            consistent_type = css_numeric_type_consistent_type(&current, canonical_child.m_type else trap("VERIFY: has_value"))
        else:
            consistent_type = canonical_child.m_type

        if consistent_type == none:
            return none

        value += canonical_child.m_value * canonical_child.m_value

    if consistent_type == none:
        return none

    return css_calculated_style_value_calculation_result_new(math.sqrt(value), consistent_type)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2195 Web::CSS::HypotCalculationNode::dump (override).
pub func css_hypot_calculation_node_dump_impl(base: CssCalculationNode*, builder: ak.StringBuilder*, indent: i32):
    let this = (CssHypotCalculationNode*)base
    css_appendff(builder, "{: >{}}HYPOT:\n", [ak.arg_str(""), ak.arg_i64((i64)indent)])
    for value in ak.vector_span_const(&this.m_values):
        css_calculation_node_dump(value, builder, indent + 2)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2202 Web::CSS::HypotCalculationNode::equals (override).
pub func css_hypot_calculation_node_equals_impl(base: CssCalculationNode*, other: CssCalculationNode*) -> bool:
    let this = (CssHypotCalculationNode*)base
    if base == other:
        return true
    if css_calculation_node_type(base) != css_calculation_node_type(other):
        return false
    # NB: The donor compares no sizes here: an `other` with fewer children reads past its end in C++ and traps here.
    let other_values = &((CssHypotCalculationNode*)other).m_values
    for i in 0..<ak.vector_size(&this.m_values):
        if not css_calculation_node_equals(*ak.vector_at_const(&this.m_values, i), *ak.vector_at_const(other_values, i)):
            return false
    return true

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2215 Web::CSS::HypotCalculationNode::is_computationally_independent (override).
pub func css_hypot_calculation_node_is_computationally_independent_impl(base: CssCalculationNode*) -> bool:
    let this = (CssHypotCalculationNode*)base
    return css_calculation_nodes_are_computationally_independent(&this.m_values)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:675 Web::CSS::HypotCalculationNode::children (override).
pub func css_hypot_calculation_node_children_impl(base: CssCalculationNode*) -> ak.Vector[CssCalculationNode*]:
    let this = (CssHypotCalculationNode*)base
    return this.m_values

# mark: LogCalculationNode =================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2220 Web::CSS::LogCalculationNode::create.
pub func css_log_calculation_node_create(x: CssCalculationNode*, y: CssCalculationNode*) -> CssLogCalculationNode*:
    let this = &(new CssLogCalculationNode[1] --- else trap("out of memory"))[0]
    css_log_calculation_node_construct(this, x, y)
    return this

## css_log_calculation_node_create as a CssCreateFromTwoChildren.
func css_log_calculation_node_create_node(x: CssCalculationNode*, y: CssCalculationNode*) -> CssCalculationNode*:
    return css_log_calculation_node_create(x, y).css_calculation_node()

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2225 Web::CSS::LogCalculationNode::LogCalculationNode.
pub func css_log_calculation_node_construct(this: CssLogCalculationNode*, x: CssCalculationNode*, y: CssCalculationNode*):
    # https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
    # «[ ]» (empty map).
    css_calculation_node_construct(this.css_calculation_node(), .log, CssNumericType())
    css_calculation_node_set_class(this.css_calculation_node(), (const CssCalculationNodeVTable*)&css_log_calculation_node_vtable, &css_log_calculation_node_class)
    this.m_x = x
    this.m_y = y

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:691 Web::CSS::LogCalculationNode::contains_percentage (override).
pub func css_log_calculation_node_contains_percentage_impl(base: CssCalculationNode*) -> bool:
    _ = base
    return false

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2236 Web::CSS::LogCalculationNode::with_simplified_children (override).
pub func css_log_calculation_node_with_simplified_children_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
    let this = (CssLogCalculationNode*)base
    return css_simplify_2_children(base, css_log_calculation_node_create_node, this.m_x, this.m_y, context, resolution_context)

# https://drafts.csswg.org/css-values-4/#funcdef-log
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2242 Web::CSS::LogCalculationNode::run_operation_if_possible (override).
pub func css_log_calculation_node_run_operation_if_possible_impl(base: CssCalculationNode*, arg0: CssCalculationContext, arg1: CssCalculationResolutionContext) -> CssCalculatedStyleValueCalculationResult?:
    _ = arg0
    _ = arg1
    let this = (CssLogCalculationNode*)base
    # The log(A, B?) function contains one or two calculations (representing the value to be logarithmed, and the
    # base of the logarithm, defaulting to e), which must resolve to <number>s, and returns the logarithm base B of
    # the value A, as a <number> with the return type made consistent with the input calculation’s type.
    let number = css_try_get_number(this.m_x)
    let base_number = css_try_get_number(this.m_y)
    if number == none or base_number == none:
        return none

    let empty_type = CssNumericType()
    let consistent_type = css_numeric_type_made_consistent_with(&empty_type, css_calculation_node_numeric_type(this.m_x) else trap("VERIFY: has_value")) else return none

    return css_calculated_style_value_calculation_result_new(math.log(number else trap("unreachable")) / math.log(base_number else trap("unreachable")), consistent_type)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2260 Web::CSS::LogCalculationNode::dump (override).
pub func css_log_calculation_node_dump_impl(base: CssCalculationNode*, builder: ak.StringBuilder*, indent: i32):
    let this = (CssLogCalculationNode*)base
    css_appendff(builder, "{: >{}}LOG:\n", [ak.arg_str(""), ak.arg_i64((i64)indent)])
    css_calculation_node_dump(this.m_x, builder, indent + 2)
    css_calculation_node_dump(this.m_y, builder, indent + 2)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2267 Web::CSS::LogCalculationNode::equals (override).
pub func css_log_calculation_node_equals_impl(base: CssCalculationNode*, other: CssCalculationNode*) -> bool:
    let this = (CssLogCalculationNode*)base
    if base == other:
        return true
    if css_calculation_node_type(base) != css_calculation_node_type(other):
        return false
    return css_calculation_node_equals(this.m_x, ((CssLogCalculationNode*)other).m_x) and css_calculation_node_equals(this.m_y, ((CssLogCalculationNode*)other).m_y)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2277 Web::CSS::LogCalculationNode::is_computationally_independent (override).
pub func css_log_calculation_node_is_computationally_independent_impl(base: CssCalculationNode*) -> bool:
    let this = (CssLogCalculationNode*)base
    return css_calculation_node_is_computationally_independent(this.m_x) and css_calculation_node_is_computationally_independent(this.m_y)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:695 Web::CSS::LogCalculationNode::children (override).
pub func css_log_calculation_node_children_impl(base: CssCalculationNode*) -> ak.Vector[CssCalculationNode*]:
    let this = (CssLogCalculationNode*)base
    var children = ak.Vector[CssCalculationNode*]()
    ak.vector_append_t(&children, this.m_x)
    ak.vector_append_t(&children, this.m_y)
    return children

# mark: ExpCalculationNode =================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2282 Web::CSS::ExpCalculationNode::create.
pub func css_exp_calculation_node_create(value: CssCalculationNode*) -> CssExpCalculationNode*:
    let this = &(new CssExpCalculationNode[1] --- else trap("out of memory"))[0]
    css_exp_calculation_node_construct(this, value)
    return this

## css_exp_calculation_node_create as a CssCreateFromChild.
func css_exp_calculation_node_create_node(value: CssCalculationNode*) -> CssCalculationNode*:
    return css_exp_calculation_node_create(value).css_calculation_node()

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2287 Web::CSS::ExpCalculationNode::ExpCalculationNode.
pub func css_exp_calculation_node_construct(this: CssExpCalculationNode*, value: CssCalculationNode*):
    # https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
    # «[ ]» (empty map).
    css_calculation_node_construct(this.css_calculation_node(), .exp, CssNumericType())
    css_calculation_node_set_class(this.css_calculation_node(), (const CssCalculationNodeVTable*)&css_exp_calculation_node_vtable, &css_exp_calculation_node_class)
    this.m_value = value

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:712 Web::CSS::ExpCalculationNode::contains_percentage (override).
pub func css_exp_calculation_node_contains_percentage_impl(base: CssCalculationNode*) -> bool:
    _ = base
    return false

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2297 Web::CSS::ExpCalculationNode::with_simplified_children (override).
pub func css_exp_calculation_node_with_simplified_children_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
    let this = (CssExpCalculationNode*)base
    return css_simplify_child(base, css_exp_calculation_node_create_node, this.m_value, context, resolution_context)

# https://drafts.csswg.org/css-values-4/#funcdef-exp
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2303 Web::CSS::ExpCalculationNode::run_operation_if_possible (override).
pub func css_exp_calculation_node_run_operation_if_possible_impl(base: CssCalculationNode*, arg0: CssCalculationContext, arg1: CssCalculationResolutionContext) -> CssCalculatedStyleValueCalculationResult?:
    _ = arg0
    _ = arg1
    let this = (CssExpCalculationNode*)base
    # The exp(A) function contains one calculation which must resolve to a <number>, and returns the same value as
    # pow(e, A) as a <number> with the return type made consistent with the input calculation’s type.
    let number = css_try_get_number(this.m_value) else return none

    let empty_type = CssNumericType()
    let consistent_type = css_numeric_type_made_consistent_with(&empty_type, css_calculation_node_numeric_type(this.m_value) else trap("VERIFY: has_value")) else return none

    return css_calculated_style_value_calculation_result_new(math.exp(number), consistent_type)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2319 Web::CSS::ExpCalculationNode::dump (override).
pub func css_exp_calculation_node_dump_impl(base: CssCalculationNode*, builder: ak.StringBuilder*, indent: i32):
    let this = (CssExpCalculationNode*)base
    css_appendff(builder, "{: >{}}EXP:\n", [ak.arg_str(""), ak.arg_i64((i64)indent)])
    css_calculation_node_dump(this.m_value, builder, indent + 2)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2325 Web::CSS::ExpCalculationNode::equals (override).
pub func css_exp_calculation_node_equals_impl(base: CssCalculationNode*, other: CssCalculationNode*) -> bool:
    let this = (CssExpCalculationNode*)base
    if base == other:
        return true
    if css_calculation_node_type(base) != css_calculation_node_type(other):
        return false
    return css_calculation_node_equals(this.m_value, ((CssExpCalculationNode*)other).m_value)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2334 Web::CSS::ExpCalculationNode::is_computationally_independent (override).
pub func css_exp_calculation_node_is_computationally_independent_impl(base: CssCalculationNode*) -> bool:
    let this = (CssExpCalculationNode*)base
    return css_calculation_node_is_computationally_independent(this.m_value)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:716 Web::CSS::ExpCalculationNode::children (override).
pub func css_exp_calculation_node_children_impl(base: CssCalculationNode*) -> ak.Vector[CssCalculationNode*]:
    let this = (CssExpCalculationNode*)base
    var children = ak.Vector[CssCalculationNode*]()
    ak.vector_append_t(&children, this.m_value)
    return children

# mark: RoundCalculationNode ====================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2339 Web::CSS::RoundCalculationNode::create.
pub func css_round_calculation_node_create(strategy: css_data.RoundingStrategy, x: CssCalculationNode*, y: CssCalculationNode*) -> CssRoundCalculationNode*:
    # https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
    # The result of adding the types of its comma-separated calculations.
    let numeric_type = css_add_the_types_css_calculation_node_css_calculation_node(x, y)
    let this = &(new CssRoundCalculationNode[1] --- else trap("out of memory"))[0]
    css_round_calculation_node_construct(this, strategy, x, y, numeric_type)
    return this

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2347 Web::CSS::RoundCalculationNode::RoundCalculationNode.
pub func css_round_calculation_node_construct(this: CssRoundCalculationNode*, mode: css_data.RoundingStrategy, x: CssCalculationNode*, y: CssCalculationNode*, numeric_type: CssNumericType?):
    css_calculation_node_construct(this.css_calculation_node(), .round, numeric_type)
    css_calculation_node_set_class(this.css_calculation_node(), (const CssCalculationNodeVTable*)&css_round_calculation_node_vtable, &css_round_calculation_node_class)
    this.m_strategy = mode
    this.m_x = x
    this.m_y = y

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2357 Web::CSS::RoundCalculationNode::contains_percentage (override).
pub func css_round_calculation_node_contains_percentage_impl(base: CssCalculationNode*) -> bool:
    let this = (CssRoundCalculationNode*)base
    return css_calculation_node_contains_percentage(this.m_x) or css_calculation_node_contains_percentage(this.m_y)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2362 Web::CSS::RoundCalculationNode::with_simplified_children (override).
pub func css_round_calculation_node_with_simplified_children_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
    let this = (CssRoundCalculationNode*)base
    let simplified_x = css_simplify_a_calculation_tree(this.m_x, context, resolution_context)
    let simplified_y = css_simplify_a_calculation_tree(this.m_y, context, resolution_context)
    if simplified_x != this.m_x or simplified_y != this.m_y:
        return css_round_calculation_node_create(this.m_strategy, simplified_x, simplified_y).css_calculation_node()
    return base

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2372 Web::CSS::RoundCalculationNode::run_operation_if_possible (override).
# https://drafts.csswg.org/css-values-4/#funcdef-round
pub func css_round_calculation_node_run_operation_if_possible_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculatedStyleValueCalculationResult?:
    let this = (CssRoundCalculationNode*)base
    # The round(<rounding-strategy>?, A, B?) function contains an optional rounding strategy, and two calculations A
    # and B, and returns the value of A, rounded according to the rounding strategy, to the nearest integer multiple of
    # B either above or below A. The argument calculations can resolve to any <number>, <dimension>, or <percentage>,
    # but must have a consistent type or else the function is invalid; the result’s type will be the consistent type.

    let maybe_a = css_try_get_value_with_canonical_unit(this.m_x, context, resolution_context)
    let maybe_b = css_try_get_value_with_canonical_unit(this.m_y, context, resolution_context)
    if maybe_a == none or maybe_b == none:
        return none
    let result_a = maybe_a else trap("unreachable")
    let result_b = maybe_b else trap("unreachable")

    let a_type = result_a.m_type else trap("VERIFY: has_value")
    let consistent_type = css_numeric_type_made_consistent_with(&a_type, result_b.m_type else trap("VERIFY: has_value"))
    if consistent_type == none:
        return none

    let a = result_a.m_value
    let b = result_b.m_value

    # https://drafts.csswg.org/css-values-4/#round-infinities
    # In round(A, B), if B is 0, the result is NaN. If A and B are both infinite, the result is NaN.
    if b == 0.0 or (math.is_infinite(a) and math.is_infinite(b)):
        return css_calculated_style_value_calculation_result_new(math.nan, consistent_type)

    # If A is infinite but B is finite, the result is the same infinity.
    if math.is_infinite(a) and math.is_finite(b):
        return css_calculated_style_value_calculation_result_new(a, consistent_type)

    # If A is finite but B is infinite, the result depends on the <rounding-strategy> and the sign of A:
    if math.is_finite(a) and math.is_infinite(b):
        let sign = math.signbit(a)
        match this.m_strategy:
            # nearest, to-zero:
            .nearest, .to_zero:
                # If A is positive or 0⁺, return 0⁺. Otherwise, return 0⁻.
                return css_calculated_style_value_calculation_result_new(0.0 if not sign else -0.0, consistent_type)
            # up:
            .up:
                var result = 0.0
                if a > 0.0:
                    # If A is positive(not zero), return +∞.
                    result = math.infinity
                else:
                    # If A is 0⁺, return 0⁺. Otherwise, return 0⁻.
                    result = 0.0 if not sign else -0.0
                return css_calculated_style_value_calculation_result_new(result, consistent_type)
            # down:
            .down:
                var result = 0.0
                if a < 0.0:
                    # If A is negative (not zero), return −∞.
                    result = -math.infinity
                else:
                    # If A is 0⁻, return 0⁻. Otherwise, return 0⁺.
                    result = -0.0 if sign else 0.0
                return css_calculated_style_value_calculation_result_new(result, consistent_type)
            _:
                _ = 0

    # If A is exactly equal to an integer multiple of B, round() resolves to A exactly (preserving whether A is 0⁻ or
    # 0⁺, if relevant).
    if math.mod(a, b) == 0.0:
        return result_a

    # Otherwise, there are two integer multiples of B that are potentially "closest" to A, lower B which is closer to
    # −∞ and upper B which is closer to +∞. The following <rounding-strategy>s dictate how to choose between them:

    # FIXME: If lower B would be zero, it is specifically equal to 0⁺;
    #        if upper B would be zero, it is specifically equal to 0⁻.
    # get_lower_b: floor(a / b) * b; get_upper_b: ceil(a / b) * b.

    var rounded = 0.0
    match this.m_strategy:
        # -> nearest
        .nearest:
            # Choose whichever of lower B and upper B that has the smallest absolute difference from A.
            # If both have an equal difference (A is exactly between the two values), choose upper B.
            let lower_b = math.floor(a / b) * b
            let upper_b = math.ceil(a / b) * b
            let lower_diff = math.abs(lower_b - a)
            let upper_diff = math.abs(upper_b - a)
            rounded = upper_b if upper_diff <= lower_diff else lower_b
        # -> up
        .up:
            # Choose upper B.
            rounded = math.ceil(a / b) * b
        # -> down
        .down:
            # Choose lower B.
            rounded = math.floor(a / b) * b
        # -> to-zero
        .to_zero:
            # Choose whichever of lower B and upper B that has the smallest absolute difference from 0.
            let lower_b = math.floor(a / b) * b
            let upper_b = math.ceil(a / b) * b
            rounded = upper_b if math.abs(upper_b) < math.abs(lower_b) else lower_b
        _:
            _ = 0

    return css_calculated_style_value_calculation_result_new(rounded, consistent_type)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2493 Web::CSS::RoundCalculationNode::dump (override).
pub func css_round_calculation_node_dump_impl(base: CssCalculationNode*, builder: ak.StringBuilder*, indent: i32):
    let this = (CssRoundCalculationNode*)base
    css_appendff(builder, "{: >{}}ROUND: {}\n", [ak.arg_str(""), ak.arg_i64((i64)indent), ak.arg_string_view(css_data.to_string_rounding_strategy(this.m_strategy))])
    css_calculation_node_dump(this.m_x, builder, indent + 2)
    css_calculation_node_dump(this.m_y, builder, indent + 2)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2500 Web::CSS::RoundCalculationNode::equals (override).
pub func css_round_calculation_node_equals_impl(base: CssCalculationNode*, other: CssCalculationNode*) -> bool:
    let this = (CssRoundCalculationNode*)base
    if base == other:
        return true
    if css_calculation_node_type(base) != css_calculation_node_type(other):
        return false
    let other_round = (CssRoundCalculationNode*)other
    return (this.m_strategy == other_round.m_strategy and css_calculation_node_equals(this.m_x, other_round.m_x) and css_calculation_node_equals(this.m_y, other_round.m_y))

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2511 Web::CSS::RoundCalculationNode::is_computationally_independent (override).
pub func css_round_calculation_node_is_computationally_independent_impl(base: CssCalculationNode*) -> bool:
    let this = (CssRoundCalculationNode*)base
    return css_calculation_node_is_computationally_independent(this.m_x) and css_calculation_node_is_computationally_independent(this.m_y)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:737 Web::CSS::RoundCalculationNode::rounding_strategy.
pub func css_round_calculation_node_rounding_strategy(this: CssRoundCalculationNode*) -> css_data.RoundingStrategy:
    return this.m_strategy

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:738 Web::CSS::RoundCalculationNode::children (override).
# NOTE: This excludes the rounding strategy!
pub func css_round_calculation_node_children_impl(base: CssCalculationNode*) -> ak.Vector[CssCalculationNode*]:
    let this = (CssRoundCalculationNode*)base
    var children = ak.Vector[CssCalculationNode*]()
    ak.vector_append_t(&children, this.m_x)
    ak.vector_append_t(&children, this.m_y)
    return children

# mark: ModCalculationNode =================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2516 Web::CSS::ModCalculationNode::create.
pub func css_mod_calculation_node_create(x: CssCalculationNode*, y: CssCalculationNode*) -> CssModCalculationNode*:
    # https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
    # The result of adding the types of its comma-separated calculations.
    let numeric_type = css_add_the_types_css_calculation_node_css_calculation_node(x, y)
    let this = &(new CssModCalculationNode[1] --- else trap("out of memory"))[0]
    css_mod_calculation_node_construct(this, x, y, numeric_type)
    return this

## css_mod_calculation_node_create as a CssCreateFromTwoChildren.
func css_mod_calculation_node_create_node(x: CssCalculationNode*, y: CssCalculationNode*) -> CssCalculationNode*:
    return css_mod_calculation_node_create(x, y).css_calculation_node()

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2524 Web::CSS::ModCalculationNode::ModCalculationNode.
pub func css_mod_calculation_node_construct(this: CssModCalculationNode*, x: CssCalculationNode*, y: CssCalculationNode*, numeric_type: CssNumericType?):
    css_calculation_node_construct(this.css_calculation_node(), .mod, numeric_type)
    css_calculation_node_set_class(this.css_calculation_node(), (const CssCalculationNodeVTable*)&css_mod_calculation_node_vtable, &css_mod_calculation_node_class)
    this.m_x = x
    this.m_y = y

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2533 Web::CSS::ModCalculationNode::contains_percentage (override).
pub func css_mod_calculation_node_contains_percentage_impl(base: CssCalculationNode*) -> bool:
    let this = (CssModCalculationNode*)base
    return css_calculation_node_contains_percentage(this.m_x) or css_calculation_node_contains_percentage(this.m_y)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2538 Web::CSS::ModCalculationNode::with_simplified_children (override).
pub func css_mod_calculation_node_with_simplified_children_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
    let this = (CssModCalculationNode*)base
    return css_simplify_2_children(base, css_mod_calculation_node_create_node, this.m_x, this.m_y, context, resolution_context)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2548 Web::CSS::run_mod_or_rem_operation_if_possible.
# https://drafts.csswg.org/css-values-4/#funcdef-mod
func css_run_mod_or_rem_operation_if_possible(numerator: CssCalculationNode*, denominator: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext, mod_or_rem: CssModOrRem) -> CssCalculatedStyleValueCalculationResult?:
    # The modulus functions mod(A, B) and rem(A, B) similarly contain two calculations A and B, and return the
    # difference between A and the nearest integer multiple of B either above or below A. The argument calculations
    # can resolve to any <number>, <dimension>, or <percentage>, but must have the same type, or else the function
    # is invalid; the result will have the same type as the arguments.
    let numerator_value = css_try_get_value_with_canonical_unit(numerator, context, resolution_context)
    let denominator_value = css_try_get_value_with_canonical_unit(denominator, context, resolution_context)
    if numerator_value == none or denominator_value == none:
        return none
    let a = numerator_value else trap("unreachable")
    let b = denominator_value else trap("unreachable")

    if a.m_type != b.m_type:
        return none

    # The two functions are very similar, and in fact return identical results if both arguments are positive or both
    # are negative: the value of the function is equal to the value of A shifted by the integer multiple of B that
    # brings the value between zero and B. (Specifically, the range includes zero and excludes B.More specifically,
    # if B is positive the range starts at 0⁺, and if B is negative it starts at 0⁻.)
    #
    # Their behavior diverges if the A value and the B step are on opposite sides of zero: mod() (short for “modulus”)
    # continues to choose the integer multiple of B that puts the value between zero and B, as above (guaranteeing
    # that the result will either be zero or share the sign of B, not A), while rem() (short for "remainder") chooses
    # the integer multiple of B that puts the value between zero and -B, avoiding changing the sign of the value.
    var result = 0.0
    if mod_or_rem == CssModOrRem.mod:
        let quotient = math.floor(a.m_value / b.m_value)
        result = a.m_value - (b.m_value * quotient)
    else:
        result = math.mod(a.m_value, b.m_value)

    return css_calculated_style_value_calculation_result_new(result, a.m_type)

# https://drafts.csswg.org/css-values-4/#funcdef-mod
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2584 Web::CSS::ModCalculationNode::run_operation_if_possible (override).
pub func css_mod_calculation_node_run_operation_if_possible_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculatedStyleValueCalculationResult?:
    let this = (CssModCalculationNode*)base
    return css_run_mod_or_rem_operation_if_possible(this.m_x, this.m_y, context, resolution_context, .mod)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2589 Web::CSS::ModCalculationNode::dump (override).
pub func css_mod_calculation_node_dump_impl(base: CssCalculationNode*, builder: ak.StringBuilder*, indent: i32):
    let this = (CssModCalculationNode*)base
    css_appendff(builder, "{: >{}}MOD:\n", [ak.arg_str(""), ak.arg_i64((i64)indent)])
    css_calculation_node_dump(this.m_x, builder, indent + 2)
    css_calculation_node_dump(this.m_y, builder, indent + 2)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2596 Web::CSS::ModCalculationNode::equals (override).
pub func css_mod_calculation_node_equals_impl(base: CssCalculationNode*, other: CssCalculationNode*) -> bool:
    let this = (CssModCalculationNode*)base
    if base == other:
        return true
    if css_calculation_node_type(base) != css_calculation_node_type(other):
        return false
    return css_calculation_node_equals(this.m_x, ((CssModCalculationNode*)other).m_x) and css_calculation_node_equals(this.m_y, ((CssModCalculationNode*)other).m_y)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2606 Web::CSS::ModCalculationNode::is_computationally_independent (override).
pub func css_mod_calculation_node_is_computationally_independent_impl(base: CssCalculationNode*) -> bool:
    let this = (CssModCalculationNode*)base
    return css_calculation_node_is_computationally_independent(this.m_x) and css_calculation_node_is_computationally_independent(this.m_y)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:760 Web::CSS::ModCalculationNode::children (override).
pub func css_mod_calculation_node_children_impl(base: CssCalculationNode*) -> ak.Vector[CssCalculationNode*]:
    let this = (CssModCalculationNode*)base
    var children = ak.Vector[CssCalculationNode*]()
    ak.vector_append_t(&children, this.m_x)
    ak.vector_append_t(&children, this.m_y)
    return children