Files
Umbraco-CMS/src/Umbraco.Web/Templates/HtmlImageSourceParser.cs

93 lines
3.8 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2019-10-21 23:53:14 +11:00
using System.Text.RegularExpressions;
using Umbraco.Core;
namespace Umbraco.Web.Templates
{
2019-10-22 11:09:21 +11:00
2019-10-23 14:55:18 +11:00
public sealed class HtmlImageSourceParser
2019-10-21 23:53:14 +11:00
{
public HtmlImageSourceParser(IUmbracoContextAccessor umbracoContextAccessor)
2019-10-21 23:53:14 +11:00
{
_umbracoContextAccessor = umbracoContextAccessor;
}
private static readonly Regex ResolveImgPattern = new Regex(@"(<img[^>]*src="")([^""\?]*)([^""]*""[^>]*data-udi="")([^""]*)(""[^>]*>)",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
2019-10-21 23:53:14 +11:00
private static readonly Regex DataUdiAttributeRegex = new Regex(@"data-udi=\\?(?:""|')(?<udi>umb://[A-z0-9\-]+/[A-z0-9]+)\\?(?:""|')",
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
/// <summary>
/// Parses out UDIs from an html string based on 'data-udi' html attributes
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public IEnumerable<Udi> FindUdisFromDataAttributes(string text)
{
var matches = DataUdiAttributeRegex.Matches(text);
if (matches.Count == 0)
yield break;
foreach (Match match in matches)
{
if (match.Groups.Count == 2 && Udi.TryParse(match.Groups[1].Value, out var udi))
yield return udi;
}
}
2019-10-21 23:53:14 +11:00
/// <summary>
/// Parses the string looking for Umbraco image tags and updates them to their up-to-date image sources.
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
/// <remarks>Umbraco image tags are identified by their data-udi attributes</remarks>
public string EnsureImageSources(string text)
{
// don't attempt to proceed without a context
if (_umbracoContextAccessor?.UmbracoContext?.UrlProvider == null)
2019-10-21 23:53:14 +11:00
{
return text;
}
var urlProvider = _umbracoContextAccessor.UmbracoContext.UrlProvider;
2019-10-21 23:53:14 +11:00
return ResolveImgPattern.Replace(text, match =>
{
// match groups:
// - 1 = from the beginning of the image tag until src attribute value begins
// - 2 = the src attribute value excluding the querystring (if present)
// - 3 = anything after group 2 and before the data-udi attribute value begins
// - 4 = the data-udi attribute value
// - 5 = anything after group 4 until the image tag is closed
var udi = match.Groups[4].Value;
if (udi.IsNullOrWhiteSpace() || GuidUdi.TryParse(udi, out var guidUdi) == false)
{
return match.Value;
}
var mediaUrl = urlProvider.GetMediaUrl(guidUdi.Guid);
if (mediaUrl == null)
2019-10-21 23:53:14 +11:00
{
// image does not exist - we could choose to remove the image entirely here (return empty string),
// but that would leave the editors completely in the dark as to why the image doesn't show
return match.Value;
}
return $"{match.Groups[1].Value}{mediaUrl}{match.Groups[3].Value}{udi}{match.Groups[5].Value}";
2019-10-21 23:53:14 +11:00
});
}
/// <summary>
/// Removes media urls from &lt;img&gt; tags where a data-udi attribute is present
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
2019-10-23 14:55:18 +11:00
public string RemoveImageSources(string text)
2019-10-21 23:53:14 +11:00
// see comment in ResolveMediaFromTextString for group reference
=> ResolveImgPattern.Replace(text, "$1$3$4$5");
2019-10-21 23:53:14 +11:00
}
}