Skip to content
Open
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 @@ -143,8 +143,9 @@ private void ReplaceLocalLinks(IPublishedContentCache contentCache, IPublishedMe
mediaCache,
href,
type,
route =>
(route, contentId) =>
{
attributes["destination-id"] = contentId;
attributes["route"] = route;
attributes.Remove("href");
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Extensions;

Expand Down Expand Up @@ -58,17 +59,20 @@ private void ReplaceLocalLinks(HtmlDocument doc, IPublishedContentCache contentC
mediaCache,
link.GetAttributeValue("href", string.Empty),
link.GetAttributeValue("type", "unknown"),
route =>
(route, contentId) =>
{
link.SetAttributeValue("href", $"{route.Path}{route.QueryString}");
link.SetAttributeValue("data-destination-id", contentId.ToString("D"));
link.SetAttributeValue("data-start-item-path", route.StartItem.Path);
link.SetAttributeValue("data-start-item-id", route.StartItem.Id.ToString("D"));
link.Attributes["type"]?.Remove();
link.SetAttributeValue("data-link-type", nameof(LinkType.Content));
},
url =>
{
link.SetAttributeValue("href", url);
link.Attributes["type"]?.Remove();
link.SetAttributeValue("data-link-type", nameof(LinkType.Media));
},
() =>
{
Expand Down
10 changes: 5 additions & 5 deletions src/Umbraco.Infrastructure/DeliveryApi/ApiRichTextParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected ApiRichTextParserBase(IApiContentRouteBuilder apiContentRouteBuilder,
_apiMediaUrlProvider = apiMediaUrlProvider;
}

protected void ReplaceLocalLinks(IPublishedContentCache contentCache, IPublishedMediaCache mediaCache, string href, string type, Action<IApiContentRoute> handleContentRoute, Action<string> handleMediaUrl, Action handleInvalidLink)
protected void ReplaceLocalLinks(IPublishedContentCache contentCache, IPublishedMediaCache mediaCache, string href, string type, Action<IApiContentRoute, Guid> handleContentRoute, Action<string> handleMediaUrl, Action handleInvalidLink)
{
ReplaceStatus replaceAttempt = ReplaceLocalLink(contentCache, mediaCache, href, type, handleContentRoute, handleMediaUrl);
if (replaceAttempt == ReplaceStatus.Success)
Expand All @@ -35,7 +35,7 @@ protected void ReplaceLocalLinks(IPublishedContentCache contentCache, IPublished
}
}

private ReplaceStatus ReplaceLocalLink(IPublishedContentCache contentCache, IPublishedMediaCache mediaCache, string href, string type, Action<IApiContentRoute> handleContentRoute, Action<string> handleMediaUrl)
private ReplaceStatus ReplaceLocalLink(IPublishedContentCache contentCache, IPublishedMediaCache mediaCache, string href, string type, Action<IApiContentRoute, Guid> handleContentRoute, Action<string> handleMediaUrl)
{
Match match = LocalLinkRegex().Match(href);
if (match.Success is false)
Expand All @@ -60,7 +60,7 @@ private ReplaceStatus ReplaceLocalLink(IPublishedContentCache contentCache, IPub
if (route != null)
{
route.QueryString = match.Groups["query"].Value.NullOrWhiteSpaceAsNull();
handleContentRoute(route);
handleContentRoute(route, guid);
return ReplaceStatus.Success;
}

Expand All @@ -79,7 +79,7 @@ private ReplaceStatus ReplaceLocalLink(IPublishedContentCache contentCache, IPub
return ReplaceStatus.InvalidEntityType;
}

private ReplaceStatus ReplaceLegacyLocalLink(IPublishedContentCache contentCache, IPublishedMediaCache mediaCache, string href, Action<IApiContentRoute> handleContentRoute, Action<string> handleMediaUrl)
private ReplaceStatus ReplaceLegacyLocalLink(IPublishedContentCache contentCache, IPublishedMediaCache mediaCache, string href, Action<IApiContentRoute, Guid> handleContentRoute, Action<string> handleMediaUrl)
{
Match match = LegacyLocalLinkRegex().Match(href);
if (match.Success is false)
Expand Down Expand Up @@ -108,7 +108,7 @@ private ReplaceStatus ReplaceLegacyLocalLink(IPublishedContentCache contentCache
if (route != null)
{
route.QueryString = match.Groups["query"].Value.NullOrWhiteSpaceAsNull();
handleContentRoute(route);
handleContentRoute(route, guidUdi.Guid);
return ReplaceStatus.Success;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Microsoft.Extensions.Logging;

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

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ Getting worse: Code Duplication

introduced similar code in: ParseMarkup_CanParseContentLink,ParseMarkup_CanParseContentLink_WithPostfix,ParseMarkup_CanParseLegacyContentLink,ParseMarkup_CanParseLegacyContentLink_WithPostfix. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Cms.Core.Models.DeliveryApi;
using Umbraco.Cms.Core.Models.PublishedContent;
Expand Down Expand Up @@ -141,9 +142,12 @@
var link = element.Elements.OfType<RichTextGenericElement>().Single().Elements.Single() as RichTextGenericElement;
Assert.IsNotNull(link);
Assert.AreEqual("a", link.Tag);
Assert.AreEqual(1, link.Attributes.Count);
Assert.AreEqual("route", link.Attributes.First().Key);
var route = link.Attributes.First().Value as IApiContentRoute;
Assert.AreEqual(2, link.Attributes.Count);
Assert.IsNotNull(link.Attributes["route"]);
Assert.IsNotNull(link.Attributes["destination-id"]);
var id = (Guid)link.Attributes["destination-id"];
Assert.AreEqual(_contentKey, id);
var route = link.Attributes["route"] as IApiContentRoute;

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

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

✅ Getting better: Duplicated Assertion Blocks

The number of functions with duplicated assertion blocks decreases from 5 to 4 (reduced in ParseElement_CanHandleNonLocalLink,ParseElement_CanHandleNonLocalLink_WithPostfix,ParseElement_CanParseContentLink,ParseElement_CanParseMediaImage and 1 more functions), threshold = 2. This test file has several blocks of duplicated assertion statements. Avoid adding more.
Assert.IsNotNull(route);
Assert.AreEqual("/some-content-path", route.Path);
Assert.AreEqual(postfix.NullOrWhiteSpaceAsNull(), route.QueryString);
Expand Down Expand Up @@ -482,8 +486,10 @@

var result = parser.Parse($"<p><a href=\"/{{localLink:{_contentKey:N}}}\" type=\"document\"></a></p>");
Assert.IsTrue(result.Contains("href=\"/some-content-path\""));
Assert.IsTrue(result.Contains($"data-destination-id=\"{_contentKey:D}\""));
Assert.IsTrue(result.Contains("data-start-item-path=\"the-root-path\""));
Assert.IsTrue(result.Contains($"data-start-item-id=\"{_contentRootKey:D}\""));
Assert.IsTrue(result.Contains($"data-link-type=\"{LinkType.Content}\""));
}

[Test]
Expand All @@ -493,8 +499,10 @@

var result = parser.Parse($"<p><a href=\"/{{localLink:umb://document/{_contentKey:N}}}\"></a></p>");
Assert.IsTrue(result.Contains("href=\"/some-content-path\""));
Assert.IsTrue(result.Contains($"data-destination-id=\"{_contentKey:D}\""));
Assert.IsTrue(result.Contains("data-start-item-path=\"the-root-path\""));
Assert.IsTrue(result.Contains($"data-start-item-id=\"{_contentRootKey:D}\""));
Assert.IsTrue(result.Contains($"data-link-type=\"{LinkType.Content}\""));
}

[TestCase("#some-anchor")]
Expand All @@ -507,8 +515,10 @@

var result = parser.Parse($"<p><a href=\"/{{localLink:{_contentKey:N}}}{postfix}\" type=\"document\"></a></p>");
Assert.IsTrue(result.Contains($"href=\"/some-content-path{postfix}\""));
Assert.IsTrue(result.Contains($"data-destination-id=\"{_contentKey:D}\""));
Assert.IsTrue(result.Contains("data-start-item-path=\"the-root-path\""));
Assert.IsTrue(result.Contains($"data-start-item-id=\"{_contentRootKey:D}\""));
Assert.IsTrue(result.Contains($"data-link-type=\"{LinkType.Content}\""));
}

[TestCase("#some-anchor")]
Expand All @@ -521,8 +531,10 @@

var result = parser.Parse($"<p><a href=\"/{{localLink:umb://document/{_contentKey:N}}}{postfix}\"></a></p>");
Assert.IsTrue(result.Contains($"href=\"/some-content-path{postfix}\""));
Assert.IsTrue(result.Contains($"data-destination-id=\"{_contentKey:D}\""));
Assert.IsTrue(result.Contains("data-start-item-path=\"the-root-path\""));
Assert.IsTrue(result.Contains($"data-start-item-id=\"{_contentRootKey:D}\""));
Assert.IsTrue(result.Contains($"data-link-type=\"{LinkType.Content}\""));
}

[Test]
Expand All @@ -532,6 +544,8 @@

var result = parser.Parse($"<p><a href=\"/{{localLink:umb://media/{_mediaKey:N}}}\"></a></p>");
Assert.IsTrue(result.Contains("href=\"/some-media-url\""));
Assert.IsTrue(result.Contains($"data-link-type=\"{LinkType.Media}\""));

}

[TestCase("{localLink:umb://document/fe5bf80d37db4373adb9b206896b4a3b}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.DeliveryApi;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PublishedCache;
Expand Down Expand Up @@ -36,7 +37,7 @@ public void Can_Parse_Legacy_LocalLinks()
"<p><a href=\"/{localLink:umb://document/a1c5d649977f4ea59b1cb26055f3eed3}\" title=\"Inline\">link </a>to another page</p>";

var expectedOutput =
"<p><a href=\"/inline/\" title=\"Inline\" data-start-item-path=\"inline\" data-start-item-id=\"a1c5d649-977f-4ea5-9b1c-b26055f3eed3\">link </a>to another page</p>";
$"<p><a href=\"/inline/\" title=\"Inline\" data-destination-id=\"{key1:D}\" data-start-item-path=\"inline\" data-start-item-id=\"a1c5d649-977f-4ea5-9b1c-b26055f3eed3\" data-link-type=\"{LinkType.Content}\">link </a>to another page</p>";

var parsedHtml = parser.Parse(legacyHtml);

Expand Down Expand Up @@ -71,8 +72,8 @@ public void Can_Parse_LocalLinks()
<p>and to the <a type=""document"" href=""/{localLink:cc143afe-4cbf-46e5-b399-c9f451384373}"" title=""other page"">other page</a></p>";

var expectedOutput =
@"<p>Rich text outside of the blocks with a link to <a href=""/self/"" title=""itself"" data-start-item-path=""self"" data-start-item-id=""eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f"">itself</a><br><br></p>
<p>and to the <a href=""/other/"" title=""other page"" data-start-item-path=""other"" data-start-item-id=""cc143afe-4cbf-46e5-b399-c9f451384373"">other page</a></p>";
$@"<p>Rich text outside of the blocks with a link to <a href=""/self/"" title=""itself"" data-destination-id=""{key1:D}"" data-start-item-path=""self"" data-start-item-id=""eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f"" data-link-type=""{LinkType.Content}"">itself</a><br><br></p>
<p>and to the <a href=""/other/"" title=""other page"" data-destination-id=""{key2:D}"" data-start-item-path=""other"" data-start-item-id=""cc143afe-4cbf-46e5-b399-c9f451384373"" data-link-type=""{LinkType.Content}"">other page</a></p>";

var parsedHtml = parser.Parse(html);

Expand Down Expand Up @@ -110,8 +111,8 @@ public void Can_Parse_LocalLinks_With_Postfix(string postfix)
<p>and to the <a type=""document"" href=""/{{localLink:cc143afe-4cbf-46e5-b399-c9f451384373}}{postfix}"" title=""other page"">other page</a></p>";

var expectedOutput =
$@"<p>Rich text outside of the blocks with a link to <a href=""/self/{postfix}"" title=""itself"" data-start-item-path=""self"" data-start-item-id=""eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f"">itself</a><br><br></p>
<p>and to the <a href=""/other/{postfix}"" title=""other page"" data-start-item-path=""other"" data-start-item-id=""cc143afe-4cbf-46e5-b399-c9f451384373"">other page</a></p>";
$@"<p>Rich text outside of the blocks with a link to <a href=""/self/{postfix}"" title=""itself"" data-destination-id=""{key1:D}"" data-start-item-path=""self"" data-start-item-id=""eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f"" data-link-type=""{LinkType.Content}"">itself</a><br><br></p>
<p>and to the <a href=""/other/{postfix}"" title=""other page"" data-destination-id=""{key2:D}"" data-start-item-path=""other"" data-start-item-id=""cc143afe-4cbf-46e5-b399-c9f451384373"" data-link-type=""{LinkType.Content}"">other page</a></p>";

var parsedHtml = parser.Parse(html);

Expand Down
Loading