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

LibWeb: Implement CSS hypot()

This commit is contained in:
stelar7 2023-05-28 11:43:04 +02:00 committed by Sam Atkins
parent 0d30fb5a6e
commit fa37bb8b76
4 changed files with 145 additions and 0 deletions

View file

@ -3844,6 +3844,53 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_sqrt_function(Function const& fun
return TRY(SqrtCalculationNode::create(node.release_nonnull()));
}
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_hypot_function(Function const& function)
{
TokenStream stream { function.values() };
auto parameters = parse_a_comma_separated_list_of_component_values(stream);
if (parameters.size() == 0) {
dbgln_if(CSS_PARSER_DEBUG, "hypot() must have at least 1 parameter"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, "hypot() parameters must be valid calculations"sv);
return nullptr;
}
auto maybe_parameter_type = calculation_node->resolved_type();
if (!maybe_parameter_type.has_value()) {
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for hypot() parameter #{}"sv, calculated_parameters.size() + 1);
return nullptr;
}
auto resolved_type = maybe_parameter_type.value();
if (first) {
type = resolved_type;
first = false;
}
if (resolved_type != type) {
dbgln_if(CSS_PARSER_DEBUG, "hypot() parameters must all be of the same type"sv);
return nullptr;
}
calculated_parameters.append(calculation_node.release_nonnull());
}
return TRY(HypotCalculationNode::create(move(calculated_parameters)));
}
ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value)
{
if (component_value.is_function()) {
@ -3915,6 +3962,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
if (function.name().equals_ignoring_ascii_case("sqrt"sv))
return TRY(parse_sqrt_function(function));
if (function.name().equals_ignoring_ascii_case("hypot"sv))
return TRY(parse_hypot_function(function));
dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name());
return nullptr;
}