Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IsNullOrEmptyHtml extension method for HtmlString #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions src/storm.umbraco.contrib/Extensions/HtmlStringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using HtmlAgilityPack;
using System.Web;

namespace storm.umbraco.contrib.Extensions
{
public static class HtmlStringExtensions
{
/// <summary>
/// Indicates whether the specified <see cref="HtmlString">HtmlString</see> is null or contains only html tags with no inner text.
/// </summary>
/// <param name="value">The HtmlString to test.</param>
/// <returns></returns>
public static bool IsNullOrEmptyHtml(this IHtmlString value)
{
if (value == null)
{
return true;
}

var document = new HtmlDocument();
document.LoadHtml(value.ToHtmlString());
string textValue = HtmlEntity.DeEntitize(document.DocumentNode.InnerText);

return string.IsNullOrWhiteSpace(textValue);
}
}
}
32 changes: 32 additions & 0 deletions tests/contrib.tests/Extensions/IsNullOrEmptyHtmlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using storm.umbraco.contrib.Extensions;
using System.Web;
using Xunit;

namespace contrib.tests.Extensions
{
public class IsNullOrEmptyHtmlTests
{
[Fact]
public void Htmlstring_NullOrEmptyHtml_Null()
{
IHtmlString value = null;

Assert.True(value.IsNullOrEmptyHtml());
}

[Fact]
public void Htmlstring_NullOrEmptyHtml_EmptyHtml()
{
IHtmlString value = new HtmlString("<p> </p>");

Assert.True(value.IsNullOrEmptyHtml());
}

[Fact]
public void Htmlstring_NullOrEmptyHtml_HtmlWithContent()
{
IHtmlString value = new HtmlString("<p>Lorem ipsum dolor sit amet.</p>");
Assert.False(value.IsNullOrEmptyHtml());
}
}
}
4 changes: 4 additions & 0 deletions tests/contrib.tests/contrib.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
<ProjectReference Include="..\..\src\storm.umbraco.contrib\storm.umbraco.contrib.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Web" />
</ItemGroup>

</Project>