Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / style_values / calculated_style_value_7.lucb

#==============================================================================================
#
#   calculated_style_value_7 - CalculatedStyleValue: random(), rem() and non-math functions
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp (lines 2604-2904) and the inline
#       members of CalculatedStyleValue.h they belong with: the Random, Rem and NonMathFunction
#       nodes.
#
#       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: RandomCalculationNode ===================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2611 Web::CSS::RandomCalculationNode::create.
pub func css_random_calculation_node_create(random_value_sharing: CssRandomValueSharingStyleValue*, minimum: CssCalculationNode*, maximum: CssCalculationNode*, step: CssCalculationNode*?) -> CssRandomCalculationNode*:
    var numeric_type: CssNumericType? = none
    if let step_node = step:
        numeric_type = css_add_the_types_css_calculation_node_css_calculation_node_css_calculation_node(minimum, maximum, step_node)
    else:
        numeric_type = css_add_the_types_css_calculation_node_css_calculation_node(minimum, maximum)

    let this = &(new CssRandomCalculationNode[1] --- else trap("out of memory"))[0]
    css_random_calculation_node_construct(this, random_value_sharing, minimum, maximum, step, numeric_type)
    return this

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2623 Web::CSS::RandomCalculationNode::RandomCalculationNode.
pub func css_random_calculation_node_construct(this: CssRandomCalculationNode*, random_value_sharing: CssRandomValueSharingStyleValue*, minimum: CssCalculationNode*, maximum: CssCalculationNode*, step: CssCalculationNode*?, numeric_type: CssNumericType?):
    css_calculation_node_construct(this.css_calculation_node(), .random, numeric_type)
    css_calculation_node_set_class(this.css_calculation_node(), (const CssCalculationNodeVTable*)&css_random_calculation_node_vtable, &css_random_calculation_node_class)
    this.m_random_value_sharing = random_value_sharing
    this.m_minimum = minimum
    this.m_maximum = maximum
    this.m_step = step

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2634 Web::CSS::RandomCalculationNode::contains_percentage (override).
pub func css_random_calculation_node_contains_percentage_impl(base: CssCalculationNode*) -> bool:
    let this = (CssRandomCalculationNode*)base
    if css_calculation_node_contains_percentage(this.m_minimum) or css_calculation_node_contains_percentage(this.m_maximum):
        return true
    if let step = this.m_step:
        return css_calculation_node_contains_percentage(step)
    return false

## ValueComparingNonnullRefPtr<CalculationNode const>'s operator== (AK/ValueComparingRefPtr.h): the same node, or
## nodes that CalculationNode::equals.
pub func css_calculation_nodes_equal(a: CssCalculationNode*, b: CssCalculationNode*) -> bool:
    return a == b or css_calculation_node_equals(a, b)

