Skip to content

Commit

Permalink
V15: Allows blocks in rich text editor to leave out the "Umbraco-Bloc…
Browse files Browse the repository at this point in the history
…k" HTML comment (#17118)

* feat: allows blocks in rte to leave out the "Umbraco-Block" comment from their bodies

* test: add unit test to test blocks without html comments as well as inline blocks
  • Loading branch information
iOvergaard authored Sep 24, 2024
1 parent b93fe6c commit cd9979b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters;

internal static partial class RichTextParsingRegexes
{
[GeneratedRegex("<umb-rte-block(?:-inline)?(?: class=\"(.[^\"]*)\")? data-content-udi=\"(?<udi>.[^\"]*)\"><!--Umbraco-Block--><\\/umb-rte-block(?:-inline)?>")]
[GeneratedRegex("<umb-rte-block(?:-inline)?(?: class=\"(.[^\"]*)\")? data-content-udi=\"(?<udi>.[^\"]*)\">(?:<!--Umbraco-Block-->)?<\\/umb-rte-block(?:-inline)?>")]
public static partial Regex BlockRegex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void Can_Parse_Blocks_With_Content_Only()
{
const string input = """
{
"markup": "<p>this is some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"><!--Umbraco-Block--></umb-rte-block>",
"markup": "<p>this is some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"></umb-rte-block>",
"blocks": {
"layout": {
"Umbraco.TinyMCE": [{
Expand All @@ -157,7 +157,7 @@ public void Can_Parse_Blocks_With_Content_Only()
var result = RichTextPropertyEditorHelper.TryParseRichTextEditorValue(input, JsonSerializer(), Logger(), out RichTextEditorValue? value);
Assert.IsTrue(result);
Assert.IsNotNull(value);
Assert.AreEqual("<p>this is some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"><!--Umbraco-Block--></umb-rte-block>", value.Markup);
Assert.AreEqual("<p>this is some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"></umb-rte-block>", value.Markup);

Assert.IsNotNull(value.Blocks);

Expand All @@ -172,6 +172,57 @@ public void Can_Parse_Blocks_With_Content_Only()
Assert.AreEqual(0, value.Blocks.SettingsData.Count);
}

[Test]
public void Can_Parse_Mixed_Blocks_And_Inline_Blocks()
{
const string input = """
{
"markup": "<p>this is <umb-rte-block-inline data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf03\"></umb-rte-block-inline> some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"></umb-rte-block>",
"blocks": {
"layout": {
"Umbraco.TinyMCE": [{
"contentUdi": "umb://element/36cc710ad8a645d0a07f7bbd8742cf02"
}, {
"contentUdi": "umb://element/36cc710ad8a645d0a07f7bbd8742cf03"
}
]
},
"contentData": [{
"contentTypeKey": "b2f0806c-d231-4c78-88b2-3c97d26e1123",
"udi": "umb://element/36cc710ad8a645d0a07f7bbd8742cf02",
"contentPropertyAlias": "A content property value"
}, {
"contentTypeKey": "b2f0806c-d231-4c78-88b2-3c97d26e1124",
"udi": "umb://element/36cc710ad8a645d0a07f7bbd8742cf03",
"contentPropertyAlias": "A content property value"
}
],
"settingsData": []
}
}
""";

var result = RichTextPropertyEditorHelper.TryParseRichTextEditorValue(input, JsonSerializer(), Logger(), out RichTextEditorValue? value);
Assert.IsTrue(result);
Assert.IsNotNull(value);
Assert.AreEqual("<p>this is <umb-rte-block-inline data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf03\"></umb-rte-block-inline> some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"></umb-rte-block>", value.Markup);

Assert.IsNotNull(value.Blocks);

Guid[] contentTypeGuids = [Guid.Parse("b2f0806c-d231-4c78-88b2-3c97d26e1123"), Guid.Parse("b2f0806c-d231-4c78-88b2-3c97d26e1124")];
Guid[] itemGuids = [Guid.Parse("36cc710a-d8a6-45d0-a07f-7bbd8742cf02"), Guid.Parse("36cc710a-d8a6-45d0-a07f-7bbd8742cf03")];

Assert.AreEqual(2, value.Blocks.ContentData.Count);
for (var i = 0; i < value.Blocks.ContentData.Count; i++) {
var item = value.Blocks.ContentData[i];
Assert.AreEqual(contentTypeGuids[i], item.ContentTypeKey);
Assert.AreEqual(new GuidUdi(Constants.UdiEntityType.Element, itemGuids[i]), item.Udi);
Assert.AreEqual(itemGuids[i], item.Key);
}

Assert.AreEqual(0, value.Blocks.SettingsData.Count);
}

private IJsonSerializer JsonSerializer() => new SystemTextJsonSerializer();

private ILogger Logger() => Mock.Of<ILogger>();
Expand Down

0 comments on commit cd9979b

Please sign in to comment.