From e4a2bd7a44185bbf4bfc9b887132cde9e2a56c02 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 13 Jul 2023 14:51:11 +0100 Subject: [PATCH] LibWeb: Move RoundingMode to Enums.json In the spec this is a ``, so let's use that name. This also fixes a bug where we were serializing `to-zero` as `toward-zero`. --- Userland/Libraries/LibWeb/CSS/Enums.json | 6 ++++ .../Libraries/LibWeb/CSS/Identifiers.json | 4 +++ .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 28 ++++++++--------- .../CSS/StyleValues/CalculatedStyleValue.cpp | 31 ++++++------------- .../CSS/StyleValues/CalculatedStyleValue.h | 14 ++------- 5 files changed, 35 insertions(+), 48 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Enums.json b/Userland/Libraries/LibWeb/CSS/Enums.json index 090306c2e5..da2c619d99 100644 --- a/Userland/Libraries/LibWeb/CSS/Enums.json +++ b/Userland/Libraries/LibWeb/CSS/Enums.json @@ -269,6 +269,12 @@ "round", "space" ], + "rounding-strategy": [ + "down", + "nearest", + "to-zero", + "up" + ], "text-align": [ "center", "justify", diff --git a/Userland/Libraries/LibWeb/CSS/Identifiers.json b/Userland/Libraries/LibWeb/CSS/Identifiers.json index fd822de39d..052b876bd8 100644 --- a/Userland/Libraries/LibWeb/CSS/Identifiers.json +++ b/Userland/Libraries/LibWeb/CSS/Identifiers.json @@ -115,6 +115,7 @@ "distribute", "dotted", "double", + "down", "e-resize", "ease", "ease-in", @@ -202,6 +203,7 @@ "menulist-button", "n-resize", "ne-resize", + "nearest", "nesw-resize", "no-drop", "no-preference", @@ -307,6 +309,7 @@ "text-top", "thick", "thin", + "to-zero", "top", "textarea", "textfield", @@ -318,6 +321,7 @@ "ultra-expanded", "underline", "unsafe", + "up", "upper-alpha", "upper-latin", "upper-roman", diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index c4083b967b..895b59eec3 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -3923,25 +3923,23 @@ ErrorOr> Parser::parse_round_function(Function const& fu OwnPtr node_a = nullptr; OwnPtr node_b = nullptr; - auto mode = CalculationNode::RoundingMode::Nearest; + auto strategy = RoundingStrategy::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); + auto rounding_strategy_component = parameters[0][0]; + if (!rounding_strategy_component.is(Token::Type::Ident)) { + dbgln_if(CSS_PARSER_DEBUG, "round() strategy must be an ident"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; + auto maybe_value_id = value_id_from_string(rounding_strategy_component.token().ident()); + if (!maybe_value_id.has_value()) { + dbgln_if(CSS_PARSER_DEBUG, "round() strategy must be one of 'nearest', 'up', 'down', or 'to-zero'"sv); + return nullptr; + } + if (auto maybe_strategy = value_id_to_rounding_strategy(maybe_value_id.value()); maybe_strategy.has_value()) { + strategy = maybe_strategy.value(); } 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; } @@ -3976,7 +3974,7 @@ ErrorOr> Parser::parse_round_function(Function const& fu 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> Parser::parse_mod_function(Function const& function) diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp index 8ebfd289e9..1a80535145 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp @@ -1791,14 +1791,14 @@ ErrorOr ExpCalculationNode::dump(StringBuilder& builder, int indent) const return {}; } -ErrorOr> RoundCalculationNode::create(RoundingMode mode, NonnullOwnPtr x, NonnullOwnPtr y) +ErrorOr> RoundCalculationNode::create(RoundingStrategy strategy, NonnullOwnPtr x, NonnullOwnPtr 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 x, NonnullOwnPtr y) +RoundCalculationNode::RoundCalculationNode(RoundingStrategy mode, NonnullOwnPtr x, NonnullOwnPtr y) : CalculationNode(Type::Round) - , m_mode(mode) + , m_strategy(mode) , m_x(move(x)) , m_y(move(y)) { @@ -1810,20 +1810,7 @@ ErrorOr 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(CSS::to_string(m_strategy)); builder.append(", "sv); builder.append(TRY(m_x->to_string())); builder.append(", "sv); @@ -1868,22 +1855,22 @@ CalculatedStyleValue::CalculationResult RoundCalculationNode::resolve(Optional> create(CalculationNode::RoundingMode, NonnullOwnPtr, NonnullOwnPtr); + static ErrorOr> create(RoundingStrategy, NonnullOwnPtr, NonnullOwnPtr); ~RoundCalculationNode(); virtual ErrorOr to_string() const override; @@ -712,8 +704,8 @@ public: virtual ErrorOr dump(StringBuilder&, int indent) const override; private: - RoundCalculationNode(RoundingMode, NonnullOwnPtr, NonnullOwnPtr); - CalculationNode::RoundingMode m_mode; + RoundCalculationNode(RoundingStrategy, NonnullOwnPtr, NonnullOwnPtr); + RoundingStrategy m_strategy; NonnullOwnPtr m_x; NonnullOwnPtr m_y; };