Skip to content

Commit

Permalink
Added handling of invalid color as default color
Browse files Browse the repository at this point in the history
- adapted test to check for default value
- see svg-net#399
  • Loading branch information
mrbean-bremen committed Feb 21, 2019
1 parent 326dae7 commit aa4a6b3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
9 changes: 6 additions & 3 deletions Source/External/ExCSS/Parser.Blocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,13 @@ private bool ParseSingleValueHexColor(string color)
{
HtmlColor htmlColor;

if(HtmlColor.TryFromHex(color, out htmlColor))
if (HtmlColor.TryFromHex(color, out htmlColor))
return AddTerm(htmlColor);

return false;
else
// the value is invalid - remove the property to use the default value
RemoveCurrentProperty();

return true;
}

#region Namespace
Expand Down
25 changes: 23 additions & 2 deletions Tests/Svg.UnitTests/LexerIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,29 @@ public void Lexer_NoImportantAndImportantAfterNonHex_Success()
[TestMethod]
public void Lexer_FileWithInvalidHex_ColorTagIsIgnored()
{
Assert.Inconclusive("This test fails due to a lexer error");
GenerateLexerTestFile("color: #0046;");
// valid colors
var doc = GenerateLexerTestFile("fill: #ff0000; stroke: #ffff00");
var path = doc.GetElementById<SvgPath>("path1");
Assert.AreEqual(System.Drawing.Color.Red, ((SvgColourServer)path.Fill).Colour);
Assert.AreEqual(System.Drawing.Color.Yellow, ((SvgColourServer)path.Stroke).Colour);

// invalid/valid color combinations - default color is used for each invalid color
doc = GenerateLexerTestFile("fill: #ff00; stroke: #00ff00");
path = doc.GetElementById<SvgPath>("path1");
// default fill color is Black
Assert.AreEqual(System.Drawing.Color.Black, ((SvgColourServer)path.Fill).Colour);
Assert.AreEqual(System.Drawing.Color.Lime, ((SvgColourServer)path.Stroke).Colour);

doc = GenerateLexerTestFile("fill: #fff; stroke: 005577");
path = doc.GetElementById<SvgPath>("path1");
Assert.AreEqual(System.Drawing.Color.White, ((SvgColourServer)path.Fill).Colour);
// default stroke color is null (transparent)
Assert.IsNull(path.Stroke);

doc = GenerateLexerTestFile("fill:; stroke: #00557;");
path = doc.GetElementById<SvgPath>("path1");
Assert.AreEqual(System.Drawing.Color.Black, ((SvgColourServer)path.Fill).Colour);
Assert.IsNull(path.Stroke);
}

/// <summary>
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit aa4a6b3

Please sign in to comment.