Files
Umbraco-CMS/src/Umbraco.Core/Extensions/HtmlEncodedStringExtensions.cs
Erik-Jan Westendorp 010ea5a2aa IsNullOrWhiteSpace Extension method for HtmlEncodedString (#13747)
* Add IsNullOrWhiteSpace Extension method

IsNullOrWhiteSpace extension method for IHtmlEncodedString

* Move extension method

* Add a UnitTest for the IsNullOrWhiteSpace extension method

* Update unit test

(cherry picked from commit 9771e77243)
2023-03-17 08:21:21 +01:00

20 lines
1.0 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Umbraco.Cms.Core.Strings;
namespace Umbraco.Extensions;
public static class HtmlEncodedStringExtensions
{
/// <summary>
/// Checks if the specified <see cref="IHtmlEncodedString" /> is <c>null</c> or only contains whitespace, optionally after all HTML tags have been stripped/removed.
/// </summary>
/// <param name="htmlEncodedString">The encoded HTML string.</param>
/// <param name="stripHtml">If set to <c>true</c> strips/removes all HTML tags.</param>
/// <returns>
/// Returns <c>true</c> if the HTML string is <c>null</c> or only contains whitespace, optionally after all HTML tags have been stripped/removed.
/// </returns>
public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this IHtmlEncodedString? htmlEncodedString, bool stripHtml = false)
=> (htmlEncodedString?.ToHtmlString() is var htmlString && string.IsNullOrWhiteSpace(htmlString)) ||
(stripHtml && string.IsNullOrWhiteSpace(htmlString.StripHtml()));
}