From 46020fde39eb3d767a1f4c0d438e699c2e630a70 Mon Sep 17 00:00:00 2001 From: thecopy Date: Fri, 19 Jul 2013 09:30:50 +0200 Subject: [PATCH 1/3] Added conditional evalution on @Current inside @Each --- .../SuperSimpleViewEngineTests.cs | 25 +++++++++++++++++++ .../SuperSimpleViewEngine.cs | 20 +++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/SuperSimpleViewEngine.Tests/SuperSimpleViewEngineTests.cs b/SuperSimpleViewEngine.Tests/SuperSimpleViewEngineTests.cs index b291d7d..e0fc036 100644 --- a/SuperSimpleViewEngine.Tests/SuperSimpleViewEngineTests.cs +++ b/SuperSimpleViewEngine.Tests/SuperSimpleViewEngineTests.cs @@ -132,6 +132,31 @@ public void Should_use_multiple_current_statements_inside_each() Assert.Equal(@"", output); } + + [Fact] + public void Should_evaluate_current_conditional_inside_each() + { + const string input = @""; + dynamic model = new ExpandoObject(); + model.Users = new List() { new { Name = "Bob", IsGreekCitizen = true }, new { Name = "Malin", IsGreekCitizen = false } }; + + var output = viewEngine.Render(input, model, this.fakeHost); + + Assert.Equal(@"", output); + } + + [Fact] + public void Should_not_evaluate_current_conditional_from_outside_each() + { + const string input = @"@If.HasUsers;Yay Users!@EndIf@IfNot.HasUsers;Yay Users!@EndIf"; + dynamic model = new ExpandoObject(); + model.Users = new List() { new { Name = "Bob", IsGreekCitizen = true }, new { Name = "Malin", IsGreekCitizen = false } }; + + var output = viewEngine.Render(input, model, this.fakeHost); + + Assert.Equal(@"Yay Users!
  • Bob:Yay Greece!
  • Malin:
", output); + } + [Fact] public void Should_try_to_use_non_enumerable_in_each_shows_error() { diff --git a/SuperSimpleViewEngine/SuperSimpleViewEngine.cs b/SuperSimpleViewEngine/SuperSimpleViewEngine.cs index a847582..e29892f 100644 --- a/SuperSimpleViewEngine/SuperSimpleViewEngine.cs +++ b/SuperSimpleViewEngine/SuperSimpleViewEngine.cs @@ -383,16 +383,19 @@ private string PerformEachSubstitutions(string template, object model, IViewEngi var result = string.Empty; foreach (var item in substitutionEnumerable) { - result += ReplaceCurrentMatch(contents, item, host); + var postConditionalResult = PerformConditionalSubstitutions(contents, item, host); + + result += ReplaceCurrentMatch(postConditionalResult, item, host); } return result; }); } - + /// /// Expand a @Current match inside an @Each iterator /// + /// The template /// Contents of the @Each block /// Current item from the @Each enumerable /// View engine host @@ -411,18 +414,21 @@ private string ReplaceCurrentMatch(string contents, object item, IViewEngineHost var properties = GetCaptureGroupValues(eachMatch, "ParameterName"); var substitution = GetPropertyValueFromParameterCollection(item, properties); - - if (!substitution.Item1) + + if (!substitution.Item1) // Did value eval fail { return "[ERR!]"; } - - if (substitution.Item2 == null) + + if (substitution.Item2 == null) // Is evaluated value null { return string.Empty; } - return eachMatch.Groups["Encode"].Success ? host.HtmlEncode(substitution.Item2.ToString()) : substitution.Item2.ToString(); + + return eachMatch.Groups["Encode"].Success + ? host.HtmlEncode(substitution.Item2.ToString()) + : substitution.Item2.ToString(); }); } From 79043dc17fc309de400da28f05126c6f6baaf3fe Mon Sep 17 00:00:00 2001 From: thecopy Date: Fri, 19 Jul 2013 09:50:30 +0200 Subject: [PATCH 2/3] Cleaned up some left over comments --- SuperSimpleViewEngine/SuperSimpleViewEngine.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/SuperSimpleViewEngine/SuperSimpleViewEngine.cs b/SuperSimpleViewEngine/SuperSimpleViewEngine.cs index e29892f..79f1671 100644 --- a/SuperSimpleViewEngine/SuperSimpleViewEngine.cs +++ b/SuperSimpleViewEngine/SuperSimpleViewEngine.cs @@ -342,7 +342,7 @@ private string PerformContextSubstitutions(string template, object model, IViewE /// /// Performs @Each.PropertyName substitutions /// - /// The template. + /// The template /// The model. /// View engine host /// Template with @Each.PropertyName blocks expanded. @@ -395,7 +395,6 @@ private string PerformEachSubstitutions(string template, object model, IViewEngi /// /// Expand a @Current match inside an @Each iterator /// - /// The template /// Contents of the @Each block /// Current item from the @Each enumerable /// View engine host @@ -415,20 +414,18 @@ private string ReplaceCurrentMatch(string contents, object item, IViewEngineHost var substitution = GetPropertyValueFromParameterCollection(item, properties); - if (!substitution.Item1) // Did value eval fail + if (!substitution.Item1) { return "[ERR!]"; } - if (substitution.Item2 == null) // Is evaluated value null + if (substitution.Item2 == null) { return string.Empty; } - return eachMatch.Groups["Encode"].Success - ? host.HtmlEncode(substitution.Item2.ToString()) - : substitution.Item2.ToString(); + return eachMatch.Groups["Encode"].Success ? host.HtmlEncode(substitution.Item2.ToString()) : substitution.Item2.ToString(); }); } From 7df4d2bcb4690d5a2e4b100dbbcb5b1463f502b1 Mon Sep 17 00:00:00 2001 From: thecopy Date: Fri, 19 Jul 2013 09:56:23 +0200 Subject: [PATCH 3/3] Polishing --- SuperSimpleViewEngine/SuperSimpleViewEngine.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SuperSimpleViewEngine/SuperSimpleViewEngine.cs b/SuperSimpleViewEngine/SuperSimpleViewEngine.cs index 79f1671..b8469b4 100644 --- a/SuperSimpleViewEngine/SuperSimpleViewEngine.cs +++ b/SuperSimpleViewEngine/SuperSimpleViewEngine.cs @@ -342,7 +342,7 @@ private string PerformContextSubstitutions(string template, object model, IViewE /// /// Performs @Each.PropertyName substitutions /// - /// The template + /// The template. /// The model. /// View engine host /// Template with @Each.PropertyName blocks expanded. @@ -391,7 +391,7 @@ private string PerformEachSubstitutions(string template, object model, IViewEngi return result; }); } - + /// /// Expand a @Current match inside an @Each iterator /// @@ -413,12 +413,12 @@ private string ReplaceCurrentMatch(string contents, object item, IViewEngineHost var properties = GetCaptureGroupValues(eachMatch, "ParameterName"); var substitution = GetPropertyValueFromParameterCollection(item, properties); - + if (!substitution.Item1) { return "[ERR!]"; } - + if (substitution.Item2 == null) { return string.Empty;