forked from AngleSharp/AngleSharp.Css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCssVariables.cs
More file actions
120 lines (112 loc) · 4.95 KB
/
CssVariables.cs
File metadata and controls
120 lines (112 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#nullable disable
namespace AngleSharp.Css.Tests.Declarations
{
using AngleSharp.Css.Dom;
using AngleSharp.Css.Values;
using NUnit.Framework;
using static CssConstructionFunctions;
[TestFixture]
public class CssVariablesTests
{
[Test]
public void RootVariableCorrectlyIdentified()
{
var source = @":root { --my-variable: black; }";
var sheet = ParseStyleSheet(source);
Assert.AreEqual(1, sheet.Rules.Length);
var style = sheet.Rules[0] as ICssStyleRule;
Assert.IsNotNull(style);
Assert.AreEqual(":root", style.SelectorText);
Assert.AreEqual(1, style.Style.Length);
var propertyName = style.Style[0];
var propertyValue = style.Style[propertyName];
Assert.AreEqual("--my-variable", propertyName);
Assert.AreEqual("black", propertyValue);
}
[Test]
public void RootVariableWithInvalidIdentifier()
{
var source = @":root { --my-vari@able: black; }";
var sheet = ParseStyleSheet(source);
Assert.AreEqual(1, sheet.Rules.Length);
var style = sheet.Rules[0] as ICssStyleRule;
Assert.IsNotNull(style);
Assert.AreEqual(":root", style.SelectorText);
Assert.AreEqual(0, style.Style.Length);
}
[Test]
public void LegitVariableReferenceWithoutFallback()
{
var source = @"padding-bottom: var(--foo)";
var property = ParseDeclaration(source);
Assert.IsNotNull(property);
var variable = property.RawValue as CssReferenceValue;
Assert.IsNotNull(variable);
Assert.AreEqual(1, variable.References.Length);
Assert.AreEqual("--foo", variable.References[0].VariableName);
Assert.IsNull(variable.References[0].DefaultValue);
}
[Test]
public void LegitVariableReferenceWithFallback()
{
var source = @"padding-bottom: var(--my-bar, 24px)";
var property = ParseDeclaration(source);
Assert.IsNotNull(property);
var variable = property.RawValue as CssReferenceValue;
Assert.IsNotNull(variable);
Assert.AreEqual(1, variable.References.Length);
Assert.AreEqual("--my-bar", variable.References[0].VariableName);
Assert.AreEqual("24px", variable.References[0].DefaultValue.CssText);
}
[Test]
public void LegitVariableReferenceWithFallbackContainingComma()
{
var source = @"border-top-color: var(--color, red, blue)";
var property = ParseDeclaration(source);
Assert.IsNotNull(property);
var variable = property.RawValue as CssReferenceValue;
Assert.IsNotNull(variable);
Assert.AreEqual(1, variable.References.Length);
Assert.AreEqual("--color", variable.References[0].VariableName);
Assert.AreEqual("red, blue", variable.References[0].DefaultValue.CssText);
}
[Test]
public void LegitSingleVariableReferenceInBackgroundShorthand()
{
var source = @"background: var(--foo)";
var property = ParseDeclaration(source);
Assert.IsNotNull(property);
var variable = property.RawValue as CssReferenceValue;
Assert.IsNotNull(variable);
Assert.AreEqual(1, variable.References.Length);
Assert.AreEqual("--foo", variable.References[0].VariableName);
Assert.IsNull(variable.References[0].DefaultValue);
}
[Test]
public void LegitMixedVariableReferenceInBackgroundShorthand()
{
var source = @"background: url('http://bit.ly/2FiPrRA') 0 100%/340px no-repeat, var(--primary-color);";
var property = ParseDeclaration(source);
Assert.IsNotNull(property);
var variable = property.RawValue as CssReferenceValue;
Assert.IsNotNull(variable);
Assert.AreEqual(1, variable.References.Length);
Assert.AreEqual("--primary-color", variable.References[0].VariableName);
Assert.IsNull(variable.References[0].DefaultValue);
}
[Test]
public void LegitMultipleVariableReferenceInBorderShorthand()
{
var source = @"border: var(--width) solid var(--color, black)";
var property = ParseDeclaration(source);
Assert.IsNotNull(property);
var variable = property.RawValue as CssReferenceValue;
Assert.IsNotNull(variable);
Assert.AreEqual(2, variable.References.Length);
Assert.AreEqual("--width", variable.References[0].VariableName);
Assert.IsNull(variable.References[0].DefaultValue);
Assert.AreEqual("--color", variable.References[1].VariableName);
Assert.AreEqual("black", variable.References[1].DefaultValue.CssText);
}
}
}