Skip to content

Commit 618f8c8

Browse files
Refactor GetWidthDescriptor to use HtmlHelper.AnonymousObjectToHtmlAttributes for both anonymous objects and dictionaries
1 parent ceef4be commit 618f8c8

1 file changed

Lines changed: 17 additions & 39 deletions

File tree

src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
using System.Reflection;
2-
using System.Text.Json;
3-
using HtmlAgilityPack;
1+
using HtmlAgilityPack;
42
using Microsoft.AspNetCore.Html;
53
using Microsoft.AspNetCore.Mvc.Rendering;
64
using Microsoft.AspNetCore.Mvc.ViewFeatures;
75
using Microsoft.AspNetCore.Razor.TagHelpers;
86
using Sitecore.AspNetCore.SDK.LayoutService.Client.Response.Model.Fields;
97
using Sitecore.AspNetCore.SDK.LayoutService.Client.Response.Model.Properties;
10-
using Sitecore.AspNetCore.SDK.LayoutService.Client.Serialization;
118
using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions;
129
using Sitecore.AspNetCore.SDK.RenderingEngine.Rendering;
1310

@@ -173,50 +170,32 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
173170
}
174171
}
175172

176-
private static string? TryParseParameter(object parameters, string paramName, PropertyInfo[] properties)
177-
{
178-
PropertyInfo? prop = properties.FirstOrDefault(p => p.Name.Equals(paramName, StringComparison.OrdinalIgnoreCase));
179-
return prop?.GetValue(parameters)?.ToString();
180-
}
181-
182173
private static string? GetWidthDescriptor(object? parameters)
183174
{
184175
if (parameters == null)
185176
{
186177
return null;
187178
}
188179

189-
string? width = null;
180+
IDictionary<string, object> dictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(parameters);
190181

191-
if (parameters is Dictionary<string, object> dictionary)
182+
// Priority: w > mw > width > maxWidth (matching Content SDK behavior + legacy support)
183+
string? width = null;
184+
if (dictionary.TryGetValue("w", out var wValue))
192185
{
193-
// Priority: w > mw > width > maxWidth (matching Content SDK behavior + legacy support)
194-
if (dictionary.TryGetValue("w", out var wValue))
195-
{
196-
width = wValue.ToString();
197-
}
198-
else if (dictionary.TryGetValue("mw", out var mwValue))
199-
{
200-
width = mwValue.ToString();
201-
}
202-
else if (dictionary.TryGetValue("width", out var widthValue))
203-
{
204-
width = widthValue.ToString();
205-
}
206-
else if (dictionary.TryGetValue("maxWidth", out var maxWidthValue))
207-
{
208-
width = maxWidthValue.ToString();
209-
}
186+
width = wValue.ToString();
210187
}
211-
else
188+
else if (dictionary.TryGetValue("mw", out var mwValue))
212189
{
213-
PropertyInfo[] properties = parameters.GetType().GetProperties();
214-
215-
// Priority: w > mw > width > maxWidth
216-
width = TryParseParameter(parameters, "w", properties)
217-
?? TryParseParameter(parameters, "mw", properties)
218-
?? TryParseParameter(parameters, "width", properties)
219-
?? TryParseParameter(parameters, "maxWidth", properties);
190+
width = mwValue.ToString();
191+
}
192+
else if (dictionary.TryGetValue("width", out var widthValue))
193+
{
194+
width = widthValue.ToString();
195+
}
196+
else if (dictionary.TryGetValue("maxWidth", out var maxWidthValue))
197+
{
198+
width = maxWidthValue.ToString();
220199
}
221200

222201
if (width != null && int.TryParse(width, out int widthValueInt) && widthValueInt <= 0)
@@ -363,8 +342,7 @@ private string GenerateSrcSetAttribute(ImageField imageField)
363342

364343
List<string> srcSetEntries = new();
365344

366-
// filter out null items directly
367-
foreach (object srcSetItem in parsedSrcSet.Where(item => item != null))
345+
foreach (object srcSetItem in parsedSrcSet)
368346
{
369347
// Get width descriptor first to check if this entry should be included
370348
string? descriptor = GetWidthDescriptor(srcSetItem);

0 commit comments

Comments
 (0)