// 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.IO; namespace Microsoft.Framework.WebEncoders { /// /// Helpful extension methods for the encoder classes. /// public static class EncoderExtensions { /// /// HTML-encodes a string and writes the result to the supplied output. /// /// /// The encoded value is also safe for inclusion inside an HTML attribute /// as long as the attribute value is surrounded by single or double quotes. /// public static void HtmlEncode(this IHtmlEncoder htmlEncoder, string value, TextWriter output) { if (htmlEncoder == null) { throw new ArgumentNullException(nameof(htmlEncoder)); } if (!String.IsNullOrEmpty(value)) { htmlEncoder.HtmlEncode(value, 0, value.Length, output); } } /// /// JavaScript-escapes a string and writes the result to the supplied output. /// public static void JavaScriptStringEncode(this IJavaScriptStringEncoder javaScriptStringEncoder, string value, TextWriter output) { if (javaScriptStringEncoder == null) { throw new ArgumentNullException(nameof(javaScriptStringEncoder)); } if (!String.IsNullOrEmpty(value)) { javaScriptStringEncoder.JavaScriptStringEncode(value, 0, value.Length, output); } } /// /// URL-encodes a string and writes the result to the supplied output. /// /// /// The encoded value is safe for use in the segment, query, or /// fragment portion of a URI. /// public static void UrlEncode(this IUrlEncoder urlEncoder, string value, TextWriter output) { if (urlEncoder == null) { throw new ArgumentNullException(nameof(urlEncoder)); } if (!String.IsNullOrEmpty(value)) { urlEncoder.UrlEncode(value, 0, value.Length, output); } } } }