From bed6302ea8bfe9b0c90849cc1f34b2cc60c70747 Mon Sep 17 00:00:00 2001 From: Paul Grimshaw Date: Mon, 19 Sep 2016 11:40:43 +0100 Subject: [PATCH] Added Url encode / Url decode options --- mustache-sharp.test/FormatCompilerTester.cs | 29 +++++++++++++++++-- mustache-sharp/FormatCompiler.cs | 4 +++ mustache-sharp/UrlDecodeTagDefinition.cs | 31 ++++++++++++++++++++ mustache-sharp/UrlEncodeTagDefinition.cs | 32 +++++++++++++++++++++ mustache-sharp/mustache-sharp.csproj | 3 ++ 5 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 mustache-sharp/UrlDecodeTagDefinition.cs create mode 100644 mustache-sharp/UrlEncodeTagDefinition.cs diff --git a/mustache-sharp.test/FormatCompilerTester.cs b/mustache-sharp.test/FormatCompilerTester.cs index a7e25f7..72bcfee 100644 --- a/mustache-sharp.test/FormatCompilerTester.cs +++ b/mustache-sharp.test/FormatCompilerTester.cs @@ -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 - } } diff --git a/mustache-sharp/FormatCompiler.cs b/mustache-sharp/FormatCompiler.cs index c620d61..552f449 100644 --- a/mustache-sharp/FormatCompiler.cs +++ b/mustache-sharp/FormatCompiler.cs @@ -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); } diff --git a/mustache-sharp/UrlDecodeTagDefinition.cs b/mustache-sharp/UrlDecodeTagDefinition.cs new file mode 100644 index 0000000..2bbdfc5 --- /dev/null +++ b/mustache-sharp/UrlDecodeTagDefinition.cs @@ -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 GetChildContext(TextWriter writer, Scope keyScope, Dictionary arguments, Scope contextScope) { + NestedContext context = new NestedContext() { + KeyScope = keyScope, + Writer = new StringWriter(), + WriterNeedsConsidated = true, + }; + yield return context; + } + + public override IEnumerable GetChildContextParameters() { + return new TagParameter[] { new TagParameter("collection") }; + } + + public override string ConsolidateWriter(TextWriter writer, Dictionary arguments) { + return HttpUtility.UrlDecode(writer.ToString()); + } + } +} diff --git a/mustache-sharp/UrlEncodeTagDefinition.cs b/mustache-sharp/UrlEncodeTagDefinition.cs new file mode 100644 index 0000000..8f698d1 --- /dev/null +++ b/mustache-sharp/UrlEncodeTagDefinition.cs @@ -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 GetChildContext(TextWriter writer,Scope keyScope,Dictionary arguments,Scope contextScope) { + NestedContext context = new NestedContext() { + KeyScope = keyScope, + Writer = new StringWriter(), + WriterNeedsConsidated = true, + }; + yield return context; + } + + public override IEnumerable GetChildContextParameters() { + return new TagParameter[] { new TagParameter("collection") }; + } + + public override string ConsolidateWriter(TextWriter writer, Dictionary arguments) { + return HttpUtility.UrlEncode(writer.ToString()); + } + } +} \ No newline at end of file diff --git a/mustache-sharp/mustache-sharp.csproj b/mustache-sharp/mustache-sharp.csproj index 7bf699b..46acf69 100644 --- a/mustache-sharp/mustache-sharp.csproj +++ b/mustache-sharp/mustache-sharp.csproj @@ -32,6 +32,7 @@ + @@ -45,6 +46,8 @@ + +