Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

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

#==============================================================================================
#
#   keyword_style_value - KeywordStyleValue: a CSS keyword, and the colors keywords resolve to
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/StyleValues/KeywordStyleValue.cpp (lines 1-405) and the inline
#       members of KeywordStyleValue.h.
#       KeywordStyleValue::create's statics (the CSS-wide keywords) are module variables made in the C
#       heap on first use, as r36's CounterStyle statics.
#
#       Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
#       Copyright (c) 2021-2026, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Creation ================================================================================

# The static instances of KeywordStyleValue::create for the CSS-wide keywords, made on first use.
var css_keyword_style_value_inherit_instance: CssKeywordStyleValue*?
var css_keyword_style_value_initial_instance: CssKeywordStyleValue*?
var css_keyword_style_value_revert_instance: CssKeywordStyleValue*?
var css_keyword_style_value_revert_layer_instance: CssKeywordStyleValue*?
var css_keyword_style_value_unset_instance: CssKeywordStyleValue*?

## A static instance of KeywordStyleValue::create: made once, in the C heap.
func css_keyword_style_value_static_instance(instance: (CssKeywordStyleValue*?)*, keyword: css_data.Keyword) -> CssKeywordStyleValue*:
    if let value = *instance:
        return value
    let saved_atomic_allocator = ak.atomic_allocator
    ak.atomic_allocator = none
    defer ak.atomic_allocator = saved_atomic_allocator
    with memory.heap:
        let value = &(new CssKeywordStyleValue[1] --- else trap("out of memory"))[0]
        css_keyword_style_value_construct(value, keyword)
        *instance = value
        return value

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h:19 Web::CSS::KeywordStyleValue::create.
pub func css_keyword_style_value_create(keyword: css_data.Keyword) -> CssKeywordStyleValue*:
    match keyword:
        .inherit:
            return css_keyword_style_value_static_instance(&css_keyword_style_value_inherit_instance, css_data.Keyword.inherit)
        .initial:
            return css_keyword_style_value_static_instance(&css_keyword_style_value_initial_instance, css_data.Keyword.initial)
        .revert:
            return css_keyword_style_value_static_instance(&css_keyword_style_value_revert_instance, css_data.Keyword.revert)
        .revert_layer:
            return css_keyword_style_value_static_instance(&css_keyword_style_value_revert_layer_instance, css_data.Keyword.revert_layer)
        .unset:
            return css_keyword_style_value_static_instance(&css_keyword_style_value_unset_instance, css_data.Keyword.unset)
        _:
            let this = &(new CssKeywordStyleValue[1] --- else trap("out of memory"))[0]
            css_keyword_style_value_construct(this, keyword)
            return this

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h:74 Web::CSS::KeywordStyleValue::KeywordStyleValue.
pub func css_keyword_style_value_construct(this: CssKeywordStyleValue*, keyword: css_data.Keyword):
    css_style_value_construct(this.css_style_value(), CssStyleValueType.keyword)
    css_style_value_set_class(this.css_style_value(), (const CssStyleValueVTable*)&css_keyword_style_value_vtable, &css_keyword_style_value_class)
    this.m_keyword = keyword

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h:48 Web::CSS::KeywordStyleValue::keyword.
pub func css_keyword_style_value_keyword(this: CssKeywordStyleValue*) -> css_data.Keyword:
    return this.m_keyword

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h:83 Web::CSS::StyleValue::to_keyword.
pub func css_style_value_to_keyword(this: CssStyleValue*) -> css_data.Keyword:
    if css_style_value_is_keyword(this):
        return css_keyword_style_value_keyword((CssKeywordStyleValue*)this)
    return css_data.Keyword.invalid

# mark: Serialization and equality ==============================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp:22 Web::CSS::KeywordStyleValue::serialize (override).
pub func css_keyword_style_value_serialize_impl(base: CssStyleValue*, builder: ak.StringBuilder*, arg1: css_syntax.SerializationMode):
    _ = arg1
    let this = (CssKeywordStyleValue*)base
    ak.string_builder_append_string_view(builder, css_data.string_from_keyword(css_keyword_style_value_keyword(this)))

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp:393 Web::CSS::KeywordStyleValue::tokenize (override).
pub func css_keyword_style_value_tokenize_impl(base: CssStyleValue*) -> ak.Vector[css_syntax.ComponentValue]:
    let this = (CssKeywordStyleValue*)base
    var values = ak.Vector[css_syntax.ComponentValue]()
    let keyword_string = css_data.string_from_keyword(this.m_keyword)
    let ident = ak.fly_string_from_utf8_without_validation(ak.string_view_bytes(&keyword_string))
    ak.vector_append_t(&values, css_syntax.component_value_from_token(css_syntax.token_create_ident(ident, ak.String())))
    return values

