Skip to content

Commit

Permalink
Do not simplify calc(0px + 0%) into 0px
Browse files Browse the repository at this point in the history
Currently, when we create |Length| from calc(), and then create a
CSSPrimitiveValue from that |Length|, we may drop the percentage part
if it's zero.

As discussed at w3c/csswg-drafts#3482, zero
percentages in calcs should be preserved.

Hence, this part ensures that percentage is perserved in calc(0px + 0%)

Note: we may want to preserve 0% in all cases, but that leads to many
test failures, so we leave the investigation to future instead.

This is also preparation for crrev.com/c/1777025, which switches the
implementation of InterpolableLength to a math expression to support
min/max.

Bug: 991672
Change-Id: I386f42a323079cce3d6ee545fa00ef289406e8bf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1779721
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: Emil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#693437}
  • Loading branch information
xiaochengh authored and Commit Bot committed Sep 4, 2019
1 parent 95a99b8 commit 80cac28
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ bool CSSMathFunctionValue::AccumulateLengthArray(CSSLengthArray& length_array,

Length CSSMathFunctionValue::ConvertToLength(
const CSSToLengthConversionData& conversion_data) const {
if (IsLength())
return Length::Fixed(ComputeLengthPx(conversion_data));
return Length(ToCalcValue(conversion_data));
}

Expand Down
2 changes: 1 addition & 1 deletion third_party/blink/renderer/core/css/css_primitive_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ CSSPrimitiveValue* CSSPrimitiveValue::CreateFromLength(const Length& length,
const CalculationValue& calc = length.GetCalculationValue();
if (calc.IsExpression() || (calc.Pixels() && calc.Percent()))
return CSSMathFunctionValue::Create(length, zoom);
if (calc.Percent()) {
if (!calc.Pixels()) {
double num = calc.Percent();
if (num < 0 && calc.IsNonNegative())
num = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!doctype html>
<link rel="help" href="https://www.w3.org/TR/css-values-4/#calc-computed-value">
<link rel="help" href="https://www.w3.org/TR/CSS2/visudet.html#the-height-property">
<link rel="author" title="Xiaocheng Hu" href="mailto:xiaochengh@chromium.org">
<link rel="match" href="../reference/ref-filled-green-100px-square-only.html">
<title>0% in calc() should be preserved</title>
<script>
CSS.registerProperty({
name: '--custom-height',
syntax: '<length-percentage>',
initialValue: 'calc(0% + 0px)',
inherits: false
});
</script>
<style>
.height-filler {
height: 100px;
}

.test {
width: 100px;
height: var(--custom-height);
background-color: green;
}
</style>
<p>Test passes if there is a filled green square.</p>
<div class="test">
<div class="height-filler">
</div>
</div>

0 comments on commit 80cac28

Please sign in to comment.