Skip to content

Commit

Permalink
Revert "Add the ability for CssBuilder to properly Add multiple class…
Browse files Browse the repository at this point in the history
…es without duplicates (#2104)"

This reverts commit dbfa663.
  • Loading branch information
vnbaaij committed Jun 10, 2024
1 parent 638d566 commit c88f64f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 215 deletions.
87 changes: 13 additions & 74 deletions examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14235,25 +14235,25 @@
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Utilities.CssBuilder.AddClass(System.String)">
<summary>
Adds one or more CSS Classes to the builder with space separator.
Adds a CSS Class to the builder with space separator.
</summary>
<param name="values">Space-separated CSS Classes to add</param>
<param name="value">CSS Class to add</param>
<returns>CssBuilder</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Utilities.CssBuilder.AddClass(System.String,System.Boolean)">
<summary>
Adds one or more CSS Classes to the builder with space separator, based on a condition.
Adds a conditional CSS Class to the builder with space separator.
</summary>
<param name="value">Space-separated CSS Classes to add</param>
<param name="when">Condition in which the CSS Classes are added.</param>
<param name="value">CSS Class to conditionally add.</param>
<param name="when">Condition in which the CSS Class is added.</param>
<returns>CssBuilder</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Utilities.CssBuilder.AddClass(System.String,System.Func{System.Boolean})">
<summary>
Adds one or more CSS Classes to the builder with space separator, based on a condition.
Adds a conditional CSS Class to the builder with space separator.
</summary>
<param name="value">Space-separated CSS Classes to add</param>
<param name="when">Function that returns a condition in which the CSS Classes are added.</param>
<param name="value">CSS Class to conditionally add.</param>
<param name="when">Condition in which the CSS Class is added.</param>
<returns>CssBuilder</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Utilities.CssBuilder.Build">
Expand All @@ -14266,37 +14266,14 @@
<summary>
ToString should only and always call Build to finalize the rendered string.
</summary>
<returns>string</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Utilities.CssBuilder.IsValidClassName(System.String)">
<summary>
Validates if the provided class name is valid.
</summary>
<param name="className">CSS class name to validate</param>
<returns>True if valid, otherwise false</returns>
<returns></returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Utilities.CssBuilder.SplitAndValidate(System.String)">
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Utilities.CssBuilder.AddRaw(System.String)">
<summary>
Splits a space-separated string of class names and validates each one.
Adds a raw string to the builder that will be concatenated with the next classes or value added to the builder.
</summary>
<param name="input">Space-separated CSS Classes</param>
<returns>Enumerable of valid class names</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Utilities.CssBuilder.GenerateValidClassNameRegex">
<remarks>
Pattern:<br/>
<code>^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$</code><br/>
Options:<br/>
<code>RegexOptions.Compiled</code><br/>
Explanation:<br/>
<code>
○ Match if at the beginning of the string.<br/>
○ Match '-' atomically, optionally.<br/>
○ Match a character in the set [A-Z_a-z] greedily at least once.<br/>
○ Match a character in the set [-0-9A-Z_a-z] atomically any number of times.<br/>
○ Match if at the end of the string or if before an ending newline.<br/>
</code>
</remarks>
<param name="value"></param>
<returns>StyleBuilder</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.Utilities.InlineStyleBuilder.#ctor">
<summary>
Expand Down Expand Up @@ -14485,38 +14462,6 @@
<param name="inputSpan">The text being scanned by the regular expression.</param>
<returns>true if the regular expression matches at the current position; otherwise, false.</returns>
</member>
<member name="T:System.Text.RegularExpressions.Generated.GenerateValidClassNameRegex_1">
<summary>Custom <see cref="T:System.Text.RegularExpressions.Regex"/>-derived type for the GenerateValidClassNameRegex method.</summary>
</member>
<member name="F:System.Text.RegularExpressions.Generated.GenerateValidClassNameRegex_1.Instance">
<summary>Cached, thread-safe singleton instance.</summary>
</member>
<member name="M:System.Text.RegularExpressions.Generated.GenerateValidClassNameRegex_1.#ctor">
<summary>Initializes the instance.</summary>
</member>
<member name="T:System.Text.RegularExpressions.Generated.GenerateValidClassNameRegex_1.RunnerFactory">
<summary>Provides a factory for creating <see cref="T:System.Text.RegularExpressions.RegexRunner"/> instances to be used by methods on <see cref="T:System.Text.RegularExpressions.Regex"/>.</summary>
</member>
<member name="M:System.Text.RegularExpressions.Generated.GenerateValidClassNameRegex_1.RunnerFactory.CreateInstance">
<summary>Creates an instance of a <see cref="T:System.Text.RegularExpressions.RegexRunner"/> used by methods on <see cref="T:System.Text.RegularExpressions.Regex"/>.</summary>
</member>
<member name="T:System.Text.RegularExpressions.Generated.GenerateValidClassNameRegex_1.RunnerFactory.Runner">
<summary>Provides the runner that contains the custom logic implementing the specified regular expression.</summary>
</member>
<member name="M:System.Text.RegularExpressions.Generated.GenerateValidClassNameRegex_1.RunnerFactory.Runner.Scan(System.ReadOnlySpan{System.Char})">
<summary>Scan the <paramref name="inputSpan"/> starting from base.runtextstart for the next match.</summary>
<param name="inputSpan">The text being scanned by the regular expression.</param>
</member>
<member name="M:System.Text.RegularExpressions.Generated.GenerateValidClassNameRegex_1.RunnerFactory.Runner.TryFindNextPossibleStartingPosition(System.ReadOnlySpan{System.Char})">
<summary>Search <paramref name="inputSpan"/> starting from base.runtextpos for the next location a match could possibly start.</summary>
<param name="inputSpan">The text being scanned by the regular expression.</param>
<returns>true if a possible match was found; false if no more matches are possible.</returns>
</member>
<member name="M:System.Text.RegularExpressions.Generated.GenerateValidClassNameRegex_1.RunnerFactory.Runner.TryMatchAtCurrentPosition(System.ReadOnlySpan{System.Char})">
<summary>Determine whether <paramref name="inputSpan"/> at base.runtextpos is a match for the regular expression.</summary>
<param name="inputSpan">The text being scanned by the regular expression.</param>
<returns>true if the regular expression matches at the current position; otherwise, false.</returns>
</member>
<member name="T:System.Text.RegularExpressions.Generated.Utilities">
<summary>Helper methods used by generated <see cref="T:System.Text.RegularExpressions.Regex"/>-derived implementations.</summary>
</member>
Expand All @@ -14526,11 +14471,5 @@
<member name="F:System.Text.RegularExpressions.Generated.Utilities.s_hasTimeout">
<summary>Whether <see cref="F:System.Text.RegularExpressions.Generated.Utilities.s_defaultTimeout"/> is non-infinite.</summary>
</member>
<member name="F:System.Text.RegularExpressions.Generated.Utilities.s_ascii_20FF03FEFFFF87FEFFFF07">
<summary>Supports searching for characters in or not in "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".</summary>
</member>
<member name="F:System.Text.RegularExpressions.Generated.Utilities.s_ascii_FEFFFF87FEFFFF07">
<summary>Supports searching for characters in or not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".</summary>
</member>
</members>
</doc>
87 changes: 35 additions & 52 deletions src/Core/Utilities/CssBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System.Text.RegularExpressions;
namespace Microsoft.Fast.Components.FluentUI.Utilities;

namespace Microsoft.Fast.Components.FluentUI.Utilities;

public readonly partial struct CssBuilder
public readonly struct CssBuilder
{
private readonly HashSet<string> _classes;
private readonly string[]? _userClasses;
private static readonly Regex ValidClassNameRegex = GenerateValidClassNameRegex();
private readonly string? _userClasses;

/// <summary>
/// Initializes a new instance of the <see cref="CssBuilder"/> class.
/// </summary>

public CssBuilder()
{
_classes = new HashSet<string>();
Expand All @@ -26,83 +24,68 @@ public CssBuilder(string? userClasses)
_classes = new HashSet<string>();
_userClasses = string.IsNullOrWhiteSpace(userClasses)
? null
: SplitAndValidate(userClasses).ToArray();
: string.Join(" ", userClasses.Split(' ', StringSplitOptions.RemoveEmptyEntries));
}

/// <summary>
/// Adds one or more CSS Classes to the builder with space separator.
/// Adds a CSS Class to the builder with space separator.
/// </summary>
/// <param name="values">Space-separated CSS Classes to add</param>
/// <param name="value">CSS Class to add</param>
/// <returns>CssBuilder</returns>
public CssBuilder AddClass(string? values)
{
if (!string.IsNullOrWhiteSpace(values))
{
_classes.UnionWith(SplitAndValidate(values));
}
return this;
}
public CssBuilder AddClass(string? value) => AddRaw(value);

/// <summary>
/// Adds one or more CSS Classes to the builder with space separator, based on a condition.
/// Adds a conditional CSS Class to the builder with space separator.
/// </summary>
/// <param name="value">Space-separated CSS Classes to add</param>
/// <param name="when">Condition in which the CSS Classes are added.</param>
/// <param name="value">CSS Class to conditionally add.</param>
/// <param name="when">Condition in which the CSS Class is added.</param>
/// <returns>CssBuilder</returns>
public CssBuilder AddClass(string? value, bool when) => when ? AddClass(value) : this;
public CssBuilder AddClass(string? value, bool when = true) => when ? AddClass(value) : this;

/// <summary>
/// Adds one or more CSS Classes to the builder with space separator, based on a condition.
/// Adds a conditional CSS Class to the builder with space separator.
/// </summary>
/// <param name="value">Space-separated CSS Classes to add</param>
/// <param name="when">Function that returns a condition in which the CSS Classes are added.</param>
/// <param name="value">CSS Class to conditionally add.</param>
/// <param name="when">Condition in which the CSS Class is added.</param>
/// <returns>CssBuilder</returns>
public CssBuilder AddClass(string? value, Func<bool> when) => when() ? AddClass(value) : this;
public CssBuilder AddClass(string? value, Func<bool>? when = null) => AddClass(value, when != null && when());

/// <summary>
/// Finalize the completed CSS Classes as a string.
/// </summary>
/// <returns>string</returns>
public string? Build()
{
var allClasses = _userClasses == null
? _classes
: _classes.Union(_userClasses);
var allClasses = string.IsNullOrWhiteSpace(_userClasses)
? _classes
: _classes.Union(new[] { _userClasses });

var result = string.Join(" ", allClasses);
return string.IsNullOrWhiteSpace(result) ? null : result;
if (!allClasses.Any())
{
return null;
}

return string.Join(" ", allClasses);
}

/// <summary>
/// ToString should only and always call Build to finalize the rendered string.
/// </summary>
/// <returns>string</returns>
/// <returns></returns>
public override string? ToString() => Build();

/// <summary>
/// Validates if the provided class name is valid.
/// Adds a raw string to the builder that will be concatenated with the next classes or value added to the builder.
/// </summary>
/// <param name="className">CSS class name to validate</param>
/// <returns>True if valid, otherwise false</returns>
private static bool IsValidClassName(string className)
/// <param name="value"></param>
/// <returns>StyleBuilder</returns>
private CssBuilder AddRaw(string? value)
{
return ValidClassNameRegex.IsMatch(className);
}
if (!string.IsNullOrWhiteSpace(value))
{
_classes.Add(value.Trim());
}

/// <summary>
/// Splits a space-separated string of class names and validates each one.
/// </summary>
/// <param name="input">Space-separated CSS Classes</param>
/// <returns>Enumerable of valid class names</returns>
private static IEnumerable<string> SplitAndValidate(string input)
{
return input.Split(' ', StringSplitOptions.RemoveEmptyEntries).Where(IsValidClassName);
return this;
}

/// <summary>
/// Generates the regex used to validate CSS class names.
/// </summary>
/// <returns>A compiled regex for validating CSS class names</returns>
[GeneratedRegex(@"^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$", RegexOptions.Compiled)]
private static partial Regex GenerateValidClassNameRegex();
}
Loading

0 comments on commit c88f64f

Please sign in to comment.