Skip to content

Commit

Permalink
Make the RTE handle inline elements gracefully for JSON (#17983)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjac authored Jan 15, 2025
1 parent 4dd8fbe commit 8f42a3a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,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)
HtmlNode[] childNodes = element.ChildNodes
.Where(c => c.Name != CommentNodeName && (c.Name != TextNodeName || string.IsNullOrWhiteSpace(c.InnerText) is false))
.Where(c => c.Name != CommentNodeName && (c.Name != TextNodeName || c.NextSibling is not null || string.IsNullOrWhiteSpace(c.InnerText) is false))
.ToArray();

var tag = TagName(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,48 @@ public void ParseElement_CanHandleMixedInlineAndBlockLevelBlocks()
Assert.IsEmpty(blockLevelBlock.Elements);
}

[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;
Assert.IsNotNull(element);
var paragraphElement = element.Elements.Single() as RichTextGenericElement;
Assert.IsNotNull(paragraphElement);

var childElements = paragraphElement.Elements.ToArray();
Assert.AreEqual(7, childElements.Length);

var childElementCounter = 0;

void AssertNextChildElementIsText(string expectedText)
{
var textElement = childElements[childElementCounter++] as RichTextTextElement;
Assert.IsNotNull(textElement);
Assert.AreEqual(expectedText, textElement.Text);
}

void AssertNextChildElementIsGeneric(string expectedTag, string expectedInnerText)
{
var genericElement = childElements[childElementCounter++] as RichTextGenericElement;
Assert.IsNotNull(genericElement);
Assert.AreEqual(expectedTag, genericElement.Tag);
Assert.AreEqual(1, genericElement.Elements.Count());
var textElement = genericElement.Elements.First() as RichTextTextElement;
Assert.IsNotNull(textElement);
Assert.AreEqual(expectedInnerText, textElement.Text);
}

AssertNextChildElementIsText("What follows from ");
AssertNextChildElementIsGeneric("strong", "here");
AssertNextChildElementIsText(" ");
AssertNextChildElementIsGeneric("em", "is");
AssertNextChildElementIsText(" ");
AssertNextChildElementIsGeneric("a", "just");
AssertNextChildElementIsText(" a bunch of text.");
}

[Test]
public void ParseMarkup_CanParseContentLink()
{
Expand Down

0 comments on commit 8f42a3a

Please sign in to comment.