Skip to content

Commit 80a0a85

Browse files
committed
v3.0.0-alpha-07
1 parent 0911353 commit 80a0a85

11 files changed

+64
-58
lines changed

src/BlazorStyled.Tests/BlazorStyled.Tests.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.0-preview-20200309-01" />
11-
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
12-
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
13-
<PackageReference Include="coverlet.collector" Version="1.2.0">
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
12+
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
13+
<PackageReference Include="coverlet.collector" Version="1.2.1">
1414
<PrivateAssets>all</PrivateAssets>
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
</PackageReference>

src/BlazorStyled/BlazorStyled.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<LangVersion>8.0</LangVersion>
77
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
88
<PackageId>BlazorStyled</PackageId>
9-
<Version>3.0.0-alpha-05</Version>
9+
<Version>3.0.0-alpha-07</Version>
1010
<Authors>Chanan Braunstein</Authors>
1111
<Title>BlazorStyled</Title>
1212
<Description>CSS in Blazor Components</Description>
@@ -17,8 +17,8 @@
1717

1818

1919
<ItemGroup>
20-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.3" />
21-
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="3.1.3" />
20+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.4" />
21+
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="3.1.4" />
2222
</ItemGroup>
2323

2424
</Project>

src/BlazorStyled/Internal/Cache.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BlazorStyled.Internal
44
{
5-
class Cache
5+
internal class Cache
66
{
77
public readonly IDictionary<string, string> Seen = new SortedDictionary<string, string>();
88
}

src/BlazorStyled/Internal/ScriptManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal async Task UpdatedParsedClasses(string stylesheetId, string stylesheetN
4242
string[] rules = parsedClasses.Select(c => c.ToString()).ToArray();
4343
foreach (string rule in rules)
4444
{
45-
if(rule.IndexOf("<br") != -1)
45+
if (rule.IndexOf("<br") != -1)
4646
{
4747
Console.WriteLine("rule");
4848
}

src/BlazorStyled/Internal/StringExtensions.cs

+9-19
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static string RemoveDuplicateSpaces(this string source)
2323
for (int i = 0; i < len; i++)
2424
{
2525
char ch = src[i];
26-
if(Char.IsWhiteSpace(ch))
26+
if (char.IsWhiteSpace(ch))
2727
{
2828
if (lastWasWS == false)
2929
{
@@ -187,7 +187,7 @@ public static uint GetStableHashCode(this string str)
187187

188188
public static string GetStableHashCodeString(this string str)
189189
{
190-
uint i = (uint)str.GetStableHashCode();
190+
uint i = str.GetStableHashCode();
191191
return i.ConvertToBase64Arithmetic();
192192
}
193193

@@ -222,31 +222,21 @@ public static string ConvertToBase64Arithmetic(this uint i)
222222
return ret;
223223
}
224224

225-
private static uint To32BitFnv1aHash(this string toHash, bool separateUpperByte = false)
225+
private static uint To32BitFnv1aHash(this string toHash)
226226
{
227-
IEnumerable<byte> bytesToHash;
228-
229-
if (separateUpperByte)
230-
bytesToHash = toHash.ToCharArray()
231-
.Select(c => new[] { (byte)((c - (byte)c) >> 8), (byte)c })
232-
.SelectMany(c => c);
233-
else
234-
bytesToHash = toHash.ToCharArray()
235-
.Select(Convert.ToByte);
236-
237-
//this is the actual hash function; very simple
238-
uint hash = FnvConstants.FnvOffset32;
239-
240227
unchecked
241228
{
242-
foreach (var chunk in bytesToHash)
229+
ReadOnlySpan<char> span = toHash.AsSpan();
230+
uint hash = FnvConstants.FnvOffset32;
231+
232+
foreach (char chunk in span)
243233
{
244234
hash ^= chunk;
245235
hash *= FnvConstants.FnvPrime32;
246236
}
237+
238+
return hash;
247239
}
248-
249-
return hash;
250240
}
251241

252242
private static class FnvConstants

src/BlazorStyled/Internal/StyledImpl.cs

+28-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class StyledImpl : IStyled
1414
private readonly int _priority;
1515
private readonly Cache _cache;
1616

17-
public StyledImpl(ScriptManager scriptManager,Cache cache) : this(scriptManager, cache, DEFAULT, 100_000)
17+
public StyledImpl(ScriptManager scriptManager, Cache cache) : this(scriptManager, cache, DEFAULT, 100_000)
1818
{
1919

2020
}
@@ -32,28 +32,34 @@ public async Task<string> CssAsync(string className, string css)
3232
try
3333
{
3434
string preParseHash = css.GetStableHashCodeString();
35-
if(!_cache.Seen.ContainsKey(preParseHash))
35+
if (!_cache.Seen.ContainsKey(preParseHash))
3636
{
3737
css = css.RemoveComments().RemoveDuplicateSpaces();
3838
IList<ParsedClass> parsedClasses = css.GetClasses(className);
3939
if (parsedClasses.Count > 0)
4040
{
4141
string hash = parsedClasses.First().IsMediaQuery ? parsedClasses.First().ChildClasses.First().Name.Replace(".", string.Empty) : parsedClasses.First().Name;
4242
await _scriptManager.UpdatedParsedClasses(_id.GetStableHashCodeString(), _id, _priority, parsedClasses);
43-
_cache.Seen.Add(preParseHash, hash);
43+
if (!_cache.Seen.ContainsKey(preParseHash))
44+
{
45+
_cache.Seen.Add(preParseHash, hash);
46+
}
4447
return hash;
4548
}
4649
else
4750
{
48-
_cache.Seen.Add(preParseHash, string.Empty);
51+
if (!_cache.Seen.ContainsKey(preParseHash))
52+
{
53+
_cache.Seen.Add(preParseHash, string.Empty);
54+
}
4955
return string.Empty;
5056
}
51-
}
57+
}
5258
else
5359
{
5460
return _cache.Seen[preParseHash];
5561
}
56-
62+
5763
}
5864
catch (StyledException e)
5965
{
@@ -94,12 +100,18 @@ public string Css(string className, string css)
94100
{
95101
string hash = parsedClasses.First().IsMediaQuery ? parsedClasses.First().ChildClasses.First().Name.Replace(".", string.Empty) : parsedClasses.First().Name;
96102
Task.Run(() => _scriptManager.UpdatedParsedClasses(_id.GetStableHashCodeString(), _id, _priority, parsedClasses));
97-
_cache.Seen.Add(preParseHash, hash);
103+
if (!_cache.Seen.ContainsKey(preParseHash))
104+
{
105+
_cache.Seen.Add(preParseHash, hash);
106+
}
98107
return hash;
99108
}
100109
else
101110
{
102-
_cache.Seen.Add(preParseHash, string.Empty);
111+
if (!_cache.Seen.ContainsKey(preParseHash))
112+
{
113+
_cache.Seen.Add(preParseHash, string.Empty);
114+
}
103115
return string.Empty;
104116
}
105117
}
@@ -144,7 +156,10 @@ public async Task<string> KeyframesAsync(string css)
144156
css = "@keyframes &{" + css.RemoveComments().RemoveDuplicateSpaces() + "}";
145157
IList<ParsedClass> parsedClasses = css.GetClasses();
146158
await _scriptManager.UpdatedParsedClasses(_id.GetStableHashCodeString(), _id, _priority, parsedClasses);
147-
_cache.Seen.Add(preParseHash, parsedClasses.First().Hash);
159+
if (!_cache.Seen.ContainsKey(preParseHash))
160+
{
161+
_cache.Seen.Add(preParseHash, parsedClasses.First().Hash);
162+
}
148163
return parsedClasses.First().Hash;
149164
}
150165
else
@@ -172,7 +187,10 @@ public string Keyframes(string css)
172187
css = "@keyframes &{" + css.RemoveComments().RemoveDuplicateSpaces() + "}";
173188
IList<ParsedClass> parsedClasses = css.GetClasses();
174189
Task.Run(() => _scriptManager.UpdatedParsedClasses(_id.GetStableHashCodeString(), _id, _priority, parsedClasses));
175-
_cache.Seen.Add(preParseHash, parsedClasses.First().Hash);
190+
if (!_cache.Seen.ContainsKey(preParseHash))
191+
{
192+
_cache.Seen.Add(preParseHash, parsedClasses.First().Hash);
193+
}
176194
return parsedClasses.First().Hash;
177195
}
178196
else

src/BlazorStyled/ServiceCollectionExtensions.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using BlazorStyled.Internal;
22
using Microsoft.Extensions.DependencyInjection;
33
using System;
4-
using System.Collections.Generic;
54

65
namespace BlazorStyled
76
{

src/BlazorStyled/Styled.cs

+10-11
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private async Task ProcessParameters()
3939
{
4040
IStyled styled = Id == null ? StyledService : Priority.HasValue ? StyledService.WithId(Id, Priority.Value) : StyledService.WithId(Id);
4141

42-
if(Classname == "blazor-styled-hide")
42+
if (Classname == "blazor-styled-hide")
4343
{
4444
Classname = null;
4545
}
@@ -52,41 +52,41 @@ private async Task ProcessParameters()
5252
{
5353
if (IsKeyframes)
5454
{
55-
classname = styled.Keyframes(content);
55+
classname = await styled.KeyframesAsync(content);
5656
}
5757
else if (Classname != null && MediaQuery == MediaQueries.None && _previousClassname == null)
5858
{
5959
//html elements
60-
styled.Css(ApplyPseudoClass(Classname), content);
60+
await styled.CssAsync(ApplyPseudoClass(Classname), content);
6161
}
6262
else if (MediaQuery != MediaQueries.None && ClassnameChanged.HasDelegate)
6363
{
6464
//If ClassnameChanged has a delegate then @bind-Classname was used and this is a "new" style
6565
//Otherwise Classname was used and this an existing style which will be handled below
6666
content = WrapWithMediaQuery("&{" + content + "}");
67-
classname = styled.Css(content);
67+
classname = await styled.CssAsync(content);
6868
}
6969
else if (Classname != null && MediaQuery != MediaQueries.None && !ClassnameChanged.HasDelegate && _previousClassname == null)
7070
{
7171
//Media query support for classes where an existing Classname already exists
7272
content = WrapClass(ApplyPseudoClass(Classname), content);
73-
styled.Css(WrapWithMediaQuery(content));
73+
await styled.CssAsync(WrapWithMediaQuery(content));
7474
}
7575
else if (Classname == null && PseudoClass == PseudoClasses.None && MediaQuery != MediaQueries.None && _previousClassname == null)
7676
{
7777
//Media queries for html elements
78-
styled.Css(GetMediaQuery(), content);
78+
await styled.CssAsync(GetMediaQuery(), content);
7979
}
8080
else if (Classname != null && PseudoClass != PseudoClasses.None && MediaQuery == MediaQueries.None && _previousClassname == null)
8181
{
8282
content = WrapClass(ApplyPseudoClass(Classname), content);
83-
styled.Css(content);
83+
await styled.CssAsync(content);
8484
}
8585
else
8686
{
8787
if (PseudoClass == PseudoClasses.None && MediaQuery == MediaQueries.None)
8888
{
89-
classname = styled.Css(content);
89+
classname = await styled.CssAsync(content);
9090
}
9191
}
9292
if (ComposeAttributes == null || !ClassnameChanged.HasDelegate)
@@ -137,9 +137,8 @@ private string GetComposeClasses()
137137
{
138138
if (ComposeAttributes[key] != null)
139139
{
140-
string ifKey = key + "if";
141-
var kvp = ComposeAttributes.FirstOrDefault(x => String.Equals(x.Key, ifKey, StringComparison.InvariantCultureIgnoreCase));
142-
if(kvp.Key != null)
140+
KeyValuePair<string, object> kvp = ComposeAttributes.FirstOrDefault(x => x.Key.StartsWith(key, StringComparison.InvariantCultureIgnoreCase) && x.Key.EndsWith("if", StringComparison.InvariantCultureIgnoreCase));
141+
if (kvp.Key != null)
143142
{
144143
if (bool.TryParse(kvp.Value.ToString().ToString(), out bool result) && result)
145144
{

src/ClientSideSample/ClientSideSample.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.0-preview4.20210.8" />
10-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0-preview4.20210.8" PrivateAssets="all" />
11-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0-preview4.20210.8" PrivateAssets="all" />
12-
<PackageReference Include="System.Net.Http.Json" Version="3.2.0-preview5.20210.3" />
9+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.0-rc1.20223.4" />
10+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0-rc1.20223.4" PrivateAssets="all" />
11+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0-rc1.20223.4" PrivateAssets="all" />
12+
<PackageReference Include="System.Net.Http.Json" Version="3.2.0-rc1.20217.1" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

src/HostedSample/HostedSample.Server.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="3.2.0-preview4.20210.8" />
9+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="3.2.0-rc1.20223.4" />
1010
</ItemGroup>
1111

1212
</Project>

src/SampleCore/SampleCore.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
<ItemGroup>
99
<PackageReference Include="BlazorPrettyCode" Version="1.5.0-alpha-03" />
10-
<PackageReference Include="BlazorTypography" Version="1.5.0-alpha-04" />
11-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.3" />
12-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.3" />
10+
<PackageReference Include="BlazorTypography" Version="1.5.0-alpha-05" />
11+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.4" />
12+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.4" />
1313
<PackageReference Include="Polished" Version="1.0.0" />
1414
</ItemGroup>
1515

0 commit comments

Comments
 (0)