Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

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

#==============================================================================================
#
#   shorthand_style_value_2 - ShorthandStyleValue: the special cases of serialize
#
#   DESCRIPTION:
#       Port of the cases of ShorthandStyleValue::serialize's switch (ShorthandStyleValue.cpp
#       lines 194-552) that shorthand_style_value_1 calls, one function per shorthand, in the
#       switch's order. Each was a `case` body in C++: `return` there ends serialize, here it ends
#       the case's function, which serialize returns from at once. The lambdas of a case are
#       functions named after them.
#
#       Copyright (c) 2023, Ali Mohammad Pur <mpfard@serenityos.org>
#       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: Background ==============================================================================

## The `serialize_layer` lambda of the `background` case.
func css_shorthand_serialize_background_layer(builder: ak.StringBuilder*, mode: css_syntax.SerializationMode, color_value: CssStyleValue*?, image_value: CssStyleValue*?, position_x_value: CssStyleValue*?, position_y_value: CssStyleValue*?, size_value: CssStyleValue*?, repeat_value: CssStyleValue*?, attachment_value: CssStyleValue*?, origin_value: CssStyleValue*?, clip_value: CssStyleValue*?):
    let property_ids: css_data.PropertyId[9] = [css_data.PropertyId.background_color, css_data.PropertyId.background_image, css_data.PropertyId.background_position_x, css_data.PropertyId.background_position_y, css_data.PropertyId.background_size, css_data.PropertyId.background_repeat, css_data.PropertyId.background_attachment, css_data.PropertyId.background_origin, css_data.PropertyId.background_clip]
    let property_values: (CssStyleValue*?)[9] = [color_value, image_value, position_x_value, position_y_value, size_value, repeat_value, attachment_value, origin_value, clip_value]

    var first = true
    for i in 0..<property_ids.length:
        let value = property_values[i] else continue

        let value_string = css_style_value_to_string(value, mode)
        let initial_value_string = css_style_value_to_string(css_property_initial_value(property_ids[i]), mode)

        if not ak.string_equals(&value_string, &initial_value_string):
            if not first:
                ak.string_builder_append_u8(builder, ' ')
            ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&value_string))
            first = false

    if first:
        ak.string_builder_append(builder, "none")

## ShorthandStyleValue::serialize, `case PropertyID::Background` (ShorthandStyleValue.cpp:197).
func css_shorthand_style_value_serialize_background(this: CssShorthandStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    let color = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.background_color)
    let image = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.background_image)
    let position = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.background_position)
    let position_x = css_shorthand_style_value_longhand(css_style_value_as_shorthand(position), css_data.PropertyId.background_position_x) else trap("VERIFY: background-position-x")
    let position_y = css_shorthand_style_value_longhand(css_style_value_as_shorthand(position), css_data.PropertyId.background_position_y) else trap("VERIFY: background-position-y")
    let size = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.background_size)
    let repeat = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.background_repeat)
    let attachment = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.background_attachment)
    let origin = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.background_origin)
    let clip = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.background_clip)

    let layer_count = ak.max[usize](css_shorthand_layer_count(image), ak.max[usize](css_shorthand_layer_count(position_x), ak.max[usize](css_shorthand_layer_count(position_y), ak.max[usize](css_shorthand_layer_count(size), ak.max[usize](css_shorthand_layer_count(repeat), ak.max[usize](css_shorthand_layer_count(attachment), ak.max[usize](css_shorthand_layer_count(origin), css_shorthand_layer_count(clip))))))))

    if layer_count == 1:
        css_shorthand_serialize_background_layer(builder, mode, color, image, position_x, position_y, size, repeat, attachment, origin, clip)
        return

    for i in 0..<layer_count:
        if i != 0:
            ak.string_builder_append(builder, ", ")

        var maybe_color_value: CssStyleValue*? = none
        if i == layer_count - 1:
            maybe_color_value = color

        css_shorthand_serialize_background_layer(builder, mode, maybe_color_value, css_shorthand_layer_value(image, i), css_shorthand_layer_value(position_x, i), css_shorthand_layer_value(position_y, i), css_shorthand_layer_value(size, i), css_shorthand_layer_value(repeat, i), css_shorthand_layer_value(attachment, i), css_shorthand_layer_value(origin, i), css_shorthand_layer_value(clip, i))

## The `get_layer_value_string` lambda of the `background-position` case.
func css_shorthand_layer_value_string(style_value: CssStyleValue*, index: usize, mode: css_syntax.SerializationMode) -> ak.String:
    if css_style_value_is_value_list(style_value):
        return css_style_value_to_string(css_style_value_list_value_at(css_style_value_as_value_list(style_value), index, true), mode)
    return css_style_value_to_string(style_value, mode)

## ShorthandStyleValue::serialize, `case PropertyID::BackgroundPosition` (ShorthandStyleValue.cpp:264).
func css_shorthand_style_value_serialize_background_position(this: CssShorthandStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    let x_edges = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.background_position_x)
    let y_edges = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.background_position_y)

    # FIXME: The spec is unclear about how differing layer counts should be handled
    let layer_count = ak.max[usize](css_shorthand_layer_count(x_edges), css_shorthand_layer_count(y_edges))

    if layer_count == 1:
        css_style_value_serialize(x_edges, builder, mode)
        ak.string_builder_append_u8(builder, ' ')
        css_style_value_serialize(y_edges, builder, mode)
        return

    for i in 0..<layer_count:
        if i != 0:
            ak.string_builder_append(builder, ", ")

        let x_string = css_shorthand_layer_value_string(x_edges, i, mode)
        let y_string = css_shorthand_layer_value_string(y_edges, i, mode)
        css_appendff(builder, "{} {}", [css_arg_string(&x_string), css_arg_string(&y_string)])

# mark: Borders =================================================================================

## The `all_longhands_same_value` lambda of the `border` case.
func css_shorthand_all_longhands_same_value(shorthand: CssStyleValue*) -> bool:
    let longhands = css_shorthand_style_value_values(css_style_value_as_shorthand(shorthand))
    for longhand in ak.vector_span_const(&longhands):
        if not css_style_values_equal(longhand, *ak.vector_at_const(&longhands, 0)):
            return false
    return true

## ShorthandStyleValue::serialize, `case PropertyID::Border` (ShorthandStyleValue.cpp:294).
func css_shorthand_style_value_serialize_border(this: CssShorthandStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    # `border` only has a reasonable value if border-image is it's initial value (in which case it is omitted)
    if not css_style_value_equals(css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_image), css_property_initial_value(css_data.PropertyId.border_image)):
        return

    let border_width = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_width)
    let border_style = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_style)
    let border_color = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_color)

    # `border` only has a reasonable value if all four sides are the same.
    if not css_shorthand_all_longhands_same_value(border_width) or not css_shorthand_all_longhands_same_value(border_style) or not css_shorthand_all_longhands_same_value(border_color):
        return

    if not css_style_value_equals(border_width, css_property_initial_value(css_data.PropertyId.border_width)):
        css_style_value_serialize(border_width, builder, mode)

    if not css_style_value_equals(border_style, css_property_initial_value(css_data.PropertyId.border_style)):
        if not ak.string_builder_is_empty(builder):
            ak.string_builder_append_u8(builder, ' ')
        css_style_value_serialize(border_style, builder, mode)

    if not css_style_value_equals(border_color, css_property_initial_value(css_data.PropertyId.border_color)):
        if not ak.string_builder_is_empty(builder):
            ak.string_builder_append_u8(builder, ' ')
        css_style_value_serialize(border_color, builder, mode)

    if ak.string_builder_is_empty(builder):
        css_style_value_serialize(border_width, builder, mode)

## ShorthandStyleValue::serialize, `case PropertyID::BorderImage` (ShorthandStyleValue.cpp:331).
func css_shorthand_style_value_serialize_border_image(this: CssShorthandStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    let source = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_image_source)
    let slice = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_image_slice)
    let width = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_image_width)
    let outset = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_image_outset)
    let repeat = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_image_repeat)
    css_style_value_serialize(source, builder, mode)
    ak.string_builder_append_u8(builder, ' ')
    css_style_value_serialize(slice, builder, mode)
    ak.string_builder_append(builder, " / ")
    css_style_value_serialize(width, builder, mode)
    ak.string_builder_append(builder, " / ")
    css_style_value_serialize(outset, builder, mode)
    ak.string_builder_append_u8(builder, ' ')
    css_style_value_serialize(repeat, builder, mode)

