-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #473 from tommysor/NoEscape_inconsistent_fix
HtmlEncoding consistent with rules in handlebars.js
- Loading branch information
Showing
8 changed files
with
348 additions
and
26 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
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
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,90 @@ | ||
using System.IO; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace HandlebarsDotNet.Test | ||
{ | ||
public class HtmlEncoderLegacyTests | ||
{ | ||
private readonly HtmlEncoderLegacy _htmlEncoderLegacy; | ||
|
||
public HtmlEncoderLegacyTests() | ||
{ | ||
_htmlEncoderLegacy = new HtmlEncoderLegacy(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("&", "&")] | ||
[InlineData("<", "<")] | ||
[InlineData(">", ">")] | ||
[InlineData("\"", """)] | ||
[InlineData("â", "â")] | ||
|
||
// Don't escape. | ||
[InlineData("'", "'")] | ||
[InlineData("`", "`")] | ||
[InlineData("=", "=")] | ||
public void EscapeCorrectCharactersHandlebarsNetLegacyRules(string input, string expected) | ||
{ | ||
using var writer = new StringWriter(); | ||
|
||
_htmlEncoderLegacy.Encode(input, writer); | ||
|
||
Assert.Equal(expected, writer.ToString()); | ||
} | ||
|
||
[Theory] | ||
[InlineData("", "")] | ||
[InlineData(null, "")] | ||
[InlineData(" ", " ")] | ||
[InlineData("&", "&")] | ||
[InlineData("<", "<")] | ||
[InlineData(">", ">")] | ||
[InlineData(" > ", " > ")] | ||
[InlineData("�", "�")] | ||
[InlineData("�a", "�a")] | ||
[InlineData("\"", """)] | ||
[InlineData("&a&", "&a&")] | ||
[InlineData("a&a", "a&a")] | ||
public void EncodeTestHandlebarsNetLegacyRules(string input, string expected) | ||
{ | ||
// Arrange | ||
using var writer = new StringWriter(); | ||
|
||
// Act | ||
_htmlEncoderLegacy.Encode(input, writer); | ||
|
||
// Assert | ||
Assert.Equal(expected, writer.ToString()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "")] | ||
[InlineData("", "")] | ||
[InlineData("a", "a")] | ||
[InlineData("<", "<")] | ||
public void EncodeStringBuilderOverload(string input, string expected) | ||
{ | ||
using var writer = new StringWriter(); | ||
|
||
var inputStringBuilder = input == null ? null : new StringBuilder(input); | ||
|
||
_htmlEncoderLegacy.Encode(inputStringBuilder, writer); | ||
|
||
Assert.Equal(expected, writer.ToString()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "")] | ||
[InlineData("a", "a")] | ||
[InlineData("<", "<")] | ||
public void EncodeCharEnumeratorOverload(string input, string expected) | ||
{ | ||
using var writer = new StringWriter(); | ||
|
||
_htmlEncoderLegacy.Encode(input?.GetEnumerator(), writer); | ||
|
||
Assert.Equal(expected, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,89 @@ | ||
using System.IO; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace HandlebarsDotNet.Test | ||
{ | ||
public class HtmlEncoderTests | ||
{ | ||
private readonly HtmlEncoder _htmlEncoder; | ||
|
||
public HtmlEncoderTests() | ||
{ | ||
_htmlEncoder = new HtmlEncoder(); | ||
} | ||
|
||
[Theory] | ||
// Escape chars based on https://github.com/handlebars-lang/handlebars.js/blob/master/lib/handlebars/utils.js | ||
[InlineData("&", "&")] | ||
[InlineData("<", "<")] | ||
[InlineData(">", ">")] | ||
[InlineData("\"", """)] | ||
[InlineData("'", "'")] | ||
[InlineData("`", "`")] | ||
[InlineData("=", "=")] | ||
|
||
// Don't escape. | ||
[InlineData("â", "â")] | ||
public void EscapeCorrectCharacters(string input, string expected) | ||
{ | ||
using var writer = new StringWriter(); | ||
|
||
_htmlEncoder.Encode(input, writer); | ||
|
||
Assert.Equal(expected, writer.ToString()); | ||
} | ||
|
||
[Theory] | ||
[InlineData("", "")] | ||
[InlineData(null, "")] | ||
[InlineData(" ", " ")] | ||
[InlineData("&", "&")] | ||
[InlineData("<", "<")] | ||
[InlineData(">", ">")] | ||
[InlineData(" > "," > ")] | ||
[InlineData("�", "�")] | ||
[InlineData("�a", "�a")] | ||
[InlineData(" > ", " > ")] | ||
[InlineData("\"", """)] | ||
[InlineData("&a&", "&a&")] | ||
[InlineData("a&a", "a&a")] | ||
public void EncodeTest(string input, string expected) | ||
{ | ||
// Arrange | ||
var htmlEncoder = new HtmlEncoder(); | ||
using var writer = new StringWriter(); | ||
|
||
// Act | ||
htmlEncoder.Encode(input, writer); | ||
_htmlEncoder.Encode(input, writer); | ||
|
||
// Assert | ||
Assert.Equal(expected, writer.ToString()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "")] | ||
[InlineData("", "")] | ||
[InlineData("a", "a")] | ||
[InlineData("<", "<")] | ||
public void EncodeStringBuilderOverload(string input, string expected) | ||
{ | ||
using var writer = new StringWriter(); | ||
|
||
var inputStringBuilder = input == null ? null : new StringBuilder(input); | ||
|
||
_htmlEncoder.Encode(inputStringBuilder, writer); | ||
|
||
Assert.Equal(expected, writer.ToString()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "")] | ||
[InlineData("a", "a")] | ||
[InlineData("<", "<")] | ||
public void EncodeCharEnumeratorOverload(string input, string expected) | ||
{ | ||
using var writer = new StringWriter(); | ||
|
||
_htmlEncoder.Encode(input?.GetEnumerator(), writer); | ||
|
||
Assert.Equal(expected, 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
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
Oops, something went wrong.