Skip to content

Commit c9bc34e

Browse files
committed
Added rgb function syntax L4 #131
1 parent 15cb522 commit c9bc34e

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Released on tbd.
1313
- Added further compactification of CSS tuples (#89, #93)
1414
- Added support for 8-digit hex color codes (#132)
1515
- Added more CSSOM possibilities and helpers (#6)
16+
- Added parts of recent color spec update such as `rgb` with spaces (#131)
1617

1718
# 0.17.0
1819

src/AngleSharp.Css.Tests/Functions/CssColorFunction.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,39 @@ public void ColorNotParsedCorrectly_Issue109()
1919
var color = s.GetColor();
2020
Assert.AreEqual("rgba(0, 17, 0, 1)", color);
2121
}
22+
23+
[Test]
24+
public void ParseRgbWithSpacesL4_Issue131()
25+
{
26+
var html = @"<p style='color: rgb(255 122 127 / 80%)'>Text</p>";
27+
var dom = ParseDocument(html);
28+
var p = dom.QuerySelector("p");
29+
var s = p.GetStyle();
30+
var color = s.GetColor();
31+
Assert.AreEqual("rgba(255, 122, 127, 0.8)", color);
32+
}
33+
34+
[Test]
35+
public void ParseRgbWithSpacesInPercentL4_Issue131()
36+
{
37+
var html = @"<p style='color: rgb(100% 10% 50% / 0.7)'>Text</p>";
38+
var dom = ParseDocument(html);
39+
var p = dom.QuerySelector("p");
40+
var s = p.GetStyle();
41+
var color = s.GetColor();
42+
Assert.AreEqual("rgba(255, 26, 128, 0.7)", color);
43+
}
44+
45+
[Test]
46+
public void ParseRgbWithSpacesAndNoneL4_Issue131()
47+
{
48+
var html = @"<p style='color: rgb(100% none 50% / 35%)'>Text</p>";
49+
var dom = ParseDocument(html);
50+
var p = dom.QuerySelector("p");
51+
var s = p.GetStyle();
52+
var color = s.GetColor();
53+
Assert.AreEqual("rgba(255, 0, 128, 0.35)", color);
54+
}
2255
}
2356
}
2457

src/AngleSharp.Css/Parser/Micro/ColorParser.cs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AngleSharp.Css.Parser
22
{
33
using AngleSharp.Css.Values;
4+
using AngleSharp.Io;
45
using AngleSharp.Text;
56
using System;
67
using System.Collections.Generic;
@@ -92,6 +93,52 @@ static class ColorParser
9293
}
9394

9495
private static Color? ParseRgba(StringSource source)
96+
{
97+
var pos = source.Index;
98+
var color = ParseRgbaLegacy(source);
99+
100+
if (!color.HasValue)
101+
{
102+
source.BackTo(pos);
103+
return ParseRgbaModern(source);
104+
}
105+
106+
return color.Value;
107+
}
108+
109+
private static Color? ParseRgbaModern(StringSource source)
110+
{
111+
var r = ParseRgbOrNoneComponent(source);
112+
source.SkipSpacesAndComments();
113+
var g = ParseRgbOrNoneComponent(source);
114+
source.SkipSpacesAndComments();
115+
var b = ParseRgbOrNoneComponent(source);
116+
source.SkipSpacesAndComments();
117+
var c = source.Current;
118+
var a = new Nullable<Double>(1.0);
119+
120+
if (r != null && g != null && b != null)
121+
{
122+
source.SkipCurrentAndSpaces();
123+
124+
if (c == Symbols.Solidus)
125+
{
126+
a = ParseAlpha(source);
127+
source.SkipSpacesAndComments();
128+
c = source.Current;
129+
source.SkipCurrentAndSpaces();
130+
}
131+
132+
if (c == Symbols.RoundBracketClose)
133+
{
134+
return Color.FromRgba(r.Value, g.Value, b.Value, a.Value);
135+
}
136+
}
137+
138+
return null;
139+
}
140+
141+
private static Color? ParseRgbaLegacy(StringSource source)
95142
{
96143
var r = ParseRgbComponent(source);
97144
var c1 = source.SkipGetSkip();
@@ -205,6 +252,26 @@ static class ColorParser
205252
return null;
206253
}
207254

255+
private static Byte? ParseRgbOrNoneComponent(StringSource source)
256+
{
257+
var pos = source.Index;
258+
var value = ParseRgbComponent(source);
259+
260+
if (value.HasValue)
261+
{
262+
return value;
263+
}
264+
265+
source.BackTo(pos);
266+
267+
if (source.IsIdentifier(CssKeywords.None))
268+
{
269+
return 0;
270+
}
271+
272+
return null;
273+
}
274+
208275
private static Byte? ParseRgbComponent(StringSource source)
209276
{
210277
var unit = source.ParseUnit();
@@ -214,7 +281,7 @@ static class ColorParser
214281
{
215282
if (unit.Dimension == "%")
216283
{
217-
return (Byte)((255f * value) / 100f);
284+
return (Byte)Math.Round((255.0 * value) / 100.0);
218285
}
219286
else if (unit.Dimension == String.Empty)
220287
{

0 commit comments

Comments
 (0)