-
Notifications
You must be signed in to change notification settings - Fork 8
Enable SrcSet support with two different input formats for Image tag helper #46
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
Enable SrcSet support with two different input formats for Image tag helper #46
Conversation
…er support, Specific exception handling - Only catch JsonException instead of all exceptions, Better ASP.NET Core compatibility - Replaced HttpUtility with QueryHelpers
…e tests - Keep GetSitecoreMediaUriWithPreservation - Add 14 new test cases covering srcset parameter merging, validation, and edge cases - Fix null handling and zero/negative width filtering in ImageTagHelper - Ensure development environment compatibility with CDN/edge URLs
…ty and comprehensive test cases added Updated - Enable SrcSet support with Content SDK approach compatibility and comprehensive test cases added
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 implements comprehensive responsive image support for the ImageTagHelper by adding srcset and sizes attribute functionality. The implementation enables developers to output responsive images using Sitecore's built-in media resizing capabilities while maintaining compatibility with the Content SDK behavior.
- Added
SrcSetandSizesproperties toImageTagHelperwith support for multiple input formats (anonymous objects, dictionary arrays, JSON strings) - Implemented parameter merging logic that preserves existing URL parameters while allowing srcSet parameters to override base imageParams
- Added extensive test coverage for various srcSet scenarios including parameter precedence, validation, and edge cases
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
ImageTagHelper.cs |
Core implementation of srcSet/sizes support with parameter parsing and URL generation logic |
SitecoreFieldExtensions.cs |
Added parameter preservation methods and URL building logic for responsive images |
ImageTagHelperFixture.cs |
Comprehensive test suite covering all srcSet functionality and edge cases |
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
…ng StringComparer.OrdinalIgnoreCase for the dictionary to ensure consistent parameter key handling
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs
Outdated
Show resolved
Hide resolved
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs
Outdated
Show resolved
Hide resolved
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs
Outdated
Show resolved
Hide resolved
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs
Outdated
Show resolved
Hide resolved
tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/ImageTagHelperFixture.cs
Outdated
Show resolved
Hide resolved
…directly creating the final mergedParams dictionary
…ethod - ApplyJssMediaUrlPrefix
…t to ParseJsonSrcSet, move the type checks to the calling method and simplifying ParseJsonSrcSet
…t to ParseJsonSrcSet, move the type checks to the calling method and simplifying ParseJsonSrcSet
…he test cases to validate the fix
…ions to handle absolute and relative URIs separately
… absolute URIs separately - Replace string manipulation with Uri class properties for absolute URIs - Add proper handling for relative URIs that throw InvalidOperationException when accessing Uri.Query - Use HttpUtility.ParseQueryString for absolute URIs and QueryHelpers.ParseQuery for relative URIs - Add explanatory comments for the if/else branching logic - Simplify ParseUrlParams method while maintaining compatibility with all URI types
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/Fields/ImageTagHelper.cs
Outdated
Show resolved
Hide resolved
…ng the explicit type declarations with target-typed new expressions
…LINQ to eliminate the early return statement
…atement in the ParseUrlParams method
…tAttribute - ImageTagHelper
…8 - Style, Readability, and Improvements - Refactored methods to avoid multiple return statements and redundant branches. - Replaced explicit type declarations with target-typed new expressions (IDE0090). - Eliminated early returns in favor of switch/case patterns for better readability. - Updated substring extraction and return statements to use C# range indexers. - Removed all var usages, replacing them with explicit types. - Removed unnecessary null checks and used collection initializer syntax for lists. - Improved overall code clarity, consistency, and compliance with project style guidelines.
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Show resolved
Hide resolved
src/Sitecore.AspNetCore.SDK.RenderingEngine/Extensions/SitecoreFieldExtensions.cs
Outdated
Show resolved
Hide resolved
- Extract ParseAbsoluteUriParams and ParseRelativeUriParams; make ParseUrlParams a single-return wrapper. - Convert ParseRelativeUriParams and GetSitecoreMediaUriWithPreservation to single-return style. - Preserve existing parsing behavior for absolute and relative URIs.
…ediaUriWithPreservation
…le - PR Review - 09/15 - Extract ParseAbsoluteUriParams and ParseRelativeUriParams; make ParseUrlParams a single-return wrapper. - Convert ParseRelativeUriParams and GetSitecoreMediaUriWithPreservation to single-return style. - Preserve existing parsing behavior for absolute and relative URIs. - Improve debuggability and reduce branching/early return
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.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 two different input formats for maximum flexibility:new object[] { new { w = 800 }, new { mw = 400 } }new Dictionary<string, object> { {"w", 800} }Content SDK Parameter Priority: Implements the same parameter precedence as Content SDK:
w(width) takes priority overmw(max width) for srcset descriptorsw>mw>width>maxWidthgetSrcSetfunction behavior exactlyEnhanced Parameter Merging with Preservation:
imageParamsare merged with individualsrcSetparameterssrcSetparameters overrideimageParamsfor each srcset entryGetSitecoreMediaUriWithPreservationmethod to preserve existing query parameters{ ...imageParams, ...params }merge patternDual URL Processing Methods:
GetSitecoreMediaUri: Maintains existing behavior (strips query parameters)GetSitecoreMediaUriWithPreservation: Preserves critical parameters for responsive imagesGetMediaLinkForSrcSet: Specialized method for srcSet URL generation with parameter preservationContent SDK Compatibility:
The implementation now produces identical output to the Content SDK's
Imagecomponent:Content SDK (React):
ASP.NET Core SDK (Razor):
Technical Implementation Details:
Parameter Preservation Architecture:
MergeParameters(): Merges base and override parameters using reflection and dictionary handlingGetSitecoreMediaUriWithPreservation(): Preserves existing URL parameters while adding new onesConvertToStringDictionary(): Converts objects to string dictionaries for URL buildingGetMediaLinkForSrcSet(): Specialized extension method for responsive image URL generationJsonElement Handling: Enhanced support for JSON deserialization scenarios to handle
JsonElementtypes properlyHelper Methods: Added
AddResponsiveImageAttributes()overloads to eliminate code duplication acrossProcess(),GenerateImage(), and editable markup methodsUsage Examples:
This implementation ensures feature parity with the Content SDK while maintaining the familiar ASP.NET Core TagHelper syntax, full backward compatibility, and proper parameter preservation for functional image display.
Testing
Terms
Fix #16