mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 20:57:41 +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()));
|
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)
|
ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value)
|
||||||
{
|
{
|
||||||
if (component_value.is_function()) {
|
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))
|
if (function.name().equals_ignoring_ascii_case("exp"sv))
|
||||||
return TRY(parse_exp_function(function));
|
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());
|
dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -307,6 +307,7 @@ private:
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_hypot_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_hypot_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_log_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_log_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_exp_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_exp_function(Function const&);
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> parse_round_function(Function const&);
|
||||||
ErrorOr<RefPtr<StyleValue>> parse_dimension_value(ComponentValue const&);
|
ErrorOr<RefPtr<StyleValue>> parse_dimension_value(ComponentValue const&);
|
||||||
ErrorOr<RefPtr<StyleValue>> parse_integer_value(TokenStream<ComponentValue>&);
|
ErrorOr<RefPtr<StyleValue>> parse_integer_value(TokenStream<ComponentValue>&);
|
||||||
ErrorOr<RefPtr<StyleValue>> parse_number_value(TokenStream<ComponentValue>&);
|
ErrorOr<RefPtr<StyleValue>> parse_number_value(TokenStream<ComponentValue>&);
|
||||||
|
|
|
@ -1527,6 +1527,110 @@ ErrorOr<void> ExpCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullOwnPtr<RoundCalculationNode>> RoundCalculationNode::create(RoundingMode mode, NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_own_or_enomem(new (nothrow) RoundCalculationNode(mode, move(x), move(y)));
|
||||||
|
}
|
||||||
|
|
||||||
|
RoundCalculationNode::RoundCalculationNode(RoundingMode mode, NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
|
||||||
|
: CalculationNode(Type::Round)
|
||||||
|
, m_mode(mode)
|
||||||
|
, m_x(move(x))
|
||||||
|
, m_y(move(y))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
RoundCalculationNode::~RoundCalculationNode() = default;
|
||||||
|
|
||||||
|
ErrorOr<String> RoundCalculationNode::to_string() const
|
||||||
|
{
|
||||||
|
StringBuilder builder;
|
||||||
|
builder.append("round("sv);
|
||||||
|
switch (m_mode) {
|
||||||
|
case RoundingMode::Nearest:
|
||||||
|
builder.append("nearest"sv);
|
||||||
|
break;
|
||||||
|
case RoundingMode::Up:
|
||||||
|
builder.append("up"sv);
|
||||||
|
break;
|
||||||
|
case RoundingMode::Down:
|
||||||
|
builder.append("down"sv);
|
||||||
|
break;
|
||||||
|
case RoundingMode::TowardZero:
|
||||||
|
builder.append("toward-zero"sv);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
builder.append(", "sv);
|
||||||
|
builder.append(TRY(m_x->to_string()));
|
||||||
|
builder.append(", "sv);
|
||||||
|
builder.append(TRY(m_y->to_string()));
|
||||||
|
builder.append(")"sv);
|
||||||
|
return builder.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<CalculatedStyleValue::ResolvedType> RoundCalculationNode::resolved_type() const
|
||||||
|
{
|
||||||
|
// Note: We check during parsing that all values have the same type
|
||||||
|
return m_x->resolved_type();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RoundCalculationNode::contains_percentage() const
|
||||||
|
{
|
||||||
|
return m_x->contains_percentage() || m_y->contains_percentage();
|
||||||
|
}
|
||||||
|
|
||||||
|
CalculatedStyleValue::CalculationResult RoundCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
|
||||||
|
{
|
||||||
|
auto resolved_type = m_x->resolved_type().value();
|
||||||
|
auto node_a = m_x->resolve(context, percentage_basis);
|
||||||
|
auto node_b = m_y->resolve(context, percentage_basis);
|
||||||
|
|
||||||
|
auto node_a_value = resolve_value(node_a.value(), context);
|
||||||
|
auto node_b_value = resolve_value(node_b.value(), context);
|
||||||
|
|
||||||
|
auto upper_b = ceil(node_a_value / node_b_value) * node_b_value;
|
||||||
|
auto lower_b = floor(node_a_value / node_b_value) * node_b_value;
|
||||||
|
|
||||||
|
if (m_mode == RoundingMode::Nearest) {
|
||||||
|
auto upper_diff = fabs(upper_b - node_a_value);
|
||||||
|
auto lower_diff = fabs(node_a_value - lower_b);
|
||||||
|
auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b;
|
||||||
|
return to_resolved_type(resolved_type, rounded_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_mode == RoundingMode::Up) {
|
||||||
|
return to_resolved_type(resolved_type, upper_b);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_mode == RoundingMode::Down) {
|
||||||
|
return to_resolved_type(resolved_type, lower_b);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_mode == RoundingMode::TowardZero) {
|
||||||
|
auto upper_diff = fabs(upper_b);
|
||||||
|
auto lower_diff = fabs(lower_b);
|
||||||
|
auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b;
|
||||||
|
return to_resolved_type(resolved_type, rounded_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> RoundCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
|
||||||
|
{
|
||||||
|
TRY(m_x->for_each_child_node(callback));
|
||||||
|
TRY(m_y->for_each_child_node(callback));
|
||||||
|
TRY(callback(m_x));
|
||||||
|
TRY(callback(m_y));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> RoundCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
|
{
|
||||||
|
TRY(builder.try_appendff("{: >{}}ROUND: {}\n", "", indent, TRY(to_string())));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
|
void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
|
||||||
{
|
{
|
||||||
add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
|
add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
|
||||||
|
|
|
@ -125,6 +125,14 @@ public:
|
||||||
MinusInfinity,
|
MinusInfinity,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// https://drafts.csswg.org/css-values-4/#round-func
|
||||||
|
enum class RoundingMode {
|
||||||
|
Nearest,
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
TowardZero
|
||||||
|
};
|
||||||
|
|
||||||
enum class Type {
|
enum class Type {
|
||||||
Numeric,
|
Numeric,
|
||||||
// NOTE: Currently, any value with a `var()` or `attr()` function in it is always an
|
// NOTE: Currently, any value with a `var()` or `attr()` function in it is always an
|
||||||
|
@ -170,6 +178,12 @@ public:
|
||||||
Log,
|
Log,
|
||||||
Exp,
|
Exp,
|
||||||
|
|
||||||
|
// Stepped value functions, a sub-type of operator node
|
||||||
|
// https://drafts.csswg.org/css-values-4/#round-func
|
||||||
|
Round,
|
||||||
|
Mod,
|
||||||
|
Rem,
|
||||||
|
|
||||||
// This only exists during parsing.
|
// This only exists during parsing.
|
||||||
Unparsed,
|
Unparsed,
|
||||||
};
|
};
|
||||||
|
@ -623,4 +637,24 @@ private:
|
||||||
NonnullOwnPtr<CalculationNode> m_value;
|
NonnullOwnPtr<CalculationNode> m_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class RoundCalculationNode final : public CalculationNode {
|
||||||
|
public:
|
||||||
|
static ErrorOr<NonnullOwnPtr<RoundCalculationNode>> create(CalculationNode::RoundingMode, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||||
|
~RoundCalculationNode();
|
||||||
|
|
||||||
|
virtual ErrorOr<String> to_string() const override;
|
||||||
|
virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
|
||||||
|
virtual bool contains_percentage() const override;
|
||||||
|
virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
|
||||||
|
virtual ErrorOr<void> for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const&) override;
|
||||||
|
|
||||||
|
virtual ErrorOr<void> dump(StringBuilder&, int indent) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
RoundCalculationNode(RoundingMode, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||||
|
CalculationNode::RoundingMode m_mode;
|
||||||
|
NonnullOwnPtr<CalculationNode> m_x;
|
||||||
|
NonnullOwnPtr<CalculationNode> m_y;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue