Skip to content

Commit

Permalink
Minor performance update for HtmlContentBuilderExtensions (#14664)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Nov 13, 2023
1 parent e50e81c commit 5a91f3b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Html;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.Mvc.Utilities;
using OrchardCore.DisplayManagement.Html;
using OrchardCore.Mvc.Utilities;

namespace OrchardCore;

Expand All @@ -20,11 +20,15 @@ public static IHtmlContent GetPartWrapperClasses(this IOrchardHelper helper, Con

if (partDefinition?.PartDefinition != null)
{
builder.AppendSeparatedValue($"{PartWrapperPrefix}-{partDefinition.PartDefinition.Name.HtmlClassify()}");
builder.AppendSeparatedValue(PartWrapperPrefix);
builder.AppendHyphen();
builder.Append(partDefinition.PartDefinition.Name.HtmlClassify());

if (partDefinition.IsNamedPart())
{
builder.AppendSeparatedValue($"{PartWrapperPrefix}-{partDefinition.Name.HtmlClassify()}");
builder.AppendSeparatedValue(PartWrapperPrefix);
builder.AppendHyphen();
builder.Append(partDefinition.Name.HtmlClassify());
}
}

Expand All @@ -41,11 +45,19 @@ public static IHtmlContent GetFieldWrapperClasses(this IOrchardHelper helper, Co

if (fieldDefinition?.PartDefinition != null)
{
builder.AppendSeparatedValue($"{FieldWrapperPrefix}-{fieldDefinition.PartDefinition.Name}-{fieldDefinition.Name}".HtmlClassify());
builder.AppendSeparatedValue(FieldWrapperPrefix);
builder.AppendHyphen();
builder.Append(fieldDefinition.PartDefinition.Name.HtmlClassify());
builder.AppendHyphen();
builder.Append(fieldDefinition.Name.HtmlClassify());

if (fieldDefinition.IsNamedPart())
{
builder.AppendSeparatedValue($"{FieldWrapperPrefix}-{fieldDefinition.ContentTypePartDefinition.Name}-{fieldDefinition.Name}".HtmlClassify());
builder.AppendSeparatedValue(FieldWrapperPrefix);
builder.AppendHyphen();
builder.Append(fieldDefinition.ContentTypePartDefinition.Name.HtmlClassify());
builder.AppendHyphen();
builder.Append(fieldDefinition.Name.HtmlClassify());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace OrchardCore.DisplayManagement.Html;

public static class HtmlContentBuilderExtensions
{
private static readonly HtmlString _seperator = new(" ");
private static readonly HtmlString _whitespace = new(" ");
private static readonly HtmlString _hyphen = new("-");

public static HtmlContentBuilder AppendSeparatedValue(this HtmlContentBuilder builder, string value)
{
Expand All @@ -25,14 +26,28 @@ public static HtmlContentBuilder AppendSeparatedValue(this HtmlContentBuilder bu

// At this point, we already know that the builder has at least one entry, so we append a single space to the class name.
// We pass create 'HtmlString' here to prevent the builder from preforming string.IsNullOrWhiteSpace again for performance reason.
builder.AppendHtml(_seperator);
builder.AppendWhitespace();

// We use 'Append' here to ensure that the value is encoded.
builder.Append(value);

return builder;
}

public static HtmlContentBuilder AppendWhitespace(this HtmlContentBuilder builder)
{
builder.AppendHtml(_whitespace);

return builder;
}

public static HtmlContentBuilder AppendHyphen(this HtmlContentBuilder builder)
{
builder.AppendHtml(_hyphen);

return builder;
}

public static HtmlContentBuilder AppendSeparatedValues(this HtmlContentBuilder builder, IList<string> values)
{
if (values?.Count > 0)
Expand Down

0 comments on commit 5a91f3b

Please sign in to comment.