## ValueComparingRefPtr<CalculationNode const>'s operator==: the same pointer (null included), or two nodes that
## CalculationNode::equals.
pub func css_optional_calculation_nodes_equal(a: CssCalculationNode*?, b: CssCalculationNode*?) -> bool:
    if a == b:
        return true
    if let left = a:
        if let right = b:
            return css_calculation_node_equals(left, right)
    return false

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2639 Web::CSS::RandomCalculationNode::with_simplified_children (override).
pub func css_random_calculation_node_with_simplified_children_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
    let this = (CssRandomCalculationNode*)base
    var simplified_random_value_sharing: CssRandomValueSharingStyleValue*? = none

    # When we are in the absolutization process we should absolutize m_random_value_sharing
    if let length_resolution_context = resolution_context.length_resolution_context:
        let computation_context = CssComputationContext(
            length_resolution_context = length_resolution_context,
            abstract_element = resolution_context.abstract_element,
        )

        simplified_random_value_sharing = css_style_value_as_random_value_sharing(css_style_value_absolutized(this.m_random_value_sharing.css_style_value(), computation_context))
    else:
        simplified_random_value_sharing = this.m_random_value_sharing

    let simplified_minimum = css_simplify_a_calculation_tree(this.m_minimum, context, resolution_context)
    let simplified_maximum = css_simplify_a_calculation_tree(this.m_maximum, context, resolution_context)
    var simplified_step: CssCalculationNode*? = none
    if let step = this.m_step:
        simplified_step = css_simplify_a_calculation_tree(step, context, resolution_context)

    let sharing = simplified_random_value_sharing else trap("release_nonnull: null")
    if (css_style_values_equal(sharing.css_style_value(), this.m_random_value_sharing.css_style_value()) and css_calculation_nodes_equal(simplified_minimum, this.m_minimum) and css_calculation_nodes_equal(simplified_maximum, this.m_maximum) and css_optional_calculation_nodes_equal(simplified_step, this.m_step)):
        return base

    return css_random_calculation_node_create(sharing, simplified_minimum, simplified_maximum, simplified_step).css_calculation_node()

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2669 Web::CSS::RandomCalculationNode::run_operation_if_possible (override).
# https://drafts.csswg.org/css-values-5/#random-evaluation
pub func css_random_calculation_node_run_operation_if_possible_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculatedStyleValueCalculationResult?:
    let this = (CssRandomCalculationNode*)base
    # NB: We don't want to resolve this before computation time even if it's possible
    if resolution_context.abstract_element == none and resolution_context.length_resolution_context == none:
        match resolution_context.percentage_basis:
            .empty: return none
            _: _ = 0

    let random_base_value = css_random_value_sharing_style_value_random_base_value(this.m_random_value_sharing)

    let minimum = css_try_get_value_with_canonical_unit(this.m_minimum, context, resolution_context)
    let maximum = css_try_get_value_with_canonical_unit(this.m_maximum, context, resolution_context)

    if minimum == none or maximum == none:
        return none

    let minimum_value = (minimum else trap("unreachable")).m_value
    var maximum_value = (maximum else trap("unreachable")).m_value
    var step_value = 0.0

    if let step_node = this.m_step:
        let step = css_try_get_value_with_canonical_unit(step_node, context, resolution_context) else return none

        step_value = step.m_value

    # https://drafts.csswg.org/css-values-5/#random-infinities
    # If the maximum value is less than the minimum value, it behaves as if it’s equal to the minimum value.
    if maximum_value < minimum_value:
        maximum_value = minimum_value

    # https://drafts.csswg.org/css-values-5/#random-infinities
    # In random(A, B), if A is infinite, the result is infinite.
    if math.is_infinite(minimum_value):
        return css_calculated_style_value_calculation_result_new(math.infinity, css_calculation_node_numeric_type(base))

    # If A is finite, but the difference between A and B is either infinite or large enough to be treated as infinite
    # in the user agent, the result is NaN.
    if math.is_infinite(maximum_value):
        return css_calculated_style_value_calculation_result_new(math.nan, css_calculation_node_numeric_type(base))

    # If C is infinite, the result is A.
    if math.is_infinite(step_value):
        return css_calculated_style_value_calculation_result_new(minimum_value, css_calculation_node_numeric_type(base))

    # Note: As usual for math functions, if any argument calculation is NaN, the result is NaN.
    if math.is_nan(minimum_value) or math.is_nan(maximum_value) or math.is_nan(step_value):
        return css_calculated_style_value_calculation_result_new(math.nan, css_calculation_node_numeric_type(base))

    # If C is negative, zero, or positive but close enough to zero that the range for the step multiplier (the N
    # mentioned in § 9.3 Evaluating Random Values) would be infinite in the user agent, the step must be ignored. (The
    # function is treated as if only A and B were provided.)
    let has_step = step_value > (f64)(ak.f32_epsilon * 1000.0)

    # Given a random function with a random base value R, the value of the function is:
    # - for a random() function with min and max, but no step
    if not has_step:
        # Return min + R * (max - min)
        return css_calculated_style_value_calculation_result_new(minimum_value + (random_base_value * (maximum_value - minimum_value)), css_calculation_node_numeric_type(base))

    # for a random() function with min, max, and step
    # Let epsilon be step / 1000, or the smallest representable value greater than zero in the numeric type being used if epsilon would round to zero.
    let epsilon = step_value / 1000.0

    # Let N be the largest integer such that min + N * step is less than or equal to max.
    var n = math.floor((maximum_value - minimum_value) / step_value)

    # If N produces a value that is not within epsilon of max, but N+1 would produce a value within epsilon of max, set N to N+1.
    if math.abs(maximum_value - (n * step_value + minimum_value)) > epsilon and math.abs(maximum_value - ((n + 1.0) * step_value + minimum_value)) < epsilon:
        n = n + 1.0

    # Let step index be a random integer less than N+1, given R.
    let step_index = math.floor((n + 1.0) * random_base_value)

    # Let value be min + step index * step.
    let value = minimum_value + (step_index * step_value)

    # If step index is N and value is within epsilon of max, return max.
    if step_index == n and math.abs(maximum_value - value) < epsilon:
        return css_calculated_style_value_calculation_result_new(maximum_value, css_calculation_node_numeric_type(base))

    # Otherwise, return value.
    return css_calculated_style_value_calculation_result_new(value, css_calculation_node_numeric_type(base))

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2759 Web::CSS::RandomCalculationNode::serialize.
pub func css_random_calculation_node_serialize(this: CssRandomCalculationNode*, builder: ak.StringBuilder*, context: CssCalculationContext, serialization_mode: css_syntax.SerializationMode):
    ak.string_builder_append(builder, "random(")

    let start_length = ak.string_builder_length(builder)

    css_style_value_serialize(this.m_random_value_sharing.css_style_value(), builder, serialization_mode)

    if ak.string_builder_length(builder) > start_length:
        ak.string_builder_append(builder, ", ")

    css_serialize_a_calculation_tree(builder, this.m_minimum, context, serialization_mode)
    ak.string_builder_append(builder, ", ")
    css_serialize_a_calculation_tree(builder, this.m_maximum, context, serialization_mode)

    if let step = this.m_step:
        ak.string_builder_append(builder, ", ")
        css_serialize_a_calculation_tree(builder, step, context, serialization_mode)

    ak.string_builder_append_u8(builder, ')')

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2776 Web::CSS::RandomCalculationNode::to_string.
pub func css_random_calculation_node_to_string(this: CssRandomCalculationNode*, context: CssCalculationContext, serialization_mode: css_syntax.SerializationMode) -> ak.String:
    var builder = ak.StringBuilder()
    ak.string_builder_construct(&builder)
    css_random_calculation_node_serialize(this, &builder, context, serialization_mode)
    return ak.string_builder_to_string_without_validation(&builder)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2783 Web::CSS::RandomCalculationNode::dump (override).
pub func css_random_calculation_node_dump_impl(base: CssCalculationNode*, builder: ak.StringBuilder*, indent: i32):
    let this = (CssRandomCalculationNode*)base
    css_appendff(builder, "{: >{}}RANDOM:\n", [ak.arg_str(""), ak.arg_i64((i64)indent)])
    let sharing = css_style_value_to_string(this.m_random_value_sharing.css_style_value(), css_syntax.SerializationMode.normal)
    css_appendff(builder, "{}\n", [css_arg_string(&sharing)])
    css_calculation_node_dump(this.m_minimum, builder, indent + 2)
    css_calculation_node_dump(this.m_maximum, builder, indent + 2)
    if let step = this.m_step:
        css_calculation_node_dump(step, builder, indent + 2)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2793 Web::CSS::RandomCalculationNode::equals (override).
pub func css_random_calculation_node_equals_impl(base: CssCalculationNode*, other: CssCalculationNode*) -> bool:
    let this = (CssRandomCalculationNode*)base
    if base == other:
        return true
    if css_calculation_node_type(base) != css_calculation_node_type(other):
        return false

    let other_random = as_css_random_calculation_node(other)

    return (css_style_values_equal(this.m_random_value_sharing.css_style_value(), other_random.m_random_value_sharing.css_style_value()) and css_calculation_nodes_equal(this.m_minimum, other_random.m_minimum) and css_calculation_nodes_equal(this.m_maximum, other_random.m_maximum) and css_optional_calculation_nodes_equal(this.m_step, other_random.m_step))

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2809 Web::CSS::RandomCalculationNode::is_computationally_independent (override).
pub func css_random_calculation_node_is_computationally_independent_impl(base: CssCalculationNode*) -> bool:
    let this = (CssRandomCalculationNode*)base
    if not (css_style_value_is_computationally_independent(this.m_random_value_sharing.css_style_value()) and css_calculation_node_is_computationally_independent(this.m_minimum) and css_calculation_node_is_computationally_independent(this.m_maximum)):
        return false
    if let step = this.m_step:
        return css_calculation_node_is_computationally_independent(step)
    return true

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:782 Web::CSS::RandomCalculationNode::children (override).
# NOTE: We don't return children here as serialization is handled ad-hoc
pub func css_random_calculation_node_children_impl(base: CssCalculationNode*) -> ak.Vector[CssCalculationNode*]:
    _ = base
    return ak.Vector[CssCalculationNode*]()

