Skip to content

Commit

Permalink
Improved CSS minification of grid AngleSharp#89
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jun 14, 2023
1 parent 12f61a1 commit ad7aff4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Released on tbd.
- Fixed missing semicolon in `@page` rule (#135)
- Fixed integer serialization of keyframe stops (#128)
- Fixed ordering of rows and columns in `grid` and `grid-gap` (#137)
- Added further compactification of CSS tuples (#89, #93)

# 0.17.0

Expand Down
2 changes: 1 addition & 1 deletion src/AngleSharp.Css.Tests/Declarations/CssGridProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public void CssGridAreaTextValueLegal1()
var css = ParseStyleSheet(source);
var text = css.Rules[0].CssText;

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

Expand Down
8 changes: 8 additions & 0 deletions src/AngleSharp.Css.Tests/Styling/BasicStyling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,13 @@ public void MinifyWithMultipleDeclarations()
var result = sheet.ToCss(new MinifyStyleFormatter());
Assert.AreEqual("h1{top:0;left:2px;border:none}h2{border:1px solid rgba(255, 0, 0, 1)}", result);
}

[Test]
public void MinifyMinimizesProperties_Issue89()
{
var sheet = ParseStyleSheet("a { grid-area: aa / aa / aa / aa }");
var result = sheet.ToCss(new MinifyStyleFormatter());
Assert.AreEqual("a{grid-area:aa}", result);
}
}
}
18 changes: 14 additions & 4 deletions src/AngleSharp.Css/Declarations/GridAreaDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ sealed class GridAreaAggregator : IValueAggregator, IValueConverter

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

public ICssValue Merge(ICssValue[] values) => new CssTupleValue(values, seperator);
public ICssValue Merge(ICssValue[] values)
{
// Make single value if all resolve to the same text
if (values.Length == 4 && values[0]?.CssText == values[1]?.CssText && values[0]?.CssText == values[2]?.CssText && values[0]?.CssText == values[3]?.CssText)
{
return values[0];
}

return new CssTupleValue(values, seperator);
}

public ICssValue[] Split(ICssValue value)
{
Expand Down Expand Up @@ -75,12 +84,13 @@ private static ICssValue GetItem(CssTupleValue tuple, Int32 index)
private static ICssValue GetItemSimple(CssTupleValue tuple, Int32 index)
{
var val = UnitParser.ParseUnit(new StringSource(tuple.Items[0].CssText));

if (index <= 2)
{
if (tuple.Items.Length <= index)
{

if (!int.TryParse(tuple.Items[0].CssText, out int _))
if (!Int32.TryParse(tuple.Items[0].CssText, out var _))
{
return tuple.Items[0];
}
Expand All @@ -90,12 +100,12 @@ private static ICssValue GetItemSimple(CssTupleValue tuple, Int32 index)
{
if (tuple.Items.Length > 1)
{
if (!int.TryParse(tuple.Items[1].CssText, out int _))
if (!Int32.TryParse(tuple.Items[1].CssText, out var _))
{
return tuple.Items[1];
}
}
else if (!int.TryParse(tuple.Items[0].CssText, out int _))
else if (!Int32.TryParse(tuple.Items[0].CssText, out var _))
{
return tuple.Items[0];
}
Expand Down

0 comments on commit ad7aff4

Please sign in to comment.