## The `horizontal_radius` lambda of the `border-radius` case.
func css_shorthand_horizontal_radius(style_value: CssStyleValue*, mode: css_syntax.SerializationMode) -> ak.String:
    if css_style_value_is_border_radius(style_value):
        return css_style_value_to_string(css_border_radius_style_value_horizontal_radius(css_style_value_as_border_radius(style_value)), mode)
    return css_style_value_to_string(style_value, mode)

## The `vertical_radius` lambda of the `border-radius` case.
func css_shorthand_vertical_radius(style_value: CssStyleValue*, mode: css_syntax.SerializationMode) -> ak.String:
    if css_style_value_is_border_radius(style_value):
        return css_style_value_to_string(css_border_radius_style_value_vertical_radius(css_style_value_as_border_radius(style_value)), mode)
    return css_style_value_to_string(style_value, mode)

## The `serialize_radius` lambda of the `border-radius` case.
func css_shorthand_serialize_radius(top_left: ak.String, top_right: ak.String, bottom_right: ak.String, bottom_left: ak.String) -> ak.String:
    if css_strings_equal(top_left, top_right) and css_strings_equal(top_left, bottom_right) and css_strings_equal(top_left, bottom_left):
        return top_left
    if css_strings_equal(top_left, bottom_right) and css_strings_equal(top_right, bottom_left):
        return css_string_formatted("{} {}", [css_arg_string(&top_left), css_arg_string(&top_right)])
    if css_strings_equal(top_right, bottom_left):
        return css_string_formatted("{} {} {}", [css_arg_string(&top_left), css_arg_string(&top_right), css_arg_string(&bottom_right)])

    return css_string_formatted("{} {} {} {}", [css_arg_string(&top_left), css_arg_string(&top_right), css_arg_string(&bottom_right), css_arg_string(&bottom_left)])

## ShorthandStyleValue::serialize, `case PropertyID::BorderRadius` (ShorthandStyleValue.cpp:349).
func css_shorthand_style_value_serialize_border_radius(this: CssShorthandStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    let top_left = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_top_left_radius)
    let top_right = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_top_right_radius)
    let bottom_right = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_bottom_right_radius)
    let bottom_left = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.border_bottom_left_radius)

    let top_left_horizontal_string = css_shorthand_horizontal_radius(top_left, mode)
    let top_right_horizontal_string = css_shorthand_horizontal_radius(top_right, mode)
    let bottom_right_horizontal_string = css_shorthand_horizontal_radius(bottom_right, mode)
    let bottom_left_horizontal_string = css_shorthand_horizontal_radius(bottom_left, mode)

    let top_left_vertical_string = css_shorthand_vertical_radius(top_left, mode)
    let top_right_vertical_string = css_shorthand_vertical_radius(top_right, mode)
    let bottom_right_vertical_string = css_shorthand_vertical_radius(bottom_right, mode)
    let bottom_left_vertical_string = css_shorthand_vertical_radius(bottom_left, mode)

    let first_radius_serialization = css_shorthand_serialize_radius(top_left_horizontal_string, top_right_horizontal_string, bottom_right_horizontal_string, bottom_left_horizontal_string)
    let second_radius_serialization = css_shorthand_serialize_radius(top_left_vertical_string, top_right_vertical_string, bottom_right_vertical_string, bottom_left_vertical_string)
    if css_strings_equal(first_radius_serialization, second_radius_serialization):
        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&first_radius_serialization))
        return

    css_appendff(builder, "{} / {}", [css_arg_string(&first_radius_serialization), css_arg_string(&second_radius_serialization)])

