From a25787db1b9245bb91062b55acd9268ec32a229a Mon Sep 17 00:00:00 2001 From: Delbert Matlock Date: Tue, 27 Apr 2021 16:04:03 -0400 Subject: [PATCH 1/4] Restrict switch to specific attribute to avoid conflict with svg images. --- .../Views/Home/TestSwitch.cshtml | 91 ++++++++++++++----- LogicTagHelpers/CaseTagHelper.cs | 5 + LogicTagHelpers/DefaultTagHelper.cs | 6 +- LogicTagHelpers/SwitchTagHelper.cs | 2 +- 4 files changed, 78 insertions(+), 26 deletions(-) diff --git a/LogicTagHelpers.Demo/Views/Home/TestSwitch.cshtml b/LogicTagHelpers.Demo/Views/Home/TestSwitch.cshtml index baf7dc5..c43cafc 100644 --- a/LogicTagHelpers.Demo/Views/Home/TestSwitch.cshtml +++ b/LogicTagHelpers.Demo/Views/Home/TestSwitch.cshtml @@ -17,48 +17,91 @@

Test 3

-@try -{ - +@{ string errorResult = null; } + + @try + {

Should not display 1.

Should not display 2.

Should not display 3.

Should not display 4.

-
-} -catch (LogicTagHelperException ex) + } + catch (LogicTagHelperException ex) + { + errorResult = $"Expected exception encountered: '{ex.Message}'."; + } +
+@if (!string.IsNullOrWhiteSpace(errorResult)) { -

Expected exception encountered: '@ex.Message'.

+

@errorResult

} -

Test 4

-@try -{ - +@{ errorResult = null; } + + @try + {

Should not display 1.

Should not display 2.

Should not display 3.

Should not display 4.

-
-} -catch (LogicTagHelperException ex) + } + catch (LogicTagHelperException ex) + { + errorResult = $"Expected exception encountered: '{ex.Message}'."; + } +
+@if (!string.IsNullOrWhiteSpace(errorResult)) { -

Expected exception encountered: '@ex.Message'.

+

@errorResult

}

Test 5

-@try -{ +@{ var stringVal = "some string"; - + var result = string.Empty; +} + + @try + {

Should not display 1.

Should not display 2.

-
-} -catch (LogicTagHelperException ex) -{ -

Expected exception encountered: '@ex.Message'.

-} + } + catch (LogicTagHelperException ex) + { + result = $"Expected exception encountered: {ex.Message}."; + } +
+

@result

+ +

Test 6

+ +@{ result = string.Empty; } + + @try + { +

Should not display.

+ } + catch (LogicTagHelperException ex) + { + result = $"Expected exception encountered: {ex.Message}."; + } +
+

@result

+ +

Test 7

+ +@{ result = string.Empty; } + + @try + { +

Should not display.

+ } + catch (LogicTagHelperException ex) + { + result = $"Expected exception encountered: {ex.Message}."; + } +
+

@result

diff --git a/LogicTagHelpers/CaseTagHelper.cs b/LogicTagHelpers/CaseTagHelper.cs index 891367a..3cc2828 100644 --- a/LogicTagHelpers/CaseTagHelper.cs +++ b/LogicTagHelpers/CaseTagHelper.cs @@ -15,6 +15,11 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu { var switchContext = (SwitchContext) context.Items[SwitchContext.ContextKey]; + if (switchContext == null) + { + throw new LogicTagHelperException("Case statement inside of switch with no valid expression."); + } + if (Value.GetType() != switchContext.Expression.GetType()) { throw new LogicTagHelperException( diff --git a/LogicTagHelpers/DefaultTagHelper.cs b/LogicTagHelpers/DefaultTagHelper.cs index 453eb3a..8313ac9 100644 --- a/LogicTagHelpers/DefaultTagHelper.cs +++ b/LogicTagHelpers/DefaultTagHelper.cs @@ -12,7 +12,11 @@ public class DefaultTagHelper : TagHelper public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { var switchContext = (SwitchContext) context.Items[SwitchContext.ContextKey]; - + + if (switchContext == null) + { + throw new LogicTagHelperException("Default statement inside of switch with no valid expression."); + } if (switchContext.HasDefault) { diff --git a/LogicTagHelpers/SwitchTagHelper.cs b/LogicTagHelpers/SwitchTagHelper.cs index 5ee31b9..8cf29ec 100644 --- a/LogicTagHelpers/SwitchTagHelper.cs +++ b/LogicTagHelpers/SwitchTagHelper.cs @@ -6,7 +6,7 @@ namespace LogicTagHelpers /// /// Include at most one section based on value of an expression. /// - [HtmlTargetElement("switch")] + [HtmlTargetElement("switch", Attributes = "expression")] [RestrictChildren("case", "default")] public class SwitchTagHelper : TagHelper { From 0372876efaa12f7b6f83e22969a470bdd4ffaa29 Mon Sep 17 00:00:00 2001 From: Delbert Matlock Date: Wed, 28 Apr 2021 06:30:43 -0400 Subject: [PATCH 2/4] Add 'while' tag helper. --- .../Controllers/HomeController.cs | 5 +++ LogicTagHelpers.Demo/Views/Home/Index.cshtml | 1 + .../Views/Home/TestWhile.cshtml | 7 ++++ LogicTagHelpers/WhileTagHelper.cs | 35 +++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 LogicTagHelpers.Demo/Views/Home/TestWhile.cshtml create mode 100644 LogicTagHelpers/WhileTagHelper.cs diff --git a/LogicTagHelpers.Demo/Controllers/HomeController.cs b/LogicTagHelpers.Demo/Controllers/HomeController.cs index a395a2f..66d9051 100644 --- a/LogicTagHelpers.Demo/Controllers/HomeController.cs +++ b/LogicTagHelpers.Demo/Controllers/HomeController.cs @@ -23,5 +23,10 @@ public IActionResult TestForeach() { return View(); } + + public IActionResult TestWhile() + { + return View(); + } } } \ No newline at end of file diff --git a/LogicTagHelpers.Demo/Views/Home/Index.cshtml b/LogicTagHelpers.Demo/Views/Home/Index.cshtml index 613a21e..d241da5 100644 --- a/LogicTagHelpers.Demo/Views/Home/Index.cshtml +++ b/LogicTagHelpers.Demo/Views/Home/Index.cshtml @@ -6,4 +6,5 @@
  • Test If
  • Test Switch
  • Test Foreach
  • +
  • Test While
  • \ No newline at end of file diff --git a/LogicTagHelpers.Demo/Views/Home/TestWhile.cshtml b/LogicTagHelpers.Demo/Views/Home/TestWhile.cshtml new file mode 100644 index 0000000..49cb836 --- /dev/null +++ b/LogicTagHelpers.Demo/Views/Home/TestWhile.cshtml @@ -0,0 +1,7 @@ +

    Test 1

    + +@{ var x = 0; } + +

    Should display @x.

    + @{ x++; } +
    \ No newline at end of file diff --git a/LogicTagHelpers/WhileTagHelper.cs b/LogicTagHelpers/WhileTagHelper.cs new file mode 100644 index 0000000..e61e5ee --- /dev/null +++ b/LogicTagHelpers/WhileTagHelper.cs @@ -0,0 +1,35 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Razor.TagHelpers; + +namespace LogicTagHelpers +{ + /// + /// Re-render block of code as long as condition evaluates to true. + /// + public class WhileTagHelper : TagHelper + { + /// + /// Function to evaluate exit condition. + /// Inner content will render as long as the condition returns true. + /// + public Func Condition { get; set; } + + public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) + { + if (Condition == null) + { + throw new LogicTagHelperException("Condition may not be null."); + } + + output.TagName = null; + output.Content.Clear(); + + while (Condition.Invoke()) + { + var childContent = await output.GetChildContentAsync(false); + output.Content.AppendHtml(childContent); + } + } + } +} \ No newline at end of file From 7443cfc1005f36a232c603d2eec8dae32d5253cb Mon Sep 17 00:00:00 2001 From: Delbert Matlock Date: Wed, 28 Apr 2021 18:17:29 -0400 Subject: [PATCH 3/4] Tests for 'while' tag helper. --- LogicTagHelpers.Demo/Views/Home/TestWhile.cshtml | 15 ++++++++++++++- LogicTagHelpers.Tests/CreateClassTests.cs | 10 ++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/LogicTagHelpers.Demo/Views/Home/TestWhile.cshtml b/LogicTagHelpers.Demo/Views/Home/TestWhile.cshtml index 49cb836..dfc67f7 100644 --- a/LogicTagHelpers.Demo/Views/Home/TestWhile.cshtml +++ b/LogicTagHelpers.Demo/Views/Home/TestWhile.cshtml @@ -4,4 +4,17 @@

    Should display @x.

    @{ x++; } -
    \ No newline at end of file + + +

    Test 2

    + +@try +{ + +

    Should not display.

    +
    +} +catch (Exception ex) +{ +

    Expected exception occurred: '@ex.Message'.

    +} \ No newline at end of file diff --git a/LogicTagHelpers.Tests/CreateClassTests.cs b/LogicTagHelpers.Tests/CreateClassTests.cs index 952d985..271be3e 100644 --- a/LogicTagHelpers.Tests/CreateClassTests.cs +++ b/LogicTagHelpers.Tests/CreateClassTests.cs @@ -74,5 +74,15 @@ public void CanCreateForeachTagHelper() // Assert Assert.IsNotNull(result); } + + [TestMethod] + public void CanCreateWhileTagHelper() + { + // Act + var result = new WhileTagHelper(); + + // Assert + Assert.IsNotNull(result); + } } } \ No newline at end of file From fe98618ad73b062a950559f002685def429fd5dd Mon Sep 17 00:00:00 2001 From: Delbert Matlock Date: Thu, 29 Apr 2021 05:01:40 -0400 Subject: [PATCH 4/4] Update docs and roll version for release. --- LogicTagHelpers/LogicTagHelpers.csproj | 4 ++-- README.md | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/LogicTagHelpers/LogicTagHelpers.csproj b/LogicTagHelpers/LogicTagHelpers.csproj index 90697b3..22e980c 100644 --- a/LogicTagHelpers/LogicTagHelpers.csproj +++ b/LogicTagHelpers/LogicTagHelpers.csproj @@ -3,7 +3,7 @@ netstandard2.0 true - 0.3.0 + 0.4.0 Delbert Matlock Stuff Of Interest @@ -13,9 +13,9 @@ Done: * if/then/else * switch/case/default * foreach +* while Future: -* while * for * do https://github.com/StuffOfInterest/LogicTagHelpers diff --git a/README.md b/README.md index ea80136..f33023a 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,10 @@ * [switch](#switch) * case * [foreach](#foreach) +* [while](#while) ## Future Tags -* while * for * do @@ -61,4 +61,14 @@ Following line must be added to the `_ViewImports.cshtml` file for the logic tag (content to display for each item in collection) +``` + +### while + +```cshtml +@{ var x = 0; } + + (content to display while condition is true) + @{ x++; } + ``` \ No newline at end of file