Added Url encode / Url decode options

This commit is contained in:
Paul Grimshaw 2016-09-19 11:40:43 +01:00
parent 1ea4e00904
commit bed6302ea8
5 changed files with 97 additions and 2 deletions

View File

@ -1461,10 +1461,35 @@ Odd
Assert.AreEqual(expected, actual, "Value field didn't work");
}
#endregion
#region UrlDecodeEncode
[TestMethod]
public void TestCompile_UrlEncode() {
FormatCompiler compiler = new FormatCompiler();
const string format = @"{{#urlencode}}https://google.com{{/urlencode}}";
Generator generator = compiler.Compile(format);
string actual = generator.Render(new {});
string expected = "https%3a%2f%2fgoogle.com";
Assert.AreEqual(expected, actual, "Value field didn't work");
}
[TestMethod]
public void TestCompile_UrlDecode() {
FormatCompiler compiler = new FormatCompiler();
const string format = @"{{#urldecode}}https%3a%2f%2fgoogle.com{{/urldecode}}";
Generator generator = compiler.Compile(format);
string actual = generator.Render(new { });
string expected = "https://google.com";
Assert.AreEqual(expected, actual, "Value field didn't work");
}
#endregion
}
}

View File

@ -52,6 +52,10 @@ namespace Mustache
_tagLookup.Add(gteTagDefinition.Name, gteTagDefinition);
LteTagDefinition lteTagDefinition = new LteTagDefinition();
_tagLookup.Add(lteTagDefinition.Name, lteTagDefinition);
UrlEncodeTagDefinition urlEncodeTagDefinition = new UrlEncodeTagDefinition();
_tagLookup.Add(urlEncodeTagDefinition.Name, urlEncodeTagDefinition);
UrlDecodeTagDefinition urlDecodeTagDefinition = new UrlDecodeTagDefinition();
_tagLookup.Add(urlDecodeTagDefinition.Name,urlDecodeTagDefinition);
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
namespace Mustache {
class UrlDecodeTagDefinition: ContentTagDefinition {
public UrlDecodeTagDefinition()
: base("urldecode") {
}
public override IEnumerable<NestedContext> GetChildContext(TextWriter writer, Scope keyScope, Dictionary<string, object> arguments, Scope contextScope) {
NestedContext context = new NestedContext() {
KeyScope = keyScope,
Writer = new StringWriter(),
WriterNeedsConsidated = true,
};
yield return context;
}
public override IEnumerable<TagParameter> GetChildContextParameters() {
return new TagParameter[] { new TagParameter("collection") };
}
public override string ConsolidateWriter(TextWriter writer, Dictionary<string, object> arguments) {
return HttpUtility.UrlDecode(writer.ToString());
}
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
namespace Mustache {
public class UrlEncodeTagDefinition : ContentTagDefinition {
public UrlEncodeTagDefinition()
: base("urlencode") {
}
public override IEnumerable<NestedContext> GetChildContext(TextWriter writer,Scope keyScope,Dictionary<string, object> arguments,Scope contextScope) {
NestedContext context = new NestedContext() {
KeyScope = keyScope,
Writer = new StringWriter(),
WriterNeedsConsidated = true,
};
yield return context;
}
public override IEnumerable<TagParameter> GetChildContextParameters() {
return new TagParameter[] { new TagParameter("collection") };
}
public override string ConsolidateWriter(TextWriter writer, Dictionary<string, object> arguments) {
return HttpUtility.UrlEncode(writer.ToString());
}
}
}

View File

@ -32,6 +32,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Web" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArgumentCollection.cs" />
@ -45,6 +46,8 @@
<Compile Include="LtTagDefinition.cs" />
<Compile Include="EqTagDefinition.cs" />
<Compile Include="GtTagDefinition.cs" />
<Compile Include="UrlDecodeTagDefinition.cs" />
<Compile Include="UrlEncodeTagDefinition.cs" />
<Compile Include="VariableFoundEventArgs.cs" />
<Compile Include="SetTagDefinition.cs" />
<Compile Include="NewlineTagDefinition.cs" />