|
1 | | -using System.Reflection; |
2 | | -using System.Text.Json; |
3 | | -using HtmlAgilityPack; |
| 1 | +using HtmlAgilityPack; |
4 | 2 | using Microsoft.AspNetCore.Html; |
5 | 3 | using Microsoft.AspNetCore.Mvc.Rendering; |
6 | 4 | using Microsoft.AspNetCore.Mvc.ViewFeatures; |
7 | 5 | using Microsoft.AspNetCore.Razor.TagHelpers; |
8 | 6 | using Sitecore.AspNetCore.SDK.LayoutService.Client.Response.Model.Fields; |
9 | 7 | using Sitecore.AspNetCore.SDK.LayoutService.Client.Response.Model.Properties; |
10 | | -using Sitecore.AspNetCore.SDK.LayoutService.Client.Serialization; |
11 | 8 | using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions; |
12 | 9 | using Sitecore.AspNetCore.SDK.RenderingEngine.Rendering; |
13 | 10 |
|
@@ -173,50 +170,32 @@ public override void Process(TagHelperContext context, TagHelperOutput output) |
173 | 170 | } |
174 | 171 | } |
175 | 172 |
|
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 | | - |
182 | 173 | private static string? GetWidthDescriptor(object? parameters) |
183 | 174 | { |
184 | 175 | if (parameters == null) |
185 | 176 | { |
186 | 177 | return null; |
187 | 178 | } |
188 | 179 |
|
189 | | - string? width = null; |
| 180 | + IDictionary<string, object> dictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(parameters); |
190 | 181 |
|
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)) |
192 | 185 | { |
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(); |
210 | 187 | } |
211 | | - else |
| 188 | + else if (dictionary.TryGetValue("mw", out var mwValue)) |
212 | 189 | { |
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(); |
220 | 199 | } |
221 | 200 |
|
222 | 201 | if (width != null && int.TryParse(width, out int widthValueInt) && widthValueInt <= 0) |
@@ -363,8 +342,7 @@ private string GenerateSrcSetAttribute(ImageField imageField) |
363 | 342 |
|
364 | 343 | List<string> srcSetEntries = new(); |
365 | 344 |
|
366 | | - // filter out null items directly |
367 | | - foreach (object srcSetItem in parsedSrcSet.Where(item => item != null)) |
| 345 | + foreach (object srcSetItem in parsedSrcSet) |
368 | 346 | { |
369 | 347 | // Get width descriptor first to check if this entry should be included |
370 | 348 | string? descriptor = GetWidthDescriptor(srcSetItem); |
|
0 commit comments