Skip to content

Commit

Permalink
[css-values] Rounding of <integer> type should round half towards pos…
Browse files Browse the repository at this point in the history
…itive infinity

https://bugs.webkit.org/show_bug.cgi?id=260777
rdar://114532926

Reviewed by Darin Adler.

https://drafts.csswg.org/css-values-4/#integers
Rounding to the nearest integer requires rounding in the direction of +∞ when the fractional portion is exactly 0.5.

-1.5 should round to -1, not -2.

* LayoutTests/imported/w3c/web-platform-tests/css/css-values/calc-z-index-fractions-001-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/css/css-values/calc-z-index-fractions-001.html:
* Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp:
(WebCore::CSSPropertyParserHelpers::IntegerTypeRawKnownTokenTypeFunctionConsumer::consume):

Canonical link: https://commits.webkit.org/267339@main
  • Loading branch information
nt1m committed Aug 28, 2023
1 parent ee8d9ff commit 98b6a21
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

PASS testing z-index: calc(2.5 / 2)
PASS testing z-index: calc(3 / 2)
FAIL testing z-index: calc(-3 / 2) assert_equals: expected "-1" but got "-2"
PASS testing z-index: calc(3.5 / 2)
PASS testing z-index: calc(-2.5 / 2)
PASS testing z-index: calc(-3 / 2)
PASS testing z-index: calc(-3.5 / 2)

Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@
}

/* verifyComputedStyle(property_name, initial_value, specified_value, expected_value, description) */

verifyComputedStyle("z-index", "auto", "calc(2.5 / 2)", "1", "testing z-index: calc(2.5 / 2)");
verifyComputedStyle("z-index", "auto", "calc(3 / 2)", "2", "testing z-index: calc(3 / 2)");

verifyComputedStyle("z-index", "auto", "calc(3.5 / 2)", "2", "testing z-index: calc(3.5 / 2)");
verifyComputedStyle("z-index", "auto", "calc(-2.5 / 2)", "-1", "testing z-index: calc(-2.5 / 2)");
verifyComputedStyle("z-index", "auto", "calc(-3 / 2)", "-1", "testing z-index: calc(-3 / 2)");

verifyComputedStyle("z-index", "auto", "calc(-3.5 / 2)", "-2", "testing z-index: calc(-3.5 / 2)");
}

startTesting();
Expand Down
4 changes: 3 additions & 1 deletion Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ struct IntegerTypeRawKnownTokenTypeFunctionConsumer {
auto rangeCopy = range;
if (auto value = consumeCalcRawWithKnownTokenTypeFunction(rangeCopy, CalculationCategory::Number, { }, ValueRange::All)) {
range = rangeCopy;
return clampTo<IntType>(std::round(std::max(value->doubleValue(), computeMinimumValue(integerRange))));
// https://drafts.csswg.org/css-values-4/#integers
// Rounding to the nearest integer requires rounding in the direction of +∞ when the fractional portion is exactly 0.5.
return clampTo<IntType>(std::floor(std::max(value->doubleValue(), computeMinimumValue(integerRange)) + 0.5));
}

return std::nullopt;
Expand Down

0 comments on commit 98b6a21

Please sign in to comment.