From fbd95f1c8edfc3afc45f3e758b7894a393a40b86 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Thu, 6 Feb 2020 15:10:05 +0100 Subject: [PATCH] Fixes problem with #7530 where UmbracoContext is empty when HtmlImageSourceParser gets initialized --- .../Templates/HtmlImageSourceParser.cs | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/Umbraco.Web/Templates/HtmlImageSourceParser.cs b/src/Umbraco.Web/Templates/HtmlImageSourceParser.cs index dcc5318b89..9a3d86c9da 100644 --- a/src/Umbraco.Web/Templates/HtmlImageSourceParser.cs +++ b/src/Umbraco.Web/Templates/HtmlImageSourceParser.cs @@ -13,14 +13,10 @@ namespace Umbraco.Web.Templates this._getMediaUrl = getMediaUrl; } + private readonly IUmbracoContextAccessor _umbracoContextAccessor; public HtmlImageSourceParser(IUmbracoContextAccessor umbracoContextAccessor) { - if (umbracoContextAccessor?.UmbracoContext?.UrlProvider == null) - { - return; - } - - _getMediaUrl = (guid) => umbracoContextAccessor.UmbracoContext.UrlProvider.GetMediaUrl(guid); + _umbracoContextAccessor = umbracoContextAccessor; } private static readonly Regex ResolveImgPattern = new Regex(@"(]*src="")([^""\?]*)((?:\?[^""]*)?""[^>]*data-udi="")([^""]*)(""[^>]*>)", @@ -29,7 +25,7 @@ namespace Umbraco.Web.Templates private static readonly Regex DataUdiAttributeRegex = new Regex(@"data-udi=\\?(?:""|')(?umb://[A-z0-9\-]+/[A-z0-9]+)\\?(?:""|')", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); - private readonly Func _getMediaUrl; + private Func _getMediaUrl; /// /// Parses out media UDIs from an html string based on 'data-udi' html attributes @@ -57,12 +53,11 @@ namespace Umbraco.Web.Templates /// Umbraco image tags are identified by their data-udi attributes public string EnsureImageSources(string text) { - // no point in doing any processing if we don't have - // a function to retrieve Urls - if (_getMediaUrl == null) - { - return text; - } + if (_umbracoContextAccessor.UmbracoContext == null) + throw new InvalidOperationException("Could not parse image sources, there is no current UmbracoContext"); + + if(_getMediaUrl == null) + _getMediaUrl = (guid) => _umbracoContextAccessor.UmbracoContext.UrlProvider.GetMediaUrl(guid); return ResolveImgPattern.Replace(text, match => {