Merge branch 'dev-v7' into dev-v8

# Conflicts:
#	src/Umbraco.Web/PropertyEditors/RichTextPropertyEditor.cs
#	src/Umbraco.Web/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs
#	src/Umbraco.Web/Templates/TemplateUtilities.cs
This commit is contained in:
Sebastiaan Janssen
2019-03-20 23:03:18 +01:00
3 changed files with 49 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Examine;
using Umbraco.Web.Macros;
using Umbraco.Web.Templates;
namespace Umbraco.Web.PropertyEditors
{
@@ -73,7 +74,8 @@ namespace Umbraco.Web.PropertyEditors
if (val == null)
return null;
var parsed = MacroTagParser.FormatRichTextPersistedDataForEditor(val.ToString(), new Dictionary<string, string>());
var propertyValueWithMediaResolved = TemplateUtilities.ResolveMediaFromTextString(val.ToString());
var parsed = MacroTagParser.FormatRichTextPersistedDataForEditor(propertyValueWithMediaResolved, new Dictionary<string, string>());
return parsed;
}

View File

@@ -72,9 +72,10 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters
var sourceString = source.ToString();
// ensures string is parsed for {localLink} and urls are resolved correctly
// ensures string is parsed for {localLink} and urls and media are resolved correctly
sourceString = TemplateUtilities.ParseInternalLinks(sourceString, preview, Current.UmbracoContext);
sourceString = TemplateUtilities.ResolveUrlsFromTextString(sourceString);
sourceString = TemplateUtilities.ResolveMediaFromTextString(sourceString);
// ensure string is parsed for macros and macros are executed correctly
sourceString = RenderRteMacros(sourceString, preview);

View File

@@ -75,6 +75,9 @@ namespace Umbraco.Web.Templates
private static readonly Regex ResolveUrlPattern = new Regex("(=[\"\']?)(\\W?\\~(?:.(?![\"\']?\\s+(?:\\S+)=|[>\"\']))+.)[\"\']?",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
private static readonly Regex ResolveImgPattern = new Regex(@"(<img[^>]*src="")([^""\?]*)([^""]*""[^>]*data-udi="")([^""]*)(""[^>]*>)",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
/// <summary>
/// The RegEx matches any HTML attribute values that start with a tilde (~), those that match are passed to ResolveUrl to replace the tilde with the application path.
/// </summary>
@@ -118,5 +121,46 @@ namespace Umbraco.Web.Templates
{
return text.CleanForXss(ignoreFromClean);
}
/// <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 static string ResolveMediaFromTextString(string text)
{
// don't attempt to proceed without a context
if (Current.UmbracoContext == null || Current.UmbracoContext.MediaCache == null)
{
return text;
}
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 src = match.Groups[2].Value;
var udi = match.Groups[4].Value;
if(src.IsNullOrWhiteSpace() || udi.IsNullOrWhiteSpace() || GuidUdi.TryParse(udi, out var guidUdi) == false)
{
return match.Value;
}
var media = Current.UmbracoContext.MediaCache.GetById(guidUdi.Guid);
if(media == null)
{
// 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;
}
var url = media.Url;
return $"{match.Groups[1].Value}{url}{match.Groups[3].Value}{udi}{match.Groups[5].Value}";
});
}
}
}