Skip to content

Commit 3d75e39

Browse files
Add GetCropUrl overloads accepting MediaWithCrops on UrlHelper
1 parent c1088a3 commit 3d75e39

File tree

1 file changed

+60
-17
lines changed

1 file changed

+60
-17
lines changed

src/Umbraco.Web/UrlHelperRenderExtensions.cs

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Web;
55
using System.Web.Mvc;
66
using Umbraco.Core;
7+
using Umbraco.Core.Models;
78
using Umbraco.Core.Models.PublishedContent;
89
using Umbraco.Core.PropertyEditors.ValueConverters;
910
using Umbraco.Web.Composing;
@@ -17,9 +18,10 @@ namespace Umbraco.Web
1718
/// </summary>
1819
public static class UrlHelperRenderExtensions
1920
{
20-
2121
private static readonly IHtmlString EmptyHtmlString = new HtmlString(string.Empty);
2222

23+
private static IHtmlString CreateHtmlString(string value, bool htmlEncode) => htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(value)) : new HtmlString(value);
24+
2325
#region GetCropUrl
2426

2527
/// <summary>
@@ -42,7 +44,17 @@ public static IHtmlString GetCropUrl(this UrlHelper urlHelper, IPublishedContent
4244
if (mediaItem == null) return EmptyHtmlString;
4345

4446
var url = mediaItem.GetCropUrl(cropAlias: cropAlias, useCropDimensions: true);
45-
return htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(url)) : new HtmlString(url);
47+
48+
return CreateHtmlString(url, htmlEncode);
49+
}
50+
51+
public static IHtmlString GetCropUrl(this UrlHelper urlHelper, MediaWithCrops mediaWithCrops, string cropAlias, bool htmlEncode = true)
52+
{
53+
if (mediaWithCrops == null) return EmptyHtmlString;
54+
55+
var url = mediaWithCrops.GetCropUrl(cropAlias: cropAlias, useCropDimensions: true);
56+
57+
return CreateHtmlString(url, htmlEncode);
4658
}
4759

4860
/// <summary>
@@ -70,7 +82,17 @@ public static IHtmlString GetCropUrl(this UrlHelper urlHelper, IPublishedContent
7082
if (mediaItem == null) return EmptyHtmlString;
7183

7284
var url = mediaItem.GetCropUrl(propertyAlias: propertyAlias, cropAlias: cropAlias, useCropDimensions: true);
73-
return htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(url)) : new HtmlString(url);
85+
86+
return CreateHtmlString(url, htmlEncode);
87+
}
88+
89+
public static IHtmlString GetCropUrl(this UrlHelper urlHelper, MediaWithCrops mediaWithCrops, string propertyAlias, string cropAlias, bool htmlEncode = true)
90+
{
91+
if (mediaWithCrops == null) return EmptyHtmlString;
92+
93+
var url = mediaWithCrops.GetCropUrl(propertyAlias: propertyAlias, cropAlias: cropAlias, useCropDimensions: true);
94+
95+
return CreateHtmlString(url, htmlEncode);
7496
}
7597

7698
/// <summary>
@@ -150,10 +172,33 @@ public static IHtmlString GetCropUrl(this UrlHelper urlHelper,
150172
{
151173
if (mediaItem == null) return EmptyHtmlString;
152174

153-
var url = mediaItem.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode,
154-
imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode,
155-
upScale);
156-
return htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(url)) : new HtmlString(url);
175+
var url = mediaItem.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale);
176+
177+
return CreateHtmlString(url, htmlEncode);
178+
}
179+
180+
public static IHtmlString GetCropUrl(this UrlHelper urlHelper,
181+
MediaWithCrops mediaWithCrops,
182+
int? width = null,
183+
int? height = null,
184+
string propertyAlias = Umbraco.Core.Constants.Conventions.Media.File,
185+
string cropAlias = null,
186+
int? quality = null,
187+
ImageCropMode? imageCropMode = null,
188+
ImageCropAnchor? imageCropAnchor = null,
189+
bool preferFocalPoint = false,
190+
bool useCropDimensions = false,
191+
bool cacheBuster = true,
192+
string furtherOptions = null,
193+
ImageCropRatioMode? ratioMode = null,
194+
bool upScale = true,
195+
bool htmlEncode = true)
196+
{
197+
if (mediaWithCrops == null) return EmptyHtmlString;
198+
199+
var url = mediaWithCrops.GetCropUrl(width, height, propertyAlias, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions, ratioMode, upScale);
200+
201+
return CreateHtmlString(url, htmlEncode);
157202
}
158203

159204
/// <summary>
@@ -231,18 +276,18 @@ public static IHtmlString GetCropUrl(this UrlHelper urlHelper,
231276
bool upScale = true,
232277
bool htmlEncode = true)
233278
{
234-
var url = imageUrl.GetCropUrl(width, height, imageCropperValue, cropAlias, quality, imageCropMode,
235-
imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode,
236-
upScale);
237-
return htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(url)) : new HtmlString(url);
279+
var url = imageUrl.GetCropUrl(width, height, imageCropperValue, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale);
280+
281+
return CreateHtmlString(url, htmlEncode);
238282
}
239283

240284
public static IHtmlString GetCropUrl(this UrlHelper urlHelper, ImageCropperValue imageCropperValue, string cropAlias, bool htmlEncode = true)
241285
{
242286
if (imageCropperValue == null) return EmptyHtmlString;
243287

244288
var url = imageCropperValue.Src.GetCropUrl(imageCropperValue, cropAlias: cropAlias, useCropDimensions: true);
245-
return htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(url)) : new HtmlString(url);
289+
290+
return CreateHtmlString(url, htmlEncode);
246291
}
247292

248293
public static IHtmlString GetCropUrl(this UrlHelper urlHelper,
@@ -263,11 +308,9 @@ public static IHtmlString GetCropUrl(this UrlHelper urlHelper,
263308
{
264309
if (imageCropperValue == null) return EmptyHtmlString;
265310

266-
var imageUrl = imageCropperValue.Src;
267-
var url = imageUrl.GetCropUrl(imageCropperValue, width, height, cropAlias, quality, imageCropMode,
268-
imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode,
269-
upScale);
270-
return htmlEncode ? new HtmlString(HttpUtility.HtmlEncode(url)) : new HtmlString(url);
311+
var url = imageCropperValue.Src.GetCropUrl(imageCropperValue, width, height, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions, ratioMode, upScale);
312+
313+
return CreateHtmlString(url, htmlEncode);
271314
}
272315

273316
#endregion

0 commit comments

Comments
 (0)