Skip to content

Commit

Permalink
Added EncoderExtensions
Browse files Browse the repository at this point in the history
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
KrzysztofCwalina committed Apr 16, 2015
1 parent 140cedd commit bf092ad
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Compile Include="System\Text\Encodings\Web\AllowedCharsBitmap.cs" />
<Compile Include="System\Text\Encodings\Web\CodePointFilter.cs" />
<Compile Include="System\Text\Encodings\Web\EncoderCommon.cs" />
<Compile Include="System\Text\Encodings\Web\EncoderExtensions.cs" />
<Compile Include="System\Text\Encodings\Web\HexUtil.cs" />
<Compile Include="System\Text\Encodings\Web\HtmlEncoder.cs" />
<Compile Include="System\Text\Encodings\Web\ICodePointFilter.cs" />
Expand Down
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 src/System.Text.Encodings.Web/tests/EncoderExtensionsTests.cs
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&#x2B;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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<Compile Include="CodePointFilterTests.cs" />
<Compile Include="CommonTestEncoder.cs" />
<Compile Include="EncoderCommonTests.cs" />
<Compile Include="EncoderExtensionsTests.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="HtmlEncoderTests.cs" />
<Compile Include="JavaScriptStringEncoderTests.cs" />
Expand Down

0 comments on commit bf092ad

Please sign in to comment.