Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/CSS/StyleComputer.cpp (C++)

Code before:
    auto const absolute_length = [&]() -> CSSPixels {
        if (specified_value->is_calculated())
            return specified_value->as_calculated().resolve_length({ .length_resolution_context = computation_context.length_resolution_context })->absolute_length_to_px();

        if (specified_value->is_length())
            return specified_value->as_length().length().to_px(computation_context.length_resolution_context);

        if (specified_value->is_keyword())
            return line_width_keyword_to_css_pixels(specified_value->to_keyword());

        VERIFY_NOT_REACHED();
    }();

    return LengthStyleValue::create(Length::make_px(snap_a_length_as_a_border_width(computation_context.device_pixels_per_css_pixel, absolute_length)));
}

NonnullRefPtr<StyleValue const> StyleComputer::compute_opacity(NonnullRefPtr<StyleValue const> const& specified_value, PropertyValueComputationContext const& computation_context)
{
    // https://drafts.csswg.org/css-color-4/#transparency
    // specified number, clamped to the range [0,1]

    // <number>
    if (specified_value->is_number())
        return NumberStyleValue::create(clamp(specified_value->as_number().number(), 0, 1));

    // AD-HOC: We also support calc()'d numbers
    if (specified_value->is_calculated() && specified_value->as_calculated().resolves_to_number())
        return NumberStyleValue::create(clamp(specified_value->as_calculated().resolve_number({ .length_resolution_context = computation_context.length_resolution_context }).value(), 0, 1));

    // <percentage>
    if (specified_value->is_percentage())
        return NumberStyleValue::create(clamp(specified_value->as_percentage().percentage().as_fraction(), 0, 1));

    // AD-HOC: We also support calc'd percentages
    if (specified_value->is_calculated() && specified_value->as_calculated().resolves_to_percentage())
        return NumberStyleValue::create(clamp(specified_value->as_calculated().resolve_percentage({ .length_resolution_context = computation_context.length_resolution_context })->as_fraction(), 0, 1));

    VERIFY_NOT_REACHED();
}

void StyleComputer::compute_math_depth(ComputedProperties& style, Optional<DOM::AbstractElement> element) const
{
    // https://w3c.github.io/mathml-core/#propdef-math-depth

    auto element_to_inherit_style_from = element.has_value() ? element->element_to_inherit_style_from() : OptionalNone {};

    auto inherited_math_depth = [&]() {
        if (!element_to_inherit_style_from.has_value())
            return InitialValues::math_depth();
        return element_to_inherit_style_from->computed_properties()->math_depth();
    };

Review comment (suggestion):
This is a nit, but I wouldn't call these "ad-hoc". It's just the nature of `<number>` and `<percentage>` that they can be a calculation.

Code after:
    // absolute length, snapped as a border width; zero if the border style is none or hidden
    if (first_is_one_of(style_specified_value->to_keyword(), Keyword::None, Keyword::Hidden))
        return LengthStyleValue::create(Length::make_px(0));

    auto const absolute_length = [&]() -> CSSPixels {
        if (specified_value->is_calculated())
            return specified_value->as_calculated().resolve_length({ .length_resolution_context = computation_context.length_resolution_context })->absolute_length_to_px();

        if (specified_value->is_length())
            return specified_value->as_length().length().to_px(computation_context.length_resolution_context);

        if (specified_value->is_keyword())
            return line_width_keyword_to_css_pixels(specified_value->to_keyword());

        VERIFY_NOT_REACHED();
    }();

    return LengthStyleValue::create(Length::make_px(snap_a_length_as_a_border_width(computation_context.device_pixels_per_css_pixel, absolute_length)));
}

NonnullRefPtr<StyleValue const> StyleComputer::compute_opacity(NonnullRefPtr<StyleValue const> const& specified_value, PropertyValueComputationContext const& computation_context)
{
    // https://drafts.csswg.org/css-color-4/#transparency
    // specified number, clamped to the range [0,1]

    // <number>
    if (specified_value->is_number())
        return NumberStyleValue::create(clamp(specified_value->as_number().number(), 0, 1));

    // NOTE: We also support calc()'d numbers
    if (specified_value->is_calculated() && specified_value->as_calculated().resolves_to_number())
        return NumberStyleValue::create(clamp(specified_value->as_calculated().resolve_number({ .length_resolution_context = computation_context.length_resolution_context }).value(), 0, 1));

    // <percentage>
    if (specified_value->is_percentage())
        return NumberStyleValue::create(clamp(specified_value->as_percentage().percentage().as_fraction(), 0, 1));

    // NOTE: We also support calc()'d percentages
    if (specified_value->is_calculated() && specified_value->as_calculated().resolves_to_percentage())
        return NumberStyleValue::create(clamp(specified_value->as_calculated().resolve_percentage({ .length_resolution_context = computation_context.length_resolution_context })->as_fraction(), 0, 1));

    VERIFY_NOT_REACHED();
}

void StyleComputer::compute_math_depth(ComputedProperties& style, Optional<DOM::AbstractElement> element) const
{
    // https://w3c.github.io/mathml-core/#propdef-math-depth

    auto element_to_inherit_style_from = element.has_value() ? element->element_to_inherit_style_from() : OptionalNone {};

    auto inherited_math_depth = [&]() {