Skip to content

Commit

Permalink
Merge pull request trullock#295 from volkanceylan/bug_287_rgb_with_func
Browse files Browse the repository at this point in the history
add rgb with function / var usage and tests
  • Loading branch information
trullock authored Jan 24, 2022
2 parents f930637 + 272d305 commit c267688
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/NUglify.Tests/Css/Syntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,13 @@ public void RgbWithSpace()
var retValue = TestHelper.Instance.RunTest();
Assert.IsTrue(retValue == 0, "shouldn't have any errors");
}


[Test]
public void RgbWithFunc()
{
var retValue = TestHelper.Instance.RunTest();
Assert.IsTrue(retValue == 0, "shouldn't have any errors");
}
}
}
6 changes: 6 additions & 0 deletions src/NUglify.Tests/NUglify.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@
<Content Include="TestData\CSS\Expected\Selectors\PseudoFunctions.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\CSS\Expected\Syntax\RgbWithFunc.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\CSS\Expected\Syntax\RgbWithSpace.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -455,6 +458,9 @@
<Content Include="TestData\CSS\Input\Selectors\PseudoFunctions.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\CSS\Input\Syntax\RgbWithFunc.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\CSS\Input\Syntax\RgbWithSpace.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/NUglify.Tests/TestData/CSS/Input/Syntax/RgbWithFunc.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
a {
--rgb-nums: 110 120 130;
--rgb-comma: 110,120,130;
--r: 1;
--g: 2;
--b: 3;
--a: 0.5;
--p: 50%;
color: rgb(var(--rgb-nums));
color: rgb(var(--r), var(--g), var(--b));
color: rgb(var(--r), 3, 5);
color: rgb(1, var(--g), 5);
color: rgb(1, 3, var(--b));
color: rgb(var(--rgb-nums) / 50%);
color: rgb(var(--rgb-nums) / var(--p));
color: rgb(var(--rgb-comma), 0.5);
color: rgb(var(--rgb-comma), var(--a));
color: rgb(calc(0 + 1), calc(1 + 2), calc(2 + 3));
color: rgb(calc(0 + 1) calc(1 + 2) calc(2 + 3));
}
33 changes: 27 additions & 6 deletions src/NUglify/Css/CssParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3165,6 +3165,7 @@ Parsed ParseRgb()
}

bool? usingSpace = null;
bool usingFunc = false;
for (var ndx = 0; ndx < 4; ++ndx)
{
// if this isn't the first number, we better find a comma separator or space
Expand All @@ -3181,19 +3182,22 @@ Parsed ParseRgb()
useRGB = true;
}
}
else if (usingSpace != false && CurrentTokenType == TokenType.Number)
else if (usingSpace != false &&
(CurrentTokenType == TokenType.Number ||
CurrentTokenType == TokenType.Function))
{
sbRGB.Append(' ');
usingSpace = true;
}
else if (usingSpace == true && ndx == 3 && CurrentTokenType == TokenType.Character && CurrentTokenText == "/")
else if (((usingSpace == true && ndx == 3) || usingFunc) &&
CurrentTokenType == TokenType.Character && CurrentTokenText == "/")
{
sbRGB.Append('/');
}
else if (CurrentTokenType == TokenType.Character && CurrentTokenText == ")")
{
// 3 part is OK
if (ndx == 3)
// 3 part is OK, or used a func
if (ndx == 3 || usingFunc)
break;

ReportError(0, CssErrorCode.ExpectedComma, CurrentTokenText);
Expand All @@ -3209,7 +3213,9 @@ Parsed ParseRgb()
useRGB = true;
}

if (usingSpace != true || CurrentTokenType != TokenType.Number)
if (usingSpace != true ||
(CurrentTokenType != TokenType.Number &&
CurrentTokenType != TokenType.Function))
{
// skip to the next significant
comments = NextSignificantToken();
Expand Down Expand Up @@ -3242,7 +3248,22 @@ Parsed ParseRgb()
// we might adjust the value, so save the token text
var tokenText = CurrentTokenText;

if (CurrentTokenType != TokenType.Number && CurrentTokenType != TokenType.Percentage)
if (CurrentTokenType == TokenType.Function)
{
useRGB = true;
usingFunc = true;
Append(sbRGB.ToString());
sbRGB.Clear();

if (ParseFunction() == Parsed.False)
{
ReportError(0, CssErrorCode.ExpectedRgbNumberOrPercentage, CurrentTokenText);
return Parsed.False;
}

continue;
}
else if (CurrentTokenType != TokenType.Number && CurrentTokenType != TokenType.Percentage)
{
ReportError(0, CssErrorCode.ExpectedRgbNumberOrPercentage, CurrentTokenText);
useRGB = true;
Expand Down

0 comments on commit c267688

Please sign in to comment.