mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-25 10:59:18 +08:00
Fixing that the Text.LineEncode token didn't handle all line ending styles, fixes #7649
This commit is contained in:
@@ -44,12 +44,19 @@ namespace Orchard.Tokens.Providers {
|
||||
.Chain("HtmlEncode", "Text", HttpUtility.HtmlEncode)
|
||||
.Token("JavaScriptEncode", HttpUtility.JavaScriptStringEncode)
|
||||
.Chain("JavaScriptEncode", "Text", HttpUtility.JavaScriptStringEncode)
|
||||
.Token("LineEncode", text => text.Replace(System.Environment.NewLine, "<br />"))
|
||||
.Chain("LineEncode", "Text", text => text.Replace(System.Environment.NewLine, "<br />"))
|
||||
.Token("LineEncode", text => ReplaceNewLineCharacters(text))
|
||||
.Chain("LineEncode", "Text", text => ReplaceNewLineCharacters(text))
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
private static string ReplaceNewLineCharacters(string text) {
|
||||
return text
|
||||
.Replace("\r\n", "<br />")
|
||||
.Replace("\r", "<br />")
|
||||
.Replace("\n", "<br />");
|
||||
}
|
||||
|
||||
private static string FilterTokenParam(string tokenName, string token) {
|
||||
return token.StartsWith(tokenName, StringComparison.OrdinalIgnoreCase) ? token.Substring(tokenName.Length) : null;
|
||||
}
|
||||
@@ -69,7 +76,7 @@ namespace Orchard.Tokens.Providers {
|
||||
}
|
||||
|
||||
private static string Limit(string token, string param) {
|
||||
if(String.IsNullOrEmpty(token)) {
|
||||
if (String.IsNullOrEmpty(token)) {
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
|
||||
@@ -117,5 +117,13 @@ namespace Orchard.Tokens.Tests {
|
||||
var result = _tokenizer.Replace("{Text.LineEncode}", new { Text = str }, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode });
|
||||
Assert.That(result, Is.EqualTo("foo<br />bar<br />baz"));
|
||||
}
|
||||
|
||||
[TestCase("foo\nbar", "foo<br />bar")]
|
||||
[TestCase("foo\rbar", "foo<br />bar")]
|
||||
[TestCase("foo\r\nbar", "foo<br />bar")]
|
||||
public void TestLineEncodeWithNonWindowsStyleLineBreaks(string str, string expected) {
|
||||
var result = _tokenizer.Replace("{Text.LineEncode}", new { Text = str }, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode });
|
||||
Assert.That(result, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user