Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V15: Allows blocks in rich text editor to exclude the "Umbraco-Block" HTML comment #17118

Merged
merged 3 commits into from
Sep 24, 2024
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
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
@@ -1,4 +1,4 @@
using System.Text.Json.Nodes;

Check warning on line 1 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/RichTextPropertyEditorHelperTests.cs

View check run for this annotation

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

❌ New issue: Code Duplication

The module contains 2 functions with similar structure: Can_Parse_Blocks_With_Both_Content_And_Settings,Can_Parse_JObject. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
Expand Down Expand Up @@ -135,29 +135,29 @@
{
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": [{
"contentUdi": "umb://element/36cc710ad8a645d0a07f7bbd8742cf02"
}
]
},
"contentData": [{
"contentTypeKey": "b2f0806c-d231-4c78-88b2-3c97d26e1123",
"udi": "umb://element/36cc710ad8a645d0a07f7bbd8742cf02",
"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 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);

Check warning on line 160 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/RichTextPropertyEditorHelperTests.cs

View check run for this annotation

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

❌ New issue: Large Assertion Blocks

The test suite contains 8 assertion blocks with at least 4 assertions, threshold = 4. This test file has several blocks of large, consecutive assert statements. Avoid adding more.

Check warning on line 160 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/RichTextPropertyEditorHelperTests.cs

View check run for this annotation

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

❌ Getting worse: Duplicated Assertion Blocks

The number of functions with duplicated assertion blocks increases from 3 to 4 (introduced in Can_Parse_Mixed_Blocks_And_Inline_Blocks), threshold = 2. This test file has several blocks of duplicated assertion statements. Avoid adding more.

Assert.IsNotNull(value.Blocks);

Expand All @@ -172,6 +172,57 @@
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
Loading