[release/8.0] [Blazor] Accept an HTML encoder instance from DI if available #53213
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport of #53140 to release/8.0
/cc @javiercn
[Blazor] Accept an HTML encoder instance from DI if available
Checks the DI container to see if an instance of HtmlEncoder or JavaScriptEncoder has been registered and uses that instance when present. Falls back to the default implementation if not present. This aligns Blazor behavior in this area with MVC.
Description
Blazor hardcodes the HtmlEncoder used by the app to encode strings into HTML to the default implementation. The defaults work well for English but can be too agressive in encoding characters in other alphabets, like Cyrillic, Greek, etc. or special letters within some languages (German, Spanish).
This results in the text being encoded and the pages significantly increasing in size and possibly displaying incorrectly in some contexts.
Fixes #47477
Customer Impact
Customer's can't use more leaning HtmlEncoding options like in MVC/Razor pages. This can be a migration blocker from those if the app depends on this behavior. It's also a blocker if you need this, since there isn't a workaround that can be applied.
Regression?
[If yes, specify the version the behavior has regressed from]
Risk
The fix consists on checking in DI for the instance.
Verification
Before:
<meta name="keywords" content="Тест текст" />
After configuring a custom encoder in DI:
<meta name="keywords" content="Тест текст" />
Packaging changes reviewed?
When servicing release/2.1