Skip to content

Commit cde2430

Browse files
[release/8.0] [Blazor] Accept an HTML encoder instance from DI if available (#53213)
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? - [ ] Yes - [X] No [If yes, specify the version the behavior has regressed from] ## Risk - [ ] High - [ ] Medium - [X] Low The fix consists on checking in DI for the instance. ## Verification - [X] Manual (required) - [ ] Automated ```razor @{ var value = "Тест текст"; } <meta name="keywords" content="@value" /> ``` Before: `<meta name="keywords" content="&#x422;&#x435;&#x441;&#x442; &#x442;&#x435;&#x43A;&#x441;&#x442;" />` After configuring a custom encoder in DI: ``` builder.Services.AddWebEncoders(encoders => { encoders.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All); }); ``` `<meta name="keywords" content="Тест текст" />` ## Packaging changes reviewed? - [ ] Yes - [ ] No - [x] N/A ---- ## When servicing release/2.1 - [ ] Make necessary changes in eng/PatchConfig.props --------- Co-authored-by: jacalvar <jacalvar@microsoft.com>
1 parent 4e52054 commit cde2430

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.HtmlWriting.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public partial class StaticHtmlRenderer
2121
string.Empty,
2222
typeof(FormMappingContext));
2323

24-
private static readonly TextEncoder _javaScriptEncoder = JavaScriptEncoder.Default;
25-
private TextEncoder _htmlEncoder = HtmlEncoder.Default;
24+
private readonly TextEncoder _javaScriptEncoder;
25+
private TextEncoder _htmlEncoder;
2626
private string? _closestSelectValueAsString;
2727

2828
/// <summary>

src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics.CodeAnalysis;
55
using System.Runtime.ExceptionServices;
6+
using System.Text.Encodings.Web;
67
using Microsoft.AspNetCore.Components.RenderTree;
78
using Microsoft.AspNetCore.Components.Web;
89
using Microsoft.AspNetCore.Components.Web.HtmlRendering;
@@ -30,6 +31,8 @@ public StaticHtmlRenderer(IServiceProvider serviceProvider, ILoggerFactory logge
3031
: base(serviceProvider, loggerFactory)
3132
{
3233
_navigationManager = serviceProvider.GetService<NavigationManager>();
34+
_htmlEncoder = serviceProvider.GetService<HtmlEncoder>() ?? HtmlEncoder.Default;
35+
_javaScriptEncoder = serviceProvider.GetService<JavaScriptEncoder>() ?? JavaScriptEncoder.Default;
3336
}
3437

3538
/// <inheritdoc/>

0 commit comments

Comments
 (0)