1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 16:57:35 +00:00

LibWeb: Move RoundingMode to Enums.json

In the spec this is a `<rounding-strategy>`, so let's use that name.
This also fixes a bug where we were serializing `to-zero` as
`toward-zero`.
This commit is contained in:
Sam Atkins 2023-07-13 14:51:11 +01:00 committed by Andreas Kling
parent 3194f10ad0
commit e4a2bd7a44
5 changed files with 35 additions and 48 deletions

View file

@ -269,6 +269,12 @@
"round", "round",
"space" "space"
], ],
"rounding-strategy": [
"down",
"nearest",
"to-zero",
"up"
],
"text-align": [ "text-align": [
"center", "center",
"justify", "justify",

View file

@ -115,6 +115,7 @@
"distribute", "distribute",
"dotted", "dotted",
"double", "double",
"down",
"e-resize", "e-resize",
"ease", "ease",
"ease-in", "ease-in",
@ -202,6 +203,7 @@
"menulist-button", "menulist-button",
"n-resize", "n-resize",
"ne-resize", "ne-resize",
"nearest",
"nesw-resize", "nesw-resize",
"no-drop", "no-drop",
"no-preference", "no-preference",
@ -307,6 +309,7 @@
"text-top", "text-top",
"thick", "thick",
"thin", "thin",
"to-zero",
"top", "top",
"textarea", "textarea",
"textfield", "textfield",
@ -318,6 +321,7 @@
"ultra-expanded", "ultra-expanded",
"underline", "underline",
"unsafe", "unsafe",
"up",
"upper-alpha", "upper-alpha",
"upper-latin", "upper-latin",
"upper-roman", "upper-roman",

View file

@ -3923,25 +3923,23 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_round_function(Function const& fu
OwnPtr<CalculationNode> node_a = nullptr; OwnPtr<CalculationNode> node_a = nullptr;
OwnPtr<CalculationNode> node_b = nullptr; OwnPtr<CalculationNode> node_b = nullptr;
auto mode = CalculationNode::RoundingMode::Nearest; auto strategy = RoundingStrategy::Nearest;
if (parameters.size() == 3) { if (parameters.size() == 3) {
auto rounding_mode_component = parameters[0][0]; auto rounding_strategy_component = parameters[0][0];
if (!rounding_mode_component.is(Token::Type::Ident)) { if (!rounding_strategy_component.is(Token::Type::Ident)) {
dbgln_if(CSS_PARSER_DEBUG, "round() mode must be a string"sv); dbgln_if(CSS_PARSER_DEBUG, "round() strategy must be an ident"sv);
return nullptr; return nullptr;
} }
auto mode_string = rounding_mode_component.token().ident(); auto maybe_value_id = value_id_from_string(rounding_strategy_component.token().ident());
if (mode_string.equals_ignoring_ascii_case("nearest"sv)) { if (!maybe_value_id.has_value()) {
mode = CalculationNode::RoundingMode::Nearest; dbgln_if(CSS_PARSER_DEBUG, "round() strategy must be one of 'nearest', 'up', 'down', or 'to-zero'"sv);
} else if (mode_string.equals_ignoring_ascii_case("up"sv)) { return nullptr;
mode = CalculationNode::RoundingMode::Up; }
} else if (mode_string.equals_ignoring_ascii_case("down"sv)) { if (auto maybe_strategy = value_id_to_rounding_strategy(maybe_value_id.value()); maybe_strategy.has_value()) {
mode = CalculationNode::RoundingMode::Down; strategy = maybe_strategy.value();
} else if (mode_string.equals_ignoring_ascii_case("to-zero"sv)) {
mode = CalculationNode::RoundingMode::TowardZero;
} else { } else {
dbgln_if(CSS_PARSER_DEBUG, "round() mode must be one of 'nearest', 'up', 'down', or 'to-zero'"sv); dbgln_if(CSS_PARSER_DEBUG, "round() strategy must be one of 'nearest', 'up', 'down', or 'to-zero'"sv);
return nullptr; return nullptr;
} }
@ -3976,7 +3974,7 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_round_function(Function const& fu
return nullptr; return nullptr;
} }
return TRY(RoundCalculationNode::create(mode, node_a.release_nonnull(), node_b.release_nonnull())); return TRY(RoundCalculationNode::create(strategy, node_a.release_nonnull(), node_b.release_nonnull()));
} }
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_mod_function(Function const& function) ErrorOr<OwnPtr<CalculationNode>> Parser::parse_mod_function(Function const& function)

View file

@ -1791,14 +1791,14 @@ ErrorOr<void> ExpCalculationNode::dump(StringBuilder& builder, int indent) const
return {}; return {};
} }
ErrorOr<NonnullOwnPtr<RoundCalculationNode>> RoundCalculationNode::create(RoundingMode mode, NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y) ErrorOr<NonnullOwnPtr<RoundCalculationNode>> RoundCalculationNode::create(RoundingStrategy strategy, NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
{ {
return adopt_nonnull_own_or_enomem(new (nothrow) RoundCalculationNode(mode, move(x), move(y))); return adopt_nonnull_own_or_enomem(new (nothrow) RoundCalculationNode(strategy, move(x), move(y)));
} }
RoundCalculationNode::RoundCalculationNode(RoundingMode mode, NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y) RoundCalculationNode::RoundCalculationNode(RoundingStrategy mode, NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
: CalculationNode(Type::Round) : CalculationNode(Type::Round)
, m_mode(mode) , m_strategy(mode)
, m_x(move(x)) , m_x(move(x))
, m_y(move(y)) , m_y(move(y))
{ {
@ -1810,20 +1810,7 @@ ErrorOr<String> RoundCalculationNode::to_string() const
{ {
StringBuilder builder; StringBuilder builder;
builder.append("round("sv); builder.append("round("sv);
switch (m_mode) { builder.append(CSS::to_string(m_strategy));
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(", "sv);
builder.append(TRY(m_x->to_string())); builder.append(TRY(m_x->to_string()));
builder.append(", "sv); builder.append(", "sv);
@ -1868,22 +1855,22 @@ CalculatedStyleValue::CalculationResult RoundCalculationNode::resolve(Optional<L
auto upper_b = ceil(node_a_value / node_b_value) * node_b_value; 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; auto lower_b = floor(node_a_value / node_b_value) * node_b_value;
if (m_mode == RoundingMode::Nearest) { if (m_strategy == RoundingStrategy::Nearest) {
auto upper_diff = fabs(upper_b - node_a_value); auto upper_diff = fabs(upper_b - node_a_value);
auto lower_diff = fabs(node_a_value - lower_b); auto lower_diff = fabs(node_a_value - lower_b);
auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b; auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b;
return to_resolved_type(resolved_type, rounded_value); return to_resolved_type(resolved_type, rounded_value);
} }
if (m_mode == RoundingMode::Up) { if (m_strategy == RoundingStrategy::Up) {
return to_resolved_type(resolved_type, upper_b); return to_resolved_type(resolved_type, upper_b);
} }
if (m_mode == RoundingMode::Down) { if (m_strategy == RoundingStrategy::Down) {
return to_resolved_type(resolved_type, lower_b); return to_resolved_type(resolved_type, lower_b);
} }
if (m_mode == RoundingMode::TowardZero) { if (m_strategy == RoundingStrategy::ToZero) {
auto upper_diff = fabs(upper_b); auto upper_diff = fabs(upper_b);
auto lower_diff = fabs(lower_b); auto lower_diff = fabs(lower_b);
auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b; auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b;

View file

@ -130,14 +130,6 @@ 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
@ -699,7 +691,7 @@ private:
class RoundCalculationNode final : public CalculationNode { class RoundCalculationNode final : public CalculationNode {
public: public:
static ErrorOr<NonnullOwnPtr<RoundCalculationNode>> create(CalculationNode::RoundingMode, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>); static ErrorOr<NonnullOwnPtr<RoundCalculationNode>> create(RoundingStrategy, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
~RoundCalculationNode(); ~RoundCalculationNode();
virtual ErrorOr<String> to_string() const override; virtual ErrorOr<String> to_string() const override;
@ -712,8 +704,8 @@ public:
virtual ErrorOr<void> dump(StringBuilder&, int indent) const override; virtual ErrorOr<void> dump(StringBuilder&, int indent) const override;
private: private:
RoundCalculationNode(RoundingMode, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>); RoundCalculationNode(RoundingStrategy, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
CalculationNode::RoundingMode m_mode; RoundingStrategy m_strategy;
NonnullOwnPtr<CalculationNode> m_x; NonnullOwnPtr<CalculationNode> m_x;
NonnullOwnPtr<CalculationNode> m_y; NonnullOwnPtr<CalculationNode> m_y;
}; };