// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Text.Encodings.Web; namespace Microsoft.Extensions.WebEncoders { /// /// Contains extension methods for fetching encoders from an . /// public static class EncoderServiceProviderExtensions { /// /// Retrieves an from an . /// /// /// This method is guaranteed never to return null. /// It will return a default encoder instance if does not contain one or is null. /// public static HtmlEncoder GetHtmlEncoder(this IServiceProvider serviceProvider) { return (HtmlEncoder)serviceProvider?.GetService(typeof(HtmlEncoder)) ?? HtmlEncoder.Default; } /// /// Retrieves an from an . /// /// /// This method is guaranteed never to return null. /// It will return a default encoder instance if does not contain one or is null. /// public static JavaScriptEncoder GetJavaScriptEncoder(this IServiceProvider serviceProvider) { return (JavaScriptEncoder)serviceProvider?.GetService(typeof(JavaScriptEncoder)) ?? JavaScriptEncoder.Default; } /// /// Retrieves an from an . /// /// /// This method is guaranteed never to return null. /// It will return a default encoder instance if does not contain one or is null. /// public static UrlEncoder GetUrlEncoder(this IServiceProvider serviceProvider) { return (UrlEncoder)serviceProvider?.GetService(typeof(UrlEncoder)) ?? UrlEncoder.Default; } } }