# https://drafts.css-houdini.org/css-typed-om-1/#reify-ident
## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp:399 Web::CSS::KeywordStyleValue::reify (override).
pub func css_keyword_style_value_reify_impl(base: CssStyleValue*, realm: Realm*, associated_property: ak.FlyString) -> CssCssStyleValue*:
    _ = associated_property
    let this = (CssKeywordStyleValue*)base
    # 1. Return a new CSSKeywordValue with its value internal slot set to the serialization of ident.
    let keyword_string = css_data.string_from_keyword(this.m_keyword)
    return (CssCssStyleValue*)css_keyword_value_create(realm, ak.fly_string_from_utf8_without_validation(ak.string_view_bytes(&keyword_string)))

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h:58 Web::CSS::KeywordStyleValue::properties_equal.
pub func css_keyword_style_value_properties_equal(this: CssKeywordStyleValue*, other: CssKeywordStyleValue*) -> bool:
    return this.m_keyword == other.m_keyword

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:202 Web::CSS::StyleValueWithDefaultOperators::equals for Web::CSS::KeywordStyleValue (CRTP helper) (override).
pub func css_keyword_style_value_equals_impl(base: CssStyleValue*, other: CssStyleValue*) -> bool:
    if css_style_value_type(base) != css_style_value_type(other):
        return false
    return css_keyword_style_value_properties_equal((CssKeywordStyleValue*)base, (CssKeywordStyleValue*)other)

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h:60 Web::CSS::KeywordStyleValue::is_computationally_independent (override).
pub func css_keyword_style_value_is_computationally_independent_impl(base: CssStyleValue*) -> bool:
    let this = (CssKeywordStyleValue*)base
    if css_style_value_is_css_wide_keyword(base):
        return false
    # FIXME: Are there any other color keywords which aren't computationally independent?
    if this.m_keyword == css_data.Keyword.accentcolor or this.m_keyword == css_data.Keyword.accentcolortext:
        return false
    # FIXME: Are there any other keywords which aren't computationally independent?
    return true

