Skip to content

Commit

Permalink
Merge pull request #518 from DFE-Digital/SWCD-2379-numbered-list-bug
Browse files Browse the repository at this point in the history
SWCD-2379: numbered list bug
  • Loading branch information
mattb-hippo authored Oct 16, 2024
2 parents 63f5084 + e617678 commit d181ba7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IContent>
{
new ListItem
Expand Down Expand Up @@ -90,13 +91,59 @@ public void List_Renders()
actual.Should().Be("<ul class=\"HtmlEncode[[govuk-list govuk-list--bullet]]\"><li>AAA</li><li>BBB</li></ul>");
}

[Test]
public void Ordered_List_Renders()
{
// arrange
var stringWriter = new StringWriter();
var list = new List()
{
NodeType = "ordered-list",
Content = new List<IContent>
{
new ListItem
{
Content = new List<IContent>
{
new Paragraph
{
Content = new List<IContent> { new Text() }
}
}
},
new ListItem
{
Content = new List<IContent>
{
new Paragraph
{
Content = new List<IContent> { new Hyperlink() }
}
}

}
}
};
_textLinkRenderer.Render(Arg.Any<Text>()).Returns(new HtmlString("AAA"));
_hyperlinkRenderer.Render(Arg.Any<Hyperlink>()).Returns(new HtmlString("BBB"));

// act
var htmlContent = _sut.Render(list);
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder());
var actual = stringWriter.ToString();

// assert
actual.Should().Be("<ol class=\"HtmlEncode[[govuk-list govuk-list--number]]\"><li>AAA</li><li>BBB</li></ol>");
}

[Test]
public void List_Only_Renders_Paragraphs()
{
// arrange
var stringWriter = new StringWriter();
var list = new List()
{
NodeType = "unordered-list",
Content = new List<IContent>
{
new ListItem
Expand Down
21 changes: 16 additions & 5 deletions Childrens-Social-Care-CPD/Contentful/Renderers/ListRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ public IHtmlContent Render(List item)
return null;
}

var ul = new TagBuilder("ul");
ul.AddCssClass("govuk-list govuk-list--bullet");
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<ListItem>())
{
Expand All @@ -38,9 +49,9 @@ public IHtmlContent Render(List item)
}
}
}
ul.InnerHtml.AppendHtml(li);
listTag.InnerHtml.AppendHtml(li);
}

return ul;
return listTag;
}
}
}

0 comments on commit d181ba7

Please sign in to comment.