Skip to content

Commit 46acd51

Browse files
authored
[V14] Make the backend work with the new localLinks format (#16661)
* Support new localLink format in core link parsing * Updated devliery api to work with the new locallinks format Added tests for old and new format handling. * Fix error regarding type attribute not always being present (for example old format or non local links)
1 parent 13b77d3 commit 46acd51

File tree

6 files changed

+392
-13
lines changed

6 files changed

+392
-13
lines changed

src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ namespace Umbraco.Cms.Core.Templates;
1010
/// </summary>
1111
public sealed class HtmlLocalLinkParser
1212
{
13+
// needs to support media and document links, order of attributes should not matter nor should other attributes mess with things
14+
// <a type="media" href="/{localLink:7e21a725-b905-4c5f-86dc-8c41ec116e39}" title="media">media</a>
15+
// <a type="document" href="/{localLink:eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f}" title="other page">other page</a>
16+
internal static readonly Regex LocalLinkTagPattern = new(
17+
@"<a\s+(?:(?:(?:type=['""](?<type>document|media)['""].*?(?<locallink>href=[""']/{localLink:(?<guid>[a-fA-F0-9-]+)})[""'])|((?<locallink>href=[""']/{localLink:(?<guid>[a-fA-F0-9-]+)})[""'].*?type=(['""])(?<type>document|media)(?:['""])))|(?:(?:type=['""](?<type>document|media)['""])|(?:(?<locallink>href=[""']/{localLink:[a-fA-F0-9-]+})[""'])))[^>]*>",
18+
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
19+
1320
internal static readonly Regex LocalLinkPattern = new(
1421
@"href=""[/]?(?:\{|\%7B)localLink:([a-zA-Z0-9-://]+)(?:\}|\%7D)",
1522
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
@@ -105,6 +112,32 @@ public string EnsureInternalLinks(string text)
105112
}
106113

107114
private IEnumerable<(int? intId, GuidUdi? udi, string tagValue)> FindLocalLinkIds(string text)
115+
{
116+
MatchCollection localLinkTagMatches = LocalLinkTagPattern.Matches(text);
117+
foreach (Match linkTag in localLinkTagMatches)
118+
{
119+
if (linkTag.Groups.Count < 1)
120+
{
121+
continue;
122+
}
123+
124+
if (Guid.TryParse(linkTag.Groups["guid"].Value, out Guid guid) is false)
125+
{
126+
continue;
127+
}
128+
129+
yield return (null, new GuidUdi(linkTag.Groups["type"].Value, guid), linkTag.Groups["locallink"].Value);
130+
}
131+
132+
// also return legacy results for values that have not been migrated
133+
foreach ((int? intId, GuidUdi? udi, string tagValue) legacyResult in FindLegacyLocalLinkIds(text))
134+
{
135+
yield return legacyResult;
136+
}
137+
}
138+
139+
// todo remove at some point?
140+
private IEnumerable<(int? intId, GuidUdi? udi, string tagValue)> FindLegacyLocalLinkIds(string text)
108141
{
109142
// Parse internal links
110143
MatchCollection tags = LocalLinkPattern.Matches(text);

src/Umbraco.Infrastructure/DeliveryApi/ApiRichTextElementParser.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,15 @@ private void ReplaceLocalLinks(IPublishedSnapshot publishedSnapshot, Dictionary<
132132
return;
133133
}
134134

135+
if (attributes.ContainsKey("type") is false || attributes["type"] is not string type)
136+
{
137+
type = "unknown";
138+
}
139+
135140
ReplaceLocalLinks(
136141
publishedSnapshot,
137142
href,
143+
type,
138144
route =>
139145
{
140146
attributes["route"] = route;

src/Umbraco.Infrastructure/DeliveryApi/ApiRichTextMarkupParser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ private void ReplaceLocalLinks(HtmlDocument doc, IPublishedSnapshot publishedSna
5252
foreach (HtmlNode link in links)
5353
{
5454
ReplaceLocalLinks(
55-
publishedSnapshot,
55+
publishedSnapshot,
5656
link.GetAttributeValue("href", string.Empty),
57+
link.GetAttributeValue("type", "unknown"),
5758
route =>
5859
{
5960
link.SetAttributeValue("href", route.Path);

src/Umbraco.Infrastructure/DeliveryApi/ApiRichTextParserBase.cs

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Umbraco.Cms.Core.Models.DeliveryApi;
55
using Umbraco.Cms.Core.Models.PublishedContent;
66
using Umbraco.Cms.Core.PublishedCache;
7+
using Umbraco.Cms.Core.Templates;
78

89
namespace Umbraco.Cms.Infrastructure.DeliveryApi;
910

@@ -18,20 +19,35 @@ protected ApiRichTextParserBase(IApiContentRouteBuilder apiContentRouteBuilder,
1819
_apiMediaUrlProvider = apiMediaUrlProvider;
1920
}
2021

21-
protected void ReplaceLocalLinks(IPublishedSnapshot publishedSnapshot, string href, Action<IApiContentRoute> handleContentRoute, Action<string> handleMediaUrl, Action handleInvalidLink)
22+
protected void ReplaceLocalLinks(IPublishedSnapshot publishedSnapshot, string href, string type, Action<IApiContentRoute> handleContentRoute, Action<string> handleMediaUrl, Action handleInvalidLink)
23+
{
24+
ReplaceStatus replaceAttempt = ReplaceLocalLink(publishedSnapshot, href, type, handleContentRoute, handleMediaUrl);
25+
if (replaceAttempt == ReplaceStatus.Success)
26+
{
27+
return;
28+
}
29+
30+
if (replaceAttempt == ReplaceStatus.InvalidEntityType || ReplaceLegacyLocalLink(publishedSnapshot, href, handleContentRoute, handleMediaUrl) == ReplaceStatus.InvalidEntityType)
31+
{
32+
handleInvalidLink();
33+
}
34+
}
35+
36+
private ReplaceStatus ReplaceLocalLink(IPublishedSnapshot publishedSnapshot, string href, string type, Action<IApiContentRoute> handleContentRoute, Action<string> handleMediaUrl)
2237
{
2338
Match match = LocalLinkRegex().Match(href);
2439
if (match.Success is false)
2540
{
26-
return;
41+
return ReplaceStatus.NoMatch;
2742
}
2843

29-
if (UdiParser.TryParse(match.Groups["udi"].Value, out Udi? udi) is false)
44+
if (Guid.TryParse(match.Groups["guid"].Value, out Guid guid) is false)
3045
{
31-
return;
46+
return ReplaceStatus.NoMatch;
3247
}
3348

34-
bool handled = false;
49+
var udi = new GuidUdi(type, guid);
50+
3551
switch (udi.EntityType)
3652
{
3753
case Constants.UdiEntityType.Document:
@@ -41,26 +57,65 @@ protected void ReplaceLocalLinks(IPublishedSnapshot publishedSnapshot, string hr
4157
: null;
4258
if (route != null)
4359
{
44-
handled = true;
4560
handleContentRoute(route);
61+
return ReplaceStatus.Success;
4662
}
4763

4864
break;
4965
case Constants.UdiEntityType.Media:
5066
IPublishedContent? media = publishedSnapshot.Media?.GetById(udi);
5167
if (media != null)
5268
{
53-
handled = true;
5469
handleMediaUrl(_apiMediaUrlProvider.GetUrl(media));
70+
return ReplaceStatus.Success;
5571
}
5672

5773
break;
5874
}
5975

60-
if(handled is false)
76+
return ReplaceStatus.InvalidEntityType;
77+
}
78+
79+
private ReplaceStatus ReplaceLegacyLocalLink(IPublishedSnapshot publishedSnapshot, string href, Action<IApiContentRoute> handleContentRoute, Action<string> handleMediaUrl)
80+
{
81+
Match match = LegacyLocalLinkRegex().Match(href);
82+
if (match.Success is false)
6183
{
62-
handleInvalidLink();
84+
return ReplaceStatus.NoMatch;
85+
}
86+
87+
if (UdiParser.TryParse(match.Groups["udi"].Value, out Udi? udi) is false)
88+
{
89+
return ReplaceStatus.NoMatch;
90+
}
91+
92+
93+
switch (udi.EntityType)
94+
{
95+
case Constants.UdiEntityType.Document:
96+
IPublishedContent? content = publishedSnapshot.Content?.GetById(udi);
97+
IApiContentRoute? route = content != null
98+
? _apiContentRouteBuilder.Build(content)
99+
: null;
100+
if (route != null)
101+
{
102+
handleContentRoute(route);
103+
return ReplaceStatus.Success;
104+
}
105+
106+
break;
107+
case Constants.UdiEntityType.Media:
108+
IPublishedContent? media = publishedSnapshot.Media?.GetById(udi);
109+
if (media != null)
110+
{
111+
handleMediaUrl(_apiMediaUrlProvider.GetUrl(media));
112+
return ReplaceStatus.Success;
113+
}
114+
115+
break;
63116
}
117+
118+
return ReplaceStatus.InvalidEntityType;
64119
}
65120

66121
protected void ReplaceLocalImages(IPublishedSnapshot publishedSnapshot, string udi, Action<string> handleMediaUrl)
@@ -80,5 +135,15 @@ protected void ReplaceLocalImages(IPublishedSnapshot publishedSnapshot, string u
80135
}
81136

82137
[GeneratedRegex("{localLink:(?<udi>umb:.+)}")]
138+
private static partial Regex LegacyLocalLinkRegex();
139+
140+
[GeneratedRegex("{localLink:(?<guid>.+)}")]
83141
private static partial Regex LocalLinkRegex();
142+
143+
private enum ReplaceStatus
144+
{
145+
NoMatch,
146+
Success,
147+
InvalidEntityType
148+
}
84149
}

tests/Umbraco.Tests.UnitTests/Umbraco.Core/Templates/HtmlLocalLinkParserTests.cs

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Umbraco.Cms.Core.Templates;
1414
using Umbraco.Cms.Tests.Common;
1515
using Umbraco.Cms.Tests.UnitTests.TestHelpers.Objects;
16+
using Umbraco.Extensions;
1617

1718
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Templates;
1819

@@ -21,6 +22,32 @@ public class HtmlLocalLinkParserTests
2122
{
2223
[Test]
2324
public void Returns_Udis_From_LocalLinks()
25+
{
26+
var input = @"<p>
27+
<div>
28+
<img src='/media/12312.jpg' data-udi='umb://media/D4B18427A1544721B09AC7692F35C264' />
29+
<a type=""document"" href=""/{localLink:eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f}"" title=""other page"">other page</a>
30+
</div>
31+
</p><p><img src='/media/234234.jpg' data-udi=""umb://media-type/B726D735E4C446D58F703F3FBCFC97A5"" />
32+
<a type=""media"" href=""/{localLink:7e21a725-b905-4c5f-86dc-8c41ec116e39}"" title=""media"">media</a>
33+
</p>";
34+
35+
var umbracoContextAccessor = new TestUmbracoContextAccessor();
36+
var parser = new HtmlLocalLinkParser(umbracoContextAccessor, Mock.Of<IPublishedUrlProvider>());
37+
38+
var result = parser.FindUdisFromLocalLinks(input).ToList();
39+
40+
Assert.Multiple(() =>
41+
{
42+
Assert.AreEqual(2, result.Count);
43+
Assert.Contains(UdiParser.Parse("umb://document/eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f"), result);
44+
Assert.Contains(UdiParser.Parse("umb://media/7e21a725-b905-4c5f-86dc-8c41ec116e39"), result);
45+
});
46+
}
47+
48+
// todo remove at some point and the implementation.
49+
[Test]
50+
public void Returns_Udis_From_Legacy_LocalLinks()
2451
{
2552
var input = @"<p>
2653
<div>
@@ -36,12 +63,59 @@ public void Returns_Udis_From_LocalLinks()
3663

3764
var result = parser.FindUdisFromLocalLinks(input).ToList();
3865

39-
Assert.AreEqual(2, result.Count);
40-
Assert.AreEqual(UdiParser.Parse("umb://document/C093961595094900AAF9170DDE6AD442"), result[0]);
41-
Assert.AreEqual(UdiParser.Parse("umb://document-type/2D692FCB070B4CDA92FB6883FDBFD6E2"), result[1]);
66+
Assert.Multiple(() =>
67+
{
68+
Assert.AreEqual(2, result.Count);
69+
Assert.Contains(UdiParser.Parse("umb://document/C093961595094900AAF9170DDE6AD442"), result);
70+
Assert.Contains(UdiParser.Parse("umb://document-type/2D692FCB070B4CDA92FB6883FDBFD6E2"), result);
71+
});
72+
}
73+
74+
// todo remove at some point and the implementation.
75+
[Test]
76+
public void Returns_Udis_From_Legacy_And_Current_LocalLinks()
77+
{
78+
var input = @"<p>
79+
<div>
80+
<img src='/media/12312.jpg' data-udi='umb://media/D4B18427A1544721B09AC7692F35C264' />
81+
<a href=""{locallink:umb://document/C093961595094900AAF9170DDE6AD442}"">hello</a>
82+
</div>
83+
</p><p><img src='/media/234234.jpg' data-udi=""umb://media-type/B726D735E4C446D58F703F3FBCFC97A5"" />
84+
<a href=""{locallink:umb://document-type/2D692FCB070B4CDA92FB6883FDBFD6E2}"">hello</a>
85+
</p>
86+
<p>
87+
<div>
88+
<img src='/media/12312.jpg' data-udi='umb://media/D4B18427A1544721B09AC7692F35C264' />
89+
<a type=""document"" href=""/{localLink:eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f}"" title=""other page"">other page</a>
90+
</div>
91+
</p><p><img src='/media/234234.jpg' data-udi=""umb://media-type/B726D735E4C446D58F703F3FBCFC97A5"" />
92+
<a type=""media"" href=""/{localLink:7e21a725-b905-4c5f-86dc-8c41ec116e39}"" title=""media"">media</a>
93+
</p>";
94+
95+
var umbracoContextAccessor = new TestUmbracoContextAccessor();
96+
var parser = new HtmlLocalLinkParser(umbracoContextAccessor, Mock.Of<IPublishedUrlProvider>());
97+
98+
var result = parser.FindUdisFromLocalLinks(input).ToList();
99+
100+
Assert.Multiple(() =>
101+
{
102+
Assert.AreEqual(4, result.Count);
103+
Assert.Contains(UdiParser.Parse("umb://document/eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f"), result);
104+
Assert.Contains(UdiParser.Parse("umb://media/7e21a725-b905-4c5f-86dc-8c41ec116e39"), result);
105+
Assert.Contains(UdiParser.Parse("umb://document/C093961595094900AAF9170DDE6AD442"), result);
106+
Assert.Contains(UdiParser.Parse("umb://document-type/2D692FCB070B4CDA92FB6883FDBFD6E2"), result);
107+
});
42108
}
43109

44110
[TestCase("", "")]
111+
// current
112+
[TestCase(
113+
"<a type=\"document\" href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" title=\"world\">world</a>",
114+
"<a type=\"document\" href=\"/my-test-url\" title=\"world\">world</a>")]
115+
[TestCase(
116+
"<a type=\"media\" href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" title=\"world\">world</a>",
117+
"<a type=\"media\" href=\"/media/1001/my-image.jpg\" title=\"world\">world</a>")]
118+
// legacy
45119
[TestCase(
46120
"hello href=\"{localLink:1234}\" world ",
47121
"hello href=\"/my-test-url\" world ")]

0 commit comments

Comments
 (0)