Skip to content

Commit 5a1d528

Browse files
authored
Merge pull request #202 from jafin/fix/preserve-percent-unit-on-zero-values
Fix 0% losing its percent unit during CSS serialization
2 parents cc364f5 + 3ad9dd4 commit 5a1d528

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

src/AngleSharp.Css.Tests/Declarations/CssFlexProperty.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,36 @@ public void CssFlexShorthandGrowAndShrinkLegal()
9595
Assert.AreEqual("1 2", property.Value);
9696
}
9797

98+
[Test]
99+
public void CssFlexShorthandWithZeroPercentBasisLegal()
100+
{
101+
var snippet = "flex: 1 1 0%";
102+
var property = ParseDeclaration(snippet);
103+
Assert.AreEqual("flex", property.Name);
104+
Assert.IsTrue(property.HasValue);
105+
Assert.AreEqual("1 1 0%", property.Value);
106+
}
107+
108+
[Test]
109+
public void CssFlexBasisZeroPercentLegal()
110+
{
111+
var snippet = "flex-basis: 0%";
112+
var property = ParseDeclaration(snippet);
113+
Assert.AreEqual("flex-basis", property.Name);
114+
Assert.IsTrue(property.HasValue);
115+
Assert.AreEqual("0%", property.Value);
116+
}
117+
118+
[Test]
119+
public void CssFlexBasisZeroPxSerializesWithoutUnit()
120+
{
121+
var snippet = "flex-basis: 0px";
122+
var property = ParseDeclaration(snippet);
123+
Assert.AreEqual("flex-basis", property.Name);
124+
Assert.IsTrue(property.HasValue);
125+
Assert.AreEqual("0", property.Value);
126+
}
127+
98128
[Test]
99129
public void CssFlexWrapLegal()
100130
{

src/AngleSharp.Css.Tests/Declarations/CssObjectSizing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void CssObjectPositionLeft30Legal()
131131
Assert.IsTrue(property.IsAnimatable);
132132
Assert.IsFalse(property.IsInherited);
133133
Assert.IsTrue(property.HasValue);
134-
Assert.AreEqual("0 30px", property.Value);
134+
Assert.AreEqual("0% 30px", property.Value);
135135
}
136136
}
137137
}

src/AngleSharp.Css.Tests/Declarations/CssTransformProperty.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public void CssTransformOriginYOffsetXKeywordLegal()
230230
Assert.IsFalse(property.IsImportant);
231231
Assert.IsFalse(property.IsInherited);
232232
Assert.IsTrue(property.HasValue);
233-
Assert.AreEqual("0 2px", property.Value);
233+
Assert.AreEqual("0% 2px", property.Value);
234234
}
235235

236236
[Test]
@@ -242,7 +242,7 @@ public void CssTransformOriginXKeywordYOffsetLegal()
242242
Assert.IsFalse(property.IsImportant);
243243
Assert.IsFalse(property.IsInherited);
244244
Assert.IsTrue(property.HasValue);
245-
Assert.AreEqual("0 2px", property.Value);
245+
Assert.AreEqual("0% 2px", property.Value);
246246
}
247247

248248
[Test]
@@ -290,7 +290,7 @@ public void CssTransformOriginYXKeywordZLegal()
290290
Assert.IsFalse(property.IsImportant);
291291
Assert.IsFalse(property.IsInherited);
292292
Assert.IsTrue(property.HasValue);
293-
Assert.AreEqual("0 2px 10px", property.Value);
293+
Assert.AreEqual("0% 2px 10px", property.Value);
294294
}
295295

296296
[Test]
@@ -302,7 +302,7 @@ public void CssTransformOriginXKeywordYZLegal()
302302
Assert.IsFalse(property.IsImportant);
303303
Assert.IsFalse(property.IsInherited);
304304
Assert.IsTrue(property.HasValue);
305-
Assert.AreEqual("0 5px -3px", property.Value);
305+
Assert.AreEqual("0% 5px -3px", property.Value);
306306
}
307307

308308
[Test]

src/AngleSharp.Css/Values/Primitives/CssLengthValue.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ public String CssText
107107
}
108108
else
109109
{
110-
var unit = _value == 0.0 ? String.Empty : UnitString;
110+
// Per CSS spec, 0 is valid without a unit for lengths,
111+
// but 0% is a percentage (relative to context) and must keep its unit.
112+
var unit = _value == 0.0 && _unit != Unit.Percent ? String.Empty : UnitString;
111113
var val = _value.CssStringify();
112114
return String.Concat(val, unit);
113115
}

0 commit comments

Comments
 (0)