forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This API and tests are simply ported from ASP.NET repo. Initially, when I ported the component, I did not include the extensions in the port. I want the CoreFx component to be independent from I/O (TextWriter). But there is more work needed, to make it trully independent, and I am not sure this will ever happen, so I am including the extensions for now.
- Loading branch information
1 parent
140cedd
commit bf092ad
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/EncoderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.IO; | ||
|
||
namespace System.Text.Encodings.Web | ||
{ | ||
/// <summary> | ||
/// Helpful extension methods for the encoder classes. | ||
/// </summary> | ||
public static class EncoderExtensions | ||
{ | ||
/// <summary> | ||
/// HTML-encodes a string and writes the result to the supplied output. | ||
/// </summary> | ||
/// <remarks> | ||
/// 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. | ||
/// </remarks> | ||
public static void HtmlEncode(this IHtmlEncoder htmlEncoder, string value, TextWriter output) | ||
{ | ||
if (htmlEncoder == null) | ||
{ | ||
throw new ArgumentNullException("htmlEncoder"); | ||
} | ||
|
||
if (!String.IsNullOrEmpty(value)) | ||
{ | ||
htmlEncoder.HtmlEncode(value, 0, value.Length, output); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// JavaScript-escapes a string and writes the result to the supplied output. | ||
/// </summary> | ||
public static void JavaScriptStringEncode(this IJavaScriptStringEncoder javaScriptStringEncoder, string value, TextWriter output) | ||
{ | ||
if (javaScriptStringEncoder == null) | ||
{ | ||
throw new ArgumentNullException("javaScriptStringEncoder"); | ||
} | ||
|
||
if (!String.IsNullOrEmpty(value)) | ||
{ | ||
javaScriptStringEncoder.JavaScriptStringEncode(value, 0, value.Length, output); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// URL-encodes a string and writes the result to the supplied output. | ||
/// </summary> | ||
/// <remarks> | ||
/// The encoded value is safe for use in the segment, query, or | ||
/// fragment portion of a URI. | ||
/// </remarks> | ||
public static void UrlEncode(this IUrlEncoder urlEncoder, string value, TextWriter output) | ||
{ | ||
if (urlEncoder == null) | ||
{ | ||
throw new ArgumentNullException("urlEncoder"); | ||
} | ||
|
||
if (!String.IsNullOrEmpty(value)) | ||
{ | ||
urlEncoder.UrlEncode(value, 0, value.Length, output); | ||
} | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/System.Text.Encodings.Web/tests/EncoderExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. 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; | ||
using Xunit; | ||
|
||
namespace System.Text.Encodings.Web | ||
{ | ||
public class EncoderExtensionsTests | ||
{ | ||
[Fact] | ||
public void HtmlEncode_ParameterChecks() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => EncoderExtensions.HtmlEncode(null, "Hello!", new StringWriter())); | ||
} | ||
|
||
[Fact] | ||
public void HtmlEncode_PositiveTestCase() | ||
{ | ||
// Arrange | ||
IHtmlEncoder encoder = new HtmlEncoder(UnicodeRanges.All); | ||
StringWriter writer = new StringWriter(); | ||
|
||
// Act | ||
encoder.HtmlEncode("Hello+there!", writer); | ||
|
||
// Assert | ||
Assert.Equal("Hello+there!", writer.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void JavaScriptStringEncode_ParameterChecks() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => EncoderExtensions.JavaScriptStringEncode(null, "Hello!", new StringWriter())); | ||
} | ||
|
||
[Fact] | ||
public void JavaScriptStringEncode_PositiveTestCase() | ||
{ | ||
// Arrange | ||
IJavaScriptStringEncoder encoder = new JavaScriptStringEncoder(UnicodeRanges.All); | ||
StringWriter writer = new StringWriter(); | ||
|
||
// Act | ||
encoder.JavaScriptStringEncode("Hello+there!", writer); | ||
|
||
// Assert | ||
Assert.Equal(@"Hello\u002Bthere!", writer.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void UrlEncode_ParameterChecks() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => EncoderExtensions.UrlEncode(null, "Hello!", new StringWriter())); | ||
} | ||
|
||
[Fact] | ||
public void UrlEncode_PositiveTestCase() | ||
{ | ||
// Arrange | ||
IUrlEncoder encoder = new UrlEncoder(UnicodeRanges.All); | ||
StringWriter writer = new StringWriter(); | ||
|
||
// Act | ||
encoder.UrlEncode("Hello+there!", writer); | ||
|
||
// Assert | ||
Assert.Equal("Hello%2Bthere!", writer.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters