Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / flex.lucb

#==============================================================================================
#
#   flex - Flex: a <flex> value (fr) and its unit
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Flex.cpp (lines 1-67) and the inline members of Flex.h. C++'s
#       operator== and operator<=> are css_flex_eq and css_flex_cmp.
#
#       Copyright (c) 2023-2025, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/CSS/Flex.cpp:15 Web::CSS::Flex::Flex.
pub func css_flex_construct(this: CssFlex*, value: f64, unit_: css_data.FlexUnit):
    this.m_unit = unit_
    this.m_value = value

## `Flex { value, unit }` as a value.
pub func css_flex_new(value: f64, unit_: css_data.FlexUnit) -> CssFlex:
    var flex: CssFlex = ---
    css_flex_construct(&flex, value, unit_)
    return flex

## Ported from Libraries/LibWeb/CSS/Flex.cpp:21 Web::CSS::Flex::make_fr.
pub func css_flex_make_fr(value: f64) -> CssFlex:
    return css_flex_new(value, css_data.FlexUnit.fr)

## Ported from Libraries/LibWeb/CSS/Flex.cpp:26 Web::CSS::Flex::from_style_value.
pub func css_flex_from_style_value(value: CssStyleValue*) -> CssFlex:
    if css_style_value_is_flex(value):
        return css_flex_style_value_flex(css_style_value_as_flex(value))

    if css_style_value_is_calculated(value):
        return css_calculated_style_value_resolve_flex(css_style_value_as_calculated(value), CssCalculationResolutionContext()) else trap("VERIFY: a resolved flex")

    trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/CSS/Flex.cpp:37 Web::CSS::Flex::percentage_of.
pub func css_flex_percentage_of(this: const CssFlex*, percentage: CssPercentage) -> CssFlex:
    return css_flex_new(css_percentage_as_fraction(&percentage) * this.m_value, this.m_unit)

## Ported from Libraries/LibWeb/CSS/Flex.cpp:42 Web::CSS::Flex::serialize.
pub func css_flex_serialize(this: const CssFlex*, builder: ak.StringBuilder*, serialization_mode: css_syntax.SerializationMode = css_syntax.SerializationMode.normal):
    # https://drafts.csswg.org/cssom/#serialize-a-css-value
    # AD-HOC: No spec definition, so copy the other <dimension> definitions
    if serialization_mode == css_syntax.SerializationMode.resolved_value:
        css_syntax.serialize_a_number_string_builder(builder, css_flex_to_fr(this))
        ak.string_builder_append(builder, "fr")
        return
    css_syntax.serialize_a_number_string_builder(builder, css_flex_raw_value(this))
    let unit_name = css_flex_unit_name(this)
    ak.string_builder_append_string_view(builder, ak.fly_string_bytes_as_string_view(&unit_name))

## Ported from Libraries/LibWeb/CSS/Flex.cpp:55 Web::CSS::Flex::to_string.
pub func css_flex_to_string(this: const CssFlex*, serialization_mode: css_syntax.SerializationMode = css_syntax.SerializationMode.normal) -> ak.String:
    var builder = ak.string_builder_new()
    css_flex_serialize(this, &builder, serialization_mode)
    return ak.string_builder_to_string_without_validation(&builder)

## Ported from Libraries/LibWeb/CSS/Flex.cpp:62 Web::CSS::Flex::to_fr.
pub func css_flex_to_fr(this: const CssFlex*) -> f64:
    return css_data.ratio_between_units_flex_unit(this.m_unit, css_data.FlexUnit.fr) * this.m_value

## Ported from Libraries/LibWeb/CSS/Flex.h:30 Web::CSS::Flex::raw_value.
pub func css_flex_raw_value(this: const CssFlex*) -> f64:
    return this.m_value

## Ported from Libraries/LibWeb/CSS/Flex.h:31 Web::CSS::Flex::unit.
pub func css_flex_unit(this: const CssFlex*) -> css_data.FlexUnit:
    return this.m_unit

## Ported from Libraries/LibWeb/CSS/Flex.h:32 Web::CSS::Flex::unit_name.
pub func css_flex_unit_name(this: const CssFlex*) -> ak.FlyString:
    return css_data.to_string_flex_unit(this.m_unit)

## Ported from Libraries/LibWeb/CSS/Flex.h:34 Web::CSS::Flex::operator==.
pub func css_flex_eq(this: const CssFlex*, other: CssFlex) -> bool:
    return this.m_unit == other.m_unit and this.m_value == other.m_value

## Ported from Libraries/LibWeb/CSS/Flex.h:39 Web::CSS::Flex::operator<=>.
pub func css_flex_cmp(this: const CssFlex*, other: CssFlex) -> i32:
    let this_fr = css_flex_to_fr(this)
    let other_fr = css_flex_to_fr(&other)

    if this_fr < other_fr:
        return -1
    if this_fr > other_fr:
        return 1
    return 0