Skip to content

Commit 78d67ab

Browse files
committed
Refined initial value handling
1 parent 20e091d commit 78d67ab

File tree

5 files changed

+41
-34
lines changed

5 files changed

+41
-34
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,11 @@ public void CssSheetCloseStringsEndOfLine()
142142
Assert.AreEqual(1, sheet.Rules.Length);
143143
Assert.IsInstanceOf<CssStyleRule>(sheet.Rules[0]);
144144
var p = sheet.Rules[0] as ICssStyleRule;
145-
Assert.AreEqual(2, p.Style.Length);
145+
Assert.AreEqual(1, p.Style.Length);
146146
Assert.AreEqual("p", p.SelectorText);
147147
Assert.AreEqual("color", p.Style[0]);
148-
Assert.AreEqual("font-family", p.Style[1]);
149148
Assert.AreEqual("rgba(0, 128, 0, 1)", p.Style.GetColor());
150-
Assert.AreEqual("initial", p.Style.GetFontFamily());
149+
Assert.AreEqual("", p.Style.GetFontFamily());
151150
}
152151

153152
[Test]
@@ -304,8 +303,8 @@ public void CssSheetIgnoreInvalidValueFloat()
304303
Assert.IsInstanceOf<CssStyleRule>(sheet.Rules[0]);
305304
var img = sheet.Rules[0] as ICssStyleRule;
306305
Assert.AreEqual("img", img.SelectorText);
307-
Assert.AreEqual(1, img.Style.Length);
308-
Assert.AreEqual("initial", img.Style.GetFloat());
306+
Assert.AreEqual(0, img.Style.Length);
307+
Assert.AreEqual("", img.Style.GetFloat());
309308
}
310309

311310
[Test]

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public void ParsedCssCanHaveExtraWhitespace()
6161
IsIncludingUnknownRules = true
6262
});
6363
var div = doc.QuerySelector<IHtmlElement>("div");
64-
Assert.AreEqual("initial", div.GetStyle()["background-color"]);
65-
Assert.AreEqual("background-color: initial", div.GetStyle().CssText);
64+
Assert.AreEqual("", div.GetStyle()["background-color"]);
65+
Assert.AreEqual("", div.GetStyle().CssText);
6666
}
6767

6868
[Test]
@@ -326,7 +326,7 @@ public void SetStyleAttributeAfterPageLoadWithInvalidColor()
326326
// hang occurs only if this line is executed prior to setting the attribute
327327
// hang occurs when executing next line
328328
div.SetAttribute("style", "background-color: http://www.codeplex.com?url=&lt;SCRIPT&gt;a=/XSS/alert(a.source)&lt;/SCRIPT&gt;");
329-
Assert.AreEqual("initial", div.GetStyle().GetBackgroundColor());
329+
Assert.AreEqual("", div.GetStyle().GetBackgroundColor());
330330
}
331331

332332
[Test]

src/AngleSharp.Css/Dom/Internal/CssProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public ICssValue RawValue
4646

4747
public String Value
4848
{
49-
get => _value?.CssText ?? CssKeywords.Initial;
49+
get => _value?.CssText ?? String.Empty;
5050
set => _value = _converter.Convert(value);
5151
}
5252

src/AngleSharp.Css/Dom/Internal/CssStyleDeclaration.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,40 @@ public void Update(String value)
100100

101101
private ICssProperty TryCreateShorthand(String shorthandName, IEnumerable<String> serialized, List<String> usedProperties, Boolean force)
102102
{
103-
var longhands = Declarations.Where(m => !serialized.Contains(m.Name)).ToList();
104103
var factory = _context.GetFactory<IDeclarationFactory>();
105104
var shorthand = factory.Create(shorthandName);
106105
var requiredProperties = shorthand.Longhands;
107-
var values = new ICssValue[requiredProperties.Length];
108-
var important = 0;
109-
var count = 0;
110106

111-
for (var i = 0; i < values.Length; i++)
107+
if (requiredProperties.Length > 0)
112108
{
113-
var name = requiredProperties[i];
114-
var propInfo = factory.Create(name);
115-
var property = propInfo.Longhands.Any() ?
116-
TryCreateShorthand(name, serialized, usedProperties, force) :
117-
longhands.Where(m => m.Name == name).FirstOrDefault();
109+
var longhands = Declarations.Where(m => !serialized.Contains(m.Name)).ToList();
110+
var values = new ICssValue[requiredProperties.Length];
111+
var important = 0;
112+
var count = 0;
118113

119-
if (property != null)
114+
for (var i = 0; i < values.Length; i++)
120115
{
121-
usedProperties.Add(name);
122-
count = count + 1;
123-
important = important + (property.IsImportant ? 1 : 0);
124-
values[i] = property.RawValue;
116+
var name = requiredProperties[i];
117+
var propInfo = factory.Create(name);
118+
var property = propInfo.Longhands.Any() ?
119+
TryCreateShorthand(name, serialized, usedProperties, force) :
120+
longhands.Where(m => m.Name == name).FirstOrDefault();
121+
122+
if (property != null)
123+
{
124+
usedProperties.Add(name);
125+
count = count + 1;
126+
important = important + (property.IsImportant ? 1 : 0);
127+
values[i] = property.RawValue;
128+
}
125129
}
130+
131+
var valid = count == values.Length && (important == 0 || important == count);
132+
var result = force || valid ? _context.CreateShorthand(shorthandName, values, important != 0) : null;
133+
return force || result?.RawValue != null ? result : null;
126134
}
127135

128-
var valid = count == values.Length && (important == 0 || important == count);
129-
var result = force || valid ? _context.CreateShorthand(shorthandName, values, important != 0) : null;
130-
return force || result?.RawValue != null ? result : null;
136+
return _context.CreateProperty(shorthandName);
131137
}
132138

133139
public String ToCssBlock(IStyleFormatter formatter)
@@ -264,9 +270,13 @@ public void SetProperty(String propertyName, String propertyValue, String priori
264270
if (property != null)
265271
{
266272
property.Value = propertyValue;
267-
property.IsImportant = priority != null;
268-
SetProperty(property);
269-
RaiseChanged();
273+
274+
if (property.RawValue != null)
275+
{
276+
property.IsImportant = priority != null;
277+
SetProperty(property);
278+
RaiseChanged();
279+
}
270280
}
271281
}
272282
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ static class ColorParser
3232
return result;
3333
}
3434

35-
public static Color? ParseCurrentColor(this StringSource source)
36-
{
37-
return source.IsIdentifier(CssKeywords.CurrentColor) ? Color.CurrentColor : ColorParser.ParseColor(source);
38-
}
35+
public static Color? ParseCurrentColor(this StringSource source) =>
36+
source.IsIdentifier(CssKeywords.CurrentColor) ? Color.CurrentColor : ColorParser.ParseColor(source);
3937

4038
private static Color? Start(StringSource source)
4139
{

0 commit comments

Comments
 (0)