## ShorthandStyleValue::serialize, `case PropertyID::Columns` (ShorthandStyleValue.cpp:395).
func css_shorthand_style_value_serialize_columns(this: CssShorthandStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    let column_width = css_style_value_to_string(css_shorthand_style_value_required_longhand(this, css_data.PropertyId.column_width), mode)
    let column_count = css_style_value_to_string(css_shorthand_style_value_required_longhand(this, css_data.PropertyId.column_count), mode)
    let column_height = css_style_value_to_string(css_shorthand_style_value_required_longhand(this, css_data.PropertyId.column_height), mode)

    if css_strings_equal(column_width, column_count):
        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&column_width))
    elif ak.string_equals_ignoring_ascii_case_string_view(&column_width, ak.sv("auto")):
        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&column_count))
    elif ak.string_equals_ignoring_ascii_case_string_view(&column_count, ak.sv("auto")):
        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&column_width))
    else:
        css_appendff(builder, "{} {}", [css_arg_string(&column_width), css_arg_string(&column_count)])

    if not ak.string_equals_ignoring_ascii_case_string_view(&column_height, ak.sv("auto")):
        ak.string_builder_append(builder, " / ")
        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&column_height))

# mark: Fonts ===================================================================================

## The `font_width_as_keyword` lambda of the `font` case.
func css_shorthand_font_width_as_keyword(font_width: CssStyleValue*) -> css_data.Keyword?:
    let keyword = css_style_value_to_keyword(font_width)
    match keyword:
        .normal, .ultra_condensed, .extra_condensed, .condensed, .semi_condensed, .semi_expanded, .expanded, .extra_expanded, .ultra_expanded:
            return keyword
        _: _ = keyword

    var font_width_as_percentage: f64? = none

    if css_style_value_is_percentage(font_width):
        font_width_as_percentage = css_dimension_style_value_raw_value(css_style_value_as_percentage(font_width).css_dimension_style_value())
    elif css_style_value_is_calculated(font_width):
        # NOTE: We don't pass a length resolution context but that's fine because either:
        #  - We are working with declarations in which case relative units can't be mapped so their mere
        #    presence means we can't serialize this font shorthand
        #  - We are working with computed values in which case we would have already converted any
        #    CalculatedStyleValues values to normal PercentageStyleValues
        if let percentage = css_calculated_style_value_resolve_percentage(css_style_value_as_calculated(font_width), CssCalculationResolutionContext()):
            font_width_as_percentage = css_percentage_value(&percentage)

    let percentage = font_width_as_percentage else return none

    # ultra-condensed 50%
    if percentage == 50.0:
        return css_data.Keyword.ultra_condensed

    # extra-condensed 62.5%
    if percentage == 62.5:
        return css_data.Keyword.extra_condensed

    # condensed 75%
    if percentage == 75.0:
        return css_data.Keyword.condensed

    # semi-condensed 87.5%
    if percentage == 87.5:
        return css_data.Keyword.semi_condensed

    # normal 100%
    if percentage == 100.0:
        return css_data.Keyword.normal

    # semi-expanded 112.5%
    if percentage == 112.5:
        return css_data.Keyword.semi_expanded

    # expanded 125%
    if percentage == 125.0:
        return css_data.Keyword.expanded

    # extra-expanded 150%
    if percentage == 150.0:
        return css_data.Keyword.extra_expanded

    # ultra-expanded 200%
    if percentage == 200.0:
        return css_data.Keyword.ultra_expanded

    return none

## The `append` lambda of the `font` case.
func css_shorthand_font_append(builder: ak.StringBuilder*, string: ak.StringView):
    if not ak.string_builder_is_empty(builder):
        ak.string_builder_append_u8(builder, ' ')
    ak.string_builder_append_string_view(builder, string)

