diff --git a/Nustache.Compilation.Tests/Nustache.Compilation.Tests.csproj b/Nustache.Compilation.Tests/Nustache.Compilation.Tests.csproj index 96c89ae..da65814 100644 --- a/Nustache.Compilation.Tests/Nustache.Compilation.Tests.csproj +++ b/Nustache.Compilation.Tests/Nustache.Compilation.Tests.csproj @@ -33,6 +33,14 @@ ..\packages\Newtonsoft.Json.5.0.6\lib\net40\Newtonsoft.Json.dll + + ..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll + False + + + ..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.interfaces.dll + False + ..\packages\NUnit.2.6.2\lib\nunit.framework.dll @@ -51,6 +59,7 @@ + diff --git a/Nustache.Compilation.Tests/UseEncoderDelegate.cs b/Nustache.Compilation.Tests/UseEncoderDelegate.cs new file mode 100644 index 0000000..a58eef8 --- /dev/null +++ b/Nustache.Compilation.Tests/UseEncoderDelegate.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using System.IO; +using Nustache.Compilation; +using Nustache.Core; + +namespace Nustache.Compilation.Tests +{ + [TestFixture] + public class Encoder_Delegate_Usage + { + class TemplateData + { + public string Value { get; set; } + } + + [Test] + public void ReplacingHtmlEncodeWorksForCompiledTemplates() + { + // replace the default encoder with one that wraps the input in "--" + Encoders.HtmlEncoder encoder = (input) => "--" + input + "--"; + Encoders.HtmlEncode = encoder; + + var template = Template("{{Value}}"); + var compiled = template.Compile(null); + + + var inputText = "Some cool text"; + var data = new TemplateData() + { + Value = inputText + }; + + var expectedOutput = encoder(inputText); + + Assert.AreEqual(expectedOutput, compiled(data)); + + // reset the used HTML encoder to default + Encoders.HtmlEncode = Encoders.DefaultHtmlEncode; + } + + private Template Template(string text) + { + var template = new Template(); + template.Load(new StringReader(text)); + return template; + } + } +} diff --git a/Nustache.Compilation/CompilePartVisitor.cs b/Nustache.Compilation/CompilePartVisitor.cs index d86a177..1844940 100644 --- a/Nustache.Compilation/CompilePartVisitor.cs +++ b/Nustache.Compilation/CompilePartVisitor.cs @@ -119,7 +119,9 @@ public void Visit(VariableReference variable) if (variable.Escaped) { - parts.Add(CompoundExpression.IndentOnLineEnd(Expression.Call(null, typeof(Encoders).GetMethod("DefaultHtmlEncode"), getter), context)); + var escaperProperty = typeof(Encoders).GetProperty("HtmlEncode", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); + var escaperMethod = (Delegate)escaperProperty.GetValue(null, null); + parts.Add(CompoundExpression.IndentOnLineEnd(Expression.Call(null, escaperMethod.Method, getter), context)); } else { diff --git a/Nustache.Core.Tests/Example_DataTable.cs b/Nustache.Core.Tests/Example_DataTable.cs index 5b13613..1009a1f 100644 --- a/Nustache.Core.Tests/Example_DataTable.cs +++ b/Nustache.Core.Tests/Example_DataTable.cs @@ -36,5 +36,51 @@ public void It_can_Render_Datatables_Case_Insensitive() Assert.AreEqual("123", result); } + + [Test] + public void It_Should_Render_Inverted_When_Having_No_Rows() + { + var dt = new System.Data.DataTable(); + dt.Columns.Add("Foo"); + + var result = Render.StringToString( + @" + + + + + + + {{#Data}} + + + + {{/Data}} + {{^Data}} + + + + {{/Data}} + +
Foo
{{Foo}}
+ No data exists. +
", new { Data = dt }); + + Assert.AreEqual( + @" + + + + + + + + + + +
Foo
+ No data exists. +
", result); + } } } diff --git a/Nustache.Core/RenderContext.cs b/Nustache.Core/RenderContext.cs index 63354e9..ad9c508 100644 --- a/Nustache.Core/RenderContext.cs +++ b/Nustache.Core/RenderContext.cs @@ -240,6 +240,11 @@ public bool IsTruthy(object value) return ((IEnumerable)value).GetEnumerator().MoveNext(); } + if (value is DataTable) + { + return ((DataTable)value).Rows.Count > 0; + } + return true; }