-
Notifications
You must be signed in to change notification settings - Fork 0
Enable SrcSet support with three different input formats for Image tag helper #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Enable SrcSet support with three different input formats for Image tag helper #4
Conversation
…es in ImageTagHelperFixture
…ageTagHelper to match the Content SDK output
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds responsive image support to the ImageTagHelper by enabling srcset and sizes attributes with three input formats and preserves existing query parameters in generated media URLs.
- Introduced
SrcSetandSizesproperties with parsing and generation logic - Extended
GetSitecoreMediaUrito merge original URL parameters with new ones - Updated tests to validate
srcset,sizes, and parameter merging behavior
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/.../ImageTagHelperFixture.cs | Updated expected HTML for merged query parameters and added comprehensive srcset tests |
| src/.../ImageTagHelper.cs | Added parsing (ParseSrcSet), merging (MergeParameters), descriptor (GetWidthDescriptor), and generation (GenerateSrcSetAttribute) methods, and wired up srcset/sizes in rendering |
| src/.../SitecoreFieldExtensions.cs | Enhanced GetSitecoreMediaUri to preserve existing query parameters and added ParseUrlParameters helper |
Comments suppressed due to low confidence (2)
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs:956
- There’s no test covering the case where
SrcSetis provided as a single object (not an array or JSON string). Adding a test for that scenario would improve coverage forParseSrcSet.
private static ModelExpression GetModelExpression(Field model)
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs:71
- Missing
using Microsoft.AspNetCore.WebUtilities;at the top of this file.QueryHelperslives in that namespace, so add the import to prevent compilation errors.
url = QueryHelpers.AddQueryString(url, key, mergedParams[key]?.ToString() ?? string.Empty);
| { | ||
| output.Attributes.Add(ScrAttribute, field.GetMediaLink(ImageParams)); | ||
|
|
||
| // Add srcset if configured |
Copilot
AI
Jul 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The blocks that add srcset and sizes attributes are duplicated in Process, GenerateImage, and MergeEditableMarkupWithCustomAttributes. Consider extracting shared logic into a helper method to reduce repetition.
| { | ||
| try | ||
| { | ||
| var parsed = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>[]>(jsonString); |
Copilot
AI
Jul 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deserializing into Dictionary<string, object> causes values to be JsonElement instances, so calling ToString() on string parameters may include extra quotes. Consider using a converter or deserializing to a custom DTO to handle string vs numeric values cleanly.
Description / Motivation
This PR implements responsive image support for the
ImageTagHelperby addingsrcsetandsizesattribute functionality, enabling developers to output responsive images using Sitecore's built-in media resizing capabilities. The implementation matches the Content SDK (JSS replacement) behavior to ensure consistent output across .NET and JavaScript frameworks.Key Changes:
Added
SrcSetandSizesProperties: Extended theImageTagHelperwith new properties to support responsive image configuration for both<sc-img>and<img>tags.Multiple Input Format Support: The
SrcSetproperty accepts three different input formats for maximum flexibility:new object[] { new { w = 800 }, new { mw = 400 } }new Dictionary<string, object> { {"w", 800} }[{"w": 800}, {"mw": 400}](most JSS-like syntax)Content SDK Parameter Priority: Implements the same parameter precedence as Content SDK:
w(width) takes priority overmw(max width) for srcset descriptorswormwparameters are included in srcsetgetSrcSetfunction behavior exactlyEnhanced Parameter Merging:
imageParamsare merged with individualsrcSetparameterssrcSetparameters overrideimageParamsfor each srcset entry{ ...imageParams, ...params }merge patternURL Transformation:
/media/URLs to/jssmedia/(Content SDK compatible)Enhanced HTML Output: Generates proper HTML5 responsive image markup:
Experience Editor Support: Works seamlessly with both normal rendering and Experience Editor editable markup.
Backward Compatibility: All existing functionality remains unchanged; new features are opt-in.
Content SDK Compatibility:
The implementation now produces identical output to the Content SDK's
Imagecomponent:Content SDK (React):
ASP.NET Core SDK (Razor):
Both produce identical HTML:
Usage Examples:
This implementation ensures feature parity with the Content SDK while maintaining the familiar ASP.NET Core TagHelper syntax and full backward compatibility.
Testing
<sc-img>and<img>tag scenariosTerms
Fix #16