Skip to content

Commit cad9003

Browse files
committed
Added null guard to AddClassFromAttributes fixes #12
1 parent 0a491e3 commit cad9003

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

BlazorComponentUtilities.Tests/CssBuilderTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,23 @@ public void ShouldNotThrowWhenNullFor_BuildClassesFromAttributes()
133133
}
134134
}
135135

136+
[Fact]
137+
public void ShouldNotThrowWhenNullForAttributeItem_BuildClassesFromAttributes()
138+
{
139+
{
140+
//arrange
141+
// Simulates Razor Components attribute splatting feature
142+
IReadOnlyDictionary<string, object> attributes = new Dictionary<string, object> { { "class", null } };
143+
144+
//act
145+
var ClassToRender = new CssBuilder("item-one")
146+
.AddClassFromAttributes(attributes)
147+
.Build();
148+
//assert
149+
ClassToRender.Should().Be("item-one");
150+
}
151+
}
152+
136153
[Fact]
137154
public void ForceNullForWhitespace_BuildClassesFromAttributes()
138155
{

BlazorComponentUtilities/BlazorComponentUtilities.csproj

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
<Authors>Ed Charbeneau</Authors>
1616
<Company>BlazorPro</Company>
1717
<Product>BlazorComponentUtilities CssBuilder</Product>
18-
<PackageReleaseNotes>1.7.1
19-
Fixed trivial performance issue with redundant assignment when building strings
20-
1.7.0
21-
Added SetPrefix method to reduce reptititive class prefixing
22-
1.6.0
23-
Added Func&lt;string&gt; value overload for ValueBuilder
18+
<PackageReleaseNotes>
19+
1.8.0
20+
Fixed null ref error when using AddClassFromAttributes and passing a null attribute value.
21+
1.7.1
22+
Fixed trivial performance issue with redundant assignment when building strings
23+
1.7.0
24+
Added SetPrefix method to reduce reptititive class prefixing
25+
1.6.0
26+
Added Func&lt;string&gt; value overload for ValueBuilder
2427
1.5.0
2528
Added Func&lt;string&gt; value overload for StyleBuilder
2629
Minor performance improvements for .Add*Attributes
@@ -32,10 +35,10 @@
3235
Added utility method for returning null for empty strings. This is useful for splatting scenarios.
3336
Added utility method for starting off new builders.
3437
</PackageReleaseNotes>
35-
<Version>1.7.1</Version>
38+
<Version>1.8.0</Version>
3639
<PackageIcon>blazor-glove-256.jpg</PackageIcon>
37-
<AssemblyVersion>1.7.1.0</AssemblyVersion>
38-
<FileVersion>1.7.1.0</FileVersion>
40+
<AssemblyVersion>1.8.0.0</AssemblyVersion>
41+
<FileVersion>1.8.0.0</FileVersion>
3942
</PropertyGroup>
4043

4144
<ItemGroup>

BlazorComponentUtilities/CssBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public CssBuilder AddValue(string value)
117117
/// <returns>CssBuilder</returns>
118118
public CssBuilder AddClassFromAttributes(IReadOnlyDictionary<string, object> additionalAttributes) =>
119119
additionalAttributes == null ? this :
120-
additionalAttributes.TryGetValue("class", out var c) ? AddClass(c.ToString()) : this;
120+
additionalAttributes.TryGetValue("class", out var c) && c != null ? AddClass(c.ToString()) : this;
121121

122122
/// <summary>
123123
/// Finalize the completed CSS Classes as a string.

0 commit comments

Comments
 (0)