From 5a91f3be2046346ca8db9370ab91444280571c96 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Sun, 12 Nov 2023 16:43:04 -0800 Subject: [PATCH] Minor performance update for HtmlContentBuilderExtensions (#14664) --- .../Razor/CssOrchardHelperExtensions.cs | 22 ++++++++++++++----- .../Html/HtmlContentBuilderExtensions.cs | 19 ++++++++++++++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/OrchardCore/OrchardCore.ContentManagement.Display/Razor/CssOrchardHelperExtensions.cs b/src/OrchardCore/OrchardCore.ContentManagement.Display/Razor/CssOrchardHelperExtensions.cs index f0b79d2b6a7..d744e1ec6fe 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.Display/Razor/CssOrchardHelperExtensions.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.Display/Razor/CssOrchardHelperExtensions.cs @@ -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; @@ -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()); } } @@ -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()); } } diff --git a/src/OrchardCore/OrchardCore.DisplayManagement/Html/HtmlContentBuilderExtensions.cs b/src/OrchardCore/OrchardCore.DisplayManagement/Html/HtmlContentBuilderExtensions.cs index 7d1fa591c7c..9f763a27cb8 100644 --- a/src/OrchardCore/OrchardCore.DisplayManagement/Html/HtmlContentBuilderExtensions.cs +++ b/src/OrchardCore/OrchardCore.DisplayManagement/Html/HtmlContentBuilderExtensions.cs @@ -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) { @@ -25,7 +26,7 @@ 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); @@ -33,6 +34,20 @@ public static HtmlContentBuilder AppendSeparatedValue(this HtmlContentBuilder bu 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 values) { if (values?.Count > 0)