## ShorthandStyleValue::serialize, `case PropertyID::Font` (ShorthandStyleValue.cpp:425).
func css_shorthand_style_value_serialize_font(this: CssShorthandStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    let font_style = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_style)
    let font_variant = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_variant)
    let font_weight = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_weight)
    let font_width = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_width)
    let font_size = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_size)
    let line_height = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.line_height)
    let font_family = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_family)

    let reset_only_sub_properties: css_data.PropertyId[5] = [css_data.PropertyId.font_feature_settings, css_data.PropertyId.font_kerning, css_data.PropertyId.font_language_override, css_data.PropertyId.font_optical_sizing, css_data.PropertyId.font_variation_settings]
    for reset_only_sub_property in reset_only_sub_properties:
        let value = css_shorthand_style_value_required_longhand(this, reset_only_sub_property)

        if not css_style_value_equals(value, css_property_initial_value(reset_only_sub_property)):
            return

    # Some longhands prevent serialization if they are not allowed in the shorthand.
    # <font-variant-css2> = normal | small-caps
    let font_variant_string = css_style_value_to_string(font_variant, mode)
    let font_variant_view = ak.string_bytes_as_string_view(&font_variant_string)
    if not ak.string_view_equals(&font_variant_view, "normal") and not ak.string_view_equals(&font_variant_view, "small-caps") and not css_data.is_css_wide_keyword(font_variant_view):
        return

    # <font-width-css3> = normal | ultra-condensed | extra-condensed | condensed | semi-condensed | semi-expanded | expanded | extra-expanded | ultra-expanded
    let font_width_as_keyword = css_shorthand_font_width_as_keyword(font_width) else return

    let font_style_string = css_style_value_to_string(font_style, mode)
    if not ak.string_eq_string_view(&font_style_string, ak.sv("normal")):
        css_shorthand_font_append(builder, ak.string_bytes_as_string_view(&font_style_string))
    if not ak.string_view_equals(&font_variant_view, "normal"):
        css_shorthand_font_append(builder, font_variant_view)
    let font_weight_string = css_style_value_to_string(font_weight, mode)
    if not ak.string_eq_string_view(&font_weight_string, ak.sv("normal")) and not ak.string_eq_string_view(&font_weight_string, ak.sv("400")):
        css_shorthand_font_append(builder, ak.string_bytes_as_string_view(&font_weight_string))
    if font_width_as_keyword != css_data.Keyword.normal:
        css_shorthand_font_append(builder, css_data.string_from_keyword(font_width_as_keyword))
    let font_size_string = css_style_value_to_string(font_size, mode)
    css_shorthand_font_append(builder, ak.string_bytes_as_string_view(&font_size_string))
    if css_style_value_to_keyword(line_height) != css_data.Keyword.normal:
        let line_height_string = css_style_value_to_string(line_height, mode)
        let slash_line_height = css_string_formatted("/ {}", [css_arg_string(&line_height_string)])
        css_shorthand_font_append(builder, ak.string_bytes_as_string_view(&slash_line_height))
    let font_family_string = css_style_value_to_string(font_family, mode)
    css_shorthand_font_append(builder, ak.string_bytes_as_string_view(&font_family_string))

## ShorthandStyleValue::serialize, `case PropertyID::FontVariant` (ShorthandStyleValue.cpp:515).
func css_shorthand_style_value_serialize_font_variant(this: CssShorthandStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    let ligatures = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_variant_ligatures)
    let caps = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_variant_caps)
    let alternates = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_variant_alternates)
    let numeric = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_variant_numeric)
    let east_asian = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_variant_east_asian)
    let position = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_variant_position)
    let emoji = css_shorthand_style_value_required_longhand(this, css_data.PropertyId.font_variant_emoji)

    # If ligatures is `none` and any other value isn't `normal`, that's invalid.
    if css_style_value_to_keyword(ligatures) == css_data.Keyword.none_:
        let others: CssStyleValue*[6] = [caps, alternates, numeric, east_asian, position, emoji]
        for other in others:
            if css_style_value_to_keyword(other) != css_data.Keyword.normal:
                return

    var values = ak.Vector[ak.String]()
    let longhands: CssStyleValue*[7] = [ligatures, caps, alternates, numeric, east_asian, position, emoji]
    for longhand in longhands:
        if css_style_value_to_keyword(longhand) != css_data.Keyword.normal:
            ak.vector_append_t(&values, css_style_value_to_string(longhand, mode))

    if ak.vector_is_empty(&values):
        ak.string_builder_append(builder, "normal")
        return
    let joined = ak.string_join_string(ak.sv(" "), ak.vector_span_const(&values)) else trap("MUST: String::join")
    ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&joined))