dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / style_values / calculated_style_value_1.lucb
#==============================================================================================
#
# calculated_style_value_1 - CalculatedStyleValue: type arithmetic and simplification helpers
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp (lines 1-228) and the inline
# members of CalculatedStyleValue.h they belong with: CalculationContext::for_property,
# "add the types" / "multiply the types", the simplify_* templates (which take the node
# class's `create` as a function pointer, since a generic body cannot name T::create),
# clamping and censoring of numeric values and reify_children.
# The `visit`s over CalculationNode::NumericValue (a Variant of Number, Angle, Flex,
# Frequency, Length, Percentage, Resolution and Time) are the css_numeric_value_* helpers
# at the top.
#
# 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: Visiting a NumericValue =================================================================
## `value.visit([&](auto const& value) { value.serialize(builder, mode); })`.
pub func css_numeric_value_serialize(value: const CssCalculationNodeNumericValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode = css_syntax.SerializationMode.normal):
match *value:
.number(number): css_syntax.number_serialize(&number, builder, mode)
.css_angle(angle): css_angle_serialize(&angle, builder, mode)
.css_flex(flex): css_flex_serialize(&flex, builder, mode)
.css_frequency(frequency): css_frequency_serialize(&frequency, builder, mode)
.css_length(length): css_length_serialize(&length, builder, mode)
.css_percentage(percentage): css_percentage_serialize(&percentage, builder, mode)
.css_resolution(resolution): css_resolution_serialize(&resolution, builder, mode)
.css_time(time): css_time_serialize(&time, builder, mode)
## `value.visit([](auto& it) { return it.to_string(); })`.
pub func css_numeric_value_to_string(value: const CssCalculationNodeNumericValue*) -> ak.String:
match *value:
.number(number): return css_syntax.number_to_string(&number)
.css_angle(angle): return css_angle_to_string(&angle)
.css_flex(flex): return css_flex_to_string(&flex)
.css_frequency(frequency): return css_frequency_to_string(&frequency)
.css_length(length): return css_length_to_string(&length)
.css_percentage(percentage): return css_percentage_to_string(&percentage)
.css_resolution(resolution): return css_resolution_to_string(&resolution)
.css_time(time): return css_time_to_string(&time)
## `NumericValue == NumericValue` (Variant's operator==: the same alternative, and equal values).
pub func css_numeric_values_equal(a: const CssCalculationNodeNumericValue*, b: const CssCalculationNodeNumericValue*) -> bool:
match *a:
.number(number):
match *b:
.number(other): return css_syntax.number_eq(&number, other)
_: return false
.css_angle(angle):
match *b:
.css_angle(other): return css_angle_eq(&angle, other)
_: return false
.css_flex(flex):
match *b:
.css_flex(other): return css_flex_eq(&flex, other)
_: return false
.css_frequency(frequency):
match *b:
.css_frequency(other): return css_frequency_eq(&frequency, other)
_: return false
.css_length(length):
match *b:
.css_length(other): return css_length_eq(&length, other)
_: return false
.css_percentage(percentage):
match *b:
.css_percentage(other): return css_percentage_eq(&percentage, other)
_: return false
.css_resolution(resolution):
match *b:
.css_resolution(other): return css_resolution_eq(&resolution, other)
_: return false
.css_time(time):
match *b:
.css_time(other): return css_time_eq(&time, other)
_: return false
## `value.has<Number>()`.
pub func css_numeric_value_is_number(value: const CssCalculationNodeNumericValue*) -> bool:
match *value:
.number(_): return true
_: return false
## `value.has<Percentage>()`.
pub func css_numeric_value_is_percentage(value: const CssCalculationNodeNumericValue*) -> bool:
match *value:
.css_percentage(_): return true
_: return false
## `value.get_pointer<Number>()`, as a copy.
pub func css_numeric_value_number(value: const CssCalculationNodeNumericValue*) -> css_syntax.Number?:
match *value:
.number(number): return number
_: return none
## `value.get_pointer<Percentage>()`, as a copy.
pub func css_numeric_value_percentage(value: const CssCalculationNodeNumericValue*) -> CssPercentage?:
match *value:
.css_percentage(percentage): return percentage
_: return none
## The `raw_value()` of a dimension alternative (`[](auto const& dimension) { return dimension.raw_value(); }`); traps
## on a number or a percentage.
pub func css_numeric_value_dimension_raw_value(value: const CssCalculationNodeNumericValue*) -> f64:
match *value:
.css_angle(angle): return css_angle_raw_value(&angle)
.css_flex(flex): return css_flex_raw_value(&flex)
.css_frequency(frequency): return css_frequency_raw_value(&frequency)
.css_length(length): return css_length_raw_value(&length)
.css_resolution(resolution): return css_resolution_raw_value(&resolution)
.css_time(time): return css_time_raw_value(&time)
_: trap("VERIFY_NOT_REACHED: not a dimension")
## The `unit_name()` of a dimension alternative; traps on a number or a percentage.
pub func css_numeric_value_dimension_unit_name(value: const CssCalculationNodeNumericValue*) -> ak.FlyString:
match *value:
.css_angle(angle): return css_angle_unit_name(&angle)
.css_flex(flex): return css_flex_unit_name(&flex)
.css_frequency(frequency): return css_frequency_unit_name(&frequency)
.css_length(length): return css_length_unit_name(&length)
.css_resolution(resolution): return css_resolution_unit_name(&resolution)
.css_time(time): return css_time_unit_name(&time)
_: trap("VERIFY_NOT_REACHED: not a dimension")
## `T(raw_value, value.unit())` for the dimension alternative T of `value` (the generic lambdas that rebuild a dimension
## with a new raw value in the same unit); traps on a number or a percentage.
pub func css_numeric_value_dimension_with_raw_value(value: const CssCalculationNodeNumericValue*, raw_value: f64) -> CssCalculationNodeNumericValue:
match *value:
.css_angle(angle): return CssCalculationNodeNumericValue.css_angle(css_angle_new(raw_value, css_angle_unit(&angle)))
.css_flex(flex): return CssCalculationNodeNumericValue.css_flex(css_flex_new(raw_value, css_flex_unit(&flex)))
.css_frequency(frequency): return CssCalculationNodeNumericValue.css_frequency(css_frequency_new(raw_value, css_frequency_unit(&frequency)))
.css_length(length): return CssCalculationNodeNumericValue.css_length(css_length_new(raw_value, css_length_unit(&length)))
.css_resolution(resolution): return CssCalculationNodeNumericValue.css_resolution(css_resolution_new(raw_value, css_resolution_unit(&resolution)))
.css_time(time): return CssCalculationNodeNumericValue.css_time(css_time_new(raw_value, css_time_unit(&time)))
_: trap("VERIFY_NOT_REACHED: not a dimension")
## `a.index() == b.index()`: whether two NumericValues hold the same alternative.
pub func css_numeric_values_have_same_alternative(a: const CssCalculationNodeNumericValue*, b: const CssCalculationNodeNumericValue*) -> bool:
match *a:
.number(_):
match *b:
.number(_): return true
_: return false
.css_angle(_):
match *b:
.css_angle(_): return true
_: return false
.css_flex(_):
match *b:
.css_flex(_): return true
_: return false
.css_frequency(_):
match *b:
.css_frequency(_): return true
_: return false
.css_length(_):
match *b:
.css_length(_): return true
_: return false
.css_percentage(_):
match *b:
.css_percentage(_): return true
_: return false
.css_resolution(_):
match *b:
.css_resolution(_): return true
_: return false
.css_time(_):
match *b:
.css_time(_): return true
_: return false
## Whether two dimension alternatives have the same unit (`other->unit() == value.unit()`); false for numbers and
## percentages.
func css_numeric_values_have_same_unit(a: const CssCalculationNodeNumericValue*, b: const CssCalculationNodeNumericValue*) -> bool:
match *a:
.css_angle(angle):
match *b:
.css_angle(other): return css_angle_unit(&other) == css_angle_unit(&angle)
_: return false
.css_flex(flex):
match *b:
.css_flex(other): return css_flex_unit(&other) == css_flex_unit(&flex)
_: return false
.css_frequency(frequency):
match *b:
.css_frequency(other): return css_frequency_unit(&other) == css_frequency_unit(&frequency)
_: return false
.css_length(length):
match *b:
.css_length(other): return css_length_unit(&other) == css_length_unit(&length)
_: return false
.css_resolution(resolution):
match *b:
.css_resolution(other): return css_resolution_unit(&other) == css_resolution_unit(&resolution)
_: return false
.css_time(time):
match *b:
.css_time(other): return css_time_unit(&other) == css_time_unit(&time)
_: return false
_: return false
# mark: CalculationContext and type arithmetic ==================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:41 Web::CSS::CalculationContext::for_property.
pub func css_calculation_context_for_property(property: CssPropertyNameAndId) -> CssCalculationContext:
# FIXME: Handle registered custom properties, which may limit which types they accept.
return CssCalculationContext(
percentages_resolve_as = css_property_resolves_percentages_relative_to(css_property_name_and_id_id(&property)),
resolve_numbers_as_integers = css_property_accepts_type(css_property_name_and_id_id(&property), CssValueType.integer),
accepted_ranges_by_type = css_property_accepted_ranges_by_value_type(css_property_name_and_id_id(&property)),
)
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:51 Web::CSS::add_the_types.
func css_add_the_types_vector(nodes: const ak.Vector[CssCalculationNode*]*) -> CssNumericType?:
var left_type: CssNumericType? = none
for value in ak.vector_span_const(nodes):
let right_type = css_calculation_node_numeric_type(value) else return none
if let left = left_type:
left_type = css_numeric_type_added_to(&left, right_type)
else:
left_type = right_type
if left_type == none:
return none
return left_type
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:72 Web::CSS::add_the_types.
func css_add_the_types_css_calculation_node_css_calculation_node(a: CssCalculationNode*, b: CssCalculationNode*) -> CssNumericType?:
let a_type = css_calculation_node_numeric_type(a) else return none
let b_type = css_calculation_node_numeric_type(b) else return none
return css_numeric_type_added_to(&a_type, b_type)
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:81 Web::CSS::add_the_types.
func css_add_the_types_css_calculation_node_css_calculation_node_css_calculation_node(a: CssCalculationNode*, b: CssCalculationNode*, c_: CssCalculationNode*) -> CssNumericType?:
let a_type = css_calculation_node_numeric_type(a) else return none
let b_type = css_calculation_node_numeric_type(b) else return none
let c_type = css_calculation_node_numeric_type(c_) else return none
let a_and_b_type = css_numeric_type_added_to(&a_type, b_type) else return none
return css_numeric_type_added_to(&a_and_b_type, c_type)
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:96 Web::CSS::multiply_the_types.
func css_multiply_the_types(nodes: const ak.Vector[CssCalculationNode*]*) -> CssNumericType?:
# At a * sub-expression, multiply the types of the left and right arguments.
# The sub-expression’s type is the returned result.
var left_type: CssNumericType? = none
for value in ak.vector_span_const(nodes):
let right_type = css_calculation_node_numeric_type(value) else return none
if let left = left_type:
left_type = css_numeric_type_multiplied_by(&left, right_type)
else:
left_type = right_type
if left_type == none:
return none
return left_type
# mark: Simplifying children ====================================================================
## `T::create(Vector<...>)` of a node class with a list of children.
type CssCreateFromChildren = func(ak.Vector[CssCalculationNode*]) -> CssCalculationNode*
## `T::create(child)` of a node class with one child.
type CssCreateFromChild = func(CssCalculationNode*) -> CssCalculationNode*
## `T::create(child_1, child_2)` of a node class with two children.
type CssCreateFromTwoChildren = func(CssCalculationNode*, CssCalculationNode*) -> CssCalculationNode*
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:120 Web::CSS::simplify_children_vector.
func css_simplify_children_vector(original: CssCalculationNode*, create: CssCreateFromChildren, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
let children = css_calculation_node_children(original)
var simplified_children = ak.Vector[CssCalculationNode*]()
ak.vector_ensure_capacity(&simplified_children, ak.vector_size(&children))
var any_changed = false
for child in ak.vector_span_const(&children):
let simplified = css_simplify_a_calculation_tree(child, context, resolution_context)
if simplified != child:
any_changed = true
ak.vector_append_t(&simplified_children, simplified)
if any_changed:
return create(simplified_children)
return original
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:139 Web::CSS::simplify_child.
func css_simplify_child(original: CssCalculationNode*, create: CssCreateFromChild, child: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
let simplified = css_simplify_a_calculation_tree(child, context, resolution_context)
if simplified != child:
return create(simplified)
return original
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:148 Web::CSS::simplify_2_children.
func css_simplify_2_children(original: CssCalculationNode*, create: CssCreateFromTwoChildren, child_1: CssCalculationNode*, child_2: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
let simplified_1 = css_simplify_a_calculation_tree(child_1, context, resolution_context)
let simplified_2 = css_simplify_a_calculation_tree(child_2, context, resolution_context)
if simplified_1 != child_1 or simplified_2 != child_2:
return create(simplified_1, simplified_2)
return original
# mark: Clamping and censoring ==================================================================
## The accepted range for `value`'s type (the first `visit` of clamp_and_censor_numeric_value).
func css_accepted_range_for_numeric_value(value: const CssCalculationNodeNumericValue*, context: const CssCalculationContext*) -> CssNumericRange?:
var value_type = CssValueType.number
match *value:
.number(_): value_type = CssValueType.integer if context.resolve_numbers_as_integers else CssValueType.number
.css_angle(_): value_type = CssValueType.angle
.css_flex(_): value_type = CssValueType.flex
.css_frequency(_): value_type = CssValueType.frequency
.css_length(_): value_type = CssValueType.length
.css_percentage(_): value_type = CssValueType.percentage
.css_resolution(_): value_type = CssValueType.resolution
.css_time(_): value_type = CssValueType.time
if let range = ak.hash_map_get_k_const(&context.accepted_ranges_by_type, value_type):
return *range
return none
## `Optional<NumericType>` formatted as AK formats it: the type's dump, or "None".
func css_optional_numeric_type_to_string(type_: CssNumericType?) -> ak.String:
if let value = type_:
return css_numeric_type_dump(&value)
return ak.string_from_str("None")
## clamp_and_censor_numeric_value's `clamp_and_censor` lambda.
func css_clamp_and_censor(value: f64, min: f64, max: f64) -> f64:
var result = value
# https://drafts.csswg.org/css-values/#calc-ieee
# NaN does not escape a top-level calculation; it’s censored into a zero value.
if math.is_nan(result):
result = 0.0
# https://drafts.csswg.org/css-values/#calc-range
# the value resulting from a top-level calculation must be clamped to the range allowed in the target context.
# Clamping is performed on computed values to the extent possible, and also on used values if computation was
# unable to sufficiently simplify the expression to allow range-checking.
return ak.clamp_f64(result, min, max)
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:157 Web::CSS::clamp_and_censor_numeric_value.
func css_clamp_and_censor_numeric_value(node: CssNumericCalculationNode*, context: CssCalculationContext) -> CssCalculationNodeNumericValue:
let value = css_numeric_calculation_node_value(node)
var accepted_range = css_accepted_range_for_numeric_value(&value, &context)
if accepted_range == none:
if css_libweb_css_debug:
let dumped = css_optional_numeric_type_to_string(css_calculation_node_numeric_type(node.css_calculation_node()))
ak.dbgln(f"FIXME: Calculation context missing accepted range {ak.string_view_text(ak.string_bytes_as_string_view(&dumped))}")
# FIXME: Min and max values for Integer should be based on i32 rather than float
accepted_range = CssNumericRange(min = (f64)ak.f32_lowest, max = (f64)ak.f32_max)
let range = accepted_range else trap("unreachable")
match value:
.number(number):
let raw_value = f64(css_syntax.number_integer_value(&number)) if context.resolve_numbers_as_integers else css_syntax.number_value(&number)
return CssCalculationNodeNumericValue.number(css_syntax.number_create(css_syntax.number_type(&number), css_clamp_and_censor(raw_value, range.min, range.max)))
.css_angle(angle):
return CssCalculationNodeNumericValue.css_angle(css_angle_new(css_clamp_and_censor(css_angle_raw_value(&angle), range.min, range.max), css_angle_unit(&angle)))
.css_flex(flex):
return CssCalculationNodeNumericValue.css_flex(css_flex_new(css_clamp_and_censor(css_flex_raw_value(&flex), range.min, range.max), css_flex_unit(&flex)))
.css_frequency(frequency):
return CssCalculationNodeNumericValue.css_frequency(css_frequency_new(css_clamp_and_censor(css_frequency_raw_value(&frequency), range.min, range.max), css_frequency_unit(&frequency)))
.css_length(length):
return CssCalculationNodeNumericValue.css_length(css_length_new(css_clamp_and_censor(css_length_raw_value(&length), range.min, range.max), css_length_unit(&length)))
.css_percentage(percentage):
return CssCalculationNodeNumericValue.css_percentage(css_percentage_new(css_clamp_and_censor(css_percentage_value(&percentage), range.min, range.max)))
.css_resolution(resolution):
return CssCalculationNodeNumericValue.css_resolution(css_resolution_new(css_clamp_and_censor(css_resolution_raw_value(&resolution), range.min, range.max), css_resolution_unit(&resolution)))
.css_time(time):
return CssCalculationNodeNumericValue.css_time(css_time_new(css_clamp_and_censor(css_time_raw_value(&time), range.min, range.max), css_time_unit(&time)))
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:217 Web::CSS::reify_children.
func css_reify_children(realm: Realm*, children: const (CssCalculationNode*)[]) -> CssNumericArray*?:
var reified_children = ak.Vector[CssNumericValue*]()
for child in children:
let reified_child = css_calculation_node_reify((CssCalculationNode*)child, realm) else return none
ak.vector_append_t(&reified_children, reified_child)
return css_numeric_array_create(realm, reified_children)