Skip to content

Commit

Permalink
Added null guard to AddClassFromAttributes fixes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
EdCharbeneau committed Dec 10, 2021
1 parent 0a491e3 commit cad9003
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
17 changes: 17 additions & 0 deletions BlazorComponentUtilities.Tests/CssBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ public void ShouldNotThrowWhenNullFor_BuildClassesFromAttributes()
}
}

[Fact]
public void ShouldNotThrowWhenNullForAttributeItem_BuildClassesFromAttributes()
{
{
//arrange
// Simulates Razor Components attribute splatting feature
IReadOnlyDictionary<string, object> attributes = new Dictionary<string, object> { { "class", null } };

//act
var ClassToRender = new CssBuilder("item-one")
.AddClassFromAttributes(attributes)
.Build();
//assert
ClassToRender.Should().Be("item-one");
}
}

[Fact]
public void ForceNullForWhitespace_BuildClassesFromAttributes()
{
Expand Down
21 changes: 12 additions & 9 deletions BlazorComponentUtilities/BlazorComponentUtilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
<Authors>Ed Charbeneau</Authors>
<Company>BlazorPro</Company>
<Product>BlazorComponentUtilities CssBuilder</Product>
<PackageReleaseNotes>1.7.1
Fixed trivial performance issue with redundant assignment when building strings
1.7.0
Added SetPrefix method to reduce reptititive class prefixing
1.6.0
Added Func&lt;string&gt; value overload for ValueBuilder
<PackageReleaseNotes>
1.8.0
Fixed null ref error when using AddClassFromAttributes and passing a null attribute value.
1.7.1
Fixed trivial performance issue with redundant assignment when building strings
1.7.0
Added SetPrefix method to reduce reptititive class prefixing
1.6.0
Added Func&lt;string&gt; value overload for ValueBuilder
1.5.0
Added Func&lt;string&gt; value overload for StyleBuilder
Minor performance improvements for .Add*Attributes
Expand All @@ -32,10 +35,10 @@
Added utility method for returning null for empty strings. This is useful for splatting scenarios.
Added utility method for starting off new builders.
</PackageReleaseNotes>
<Version>1.7.1</Version>
<Version>1.8.0</Version>
<PackageIcon>blazor-glove-256.jpg</PackageIcon>
<AssemblyVersion>1.7.1.0</AssemblyVersion>
<FileVersion>1.7.1.0</FileVersion>
<AssemblyVersion>1.8.0.0</AssemblyVersion>
<FileVersion>1.8.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion BlazorComponentUtilities/CssBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public CssBuilder AddValue(string value)
/// <returns>CssBuilder</returns>
public CssBuilder AddClassFromAttributes(IReadOnlyDictionary<string, object> additionalAttributes) =>
additionalAttributes == null ? this :
additionalAttributes.TryGetValue("class", out var c) ? AddClass(c.ToString()) : this;
additionalAttributes.TryGetValue("class", out var c) && c != null ? AddClass(c.ToString()) : this;

/// <summary>
/// Finalize the completed CSS Classes as a string.
Expand Down

0 comments on commit cad9003

Please sign in to comment.