mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 19:07: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:
parent
3194f10ad0
commit
e4a2bd7a44
5 changed files with 35 additions and 48 deletions
|
@ -269,6 +269,12 @@
|
|||
"round",
|
||||
"space"
|
||||
],
|
||||
"rounding-strategy": [
|
||||
"down",
|
||||
"nearest",
|
||||
"to-zero",
|
||||
"up"
|
||||
],
|
||||
"text-align": [
|
||||
"center",
|
||||
"justify",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -3923,25 +3923,23 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_round_function(Function const& fu
|
|||
|
||||
OwnPtr<CalculationNode> node_a = nullptr;
|
||||
OwnPtr<CalculationNode> 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<OwnPtr<CalculationNode>> 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<OwnPtr<CalculationNode>> Parser::parse_mod_function(Function const& function)
|
||||
|
|
|
@ -1791,14 +1791,14 @@ ErrorOr<void> ExpCalculationNode::dump(StringBuilder& builder, int indent) const
|
|||
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)
|
||||
, m_mode(mode)
|
||||
, m_strategy(mode)
|
||||
, m_x(move(x))
|
||||
, m_y(move(y))
|
||||
{
|
||||
|
@ -1810,20 +1810,7 @@ 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(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<L
|
|||
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) {
|
||||
if (m_strategy == RoundingStrategy::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) {
|
||||
if (m_strategy == RoundingStrategy::Up) {
|
||||
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);
|
||||
}
|
||||
|
||||
if (m_mode == RoundingMode::TowardZero) {
|
||||
if (m_strategy == RoundingStrategy::ToZero) {
|
||||
auto upper_diff = fabs(upper_b);
|
||||
auto lower_diff = fabs(lower_b);
|
||||
auto rounded_value = upper_diff < lower_diff ? upper_b : lower_b;
|
||||
|
|
|
@ -130,14 +130,6 @@ public:
|
|||
MinusInfinity,
|
||||
};
|
||||
|
||||
// https://drafts.csswg.org/css-values-4/#round-func
|
||||
enum class RoundingMode {
|
||||
Nearest,
|
||||
Up,
|
||||
Down,
|
||||
TowardZero
|
||||
};
|
||||
|
||||
enum class Type {
|
||||
Numeric,
|
||||
// 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 {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<RoundCalculationNode>> create(CalculationNode::RoundingMode, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||
static ErrorOr<NonnullOwnPtr<RoundCalculationNode>> create(RoundingStrategy, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||
~RoundCalculationNode();
|
||||
|
||||
virtual ErrorOr<String> to_string() const override;
|
||||
|
@ -712,8 +704,8 @@ public:
|
|||
virtual ErrorOr<void> dump(StringBuilder&, int indent) const override;
|
||||
|
||||
private:
|
||||
RoundCalculationNode(RoundingMode, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||
CalculationNode::RoundingMode m_mode;
|
||||
RoundCalculationNode(RoundingStrategy, NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
|
||||
RoundingStrategy m_strategy;
|
||||
NonnullOwnPtr<CalculationNode> m_x;
|
||||
NonnullOwnPtr<CalculationNode> m_y;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue