Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

CSS: Support omitting semicolon from last property #14306

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Xamarin.Forms.Core.UnitTests/StyleSheets/StyleParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.IO;
using NUnit.Framework;

namespace Xamarin.Forms.StyleSheets.UnitTests
{
[TestFixture]
public class StyleParserTests
{
[TestCase("font-size: 10", 1)]
[TestCase("font-size: 10;", 1)]
[TestCase("font-size: 10 background-color: blue", 0)]
[TestCase("font-size: 10; background-color: blue", 2)]
[TestCase("font-size: 10; background-color: blue;", 2)]
public void TestCase(string css, int propertyCount)
{
using (var reader = new StringReader(css))
{
var parser = Style.Parse(new CssReader(reader));

Assert.AreEqual(propertyCount, parser.Declarations.Count);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
<Compile Include="PathSegmentTests.cs" />
<Compile Include="GeometryTests.cs" />
<Compile Include="ShellParameterPassingTests.cs" />
<Compile Include="StyleSheets\StyleParserTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Xamarin.Forms.Core\Xamarin.Forms.Core.csproj">
Expand Down
29 changes: 25 additions & 4 deletions Xamarin.Forms.Core/StyleSheets/Style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public static Style Parse(CssReader reader, char stopChar = '\0')
Style style = new Style();
string propertyName = null, propertyValue = null;

void AddAndClearLastDeclaration()
{
if (!string.IsNullOrEmpty(propertyName) && !string.IsNullOrEmpty(propertyValue))
style.Declarations.Add(propertyName, propertyValue);
propertyName = propertyValue = null;
}

int p;
reader.SkipWhiteSpaces();
bool readingName = true;
Expand All @@ -35,15 +42,17 @@ public static Style Parse(CssReader reader, char stopChar = '\0')
break;
case ';':
reader.Read();
if (!string.IsNullOrEmpty(propertyName) && !string.IsNullOrEmpty(propertyValue))
style.Declarations.Add(propertyName, propertyValue);
propertyName = propertyValue = null;
AddAndClearLastDeclaration();
readingName = true;
reader.SkipWhiteSpaces();
break;
default:
if ((char)p == stopChar)
{
AddAndClearLastDeclaration();

return style;
}

if (readingName)
{
Expand All @@ -52,10 +61,22 @@ public static Style Parse(CssReader reader, char stopChar = '\0')
throw new Exception();
}
else
propertyValue = reader.ReadUntil(stopChar, ';', ':');
{
propertyValue = reader.ReadUntil(out var limitedBy, stopChar, ';', ':');

if (limitedBy.HasValue && limitedBy == ':')
{
style.Declarations.Clear();
return style;
}
}

break;
}
}

AddAndClearLastDeclaration();

return style;
}

Expand Down
7 changes: 6 additions & 1 deletion Xamarin.Forms.Core/StyleSheets/TextReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ public static string ReadName(this TextReader reader)
return sb.ToString();
}

public static string ReadUntil(this TextReader reader, params char[] limit)
public static string ReadUntil(this TextReader reader, out char? limitedBy, params char[] limit)
{
limitedBy = null;

var sb = new StringBuilder();
int p;
while ((p = reader.Peek()) > 0)
{
var c = unchecked((char)p);
if (limit != null && limit.Contains(c))
{
limitedBy = c;
break;
}
reader.Read();
sb.Append(c);
}
Expand Down