Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using HtmlAgilityPack;
using Microsoft.Extensions.DependencyInjection;
using HtmlAgilityPack;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Cms.Core.Models.DeliveryApi;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Infrastructure.Extensions;
using Umbraco.Extensions;

Expand Down Expand Up @@ -101,9 +98,9 @@ private T ParseElement<T>(HtmlNode element, IPublishedSnapshot publishedSnapshot
// - non-#comment nodes
// - non-#text nodes
// - non-empty #text nodes
// - empty #text between inline elements (see #17037)
// - empty #text between inline elements (see #17037) but not #text with only newlines (see #19388)
HtmlNode[] childNodes = element.ChildNodes
.Where(c => c.Name != CommentNodeName && (c.Name != TextNodeName || c.NextSibling is not null || string.IsNullOrWhiteSpace(c.InnerText) is false))
.Where(c => c.Name != CommentNodeName && (c.Name != TextNodeName || IsNonEmptyElement(c)))
.ToArray();

var tag = TagName(element);
Expand All @@ -124,6 +121,9 @@ private T ParseElement<T>(HtmlNode element, IPublishedSnapshot publishedSnapshot
return createElement(tag, attributes, childElements);
}

private static bool IsNonEmptyElement(HtmlNode htmlNode) =>
string.IsNullOrWhiteSpace(htmlNode.InnerText) is false || htmlNode.InnerText.Any(c => c != '\n' && c != '\r');

private string TagName(HtmlNode htmlNode) => htmlNode.Name;

private void ReplaceLocalLinks(IPublishedSnapshot publishedSnapshot, Dictionary<string, object> attributes)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;

Check notice on line 1 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/DeliveryApi/RichTextParserTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

✅ Getting better: String Heavy Function Arguments

The ratio of strings in function arguments decreases from 42.86% to 42.11%, threshold = 39.0%. The functions in this file have a high ratio of strings as arguments. Avoid adding more.
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
Expand Down Expand Up @@ -357,16 +357,71 @@
Assert.IsEmpty(blockLevelBlock.Elements);
}

private const string TestParagraph = "What follows from <strong>here</strong> <em>is</em> <a href=\"#\">just</a> a bunch of text.";

[Test]
public void ParseElement_CanHandleWhitespaceAroundInlineElemements()
{
var parser = CreateRichTextElementParser();

var element = parser.Parse("<p>What follows from <strong>here</strong> <em>is</em> <a href=\"#\">just</a> a bunch of text.</p>") as RichTextRootElement;
var element = parser.Parse($"<p>{TestParagraph}</p>") as RichTextRootElement;
Assert.IsNotNull(element);
var paragraphElement = element.Elements.Single() as RichTextGenericElement;
Assert.IsNotNull(paragraphElement);

AssertTestParagraph(paragraphElement);
}

[TestCase(1, "\n")]
[TestCase(2, "\n")]
[TestCase(1, "\r")]
[TestCase(2, "\r")]
[TestCase(1, "\r\n")]
[TestCase(2, "\r\n")]
public void ParseElement_RemovesNewLinesAroundHtmlStructuralElements(int numberOfNewLineCharacters, string newlineCharacter)
{
var parser = CreateRichTextElementParser();

var newLineSeparator = string.Concat(Enumerable.Repeat(newlineCharacter, numberOfNewLineCharacters));
var element = parser.Parse($"<table>{newLineSeparator}<tr>{newLineSeparator}<td>{TestParagraph}</td>{newLineSeparator}</tr>{newLineSeparator}</table>") as RichTextRootElement;
Assert.IsNotNull(element);
var tableElement = element.Elements.Single() as RichTextGenericElement;
Assert.IsNotNull(tableElement);

var rowElement = tableElement.Elements.Single() as RichTextGenericElement;
Assert.IsNotNull(rowElement);

var cellElement = rowElement.Elements.Single() as RichTextGenericElement;
Assert.IsNotNull(cellElement);

AssertTestParagraph(cellElement);
}

[TestCase(1, "\n")]
[TestCase(2, "\n")]
[TestCase(1, "\r")]
[TestCase(2, "\r")]
[TestCase(1, "\r\n")]
[TestCase(2, "\r\n")]
public void ParseElement_RemovesNewLinesAroundHtmlContentElements(int numberOfNewLineCharacters, string newlineCharacter)
{
var parser = CreateRichTextElementParser();

var newLineSeparator = string.Concat(Enumerable.Repeat(newlineCharacter, numberOfNewLineCharacters));
var element = parser.Parse($"<div><p>{TestParagraph}</p>{newLineSeparator}<p></p>{newLineSeparator}<p>&nbsp;</p>{newLineSeparator}<p>{TestParagraph}</p></div>") as RichTextRootElement;
Assert.IsNotNull(element);
var divElement = element.Elements.Single() as RichTextGenericElement;
Assert.IsNotNull(divElement);

var paragraphELements = divElement.Elements;
Assert.AreEqual(4, paragraphELements.Count());

AssertTestParagraph(paragraphELements.First() as RichTextGenericElement);
AssertTestParagraph(paragraphELements.Last() as RichTextGenericElement);
}

private static void AssertTestParagraph(RichTextGenericElement paragraphElement)
{
var childElements = paragraphElement.Elements.ToArray();
Assert.AreEqual(7, childElements.Length);

Expand Down
Loading