dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / style_values / calculated_style_value_9.lucb
#==============================================================================================
#
# calculated_style_value_9 - CalculatedStyleValue: simplifying a calculation tree
#
# DESCRIPTION:
# Port of LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp (lines 3213-3741):
# find_numeric_child_with_same_unit, make_calculation_node and "simplify a calculation
# tree" (css-values-4), split into one function per step that needs more than a few lines:
# resolving a numeric leaf, the partial simplification of min()/max(), Negate, Invert, Sum
# and Product nodes. Each step function is the donor's step body; the root function
# keeps the donor's order of steps.
#
# 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: Helpers =================================================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:3219 Web::CSS::find_numeric_child_with_same_unit.
func css_find_numeric_child_with_same_unit(children: const ak.Vector[CssCalculationNode*]*, target: CssNumericCalculationNode*) -> CssNumericChildAndIndex?:
for i in 0..<ak.vector_size(children):
let child = *ak.vector_at_const(children, i)
if css_calculation_node_type(child) != CssCalculationNodeType.numeric:
continue
let child_numeric = as_css_numeric_calculation_node(child)
if not css_numeric_values_have_same_alternative(&child_numeric.m_value, &target.m_value):
continue
var matches = false
match child_numeric.m_value:
.css_percentage(_): matches = css_numeric_value_is_percentage(&target.m_value)
.number(_): matches = css_numeric_value_is_number(&target.m_value)
_: matches = css_numeric_values_have_same_unit(&child_numeric.m_value, &target.m_value)
if matches:
return CssNumericChildAndIndex(child = child_numeric, index = i)
return none
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:3250 Web::CSS::make_calculation_node.
func css_make_calculation_node(calculation_result: CssCalculatedStyleValueCalculationResult, context: CssCalculationContext) -> CssNumericCalculationNode*?:
let accumulated_type = calculation_result.m_type else trap("VERIFY: has_value")
let value = calculation_result.m_value
if css_numeric_type_matches_number(&accumulated_type, context.percentages_resolve_as):
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.number(css_syntax.number_create(css_syntax.NumberType.number, value)), context)
if css_numeric_type_matches_percentage(&accumulated_type):
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_percentage(css_percentage_new(value)), context)
if css_numeric_type_matches_angle(&accumulated_type, context.percentages_resolve_as):
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_angle(css_angle_make_degrees(value)), context)
if css_numeric_type_matches_flex(&accumulated_type, context.percentages_resolve_as):
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_flex(css_flex_make_fr(value)), context)
if css_numeric_type_matches_frequency(&accumulated_type, context.percentages_resolve_as):
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_frequency(css_frequency_make_hertz(value)), context)
if css_numeric_type_matches_length(&accumulated_type, context.percentages_resolve_as):
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_length(css_length_make_px_f64(value)), context)
if css_numeric_type_matches_resolution(&accumulated_type, context.percentages_resolve_as):
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_resolution(css_resolution_make_dots_per_pixel(value)), context)
if css_numeric_type_matches_time(&accumulated_type, context.percentages_resolve_as):
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_time(css_time_make_seconds(value)), context)
return none
# mark: The steps of simplify_a_calculation_tree ================================================
## Step 1 of simplify_a_calculation_tree for a numeric root: a percentage resolved against the percentage basis, a
## dimension converted to its canonical unit, or none ("use the original").
func css_simplify_numeric_root(root_numeric: CssNumericCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*?:
# 1. If root is a percentage that will be resolved against another value, and there is enough information
# available to resolve it, do so, and express the resulting numeric value in the appropriate canonical unit.
# Return the value.
if let percentage = css_numeric_value_percentage(&root_numeric.m_value):
if context.percentages_resolve_as != none:
# NOTE: We use nullptr here to signify "use the original".
match resolution_context.percentage_basis:
.empty:
return none
.css_angle(angle):
assert(context.percentages_resolve_as == CssValueType.angle)
if css_angle_unit(&angle) == css_data.AngleUnit.deg:
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_angle(css_angle_percentage_of(&angle, percentage)), context).css_calculation_node()
let degrees = css_angle_make_degrees(css_angle_to_degrees(&angle))
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_angle(css_angle_percentage_of(°rees, percentage)), context).css_calculation_node()
.css_frequency(frequency):
assert(context.percentages_resolve_as == CssValueType.frequency)
if css_frequency_unit(&frequency) == css_data.FrequencyUnit.hz:
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_frequency(css_frequency_percentage_of(&frequency, percentage)), context).css_calculation_node()
let hertz = css_frequency_make_hertz(css_frequency_to_hertz(&frequency))
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_frequency(css_frequency_percentage_of(&hertz, percentage)), context).css_calculation_node()
.css_length(length):
assert(context.percentages_resolve_as == CssValueType.length)
if css_length_unit(&length) == css_data.LengthUnit.px:
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_length(css_length_percentage_of(&length, percentage)), context).css_calculation_node()
if css_length_is_absolute(&length):
let px = css_length_make_px_css_pixels(css_length_absolute_length_to_px(&length))
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_length(css_length_percentage_of(&px, percentage)), context).css_calculation_node()
if let length_resolution_context = resolution_context.length_resolution_context:
let px = css_length_make_px_f64(css_length_to_px_without_rounding(&length, length_resolution_context))
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_length(css_length_percentage_of(&px, percentage)), context).css_calculation_node()
return none
.css_time(time):
assert(context.percentages_resolve_as == CssValueType.time)
if css_time_unit(&time) == css_data.TimeUnit.s:
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_time(css_time_percentage_of(&time, percentage)), context).css_calculation_node()
let seconds = css_time_make_seconds(css_time_to_seconds(&time))
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_time(css_time_percentage_of(&seconds, percentage)), context).css_calculation_node()
# 2. If root is a dimension that is not expressed in its canonical unit, and there is enough information available
# to convert it to the canonical unit, do so, and return the value.
# NOTE: We use nullptr here to signify "use the original".
match root_numeric.m_value:
.css_angle(angle):
if css_angle_unit(&angle) == css_data.AngleUnit.deg:
return none
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_angle(css_angle_make_degrees(css_angle_to_degrees(&angle))), context).css_calculation_node()
.css_flex(flex):
if css_flex_unit(&flex) == css_data.FlexUnit.fr:
return none
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_flex(css_flex_make_fr(css_flex_to_fr(&flex))), context).css_calculation_node()
.css_frequency(frequency):
if css_frequency_unit(&frequency) == css_data.FrequencyUnit.hz:
return none
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_frequency(css_frequency_make_hertz(css_frequency_to_hertz(&frequency))), context).css_calculation_node()
.css_length(length):
if css_length_unit(&length) == css_data.LengthUnit.px:
return none
if css_length_is_absolute(&length):
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_length(css_length_make_px_f64(css_length_absolute_length_to_px_without_rounding(&length))), context).css_calculation_node()
if let length_resolution_context = resolution_context.length_resolution_context:
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_length(css_length_make_px_f64(css_length_to_px_without_rounding(&length, length_resolution_context))), context).css_calculation_node()
return none
.number(_):
return none
.css_percentage(_):
return none
.css_resolution(resolution):
if css_resolution_unit(&resolution) == css_data.ResolutionUnit.dppx:
return none
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_resolution(css_resolution_make_dots_per_pixel(css_resolution_to_dots_per_pixel(&resolution))), context).css_calculation_node()
.css_time(time):
if css_time_unit(&time) == css_data.TimeUnit.s:
return none
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_time(css_time_make_seconds(css_time_to_seconds(&time))), context).css_calculation_node()
## Whether `child_numeric` should replace `existing` in min()/max()'s partial simplification (the `visit` of step 5).
func css_should_replace_existing_value(existing: CssNumericCalculationNode*, child_numeric: CssNumericCalculationNode*, is_min: bool) -> bool:
match existing.m_value:
.css_percentage(percentage):
let child_percentage = css_numeric_value_percentage(&child_numeric.m_value) else trap("get_pointer<Percentage>: null")
let child_value = css_percentage_value(&child_percentage)
if is_min:
return child_value < css_percentage_value(&percentage)
return child_value > css_percentage_value(&percentage)
.number(number):
let child_number = css_numeric_value_number(&child_numeric.m_value) else trap("get_pointer<Number>: null")
let child_value = css_syntax.number_value(&child_number)
if is_min:
return child_value < css_syntax.number_value(&number)
return child_value > css_syntax.number_value(&number)
_:
let child_value = css_numeric_value_dimension_raw_value(&child_numeric.m_value)
if is_min:
return child_value < css_numeric_value_dimension_raw_value(&existing.m_value)
return child_value > css_numeric_value_dimension_raw_value(&existing.m_value)
## Step 5 of simplify_a_calculation_tree: attempt to partially simplify a Min or Max root.
func css_simplify_min_or_max_root(root: CssCalculationNode*, context: CssCalculationContext) -> CssCalculationNode*:
let is_min = css_calculation_node_type(root) == CssCalculationNodeType.min
let children = css_calculation_node_children(as_css_min_calculation_node(root).css_calculation_node()) if is_min else css_calculation_node_children(as_css_max_calculation_node(root).css_calculation_node())
# 1. For each node child of root’s children:
# If child is a numeric value with enough information to compare magnitudes with another child of the same
# unit (see note in previous step), and there are other children of root that are numeric values with the
# same unit, combine all such children with the appropriate operator per root, and replace child with the
# result, removing all other child nodes involved.
var simplified_children = ak.Vector[CssCalculationNode*]()
ak.vector_ensure_capacity(&simplified_children, ak.vector_size(&children))
for child in ak.vector_span_const(&children):
if css_calculation_node_type(child) != CssCalculationNodeType.numeric or ak.vector_is_empty(&simplified_children):
ak.vector_append_t(&simplified_children, child)
continue
let child_numeric = as_css_numeric_calculation_node(child)
if context.percentages_resolve_as != none and css_numeric_value_is_percentage(&child_numeric.m_value):
# NOTE: We can't compare this percentage yet.
ak.vector_append_t(&simplified_children, child)
continue
if let existing_child_and_index = css_find_numeric_child_with_same_unit(&simplified_children, child_numeric):
let should_replace_existing_value = css_should_replace_existing_value(existing_child_and_index.child, child_numeric, is_min)
if should_replace_existing_value:
*ak.vector_at(&simplified_children, existing_child_and_index.index) = child_numeric.css_calculation_node()
else:
ak.vector_append_t(&simplified_children, child)
# 2. If root has only one child, return the child.
# Otherwise, return root.
if ak.vector_size(&simplified_children) == 1:
return *ak.vector_first(&simplified_children)
# NOTE: Because our root is immutable, we have to return a new node with the modified children.
if is_min:
return css_min_calculation_node_create(simplified_children).css_calculation_node()
return css_max_calculation_node_create(simplified_children).css_calculation_node()
## Step 6 of simplify_a_calculation_tree: a Negate root.
func css_simplify_negate_root(root: CssCalculationNode*, context: CssCalculationContext) -> CssCalculationNode*:
let root_negate = as_css_negate_calculation_node(root)
let child = css_negate_calculation_node_child(root_negate)
# 1. If root’s child is a numeric value, return an equivalent numeric value, but with the value negated (0 - value).
if css_calculation_node_type(child) == CssCalculationNodeType.numeric:
return css_numeric_calculation_node_negated(as_css_numeric_calculation_node(child), context).css_calculation_node()
# 2. If root’s child is a Negate node, return the child’s child.
if css_calculation_node_type(child) == CssCalculationNodeType.negate:
return css_negate_calculation_node_child(as_css_negate_calculation_node(child))
# AD-HOC: Convert negated sums into sums of negated nodes - see https://github.com/w3c/csswg-drafts/issues/13020
if css_calculation_node_type(child) == CssCalculationNodeType.sum:
var negated_sum_components = ak.Vector[CssCalculationNode*]()
let sum_children = css_calculation_node_children(child)
for sum_child in ak.vector_span_const(&sum_children):
if css_calculation_node_type(sum_child) == CssCalculationNodeType.numeric:
ak.vector_append_t(&negated_sum_components, css_numeric_calculation_node_negated(as_css_numeric_calculation_node(sum_child), context).css_calculation_node())
elif css_calculation_node_type(sum_child) == CssCalculationNodeType.negate:
ak.vector_append_t(&negated_sum_components, css_negate_calculation_node_child(as_css_negate_calculation_node(sum_child)))
else:
ak.vector_append_t(&negated_sum_components, css_negate_calculation_node_create(sum_child).css_calculation_node())
return css_sum_calculation_node_create(negated_sum_components).css_calculation_node()
# 3. Return root.
# NOTE: Because our root is immutable, we have to return a new node if the child was modified.
if child == css_negate_calculation_node_child(root_negate):
return root
return css_negate_calculation_node_create(child).css_calculation_node()
## Step 7 of simplify_a_calculation_tree: an Invert root.
func css_simplify_invert_root(root: CssCalculationNode*, context: CssCalculationContext) -> CssCalculationNode*:
let root_invert = as_css_invert_calculation_node(root)
let child = css_invert_calculation_node_child(root_invert)
# 1. If root’s child is a number (not a percentage or dimension) return the reciprocal of the child’s value.
if css_calculation_node_type(child) == CssCalculationNodeType.numeric:
if let number = css_numeric_value_number(&as_css_numeric_calculation_node(child).m_value):
# TODO: Ensure we're doing the right thing for weird divisions.
return css_numeric_calculation_node_create(CssCalculationNodeNumericValue.number(css_syntax.number_create(css_syntax.NumberType.number, 1.0 / css_syntax.number_value(&number))), context).css_calculation_node()
# 2. If root’s child is an Invert node, return the child’s child.
if css_calculation_node_type(child) == CssCalculationNodeType.invert:
return css_invert_calculation_node_child(as_css_invert_calculation_node(child))
# 3. Return root.
# NOTE: Because our root is immutable, we have to return a new node if the child was modified.
if child == css_invert_calculation_node_child(root_invert):
return root
return css_invert_calculation_node_create(child).css_calculation_node()
## The sum of two numeric values with the same unit (the `visit` of step 8.2).
func css_numeric_values_summed(existing: const CssCalculationNodeNumericValue*, child: const CssCalculationNodeNumericValue*) -> CssCalculationNodeNumericValue:
match *existing:
.css_percentage(percentage):
let child_percentage = css_numeric_value_percentage(child) else trap("VERIFY: Variant::get<Percentage>")
return CssCalculationNodeNumericValue.css_percentage(css_percentage_new(css_percentage_value(&percentage) + css_percentage_value(&child_percentage)))
.number(number):
let child_number = css_numeric_value_number(child) else trap("VERIFY: Variant::get<Number>")
return CssCalculationNodeNumericValue.number(css_syntax.number_create(css_syntax.NumberType.number, css_syntax.number_value(&number) + css_syntax.number_value(&child_number)))
_:
assert(css_numeric_values_have_same_alternative(existing, child))
return css_numeric_value_dimension_with_raw_value(existing, css_numeric_value_dimension_raw_value(existing) + css_numeric_value_dimension_raw_value(child))
## Step 8 of simplify_a_calculation_tree: a Sum root.
func css_simplify_sum_root(root: CssCalculationNode*, context: CssCalculationContext) -> CssCalculationNode*:
let root_sum = as_css_sum_calculation_node(root)
let root_children = css_calculation_node_children(root_sum.css_calculation_node())
var flattened_children = ak.Vector[CssCalculationNode*]()
ak.vector_ensure_capacity(&flattened_children, ak.vector_size(&root_children))
# 1. For each of root’s children that are Sum nodes, replace them with their children.
for child in ak.vector_span_const(&root_children):
if css_calculation_node_type(child) == CssCalculationNodeType.sum:
let grandchildren = css_calculation_node_children(as_css_sum_calculation_node(child).css_calculation_node())
ak.vector_extend_2(&flattened_children, &grandchildren)
else:
ak.vector_append_t(&flattened_children, child)
# 2. For each set of root’s children that are numeric values with identical units, remove those children and
# replace them with a single numeric value containing the sum of the removed nodes, and with the same unit.
# (E.g. combine numbers, combine percentages, combine px values, etc.)
# NOTE: For each child, scan this summed_children list for the first one that has the same type, then replace that with the new summed value.
var summed_children = ak.Vector[CssCalculationNode*]()
for child in ak.vector_span_const(&flattened_children):
if css_calculation_node_type(child) != CssCalculationNodeType.numeric:
ak.vector_append_t(&summed_children, child)
continue
let child_numeric = as_css_numeric_calculation_node(child)
if let existing_child_and_index = css_find_numeric_child_with_same_unit(&summed_children, child_numeric):
let new_value = css_numeric_calculation_node_create(css_numeric_values_summed(&existing_child_and_index.child.m_value, &child_numeric.m_value), context)
*ak.vector_at(&summed_children, existing_child_and_index.index) = new_value.css_calculation_node()
else:
ak.vector_append_t(&summed_children, child)
# 3. If root has only a single child at this point, return the child. Otherwise, return root.
if ak.vector_size(&summed_children) == 1:
return *ak.vector_first(&summed_children)
# NOTE: Because our root is immutable, we have to return a new node with the modified children.
return css_sum_calculation_node_create(summed_children).css_calculation_node()
## A numeric value multiplied by a number (the `visit` of step 9.3).
func css_numeric_value_multiplied(value: const CssCalculationNodeNumericValue*, multiplier: f64) -> CssCalculationNodeNumericValue:
match *value:
.css_percentage(percentage):
return CssCalculationNodeNumericValue.css_percentage(css_percentage_new(css_percentage_value(&percentage) * multiplier))
.number(number):
return CssCalculationNodeNumericValue.number(css_syntax.number_create(css_syntax.NumberType.number, css_syntax.number_value(&number) * multiplier))
_:
return css_numeric_value_dimension_with_raw_value(value, css_numeric_value_dimension_raw_value(value) * multiplier)
## Step 9.4's `accumulate` lambda: fold one numeric child (inverted for an Invert node's child) into the accumulated
## result; false when the product cannot be computed.
func css_accumulate_product_child(accumulated_result: (CssCalculatedStyleValueCalculationResult?)*, resolution_context: const CssCalculationResolutionContext*, numeric_child: CssNumericCalculationNode*, invert: bool) -> bool:
var child_type = css_calculation_node_numeric_type(numeric_child.css_calculation_node())
if child_type == none:
return false
# FIXME: The spec doesn't cover how to handle values in non-canonical units
if not css_numeric_calculation_node_is_in_canonical_unit(numeric_child):
return false
# AD-HOC: The spec doesn't cover how to handle unresolved percentages, to handle this we force percentages
# back to the percent type (e.g. { hint: None, "percent" → 1 } rather than
# { hint: length, "length" → 1 }), this avoids a situation calling make_calculation_node below
# where we would treat the value as an absolute value expressed in canonical units rather than a
# percent. `make_calculation_node` will still calculate the correct numeric type for the
# simplified node. See spec issue: https://github.com/w3c/csswg-drafts/issues/11588
if css_numeric_value_is_percentage(&numeric_child.m_value):
child_type = css_numeric_type_new(.percent, 1)
var child_value = css_calculated_style_value_calculation_result_from_value(numeric_child.m_value, *resolution_context, child_type)
if invert:
css_calculated_style_value_calculation_result_invert(&child_value)
if let accumulated = *accumulated_result:
var product = accumulated
css_calculated_style_value_calculation_result_multiply_by(&product, &child_value)
*accumulated_result = product
else:
*accumulated_result = child_value
let result = *accumulated_result else trap("unreachable")
if result.m_type == none:
return false
return true
## Step 9 of simplify_a_calculation_tree: a Product root.
func css_simplify_product_root(root: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
let root_product = as_css_product_calculation_node(root)
let root_children = css_calculation_node_children(root_product.css_calculation_node())
var children = ak.Vector[CssCalculationNode*]()
ak.vector_ensure_capacity(&children, ak.vector_size(&root_children))
# 1. For each of root’s children that are Product nodes, replace them with their children.
for child in ak.vector_span_const(&root_children):
if css_calculation_node_type(child) == CssCalculationNodeType.product:
let grandchildren = css_calculation_node_children(as_css_product_calculation_node(child).css_calculation_node())
ak.vector_extend_2(&children, &grandchildren)
else:
ak.vector_append_t(&children, child)
# 2. If root has multiple children that are numbers (not percentages or dimensions),
# remove them and replace them with a single number containing the product of the removed nodes.
var number_index: usize? = none
var i: usize = 0
while i < ak.vector_size(&children):
let child = *ak.vector_at(&children, i)
if css_calculation_node_type(child) == CssCalculationNodeType.numeric:
if let number = css_numeric_value_number(&as_css_numeric_calculation_node(child).m_value):
if let first_index = number_index:
let first_number = css_numeric_value_number(&as_css_numeric_calculation_node(*ak.vector_at(&children, first_index)).m_value) else trap("VERIFY: Variant::get<Number>")
*ak.vector_at(&children, first_index) = css_numeric_calculation_node_create(CssCalculationNodeNumericValue.number(css_syntax.number_mul(&first_number, number)), context).css_calculation_node()
ak.vector_remove(&children, i)
# Look at this same index again next loop.
continue
number_index = i
i += 1
# 3. If root contains only two children, one of which is a number(not a percentage or dimension) and the other
# of which is a Sum whose children are all numeric values, multiply all of the Sum’ s children by the number,
# then return the Sum.
if ak.vector_size(&children) == 2:
let child_1 = *ak.vector_at(&children, 0)
let child_2 = *ak.vector_at(&children, 1)
var multiplier: css_syntax.Number? = none
var sum: CssSumCalculationNode*? = none
if css_calculation_node_type(child_1) == CssCalculationNodeType.numeric and css_calculation_node_type(child_2) == CssCalculationNodeType.sum:
if let maybe_multiplier = css_numeric_value_number(&as_css_numeric_calculation_node(child_1).m_value):
multiplier = maybe_multiplier
sum = as_css_sum_calculation_node(child_2)
if css_calculation_node_type(child_1) == CssCalculationNodeType.sum and css_calculation_node_type(child_2) == CssCalculationNodeType.numeric:
if let maybe_multiplier = css_numeric_value_number(&as_css_numeric_calculation_node(child_2).m_value):
multiplier = maybe_multiplier
sum = as_css_sum_calculation_node(child_1)
if let multiplier_number = multiplier:
if let sum_node = sum:
let sum_children = css_calculation_node_children(sum_node.css_calculation_node())
var multiplied_children = ak.Vector[CssCalculationNode*]()
ak.vector_ensure_capacity(&multiplied_children, ak.vector_size(&sum_children))
var all_numeric = true
for sum_child in ak.vector_span_const(&sum_children):
if css_calculation_node_type(sum_child) != CssCalculationNodeType.numeric:
all_numeric = false
break
let child_value = &as_css_numeric_calculation_node(sum_child).m_value
ak.vector_append_t(&multiplied_children, css_numeric_calculation_node_create(css_numeric_value_multiplied(child_value, css_syntax.number_value(&multiplier_number)), context).css_calculation_node())
if all_numeric:
return css_sum_calculation_node_create(multiplied_children).css_calculation_node()
# 4. If root contains only numeric values and/or Invert nodes containing numeric values, and multiplying the
# types of all the children (noting that the type of an Invert node is the inverse of its child’s type)
# results in a type that matches any of the types that a math function can resolve to, return the result of
# multiplying all the values of the children (noting that the value of an Invert node is the reciprocal of
# its child’s value), expressed in the result’s canonical unit.
var accumulated_result: CssCalculatedStyleValueCalculationResult? = none
var is_valid = true
for child in ak.vector_span_const(&children):
if css_calculation_node_type(child) == CssCalculationNodeType.numeric:
if not css_accumulate_product_child(&accumulated_result, &resolution_context, as_css_numeric_calculation_node(child), false):
is_valid = false
break
continue
if css_calculation_node_type(child) == CssCalculationNodeType.invert:
let invert_child = as_css_invert_calculation_node(child)
let inverted = css_invert_calculation_node_child(invert_child)
if css_calculation_node_type(inverted) != CssCalculationNodeType.numeric or not css_accumulate_product_child(&accumulated_result, &resolution_context, as_css_numeric_calculation_node(inverted), true):
is_valid = false
break
continue
is_valid = false
break
if is_valid:
if let node = css_make_calculation_node(accumulated_result else trap("VERIFY: has_value"), context):
return node.css_calculation_node()
# 5. Return root.
# NOTE: Because our root is immutable, we have to return a new node with the modified children.
return css_product_calculation_node_create(children).css_calculation_node()
# mark: simplify_a_calculation_tree =============================================================
## Ported from Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp:3274 Web::CSS::simplify_a_calculation_tree.
# https://drafts.csswg.org/css-values-4/#calc-simplification
pub func css_simplify_a_calculation_tree(original_root: CssCalculationNode*, context: CssCalculationContext, resolution_context: CssCalculationResolutionContext) -> CssCalculationNode*:
# To simplify a calculation tree root:
# FIXME: If needed, we could detect that nothing has changed and then return the original `root`, in more places.
var root = original_root
# 1. If root is a numeric value:
if css_calculation_node_type(root) == CssCalculationNodeType.numeric:
# 1. and 2.: resolve a percentage, or convert a dimension to its canonical unit.
if let resolved = css_simplify_numeric_root(as_css_numeric_calculation_node(root), context, resolution_context):
return resolved
# 3. If root is a <calc-keyword> that can be resolved, return what it resolves to, simplified.
# NOTE: We already resolve our `<calc-keyword>`s at parse-time.
# FIXME: Revisit this once we support any keywords that need resolving later.
# 4. Otherwise, return root.
return root
# 2. If root is any other leaf node (not an operator node):
if css_calculation_node_type(root) == CssCalculationNodeType.non_math_function:
# 1. If there is enough information available to determine its numeric value, return its value, expressed in
# the value’s canonical unit.
if let resolved_calculation_node = css_abstract_non_math_calc_function_style_value_resolve_to_calculation_node(as_css_non_math_function_calculation_node(root).m_function, context, resolution_context):
return resolved_calculation_node
# 2. Otherwise, return root.
return root
# 3. At this point, root is an operator node. Simplify all the calculation children of root.
root = css_calculation_node_with_simplified_children(root, context, resolution_context)
# 4. If root is an operator node that’s not one of the calc-operator nodes, and all of its calculation children
# are numeric values with enough information to compute the operation root represents, return the result of
# running root’s operation using its children, expressed in the result’s canonical unit.
if css_calculation_node_is_math_function_node(root):
if let maybe_simplified = css_calculation_node_run_operation_if_possible(root, context, resolution_context):
if let node = css_make_calculation_node(maybe_simplified, context):
return node.css_calculation_node()
return root
# 5. If root is a Min or Max node, attempt to partially simplify it:
if css_calculation_node_type(root) == CssCalculationNodeType.min or css_calculation_node_type(root) == CssCalculationNodeType.max:
return css_simplify_min_or_max_root(root, context)
# 6. If root is a Negate node:
if css_calculation_node_type(root) == CssCalculationNodeType.negate:
return css_simplify_negate_root(root, context)
# 7. If root is an Invert node:
if css_calculation_node_type(root) == CssCalculationNodeType.invert:
return css_simplify_invert_root(root, context)
# 8. If root is a Sum node:
if css_calculation_node_type(root) == CssCalculationNodeType.sum:
return css_simplify_sum_root(root, context)
# 9. If root is a Product node:
if css_calculation_node_type(root) == CssCalculationNodeType.product:
return css_simplify_product_root(root, context, resolution_context)
# 10. Return root.
return root