Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / tools / gen_css_values / emit_generated_css_style_properties.lucb

#==============================================================================================
#
#   emit_generated_css_style_properties - the per-property attributes of CSSStyleProperties
#
#   DESCRIPTION:
#       Port of Meta/Generators/generate_libweb_css_style_properties.py (write_header_file and
#       write_implementation_file; the IDL file is for the bindings, phase 3). For each
#       property of Properties.json, in file order, a setter and a getter of the
#       GeneratedCSSStyleProperties mixin that forward to setProperty() and getPropertyValue()
#       of the CSSStyleProperties the mixin is part of.
#
#==============================================================================================

## Python's `snake_casify(name, trim_leading_underscores=True)`: `-moz-appearance` is `moz_appearance`.
func style_property_snake_name(name: str) -> str!:
    let snake = try snake_casify(name)
    var start: usize = 0
    while start < snake.length and snake.bytes[start] == '_':
        start += 1
    return slice(snake, start, snake.length)

## Python's make_name_acceptable_cpp: `float` would be `float_`.
func make_name_acceptable_cpp(name: str) -> str:
    if name == "float":
        return "float_"
    return name

## The setter and the getter of one property: C++'s `set_<name>` and `<name>` (the line numbers are those of
## GeneratedCSSStyleProperties.cpp, which writes 10 lines per property from line 8).
func put_style_property_accessors(out: Out*, index: usize, name: str, after_mark: bool = false) -> !:
    let cpp_name = make_name_acceptable_cpp(try style_property_snake_name(name))
    let luce_name = try luce_snake(cpp_name)
    let setter_line = 8 + 10 * index
    if not after_mark:
        try out.blank()
    try out.line(f"## Ported from Build/Libraries/LibWeb/CSS/GeneratedCSSStyleProperties.cpp:{setter_line} Web::Bindings::GeneratedCSSStyleProperties::set_{cpp_name}.")
    try out.line(f"pub func bind_generated_css_style_properties_set_{luce_name}(this: BindGeneratedCssStyleProperties*, value: ak.StringView) -> !:")
    try out.line(f"    try css_style_declaration_set_property(bind_generated_css_style_properties_generated_style_properties_to_css_style_properties(this).css_style_declaration(), ak.fly_string_from_str({try literal(name)}), value, ak.sv(\"\"))")
    try out.blank()
    try out.line(f"## Ported from Build/Libraries/LibWeb/CSS/GeneratedCSSStyleProperties.cpp:{setter_line + 5} Web::Bindings::GeneratedCSSStyleProperties::{cpp_name}.")
    try out.line(f"pub func bind_generated_css_style_properties_{luce_name}(this: BindGeneratedCssStyleProperties*) -> ak.String:")
    try out.line(f"    return css_style_declaration_get_property_value(bind_generated_css_style_properties_generated_style_properties_to_css_style_properties_const(this).css_style_declaration(), ak.fly_string_from_str({try literal(name)}))")

## Emit generated/generated_css_style_properties.lucb from Properties.json.
func emit_generated_css_style_properties(properties: const Json*) -> str!:
    var out = try Out.create()
    try put_header(&out, "generated_css_style_properties", "the per-property attributes of CSSStyleProperties", [
        "Generated by tools/gen_css_values from data/css/Properties.json; do not edit. The C++ it",
        "stands for, at Ladybird 47c82b38d0, is Build/Libraries/LibWeb/CSS/GeneratedCSSStyleProperties.h",
        "and .cpp, written by Meta/Generators/generate_libweb_css_style_properties.py: for each",
        "property, `set_<name>(value)` is setProperty(name, value, \"\") and `<name>()` is",
        "getPropertyValue(name), on the CSSStyleProperties the GeneratedCSSStyleProperties mixin is",
        "part of.",
        "",
        "Copyright (c) 2024, Luke Wilde <luke@ladybird.org>",
        "Copyright (c) 2026-present, the Ladybird developers.",
        "Copyright (c) 2026 the luce-browser authors.",
        "SPDX-License-Identifier: BSD-2-Clause (see data/css/LICENSE)",
    ])

    # The header's last lines: the mixin's virtual and its const overload, after the accessors (3 lines each from line 11).
    let virtual_line = 13 + 3 * properties.count + 3
    try put_mark(&out, "The mixin's link to its CSSStyleProperties")
    try out.line(f"## Ported from Build/Libraries/LibWeb/CSS/GeneratedCSSStyleProperties.h:{virtual_line} Web::Bindings::GeneratedCSSStyleProperties::generated_style_properties_to_css_style_properties (pure virtual).")
    try out.line("pub func bind_generated_css_style_properties_generated_style_properties_to_css_style_properties_impl(this: BindGeneratedCssStyleProperties*) -> CssStyleProperties*:")
    try out.line("    _ = this")
    try out.line("    trap(\"pure virtual: Web::Bindings::GeneratedCSSStyleProperties::generated_style_properties_to_css_style_properties\")")
    try out.blank()
    try out.line(f"## Ported from Build/Libraries/LibWeb/CSS/GeneratedCSSStyleProperties.h:{virtual_line + 1} Web::Bindings::GeneratedCSSStyleProperties::generated_style_properties_to_css_style_properties (const).")
    try out.line("pub func bind_generated_css_style_properties_generated_style_properties_to_css_style_properties_const(this: BindGeneratedCssStyleProperties*) -> CssStyleProperties*:")
    try out.line("    return bind_generated_css_style_properties_generated_style_properties_to_css_style_properties(this)")

    try put_mark(&out, "The per-property attributes")
    for index in 0..<properties.count:
        try put_style_property_accessors(&out, index, properties.key(index), after_mark = index == 0)
    return out.text()