1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:38:11 +00:00

LibWeb: Parse clamp() css math function

This commit is contained in:
stelar7 2023-05-26 15:40:39 +02:00 committed by Andreas Kling
parent eaee0ecd88
commit 2ef6aa5f3d
4 changed files with 145 additions and 0 deletions

View file

@ -3512,6 +3512,45 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_max_function(Function const& func
return TRY(MaxCalculationNode::create(move(calculated_parameters)));
}
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_clamp_function(Function const& function)
{
TokenStream stream { function.values() };
auto parameters = parse_a_comma_separated_list_of_component_values(stream);
if (parameters.size() != 3) {
dbgln_if(CSS_PARSER_DEBUG, "clamp() must have exactly three parameters"sv);
return nullptr;
}
Vector<NonnullOwnPtr<CalculationNode>> calculated_parameters;
calculated_parameters.ensure_capacity(parameters.size());
CalculatedStyleValue::ResolvedType type;
bool first = true;
for (auto& parameter : parameters) {
auto calculation_node = TRY(parse_a_calculation(parameter));
if (!calculation_node) {
dbgln_if(CSS_PARSER_DEBUG, "clamp() parameters must be valid calculations"sv);
return nullptr;
}
if (first) {
type = calculation_node->resolved_type().value();
first = false;
}
if (calculation_node->resolved_type().value() != type) {
dbgln_if(CSS_PARSER_DEBUG, "clamp() parameters must all be of same type"sv);
return nullptr;
}
calculated_parameters.append(calculation_node.release_nonnull());
}
return TRY(ClampCalculationNode::create(move(calculated_parameters[0]), move(calculated_parameters[1]), move(calculated_parameters[2])));
}
ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value)
{
if (component_value.is_function()) {
@ -3544,6 +3583,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
if (function.name().equals_ignoring_ascii_case("max"sv))
return TRY(parse_max_function(function));
if (function.name().equals_ignoring_ascii_case("clamp"sv))
return TRY(parse_clamp_function(function));
dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name());
return nullptr;
}