Skip to content

[release/8.0] [Blazor] Accept an HTML encoder instance from DI if available #53213

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

Merged
merged 2 commits into from
Jan 17, 2024

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Jan 8, 2024

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
  • No

[If yes, specify the version the behavior has regressed from]

Risk

  • High
  • Medium
  • Low

The fix consists on checking in DI for the instance.

Verification

  • Manual (required)
  • Automated
    @{
        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
  • N/A

When servicing release/2.1

  • Make necessary changes in eng/PatchConfig.props

@github-actions github-actions bot requested a review from a team as a code owner January 8, 2024 13:25
@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Jan 8, 2024
@ghost ghost added this to the 8.0.x milestone Jan 8, 2024
@ghost
Copy link

ghost commented Jan 8, 2024

Hi @github-actions[bot]. If this is not a tell-mode PR, please make sure to follow the instructions laid out in the servicing process document.
Otherwise, please add tell-mode label.

@javiercn
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@mkArtakMSFT mkArtakMSFT added the Servicing-consider Shiproom approval is required for the issue label Jan 11, 2024
@ghost
Copy link

ghost commented Jan 11, 2024

Hi @github-actions[bot]. Please make sure you've updated the PR description to use the Shiproom Template. Also, make sure this PR is not marked as a draft and is ready-to-merge.

To learn more about how to prepare a servicing PR click here.

@mkArtakMSFT
Copy link
Contributor

@javiercn why can't we have an automated test for this? Will it be too hard to do?

@mkArtakMSFT mkArtakMSFT added Servicing-approved Shiproom has approved the issue and removed Servicing-consider Shiproom approval is required for the issue labels Jan 17, 2024
@ghost
Copy link

ghost commented Jan 17, 2024

Hi @github-actions[bot]. This PR was just approved to be included in the upcoming servicing release. Somebody from the @dotnet/aspnet-build team will get it merged when the branches are open. Until then, please make sure all the CI checks pass and the PR is reviewed.

@mkArtakMSFT mkArtakMSFT merged commit cde2430 into release/8.0 Jan 17, 2024
@mkArtakMSFT mkArtakMSFT deleted the backport/pr-53140-to-release/8.0 branch January 17, 2024 04:06
@ghost ghost modified the milestones: 8.0.x, 8.0.2 Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components Servicing-approved Shiproom has approved the issue
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants