mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 08:48:11 +00:00
LibWeb: Implement CSS round()
This commit is contained in:
parent
ec0054da06
commit
b2230c826b
4 changed files with 210 additions and 0 deletions
|
@ -3963,6 +3963,74 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_exp_function(Function const& func
|
|||
return TRY(ExpCalculationNode::create(node_a.release_nonnull()));
|
||||
}
|
||||
|
||||
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_round_function(Function const& function)
|
||||
{
|
||||
TokenStream stream { function.values() };
|
||||
auto parameters = parse_a_comma_separated_list_of_component_values(stream);
|
||||
|
||||
if (parameters.size() != 2 && parameters.size() != 3) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "round() must have exactly two or three parameters"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
OwnPtr<CalculationNode> node_a = nullptr;
|
||||
OwnPtr<CalculationNode> node_b = nullptr;
|
||||
auto mode = CalculationNode::RoundingMode::Nearest;
|
||||
if (parameters.size() == 3) {
|
||||
auto rounding_mode_component = parameters[0][0];
|
||||
if (!rounding_mode_component.is(Token::Type::Ident)) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "round() mode must be a string"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto mode_string = rounding_mode_component.token().ident();
|
||||
if (mode_string.equals_ignoring_ascii_case("nearest"sv)) {
|
||||
mode = CalculationNode::RoundingMode::Nearest;
|
||||
} else if (mode_string.equals_ignoring_ascii_case("up"sv)) {
|
||||
mode = CalculationNode::RoundingMode::Up;
|
||||
} else if (mode_string.equals_ignoring_ascii_case("down"sv)) {
|
||||
mode = CalculationNode::RoundingMode::Down;
|
||||
} else if (mode_string.equals_ignoring_ascii_case("to-zero"sv)) {
|
||||
mode = CalculationNode::RoundingMode::TowardZero;
|
||||
} else {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "round() mode must be one of 'nearest', 'up', 'down', or 'to-zero'"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
node_a = TRY(parse_a_calculation(parameters[1]));
|
||||
node_b = TRY(parse_a_calculation(parameters[2]));
|
||||
} else {
|
||||
node_a = TRY(parse_a_calculation(parameters[0]));
|
||||
node_b = TRY(parse_a_calculation(parameters[1]));
|
||||
}
|
||||
|
||||
if (!node_a || !node_b) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "round() parameters must be valid calculations"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto node_a_maybe_parameter_type = node_a->resolved_type();
|
||||
if (!node_a_maybe_parameter_type.has_value()) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for round() parameter 1"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto node_b_maybe_parameter_type = node_b->resolved_type();
|
||||
if (!node_b_maybe_parameter_type.has_value()) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for round() parameter 2"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto node_a_resolved_type = node_a_maybe_parameter_type.value();
|
||||
auto node_b_resolved_type = node_b_maybe_parameter_type.value();
|
||||
if (node_a_resolved_type != node_b_resolved_type) {
|
||||
dbgln_if(CSS_PARSER_DEBUG, "round() parameters must all be of the same type"sv);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return TRY(RoundCalculationNode::create(mode, node_a.release_nonnull(), node_b.release_nonnull()));
|
||||
}
|
||||
|
||||
ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value)
|
||||
{
|
||||
if (component_value.is_function()) {
|
||||
|
@ -4043,6 +4111,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
|
|||
if (function.name().equals_ignoring_ascii_case("exp"sv))
|
||||
return TRY(parse_exp_function(function));
|
||||
|
||||
if (function.name().equals_ignoring_ascii_case("round"sv))
|
||||
return TRY(parse_round_function(function));
|
||||
|
||||
dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name());
|
||||
return nullptr;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue