From 3e28407f41c25f3881bb943b991c7316358182b7 Mon Sep 17 00:00:00 2001 From: Maria Date: Tue, 8 Oct 2024 17:23:16 +0100 Subject: [PATCH 1/5] WiP: not yet correct --- .../Renderers/OrderedListRenderer.cs | 50 ++++++++++++++ ...stRenderer.cs => UnorderedListRenderer.cs} | 4 +- .../Views/Shared/_RichText.cshtml | 65 ++++++++++++++----- 3 files changed, 102 insertions(+), 17 deletions(-) create mode 100644 Childrens-Social-Care-CPD/Contentful/Renderers/OrderedListRenderer.cs rename Childrens-Social-Care-CPD/Contentful/Renderers/{ListRenderer.cs => UnorderedListRenderer.cs} (91%) diff --git a/Childrens-Social-Care-CPD/Contentful/Renderers/OrderedListRenderer.cs b/Childrens-Social-Care-CPD/Contentful/Renderers/OrderedListRenderer.cs new file mode 100644 index 00000000..7ce5dccf --- /dev/null +++ b/Childrens-Social-Care-CPD/Contentful/Renderers/OrderedListRenderer.cs @@ -0,0 +1,50 @@ +using Contentful.Core.Models; +using Microsoft.AspNetCore.Html; +using Microsoft.AspNetCore.Mvc.Rendering; +using Text = Contentful.Core.Models.Text; + +namespace Childrens_Social_Care_CPD.Contentful.Renderers; + +public class OrderedListRenderer(IRenderer textRenderer, IRenderer hyperlinkRenderer) : IRenderer +{ + public IHtmlAsyncContent Render(List item) + { + if (item.Content.Count == 0) + { + return null; + } + + var ol = new TagBuilder("ol"); + ol.AddCssClass("govuk-list govuk-list--number"); + + foreach (var listItem in item.Content.OfType()) + { + if (listItem.Content[0] is not Paragraph paragraph) continue; + + var li = new TagBuilder("li"); + foreach (var content in paragraph.Content) + { + switch (content) + { + case Text text: + { + li.InnerHtml.AppendHtml(textRenderer.Render(text)); + break; + } + case Hyperlink hyperlink: + { + li.InnerHtml.AppendHtml(hyperlinkRenderer.Render(hyperlink)); + break; + } + } + } + ol.InnerHtml.AppendHtml(li); + } + return (IHtmlAsyncContent)ol; + } + + IHtmlContent IRenderer.Render(List item) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs b/Childrens-Social-Care-CPD/Contentful/Renderers/UnorderedListRenderer.cs similarity index 91% rename from Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs rename to Childrens-Social-Care-CPD/Contentful/Renderers/UnorderedListRenderer.cs index 14b77c62..c9216a54 100644 --- a/Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs +++ b/Childrens-Social-Care-CPD/Contentful/Renderers/UnorderedListRenderer.cs @@ -5,7 +5,7 @@ namespace Childrens_Social_Care_CPD.Contentful.Renderers; -public class ListRenderer(IRenderer textRenderer, IRenderer hyperlinkRenderer) : IRenderer +public class UnorderedListRenderer(IRenderer textRenderer, IRenderer hyperlinkRenderer) : IRenderer { public IHtmlContent Render(List item) { @@ -43,4 +43,4 @@ public IHtmlContent Render(List item) return ul; } -} +} \ No newline at end of file diff --git a/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml b/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml index c1a6816b..94c29ddc 100644 --- a/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml +++ b/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml @@ -16,7 +16,8 @@ @inject IRenderer _quoteRenderer @inject IRenderer _horizontalRulerRenderer @inject IRenderer _tableRenderer -@inject IRenderer _listRenderer +@inject IRenderer _unorderedlistRenderer +@inject IRenderer _orderedListRenderer @inject IRenderer _roleListRenderer @inject IRenderer _assetStructureRenderer @@ -27,26 +28,60 @@ { switch (node) { - case HorizontalRuler hr: @_horizontalRulerRenderer.Render(hr) break; - case Paragraph paragraph: @_paragraphRenderer.Render(paragraph) break; - case List list: @_listRenderer.Render(list) break; - case Heading1 heading1: @_heading1Renderer.Render(heading1) break; - case Heading2 heading2: @_heading2Renderer.Render(heading2) break; - case Heading3 heading3: @_heading3Renderer.Render(heading3) break; - case Heading4 heading4: @_heading4Renderer.Render(heading4) break; - case Heading5 heading5: @_heading5Renderer.Render(heading5) break; - case Heading6 heading6: @_heading6Renderer.Render(heading6) break; - case Table table: @_tableRenderer.Render(table) break; - case Quote quote: @_quoteRenderer.Render(quote) break; - case EntryStructure entryStructure: { + case HorizontalRuler hr: + @_horizontalRulerRenderer.Render(hr) + break; + case Paragraph paragraph: + @_paragraphRenderer.Render(paragraph) + break; + case List list: + if (((Contentful.Core.Models.List)node).NodeType == "unordered-list") + { + @_unorderedlistRenderer.Render(list) + ; + } + else + { + @_orderedListRenderer.Render(list) + } + break; + case Heading1 heading1: + @_heading1Renderer.Render(heading1) + break; + case Heading2 heading2: + @_heading2Renderer.Render(heading2) + break; + case Heading3 heading3: + @_heading3Renderer.Render(heading3) + break; + case Heading4 heading4: + @_heading4Renderer.Render(heading4) + break; + case Heading5 heading5: + @_heading5Renderer.Render(heading5) + break; + case Heading6 heading6: + @_heading6Renderer.Render(heading6) + break; + case Table table: + @_tableRenderer.Render(table) + break; + case Quote quote: + @_quoteRenderer.Render(quote) + break; + case EntryStructure entryStructure: + { switch (entryStructure.Data.Target) { - case RoleList roleList: @_roleListRenderer.Render(roleList) + case RoleList roleList: + @_roleListRenderer.Render(roleList) break; } break; } - case AssetStructure assetStructure: @_assetStructureRenderer.Render(assetStructure) break; + case AssetStructure assetStructure: + @_assetStructureRenderer.Render(assetStructure) + break; } } } \ No newline at end of file From 689941438725dce5f5a73df5966ed387472bb4c5 Mon Sep 17 00:00:00 2001 From: Maria Date: Wed, 9 Oct 2024 10:45:52 +0100 Subject: [PATCH 2/5] numbered list rendering correctly --- ...orderedListRenderer.cs => ListRenderer.cs} | 22 ++++++-- .../Renderers/OrderedListRenderer.cs | 50 ------------------- .../Views/Shared/_RichText.cshtml | 13 +---- 3 files changed, 19 insertions(+), 66 deletions(-) rename Childrens-Social-Care-CPD/Contentful/Renderers/{UnorderedListRenderer.cs => ListRenderer.cs} (63%) delete mode 100644 Childrens-Social-Care-CPD/Contentful/Renderers/OrderedListRenderer.cs diff --git a/Childrens-Social-Care-CPD/Contentful/Renderers/UnorderedListRenderer.cs b/Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs similarity index 63% rename from Childrens-Social-Care-CPD/Contentful/Renderers/UnorderedListRenderer.cs rename to Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs index c9216a54..b457f2b4 100644 --- a/Childrens-Social-Care-CPD/Contentful/Renderers/UnorderedListRenderer.cs +++ b/Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs @@ -5,7 +5,7 @@ namespace Childrens_Social_Care_CPD.Contentful.Renderers; -public class UnorderedListRenderer(IRenderer textRenderer, IRenderer hyperlinkRenderer) : IRenderer +public class ListRenderer(IRenderer textRenderer, IRenderer hyperlinkRenderer) : IRenderer { public IHtmlContent Render(List item) { @@ -14,8 +14,20 @@ public IHtmlContent Render(List item) return null; } - var ul = new TagBuilder("ul"); - ul.AddCssClass("govuk-list govuk-list--bullet"); + // if node type = ol then use tag builder for orderedlist + TagBuilder listTag; + string cssClass; + if (item.NodeType == "unordered-list") + { + listTag = new TagBuilder("ul"); + cssClass = "govuk-list govuk-list--bullet"; + } + else + { + listTag = new TagBuilder("ol"); + cssClass = "govuk-list govuk-list--number"; + } + listTag.AddCssClass(cssClass); foreach (var listItem in item.Content.OfType()) { @@ -38,9 +50,9 @@ public IHtmlContent Render(List item) } } } - ul.InnerHtml.AppendHtml(li); + listTag.InnerHtml.AppendHtml(li); } - return ul; + return listTag; } } \ No newline at end of file diff --git a/Childrens-Social-Care-CPD/Contentful/Renderers/OrderedListRenderer.cs b/Childrens-Social-Care-CPD/Contentful/Renderers/OrderedListRenderer.cs deleted file mode 100644 index 7ce5dccf..00000000 --- a/Childrens-Social-Care-CPD/Contentful/Renderers/OrderedListRenderer.cs +++ /dev/null @@ -1,50 +0,0 @@ -using Contentful.Core.Models; -using Microsoft.AspNetCore.Html; -using Microsoft.AspNetCore.Mvc.Rendering; -using Text = Contentful.Core.Models.Text; - -namespace Childrens_Social_Care_CPD.Contentful.Renderers; - -public class OrderedListRenderer(IRenderer textRenderer, IRenderer hyperlinkRenderer) : IRenderer -{ - public IHtmlAsyncContent Render(List item) - { - if (item.Content.Count == 0) - { - return null; - } - - var ol = new TagBuilder("ol"); - ol.AddCssClass("govuk-list govuk-list--number"); - - foreach (var listItem in item.Content.OfType()) - { - if (listItem.Content[0] is not Paragraph paragraph) continue; - - var li = new TagBuilder("li"); - foreach (var content in paragraph.Content) - { - switch (content) - { - case Text text: - { - li.InnerHtml.AppendHtml(textRenderer.Render(text)); - break; - } - case Hyperlink hyperlink: - { - li.InnerHtml.AppendHtml(hyperlinkRenderer.Render(hyperlink)); - break; - } - } - } - ol.InnerHtml.AppendHtml(li); - } - return (IHtmlAsyncContent)ol; - } - - IHtmlContent IRenderer.Render(List item) - { - throw new NotImplementedException(); - } -} \ No newline at end of file diff --git a/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml b/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml index 94c29ddc..b19d98fb 100644 --- a/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml +++ b/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml @@ -16,8 +16,7 @@ @inject IRenderer _quoteRenderer @inject IRenderer _horizontalRulerRenderer @inject IRenderer
_tableRenderer -@inject IRenderer _unorderedlistRenderer -@inject IRenderer _orderedListRenderer +@inject IRenderer _listRenderer @inject IRenderer _roleListRenderer @inject IRenderer _assetStructureRenderer @@ -35,15 +34,7 @@ @_paragraphRenderer.Render(paragraph) break; case List list: - if (((Contentful.Core.Models.List)node).NodeType == "unordered-list") - { - @_unorderedlistRenderer.Render(list) - ; - } - else - { - @_orderedListRenderer.Render(list) - } + @_listRenderer.Render(list) break; case Heading1 heading1: @_heading1Renderer.Render(heading1) From ed88e66f492c7f8e345d47878374c3e0b442800b Mon Sep 17 00:00:00 2001 From: Maria Date: Wed, 9 Oct 2024 11:05:04 +0100 Subject: [PATCH 3/5] test added and comments removed --- .../Contentful/Renderers/ListRendererTests.cs | 45 +++++++++++++++++++ .../Contentful/Renderers/ListRenderer.cs | 1 - 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ListRendererTests.cs b/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ListRendererTests.cs index 129c170b..ce3d50b1 100644 --- a/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ListRendererTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ListRendererTests.cs @@ -131,4 +131,49 @@ public void List_Only_Renders_Paragraphs() actual.Should().Be("
  • AAA
"); } + [Test] + public void Numbered_List_Renders() + { + // arrange + var stringWriter = new StringWriter(); + var list = new List() + { + NodeType = "ordered-list", + Content = new List + { + new ListItem + { + Content = new List + { + new Paragraph + { + Content = new List { new Text() } + } + } + }, + new ListItem + { + Content = new List + { + new Paragraph + { + Content = new List { new Hyperlink() } + } + } + + } + } + }; + _textLinkRenderer.Render(Arg.Any()).Returns(new HtmlString("AAA")); + _hyperlinkRenderer.Render(Arg.Any()).Returns(new HtmlString("BBB")); + + // act + var htmlContent = _sut.Render(list); + htmlContent.WriteTo(stringWriter, new HtmlTestEncoder()); + var actual = stringWriter.ToString(); + + // assert + actual.Should().Be("
  1. AAA
  2. BBB
"); + } + } diff --git a/Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs b/Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs index b457f2b4..8c295c61 100644 --- a/Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs +++ b/Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs @@ -14,7 +14,6 @@ public IHtmlContent Render(List item) return null; } - // if node type = ol then use tag builder for orderedlist TagBuilder listTag; string cssClass; if (item.NodeType == "unordered-list") From 1f19629264128280ed16d59b6f4f7b0a710209ff Mon Sep 17 00:00:00 2001 From: Maria Date: Wed, 9 Oct 2024 11:35:33 +0100 Subject: [PATCH 4/5] tests amended --- .../Contentful/Renderers/ListRendererTests.cs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ListRendererTests.cs b/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ListRendererTests.cs index ce3d50b1..3735c93e 100644 --- a/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ListRendererTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Contentful/Renderers/ListRendererTests.cs @@ -47,12 +47,13 @@ public void List_Does_Not_Render_When_No_Items() } [Test] - public void List_Renders() + public void Unordered_List_Renders() { // arrange var stringWriter = new StringWriter(); var list = new List() { + NodeType = "unordered-list", Content = new List { new ListItem @@ -91,12 +92,13 @@ public void List_Renders() } [Test] - public void List_Only_Renders_Paragraphs() + public void Ordered_List_Renders() { // arrange var stringWriter = new StringWriter(); var list = new List() { + NodeType = "ordered-list", Content = new List { new ListItem @@ -113,7 +115,10 @@ public void List_Only_Renders_Paragraphs() { Content = new List { - new Text() + new Paragraph + { + Content = new List { new Hyperlink() } + } } } @@ -128,19 +133,19 @@ public void List_Only_Renders_Paragraphs() var actual = stringWriter.ToString(); // assert - actual.Should().Be("
  • AAA
"); + actual.Should().Be("
  1. AAA
  2. BBB
"); } [Test] - public void Numbered_List_Renders() + public void List_Only_Renders_Paragraphs() { // arrange var stringWriter = new StringWriter(); var list = new List() { - NodeType = "ordered-list", + NodeType = "unordered-list", Content = new List - { + { new ListItem { Content = new List @@ -155,10 +160,7 @@ public void Numbered_List_Renders() { Content = new List { - new Paragraph - { - Content = new List { new Hyperlink() } - } + new Text() } } @@ -173,7 +175,7 @@ public void Numbered_List_Renders() var actual = stringWriter.ToString(); // assert - actual.Should().Be("
  1. AAA
  2. BBB
"); + actual.Should().Be("
  • AAA
"); } } From 0ee9cb5eee79e66e44f39756be04d64b77866cb2 Mon Sep 17 00:00:00 2001 From: Matt Barton Date: Wed, 16 Oct 2024 10:33:59 +0100 Subject: [PATCH 5/5] Reverting code formatting changes --- .../Views/Shared/_RichText.cshtml | 54 +++++-------------- 1 file changed, 14 insertions(+), 40 deletions(-) diff --git a/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml b/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml index b19d98fb..c1a6816b 100644 --- a/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml +++ b/Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml @@ -27,52 +27,26 @@ { switch (node) { - case HorizontalRuler hr: - @_horizontalRulerRenderer.Render(hr) - break; - case Paragraph paragraph: - @_paragraphRenderer.Render(paragraph) - break; - case List list: - @_listRenderer.Render(list) - break; - case Heading1 heading1: - @_heading1Renderer.Render(heading1) - break; - case Heading2 heading2: - @_heading2Renderer.Render(heading2) - break; - case Heading3 heading3: - @_heading3Renderer.Render(heading3) - break; - case Heading4 heading4: - @_heading4Renderer.Render(heading4) - break; - case Heading5 heading5: - @_heading5Renderer.Render(heading5) - break; - case Heading6 heading6: - @_heading6Renderer.Render(heading6) - break; - case Table table: - @_tableRenderer.Render(table) - break; - case Quote quote: - @_quoteRenderer.Render(quote) - break; - case EntryStructure entryStructure: - { + case HorizontalRuler hr: @_horizontalRulerRenderer.Render(hr) break; + case Paragraph paragraph: @_paragraphRenderer.Render(paragraph) break; + case List list: @_listRenderer.Render(list) break; + case Heading1 heading1: @_heading1Renderer.Render(heading1) break; + case Heading2 heading2: @_heading2Renderer.Render(heading2) break; + case Heading3 heading3: @_heading3Renderer.Render(heading3) break; + case Heading4 heading4: @_heading4Renderer.Render(heading4) break; + case Heading5 heading5: @_heading5Renderer.Render(heading5) break; + case Heading6 heading6: @_heading6Renderer.Render(heading6) break; + case Table table: @_tableRenderer.Render(table) break; + case Quote quote: @_quoteRenderer.Render(quote) break; + case EntryStructure entryStructure: { switch (entryStructure.Data.Target) { - case RoleList roleList: - @_roleListRenderer.Render(roleList) + case RoleList roleList: @_roleListRenderer.Render(roleList) break; } break; } - case AssetStructure assetStructure: - @_assetStructureRenderer.Render(assetStructure) - break; + case AssetStructure assetStructure: @_assetStructureRenderer.Render(assetStructure) break; } } } \ No newline at end of file