# mark: Colors ==================================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp:27 Web::CSS::KeywordStyleValue::is_color.
pub func css_keyword_style_value_is_color(keyword: css_data.Keyword) -> bool:
    match keyword:
        .accentcolor, .accentcolortext, .activeborder, .activecaption, .activetext, .appworkspace:
            return true
        .background, .buttonborder, .buttonface, .buttonhighlight, .buttonshadow, .buttontext:
            return true
        .canvas, .canvastext, .captiontext, .currentcolor, .field, .fieldtext:
            return true
        .graytext, .highlight, .highlighttext, .inactiveborder, .inactivecaption, .inactivecaptiontext:
            return true
        .infobackground, .infotext, .libweb_buttonfacedisabled, .libweb_buttonfacehover, .libweb_link, .libweb_palette_active_link:
            return true
        .libweb_palette_active_window_border1, .libweb_palette_active_window_border2, .libweb_palette_active_window_title, .libweb_palette_base, .libweb_palette_base_text, .libweb_palette_button:
            return true
        .libweb_palette_button_text, .libweb_palette_desktop_background, .libweb_palette_focus_outline, .libweb_palette_highlight_window_border1, .libweb_palette_highlight_window_border2, .libweb_palette_highlight_window_title:
            return true
        .libweb_palette_hover_highlight, .libweb_palette_inactive_selection, .libweb_palette_inactive_selection_text, .libweb_palette_inactive_window_border1, .libweb_palette_inactive_window_border2, .libweb_palette_inactive_window_title:
            return true
        .libweb_palette_link, .libweb_palette_menu_base, .libweb_palette_menu_base_text, .libweb_palette_menu_selection, .libweb_palette_menu_selection_text, .libweb_palette_menu_stripe:
            return true
        .libweb_palette_moving_window_border1, .libweb_palette_moving_window_border2, .libweb_palette_moving_window_title, .libweb_palette_rubber_band_border, .libweb_palette_rubber_band_fill, .libweb_palette_ruler:
            return true
        .libweb_palette_ruler_active_text, .libweb_palette_ruler_border, .libweb_palette_ruler_inactive_text, .libweb_palette_selection, .libweb_palette_selection_text, .libweb_palette_syntax_comment:
            return true
        .libweb_palette_syntax_control_keyword, .libweb_palette_syntax_identifier, .libweb_palette_syntax_keyword, .libweb_palette_syntax_number, .libweb_palette_syntax_operator, .libweb_palette_syntax_preprocessor_statement:
            return true
        .libweb_palette_syntax_preprocessor_value, .libweb_palette_syntax_punctuation, .libweb_palette_syntax_string, .libweb_palette_syntax_type, .libweb_palette_text_cursor, .libweb_palette_threed_highlight:
            return true
        .libweb_palette_threed_shadow1, .libweb_palette_threed_shadow2, .libweb_palette_visited_link, .libweb_palette_window, .libweb_palette_window_text, .linktext:
            return true
        .mark, .marktext, .menu, .menutext, .scrollbar, .selecteditem:
            return true
        .selecteditemtext, .threeddarkshadow, .threedface, .threedhighlight, .threedlightshadow, .threedshadow:
            return true
        .visitedtext, .window, .windowframe, .windowtext:
            return true
        _:
            return false

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp:136 Web::CSS::KeywordStyleValue::has_color (override).
pub func css_keyword_style_value_has_color_impl(base: CssStyleValue*) -> bool:
    let this = (CssKeywordStyleValue*)base
    return css_keyword_style_value_is_color(css_keyword_style_value_keyword(this))

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp:141 Web::CSS::KeywordStyleValue::to_color (override).
pub func css_keyword_style_value_to_color_impl(base: CssStyleValue*, color_resolution_context: CssColorResolutionContext) -> gfx.Color?:
    let this = (CssKeywordStyleValue*)base
    if css_keyword_style_value_keyword(this) == css_data.Keyword.currentcolor:
        return color_resolution_context.current_color else gfx.color_new_named_color(gfx.ColorNamedColor.black)

    let scheme = color_resolution_context.color_scheme else CssPreferredColorScheme.light

    # Calculate accent_color_text based on contrast to accent_color
    if css_keyword_style_value_keyword(this) == css_data.Keyword.accentcolortext:
        # min_contrast = 10.2 is a magic number which provides the best accessibility trade-off based on:
        # 1. https://webaim.org/resources/contrastchecker/
        # 2. Current implementation of luminosity() and contrast_ratio() methods for Color instances

        # the baseline colors with the least contrast from black and white are #757575 and #767676
        # which score over 4.5 ratio for #fff and #000 accent_color_text values correspondingly
        let min_contrast = 10.2
        let system_accent_text = css_system_color_accent_color_text(scheme)

        var accent_color = color_resolution_context.accent_color else css_system_color_accent_color(scheme)
        if (f64)gfx.color_contrast_ratio(&accent_color, system_accent_text) < min_contrast:
            return gfx.color_inverted(&system_accent_text)

        return system_accent_text

    # First, handle <system-color>s, since they don't strictly require a node.
    # https://www.w3.org/TR/css-color-4/#css-system-colors
    # https://www.w3.org/TR/css-color-4/#deprecated-system-colors
    match css_keyword_style_value_keyword(this):
        .accentcolor:
            return color_resolution_context.accent_color else css_system_color_accent_color(scheme)
        .buttonborder, .activeborder, .inactiveborder, .threeddarkshadow, .threedhighlight, .threedlightshadow, .threedshadow, .windowframe:
            return css_system_color_button_border(scheme)
        .buttonface, .buttonhighlight, .buttonshadow, .threedface:
            return css_system_color_button_face(scheme)
        .buttontext:
            return css_system_color_button_text(scheme)
        .canvas, .appworkspace, .background, .inactivecaption, .infobackground, .menu, .scrollbar, .window:
            return css_system_color_canvas(scheme)
        .canvastext, .activecaption, .captiontext, .infotext, .menutext, .windowtext:
            return css_system_color_canvas_text(scheme)
        .field:
            return css_system_color_field(scheme)
        .fieldtext:
            return css_system_color_field_text(scheme)
        .graytext, .inactivecaptiontext:
            return css_system_color_gray_text(scheme)
        .highlight:
            return css_system_color_highlight(scheme)
        .highlighttext:
            return css_system_color_highlight_text(scheme)
        .mark:
            return css_system_color_mark(scheme)
        .marktext:
            return css_system_color_mark_text(scheme)
        .selecteditem:
            return css_system_color_selected_item(scheme)
        .selecteditemtext:
            return css_system_color_selected_item_text(scheme)
        .libweb_buttonfacedisabled:
            let button_face = css_system_color_button_face(scheme)
            return gfx.color_with_alpha(&button_face, 128)
        .libweb_buttonfacehover:
            let button_face = css_system_color_button_face(scheme)
            return gfx.color_darkened(&button_face, 0.8)
        _: _ = scheme

    let document = color_resolution_context.document else return gfx.color_new_named_color(gfx.ColorNamedColor.black)
    # FIXME: Can't resolve palette colors without a document.

    match css_keyword_style_value_keyword(this):
        .libweb_link, .linktext:
            return dom_document_normal_link_color(document) else css_system_color_link_text(scheme)
        .visitedtext:
            return dom_document_visited_link_color(document) else css_system_color_visited_text(scheme)
        .activetext:
            return dom_document_active_link_color(document) else css_system_color_active_text(scheme)
        _: _ = scheme

    let palette = page_palette(dom_document_page(document))
    match css_keyword_style_value_keyword(this):
        .libweb_palette_desktop_background:
            return gfx.palette_color(&palette, gfx.ColorRole.desktop_background)
        .libweb_palette_active_window_border1:
            return gfx.palette_color(&palette, gfx.ColorRole.active_window_border1)
        .libweb_palette_active_window_border2:
            return gfx.palette_color(&palette, gfx.ColorRole.active_window_border2)
        .libweb_palette_active_window_title:
            return gfx.palette_color(&palette, gfx.ColorRole.active_window_title)
        .libweb_palette_inactive_window_border1:
            return gfx.palette_color(&palette, gfx.ColorRole.inactive_window_border1)
        .libweb_palette_inactive_window_border2:
            return gfx.palette_color(&palette, gfx.ColorRole.inactive_window_border2)
        .libweb_palette_inactive_window_title:
            return gfx.palette_color(&palette, gfx.ColorRole.inactive_window_title)
        .libweb_palette_moving_window_border1:
            return gfx.palette_color(&palette, gfx.ColorRole.moving_window_border1)
        .libweb_palette_moving_window_border2:
            return gfx.palette_color(&palette, gfx.ColorRole.moving_window_border2)
        .libweb_palette_moving_window_title:
            return gfx.palette_color(&palette, gfx.ColorRole.moving_window_title)
        .libweb_palette_highlight_window_border1:
            return gfx.palette_color(&palette, gfx.ColorRole.highlight_window_border1)
        .libweb_palette_highlight_window_border2:
            return gfx.palette_color(&palette, gfx.ColorRole.highlight_window_border2)
        .libweb_palette_highlight_window_title:
            return gfx.palette_color(&palette, gfx.ColorRole.highlight_window_title)
        .libweb_palette_menu_stripe:
            return gfx.palette_color(&palette, gfx.ColorRole.menu_stripe)
        .libweb_palette_menu_base:
            return gfx.palette_color(&palette, gfx.ColorRole.menu_base)
        .libweb_palette_menu_base_text:
            return gfx.palette_color(&palette, gfx.ColorRole.menu_base_text)
        .libweb_palette_menu_selection:
            return gfx.palette_color(&palette, gfx.ColorRole.menu_selection)
        .libweb_palette_menu_selection_text:
            return gfx.palette_color(&palette, gfx.ColorRole.menu_selection_text)
        .libweb_palette_window:
            return gfx.palette_color(&palette, gfx.ColorRole.window)
        .libweb_palette_window_text:
            return gfx.palette_color(&palette, gfx.ColorRole.window_text)
        .libweb_palette_button:
            return gfx.palette_color(&palette, gfx.ColorRole.button)
        .libweb_palette_button_text:
            return gfx.palette_color(&palette, gfx.ColorRole.button_text)
        .libweb_palette_base:
            return gfx.palette_color(&palette, gfx.ColorRole.base)
        .libweb_palette_base_text:
            return gfx.palette_color(&palette, gfx.ColorRole.base_text)
        .libweb_palette_threed_highlight:
            return gfx.palette_color(&palette, gfx.ColorRole.threed_highlight)
        .libweb_palette_threed_shadow1:
            return gfx.palette_color(&palette, gfx.ColorRole.threed_shadow1)
        .libweb_palette_threed_shadow2:
            return gfx.palette_color(&palette, gfx.ColorRole.threed_shadow2)
        .libweb_palette_hover_highlight:
            return gfx.palette_color(&palette, gfx.ColorRole.hover_highlight)
        .libweb_palette_selection:
            return gfx.palette_color(&palette, gfx.ColorRole.selection)
        .libweb_palette_selection_text:
            return gfx.palette_color(&palette, gfx.ColorRole.selection_text)
        .libweb_palette_inactive_selection:
            return gfx.palette_color(&palette, gfx.ColorRole.inactive_selection)
        .libweb_palette_inactive_selection_text:
            return gfx.palette_color(&palette, gfx.ColorRole.inactive_selection_text)
        .libweb_palette_rubber_band_fill:
            return gfx.palette_color(&palette, gfx.ColorRole.rubber_band_fill)
        .libweb_palette_rubber_band_border:
            return gfx.palette_color(&palette, gfx.ColorRole.rubber_band_border)
        .libweb_palette_link:
            return gfx.palette_color(&palette, gfx.ColorRole.link)
        .libweb_palette_active_link:
            return gfx.palette_color(&palette, gfx.ColorRole.active_link)
        .libweb_palette_visited_link:
            return gfx.palette_color(&palette, gfx.ColorRole.visited_link)
        .libweb_palette_ruler:
            return gfx.palette_color(&palette, gfx.ColorRole.ruler)
        .libweb_palette_ruler_border:
            return gfx.palette_color(&palette, gfx.ColorRole.ruler_border)
        .libweb_palette_ruler_active_text:
            return gfx.palette_color(&palette, gfx.ColorRole.ruler_active_text)
        .libweb_palette_ruler_inactive_text:
            return gfx.palette_color(&palette, gfx.ColorRole.ruler_inactive_text)
        .libweb_palette_text_cursor:
            return gfx.palette_color(&palette, gfx.ColorRole.text_cursor)
        .libweb_palette_focus_outline:
            return gfx.palette_color(&palette, gfx.ColorRole.focus_outline)
        .libweb_palette_syntax_comment:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_comment)
        .libweb_palette_syntax_number:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_number)
        .libweb_palette_syntax_string:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_string)
        .libweb_palette_syntax_type:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_type)
        .libweb_palette_syntax_punctuation:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_punctuation)
        .libweb_palette_syntax_operator:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_operator)
        .libweb_palette_syntax_keyword:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_keyword)
        .libweb_palette_syntax_control_keyword:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_control_keyword)
        .libweb_palette_syntax_identifier:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_identifier)
        .libweb_palette_syntax_preprocessor_statement:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_preprocessor_statement)
        .libweb_palette_syntax_preprocessor_value:
            return gfx.palette_color(&palette, gfx.ColorRole.syntax_preprocessor_value)
        _:
            return none

