Skip to content

Commit 14ba4d8

Browse files
committed
Fix case from PR feedback.
1 parent dde1040 commit 14ba4d8

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/Umbraco.Infrastructure/DeliveryApi/ApiRichTextElementParser.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using HtmlAgilityPack;
2-
using Microsoft.Extensions.DependencyInjection;
32
using Microsoft.Extensions.Logging;
43
using Umbraco.Cms.Core;
54
using Umbraco.Cms.Core.DeliveryApi;
6-
using Umbraco.Cms.Core.DependencyInjection;
75
using Umbraco.Cms.Core.Models.Blocks;
86
using Umbraco.Cms.Core.Models.DeliveryApi;
97
using Umbraco.Cms.Core.PublishedCache;
10-
using Umbraco.Cms.Core.Routing;
118
using Umbraco.Cms.Infrastructure.Extensions;
129
using Umbraco.Extensions;
1310

@@ -103,7 +100,7 @@ private T ParseElement<T>(HtmlNode element, IPublishedSnapshot publishedSnapshot
103100
// - non-empty #text nodes
104101
// - empty #text between inline elements (see #17037) but not #text with only newlines (see #19388)
105102
HtmlNode[] childNodes = element.ChildNodes
106-
.Where(c => c.Name != CommentNodeName && (c.Name != TextNodeName || (c.NextSibling is not null && c.PreviousSibling is not null) || IsNonEmptyElement(c)))
103+
.Where(c => c.Name != CommentNodeName && (c.Name != TextNodeName || IsNonEmptyElement(c)))
107104
.ToArray();
108105

109106
var tag = TagName(element);

tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/RichTextParserTests.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,18 @@ public void ParseElement_CanHandleWhitespaceAroundInlineElemements()
372372
AssertTestParagraph(paragraphElement);
373373
}
374374

375-
[Test]
376-
public void ParseElement_RemovesNewLinesAroundHtmlElements()
375+
[TestCase(1, "\n")]
376+
[TestCase(2, "\n")]
377+
[TestCase(1, "\r")]
378+
[TestCase(2, "\r")]
379+
[TestCase(1, "\r\n")]
380+
[TestCase(2, "\r\n")]
381+
public void ParseElement_RemovesNewLinesAroundHtmlStructuralElements(int numberOfNewLineCharacters, string newlineCharacter)
377382
{
378383
var parser = CreateRichTextElementParser();
379384

380-
var element = parser.Parse($"<table>\n<tr>\n<td>{TestParagraph}</td>\n</tr>\n</table>") as RichTextRootElement;
385+
var newLineSeparator = string.Concat(Enumerable.Repeat(newlineCharacter, numberOfNewLineCharacters));
386+
var element = parser.Parse($"<table>{newLineSeparator}<tr>{newLineSeparator}<td>{TestParagraph}</td>{newLineSeparator}</tr>{newLineSeparator}</table>") as RichTextRootElement;
381387
Assert.IsNotNull(element);
382388
var tableElement = element.Elements.Single() as RichTextGenericElement;
383389
Assert.IsNotNull(tableElement);
@@ -391,6 +397,29 @@ public void ParseElement_RemovesNewLinesAroundHtmlElements()
391397
AssertTestParagraph(cellElement);
392398
}
393399

400+
[TestCase(1, "\n")]
401+
[TestCase(2, "\n")]
402+
[TestCase(1, "\r")]
403+
[TestCase(2, "\r")]
404+
[TestCase(1, "\r\n")]
405+
[TestCase(2, "\r\n")]
406+
public void ParseElement_RemovesNewLinesAroundHtmlContentElements(int numberOfNewLineCharacters, string newlineCharacter)
407+
{
408+
var parser = CreateRichTextElementParser();
409+
410+
var newLineSeparator = string.Concat(Enumerable.Repeat(newlineCharacter, numberOfNewLineCharacters));
411+
var element = parser.Parse($"<div><p>{TestParagraph}</p>{newLineSeparator}<p></p>{newLineSeparator}<p>&nbsp;</p>{newLineSeparator}<p>{TestParagraph}</p></div>") as RichTextRootElement;
412+
Assert.IsNotNull(element);
413+
var divElement = element.Elements.Single() as RichTextGenericElement;
414+
Assert.IsNotNull(divElement);
415+
416+
var paragraphELements = divElement.Elements;
417+
Assert.AreEqual(4, paragraphELements.Count());
418+
419+
AssertTestParagraph(paragraphELements.First() as RichTextGenericElement);
420+
AssertTestParagraph(paragraphELements.Last() as RichTextGenericElement);
421+
}
422+
394423
private static void AssertTestParagraph(RichTextGenericElement paragraphElement)
395424
{
396425
var childElements = paragraphElement.Elements.ToArray();

0 commit comments

Comments
 (0)