# mark: RemCalculationNode ======================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2817 Web::CSS::RemCalculationNode::create.
pub func css_rem_calculation_node_create(x: CssCalculationNode*, y: CssCalculationNode*) -> CssRemCalculationNode*:
    # 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 CssRemCalculationNode[1] --- else trap("out of memory"))[0]
    css_rem_calculation_node_construct(this, x, y, numeric_type)
    return this

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

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2825 Web::CSS::RemCalculationNode::RemCalculationNode.
pub func css_rem_calculation_node_construct(this: CssRemCalculationNode*, x: CssCalculationNode*, y: CssCalculationNode*, numeric_type: CssNumericType?):
    css_calculation_node_construct(this.css_calculation_node(), .rem, numeric_type)
    css_calculation_node_set_class(this.css_calculation_node(), (const CssCalculationNodeVTable*)&css_rem_calculation_node_vtable, &css_rem_calculation_node_class)
    this.m_x = x
    this.m_y = y

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2834 Web::CSS::RemCalculationNode::contains_percentage (override).
pub func css_rem_calculation_node_contains_percentage_impl(base: CssCalculationNode*) -> bool:
    let this = (CssRemCalculationNode*)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:2839 Web::CSS::RemCalculationNode::with_simplified_children (override).
pub func css_rem_calculation_node_with_simplified_children_impl(base: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
    let this = (CssRemCalculationNode*)base
    return css_simplify_2_children(base, css_rem_calculation_node_create_node, this.m_x, this.m_y, context, resolution_context)

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

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2850 Web::CSS::RemCalculationNode::dump (override).
pub func css_rem_calculation_node_dump_impl(base: CssCalculationNode*, builder: ak.StringBuilder*, indent: i32):
    let this = (CssRemCalculationNode*)base
    css_appendff(builder, "{: >{}}REM:\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:2857 Web::CSS::RemCalculationNode::equals (override).
pub func css_rem_calculation_node_equals_impl(base: CssCalculationNode*, other: CssCalculationNode*) -> bool:
    let this = (CssRemCalculationNode*)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, ((CssRemCalculationNode*)other).m_x) and css_calculation_node_equals(this.m_y, ((CssRemCalculationNode*)other).m_y)

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2867 Web::CSS::RemCalculationNode::is_computationally_independent (override).
pub func css_rem_calculation_node_is_computationally_independent_impl(base: CssCalculationNode*) -> bool:
    let this = (CssRemCalculationNode*)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:808 Web::CSS::RemCalculationNode::children (override).
pub func css_rem_calculation_node_children_impl(base: CssCalculationNode*) -> ak.Vector[CssCalculationNode*]:
    let this = (CssRemCalculationNode*)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: NonMathFunctionCalculationNode ==========================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2872 Web::CSS::NonMathFunctionCalculationNode::create.
pub func css_non_math_function_calculation_node_create(function: CssAbstractNonMathCalcFunctionStyleValue*, numeric_type: CssNumericType) -> CssNonMathFunctionCalculationNode*:
    let this = &(new CssNonMathFunctionCalculationNode[1] --- else trap("out of memory"))[0]
    css_non_math_function_calculation_node_construct(this, function, numeric_type)
    return this

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2877 Web::CSS::NonMathFunctionCalculationNode::NonMathFunctionCalculationNode.
pub func css_non_math_function_calculation_node_construct(this: CssNonMathFunctionCalculationNode*, function: CssAbstractNonMathCalcFunctionStyleValue*, numeric_type: CssNumericType):
    css_calculation_node_construct(this.css_calculation_node(), .non_math_function, numeric_type)
    css_calculation_node_set_class(this.css_calculation_node(), (const CssCalculationNodeVTable*)&css_non_math_function_calculation_node_vtable, &css_non_math_function_calculation_node_class)
    this.m_function = function

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2885 Web::CSS::NonMathFunctionCalculationNode::dump (override).
pub func css_non_math_function_calculation_node_dump_impl(base: CssCalculationNode*, builder: ak.StringBuilder*, indent: i32):
    let this = (CssNonMathFunctionCalculationNode*)base
    let function_string = css_style_value_to_string(this.m_function.css_style_value(), css_syntax.SerializationMode.normal)
    css_appendff(builder, "{: >{}}NON-MATH FUNCTION: {}", [ak.arg_str(""), ak.arg_i64((i64)indent), css_arg_string(&function_string)])

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2890 Web::CSS::NonMathFunctionCalculationNode::equals (override).
pub func css_non_math_function_calculation_node_equals_impl(base: CssCalculationNode*, other: CssCalculationNode*) -> bool:
    let this = (CssNonMathFunctionCalculationNode*)base
    if base == other:
        return true
    if css_calculation_node_type(base) != css_calculation_node_type(other):
        return false
    return css_style_values_equal(css_non_math_function_calculation_node_function((CssNonMathFunctionCalculationNode*)other).css_style_value(), this.m_function.css_style_value())

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:2901 Web::CSS::NonMathFunctionCalculationNode::is_computationally_independent (override).
pub func css_non_math_function_calculation_node_is_computationally_independent_impl(base: CssCalculationNode*) -> bool:
    let this = (CssNonMathFunctionCalculationNode*)base
    return css_style_value_is_computationally_independent(this.m_function.css_style_value())

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

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:826 Web::CSS::NonMathFunctionCalculationNode::with_simplified_children (override).
pub func css_non_math_function_calculation_node_with_simplified_children_impl(base: CssCalculationNode*, arg0: CssCalculationContext, arg1: CssCalculationResolutionContext) -> CssCalculationNode*:
    _ = arg0
    _ = arg1
    return base

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:827 Web::CSS::NonMathFunctionCalculationNode::children (override).
pub func css_non_math_function_calculation_node_children_impl(base: CssCalculationNode*) -> ak.Vector[CssCalculationNode*]:
    _ = base
    return ak.Vector[CssCalculationNode*]()

## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h:833 Web::CSS::NonMathFunctionCalculationNode::function.
pub func css_non_math_function_calculation_node_function(this: CssNonMathFunctionCalculationNode*) -> CssAbstractNonMathCalcFunctionStyleValue*:
    return this.m_function