## Ported from Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.cpp:363 Web::CSS::KeywordStyleValue::absolutized (override).
pub func css_keyword_style_value_absolutized_impl(base: CssStyleValue*, context: CssComputationContext) -> CssStyleValue*:
    let this = (CssKeywordStyleValue*)base
    if not css_style_value_has_color(base):
        return base

    # The currentcolor keyword computes to itself.
    # https://drafts.csswg.org/css-color-4/#resolving-other-colors
    if css_keyword_style_value_keyword(this) == css_data.Keyword.currentcolor:
        return base

    var color_resolution_context = CssColorResolutionContext()
    if let abstract_element = context.abstract_element:
        color_resolution_context.document = dom_abstract_element_document(&abstract_element)
        color_resolution_context.calculation_resolution_context = css_calculation_resolution_context_from_computation_context(context)
        color_resolution_context.color_scheme = context.color_scheme

    let resolved_color = css_style_value_to_color(base, color_resolution_context) else return base

    return css_color_function_style_value_create(
        CssColorStyleValueColorType.rgb,
        css_number_style_value_create((f64)gfx.color_red(&resolved_color)).css_style_value(),
        css_number_style_value_create((f64)gfx.color_green(&resolved_color)).css_style_value(),
        css_number_style_value_create((f64)gfx.color_blue(&resolved_color)).css_style_value(),
        css_number_style_value_create((f64)((f32)gfx.color_alpha(&resolved_color) / 255.0f32)).css_style_value(),
        CssColorSyntax.legacy,
    ).css_style_value()