-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
#17288) * fix: anchors and query strings do not work Since the change from UDIs to localLinks in href, the pattern matched a little too much in the href section completely ignoring any "extras" such as querystrings and anchors after the locallink, which meant that the locallink did not get replaced at all if they were present. This is fixed by limiting the regexp a bit. * fix: legacy links do not follow the same regexp as new links Because we are no longer matching the whole `href` attribute but only some of its contents, we need to fix up the old pattern. It has been extended with matching groups that follow the same pattern as the new links. * feat: allow a-tags to be multiline example: ```html <a type="document" href="/{localLink:<GUID>}"> Test </a> ``` * fix: split regex into two parts: first a tokenizer for a-tags and then a type-finder * fix: ensure only "document" and "media" are matching to speed up the pattern * feat: allow a-tags to be multiline (cherry picked from commit 35e8f2e)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,15 @@ public sealed class HtmlLocalLinkParser | |
// <a type="media" href="/{localLink:7e21a725-b905-4c5f-86dc-8c41ec116e39}" title="media">media</a> | ||
// <a type="document" href="/{localLink:eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f}" title="other page">other page</a> | ||
internal static readonly Regex LocalLinkTagPattern = new( | ||
@"<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-]+})[""'])))[^>]*>", | ||
@"<a.+?href=['""](?<locallink>\/?{localLink:(?<guid>[a-fA-F0-9-]+)})[^>]*?>", | ||
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline); | ||
|
||
internal static readonly Regex TypePattern = new( | ||
"""type=['"](?<type>(?:media|document))['"]""", | ||
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); | ||
|
||
internal static readonly Regex LocalLinkPattern = new( | ||
@"href=""[/]?(?:\{|\%7B)localLink:([a-zA-Z0-9-://]+)(?:\}|\%7D)", | ||
@"href=['""](?<locallink>\/?(?:\{|\%7B)localLink:(?<guid>[a-zA-Z0-9-://]+)(?:\}|\%7D))", | ||
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); | ||
|
||
private readonly IPublishedUrlProvider _publishedUrlProvider; | ||
|
@@ -84,32 +88,28 @@ public string EnsureInternalLinks(string text) | |
{ | ||
if (tagData.Udi is not null) | ||
{ | ||
var newLink = "#"; | ||
if (tagData.Udi?.EntityType == Constants.UdiEntityType.Document) | ||
{ | ||
newLink = _publishedUrlProvider.GetUrl(tagData.Udi.Guid); | ||
} | ||
else if (tagData.Udi?.EntityType == Constants.UdiEntityType.Media) | ||
var newLink = tagData.Udi?.EntityType switch | ||
{ | ||
newLink = _publishedUrlProvider.GetMediaUrl(tagData.Udi.Guid); | ||
} | ||
|
||
Constants.UdiEntityType.Document => _publishedUrlProvider.GetUrl(tagData.Udi.Guid), | ||
Constants.UdiEntityType.Media => _publishedUrlProvider.GetMediaUrl(tagData.Udi.Guid), | ||
_ => "" | ||
Check failure on line 95 in src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs GitHub Actions / CodeQL-Build
Check failure on line 95 in src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs GitHub Actions / CodeQL-Build
Check failure on line 95 in src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs Azure Pipelines / Umbraco CMS 9+ (Build Build Umbraco CMS)src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs#L95
Check failure on line 95 in src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs Azure Pipelines / Umbraco CMS 9+src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs#L95
Check failure on line 95 in src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs Azure Pipelines / Nightly E2E Test Umbraco CMS 14 (Build Build Umbraco CMS)src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs#L95
Check failure on line 95 in src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs Azure Pipelines / Nightly E2E Test Umbraco CMS 14src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs#L95
|
||
}; | ||
|
||
text = StripTypeAttributeFromTag(text, tagData.Udi!.EntityType); | ||
text = text.Replace(tagData.TagHref, "href=\"" + newLink); | ||
text = text.Replace(tagData.TagHref, newLink); | ||
} | ||
else if (tagData.IntId.HasValue) | ||
{ | ||
var newLink = _publishedUrlProvider.GetUrl(tagData.IntId.Value); | ||
text = text.Replace(tagData.TagHref, "href=\"" + newLink); | ||
text = text.Replace(tagData.TagHref, newLink); | ||
} | ||
} | ||
|
||
return text; | ||
} | ||
|
||
// under normal circumstances, the type attribute is preceded by a space | ||
// to cover the rare occasion where it isn't, we first replace with a a space and then without. | ||
// to cover the rare occasion where it isn't, we first replace with a space and then without. | ||
private string StripTypeAttributeFromTag(string tag, string type) => | ||
tag.Replace($" type=\"{type}\"", string.Empty) | ||
.Replace($"type=\"{type}\"", string.Empty); | ||
|
@@ -119,21 +119,22 @@ private IEnumerable<LocalLinkTag> FindLocalLinkIds(string text) | |
MatchCollection localLinkTagMatches = LocalLinkTagPattern.Matches(text); | ||
foreach (Match linkTag in localLinkTagMatches) | ||
{ | ||
if (linkTag.Groups.Count < 1) | ||
if (Guid.TryParse(linkTag.Groups["guid"].Value, out Guid guid) is false) | ||
{ | ||
continue; | ||
} | ||
|
||
if (Guid.TryParse(linkTag.Groups["guid"].Value, out Guid guid) is false) | ||
// Find the type attribute | ||
Match typeMatch = TypePattern.Match(linkTag.Value); | ||
if (typeMatch.Success is false) | ||
{ | ||
continue; | ||
} | ||
|
||
yield return new LocalLinkTag( | ||
null, | ||
new GuidUdi(linkTag.Groups["type"].Value, guid), | ||
linkTag.Groups["locallink"].Value, | ||
linkTag.Value); | ||
new GuidUdi(typeMatch.Groups["type"].Value, guid), | ||
linkTag.Groups["locallink"].Value); | ||
} | ||
|
||
// also return legacy results for values that have not been migrated | ||
|
@@ -150,25 +151,26 @@ private IEnumerable<LocalLinkTag> FindLegacyLocalLinkIds(string text) | |
MatchCollection tags = LocalLinkPattern.Matches(text); | ||
foreach (Match tag in tags) | ||
{ | ||
if (tag.Groups.Count > 0) | ||
if (tag.Groups.Count <= 0) | ||
{ | ||
var id = tag.Groups[1].Value; // .Remove(tag.Groups[1].Value.Length - 1, 1); | ||
continue; | ||
} | ||
|
||
// The id could be an int or a UDI | ||
if (UdiParser.TryParse(id, out Udi? udi)) | ||
{ | ||
var guidUdi = udi as GuidUdi; | ||
if (guidUdi is not null) | ||
{ | ||
yield return new LocalLinkTag(null, guidUdi, tag.Value, null); | ||
} | ||
} | ||
var id = tag.Groups["guid"].Value; | ||
|
||
if (int.TryParse(id, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intId)) | ||
// The id could be an int or a UDI | ||
if (UdiParser.TryParse(id, out Udi? udi)) | ||
{ | ||
if (udi is GuidUdi guidUdi) | ||
{ | ||
yield return new LocalLinkTag (intId, null, tag.Value, null); | ||
yield return new LocalLinkTag(null, guidUdi, tag.Groups["locallink"].Value); | ||
} | ||
} | ||
|
||
if (int.TryParse(id, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intId)) | ||
{ | ||
yield return new LocalLinkTag (intId, null, tag.Groups["locallink"].Value); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -181,20 +183,10 @@ public LocalLinkTag(int? intId, GuidUdi? udi, string tagHref) | |
TagHref = tagHref; | ||
} | ||
|
||
public LocalLinkTag(int? intId, GuidUdi? udi, string tagHref, string? fullTag) | ||
{ | ||
IntId = intId; | ||
Udi = udi; | ||
TagHref = tagHref; | ||
FullTag = fullTag; | ||
} | ||
|
||
public int? IntId { get; } | ||
|
||
public GuidUdi? Udi { get; } | ||
|
||
public string TagHref { get; } | ||
|
||
public string? FullTag { get; } | ||
} | ||
} |