Skip to content

Commit ad7aff4

Browse files
committed
Improved CSS minification of grid #89
1 parent 12f61a1 commit ad7aff4

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Released on tbd.
88
- Fixed missing semicolon in `@page` rule (#135)
99
- Fixed integer serialization of keyframe stops (#128)
1010
- Fixed ordering of rows and columns in `grid` and `grid-gap` (#137)
11+
- Added further compactification of CSS tuples (#89, #93)
1112

1213
# 0.17.0
1314

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ public void CssGridAreaTextValueLegal1()
534534
var css = ParseStyleSheet(source);
535535
var text = css.Rules[0].CssText;
536536

537-
var expected = "#nav-header { grid-area: aaa / aaa / aaa / aaa }";
537+
var expected = "#nav-header { grid-area: aaa }";
538538
Assert.AreEqual(expected, text);
539539
}
540540

src/AngleSharp.Css.Tests/Styling/BasicStyling.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,13 @@ public void MinifyWithMultipleDeclarations()
210210
var result = sheet.ToCss(new MinifyStyleFormatter());
211211
Assert.AreEqual("h1{top:0;left:2px;border:none}h2{border:1px solid rgba(255, 0, 0, 1)}", result);
212212
}
213+
214+
[Test]
215+
public void MinifyMinimizesProperties_Issue89()
216+
{
217+
var sheet = ParseStyleSheet("a { grid-area: aa / aa / aa / aa }");
218+
var result = sheet.ToCss(new MinifyStyleFormatter());
219+
Assert.AreEqual("a{grid-area:aa}", result);
220+
}
213221
}
214222
}

src/AngleSharp.Css/Declarations/GridAreaDeclaration.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,16 @@ sealed class GridAreaAggregator : IValueAggregator, IValueConverter
3737

3838
public ICssValue Convert(StringSource source) => converter.Convert(source);
3939

40-
public ICssValue Merge(ICssValue[] values) => new CssTupleValue(values, seperator);
40+
public ICssValue Merge(ICssValue[] values)
41+
{
42+
// Make single value if all resolve to the same text
43+
if (values.Length == 4 && values[0]?.CssText == values[1]?.CssText && values[0]?.CssText == values[2]?.CssText && values[0]?.CssText == values[3]?.CssText)
44+
{
45+
return values[0];
46+
}
47+
48+
return new CssTupleValue(values, seperator);
49+
}
4150

4251
public ICssValue[] Split(ICssValue value)
4352
{
@@ -75,12 +84,13 @@ private static ICssValue GetItem(CssTupleValue tuple, Int32 index)
7584
private static ICssValue GetItemSimple(CssTupleValue tuple, Int32 index)
7685
{
7786
var val = UnitParser.ParseUnit(new StringSource(tuple.Items[0].CssText));
87+
7888
if (index <= 2)
7989
{
8090
if (tuple.Items.Length <= index)
8191
{
8292

83-
if (!int.TryParse(tuple.Items[0].CssText, out int _))
93+
if (!Int32.TryParse(tuple.Items[0].CssText, out var _))
8494
{
8595
return tuple.Items[0];
8696
}
@@ -90,12 +100,12 @@ private static ICssValue GetItemSimple(CssTupleValue tuple, Int32 index)
90100
{
91101
if (tuple.Items.Length > 1)
92102
{
93-
if (!int.TryParse(tuple.Items[1].CssText, out int _))
103+
if (!Int32.TryParse(tuple.Items[1].CssText, out var _))
94104
{
95105
return tuple.Items[1];
96106
}
97107
}
98-
else if (!int.TryParse(tuple.Items[0].CssText, out int _))
108+
else if (!Int32.TryParse(tuple.Items[0].CssText, out var _))
99109
{
100110
return tuple.Items[0];
101111
}

0 commit comments

Comments
 (0)