From cde24309c1a9d2d4f33216a591b798cfa53ce9a8 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]"
<41898282+github-actions[bot]@users.noreply.github.com>
Date: Tue, 16 Jan 2024 20:06:55 -0800
Subject: [PATCH] [release/8.0] [Blazor] Accept an HTML encoder instance from
DI if available (#53213)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
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 = "Тест текст";
}
```
Before:
``
After configuring a custom encoder in DI:
```
builder.Services.AddWebEncoders(encoders =>
{
encoders.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
});
```
``
## Packaging changes reviewed?
- [ ] Yes
- [ ] No
- [x] N/A
----
## When servicing release/2.1
- [ ] Make necessary changes in eng/PatchConfig.props
---------
Co-authored-by: jacalvar
---
.../Web/src/HtmlRendering/StaticHtmlRenderer.HtmlWriting.cs | 4 ++--
src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.cs | 3 +++
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.HtmlWriting.cs b/src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.HtmlWriting.cs
index 634b6a100f5b..9cbc12e2aecd 100644
--- a/src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.HtmlWriting.cs
+++ b/src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.HtmlWriting.cs
@@ -21,8 +21,8 @@ public partial class StaticHtmlRenderer
string.Empty,
typeof(FormMappingContext));
- private static readonly TextEncoder _javaScriptEncoder = JavaScriptEncoder.Default;
- private TextEncoder _htmlEncoder = HtmlEncoder.Default;
+ private readonly TextEncoder _javaScriptEncoder;
+ private TextEncoder _htmlEncoder;
private string? _closestSelectValueAsString;
///
diff --git a/src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.cs b/src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.cs
index ad6cd29a32af..28cc2d3e9e69 100644
--- a/src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.cs
+++ b/src/Components/Web/src/HtmlRendering/StaticHtmlRenderer.cs
@@ -3,6 +3,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.ExceptionServices;
+using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.HtmlRendering;
@@ -30,6 +31,8 @@ public StaticHtmlRenderer(IServiceProvider serviceProvider, ILoggerFactory logge
: base(serviceProvider, loggerFactory)
{
_navigationManager = serviceProvider.GetService();
+ _htmlEncoder = serviceProvider.GetService() ?? HtmlEncoder.Default;
+ _javaScriptEncoder = serviceProvider.GetService() ?? JavaScriptEncoder.Default;
}
///