From 0f8f8c20c3efca9aec83c6e9842cdd924e04cc2e Mon Sep 17 00:00:00 2001 From: AndyBoot <82056925+AndyBoot@users.noreply.github.com> Date: Wed, 12 Oct 2022 21:39:52 +0100 Subject: [PATCH 1/5] UPDATE: - Caching + Enforcing Viewbox --- .../Composing/InlineSvgTagHelperComposer.cs | 20 +++ .../OurUmbracoTagHelpersConfiguration.cs | 14 ++ ...rUmbracoTagHelpersConfigurationComposer.cs | 16 ++ Our.Umbraco.TagHelpers/InlineSvgTagHelper.cs | 138 +++++++++++++++--- .../InlineSvgTagHelperNotifications.cs | 41 ++++++ README.md | 33 +++++ 6 files changed, 243 insertions(+), 19 deletions(-) create mode 100644 Our.Umbraco.TagHelpers/Composing/InlineSvgTagHelperComposer.cs create mode 100644 Our.Umbraco.TagHelpers/Configuration/OurUmbracoTagHelpersConfiguration.cs create mode 100644 Our.Umbraco.TagHelpers/Configuration/OurUmbracoTagHelpersConfigurationComposer.cs create mode 100644 Our.Umbraco.TagHelpers/Notifications/InlineSvgTagHelperNotifications.cs diff --git a/Our.Umbraco.TagHelpers/Composing/InlineSvgTagHelperComposer.cs b/Our.Umbraco.TagHelpers/Composing/InlineSvgTagHelperComposer.cs new file mode 100644 index 0000000..63ec324 --- /dev/null +++ b/Our.Umbraco.TagHelpers/Composing/InlineSvgTagHelperComposer.cs @@ -0,0 +1,20 @@ +using Our.Umbraco.TagHelpers.Notifications; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Umbraco.Cms.Core.Composing; +using Umbraco.Cms.Core.DependencyInjection; +using Umbraco.Cms.Core.Notifications; + +namespace Our.Umbraco.TagHelpers.Composing +{ + public class InlineSvgTagHelperComposer : IComposer + { + public void Compose(IUmbracoBuilder builder) + { + builder.AddNotificationHandler(); + } + } +} diff --git a/Our.Umbraco.TagHelpers/Configuration/OurUmbracoTagHelpersConfiguration.cs b/Our.Umbraco.TagHelpers/Configuration/OurUmbracoTagHelpersConfiguration.cs new file mode 100644 index 0000000..0796cce --- /dev/null +++ b/Our.Umbraco.TagHelpers/Configuration/OurUmbracoTagHelpersConfiguration.cs @@ -0,0 +1,14 @@ +namespace Our.Umbraco.TagHelpers.Configuration +{ + public class OurUmbracoTagHelpersConfiguration + { + public InlineSvgTagHelperConfiguration OurSVG { get; set; } = new InlineSvgTagHelperConfiguration(); + } + + public class InlineSvgTagHelperConfiguration + { + public bool EnsureViewBox { get; set; } = false; + public bool Cache { get; set; } = false; + public int CacheMinutes { get; set; } = 180; + } +} diff --git a/Our.Umbraco.TagHelpers/Configuration/OurUmbracoTagHelpersConfigurationComposer.cs b/Our.Umbraco.TagHelpers/Configuration/OurUmbracoTagHelpersConfigurationComposer.cs new file mode 100644 index 0000000..bf3c1bf --- /dev/null +++ b/Our.Umbraco.TagHelpers/Configuration/OurUmbracoTagHelpersConfigurationComposer.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; +using Our.Umbraco.TagHelpers.Services; +using Umbraco.Cms.Core.Composing; +using Umbraco.Cms.Core.DependencyInjection; + +namespace Our.Umbraco.TagHelpers.Configuration +{ + public class OurUmbracoTagHelpersConfigurationComposer : IComposer + { + public void Compose(IUmbracoBuilder builder) + { + builder.Services.AddOptions() + .Bind(builder.Config.GetSection("Our.Umbraco.TagHelpers")); + } + } +} diff --git a/Our.Umbraco.TagHelpers/InlineSvgTagHelper.cs b/Our.Umbraco.TagHelpers/InlineSvgTagHelper.cs index d8a8622..d1dd8e1 100644 --- a/Our.Umbraco.TagHelpers/InlineSvgTagHelper.cs +++ b/Our.Umbraco.TagHelpers/InlineSvgTagHelper.cs @@ -1,8 +1,13 @@ -using Microsoft.AspNetCore.Hosting; +using HtmlAgilityPack; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Razor.TagHelpers; +using Microsoft.Extensions.Options; +using Our.Umbraco.TagHelpers.Configuration; +using Our.Umbraco.TagHelpers.Utils; using System; using System.IO; using System.Text.RegularExpressions; +using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.IO; using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.Routing; @@ -20,12 +25,16 @@ public class InlineSvgTagHelper : TagHelper private MediaFileManager _mediaFileManager; private IWebHostEnvironment _webHostEnvironment; private IPublishedUrlProvider _urlProvider; + private OurUmbracoTagHelpersConfiguration _globalSettings; + private AppCaches _appCaches; - public InlineSvgTagHelper(MediaFileManager mediaFileManager, IWebHostEnvironment webHostEnvironment, IPublishedUrlProvider urlProvider) + public InlineSvgTagHelper(MediaFileManager mediaFileManager, IWebHostEnvironment webHostEnvironment, IPublishedUrlProvider urlProvider, IOptions globalSettings, AppCaches appCaches) { _mediaFileManager = mediaFileManager; _webHostEnvironment = webHostEnvironment; _urlProvider = urlProvider; + _globalSettings = globalSettings.Value; + _appCaches = appCaches; } /// @@ -42,6 +51,40 @@ public InlineSvgTagHelper(MediaFileManager mediaFileManager, IWebHostEnvironment [HtmlAttributeName("media-item")] public IPublishedContent? MediaItem { get; set; } + /// + /// A classic CSS class property to apply/append a CSS class or classes. + /// + [HtmlAttributeName("class")] + public string? CssClass { get; set; } + + /// + /// A boolean to ensure a viewbox is present within the SVG tag to ensure the vector is always responsive. + /// NOTE: Use the appsettings configuration to apply this globally (e.g. "Our.Umbraco.TagHelpers": { "InlineSvgTagHelper": { "EnsureViewBox": true } } ). + /// + [HtmlAttributeName("ensure-viewbox")] + public bool EnsureViewBox { get; set; } + + /// + /// A boolean to cache the SVG contents rather than performing the operation on each page load. + /// NOTE: Use the appsettings configuration to apply this globally (e.g. "Our.Umbraco.TagHelpers": { "InlineSvgTagHelper": { "Cache": true } } ). + /// + [HtmlAttributeName("cache")] + public bool Cache { get; set; } + + /// + /// An integer to set the cache minutes. Default: 180 minutes. + /// NOTE: Use the appsettings configuration to apply this globally (e.g. "Our.Umbraco.TagHelpers": { "InlineSvgTagHelper": { "CacheMinutes": 180 } } ). + /// + [HtmlAttributeName("cache-minutes")] + public int CacheMinutes { get; set; } + + /// + /// A boolean to ignore the appsettings. + /// NOTE: Applies to 'ensure-viewbox' & 'cache' only + /// + [HtmlAttributeName("ignore-appsettings")] + public bool IgnoreAppSettings { get; set; } + public override void Process(TagHelperContext context, TagHelperOutput output) { // Can only use media-item OR src @@ -55,26 +98,66 @@ public override void Process(TagHelperContext context, TagHelperOutput output) return; } + string? cleanedFileContents = null; + + if(Cache || (_globalSettings.OurSVG.Cache && !IgnoreAppSettings)) + { + var cacheName = string.Empty; + var cacheMins = CacheMinutes > 0 ? CacheMinutes : _globalSettings.OurSVG.CacheMinutes; + + if (MediaItem is not null) + { + cacheName = string.Concat("MediaItem-SvgContents (", MediaItem.Key.ToString(), ")"); + } + else if (string.IsNullOrWhiteSpace(FileSource) == false) + { + cacheName = string.Concat("File-SvgContents (", FileSource, ")"); + } + + cleanedFileContents = _appCaches.RuntimeCache.GetCacheItem(cacheName, () => + { + return GetFileContents(); + }, TimeSpan.FromMinutes(cacheMins)); + } + else + { + cleanedFileContents = GetFileContents(); + } + + if (string.IsNullOrEmpty(cleanedFileContents)) + { + output.SuppressOutput(); + return; + } + + // Remove the src attribute or media-item from the + output.Attributes.RemoveAll("src"); + output.Attributes.RemoveAll("media-item"); + + output.TagName = null; // Remove + output.Content.SetHtmlContent(cleanedFileContents); + } + + private string? GetFileContents() + { // SVG fileContents to render to DOM var fileContents = string.Empty; - if(MediaItem is not null) + if (MediaItem is not null) { // Check Umbraco Media Item that is picked/used // has a file that uses a .svg file extension var mediaItemPath = MediaItem.Url(_urlProvider); if (mediaItemPath?.EndsWith(".svg", StringComparison.InvariantCultureIgnoreCase) != true) { - output.SuppressOutput(); - return; + return null; } // Ensure the file actually exists on disk, Azure blob provider or ... // Anywhere else defined by IFileSystem to fetch & store files if (_mediaFileManager.FileSystem.FileExists(mediaItemPath) == false) { - output.SuppressOutput(); - return; + return null; } // Read its contents (get its stream) @@ -82,13 +165,12 @@ public override void Process(TagHelperContext context, TagHelperOutput output) using var reader = new StreamReader(fileStream); fileContents = reader.ReadToEnd(); } - else if(string.IsNullOrWhiteSpace(FileSource) == false) + else if (string.IsNullOrWhiteSpace(FileSource) == false) { // Check string src filepath ends with .svg if (FileSource.EndsWith(".svg", StringComparison.InvariantCultureIgnoreCase) == false) { - output.SuppressOutput(); - return; + return null; } // Get file from wwwRoot using a path such as @@ -98,10 +180,9 @@ public override void Process(TagHelperContext context, TagHelperOutput output) var file = webRoot.GetFileInfo(FileSource); // Ensure file exists in wwwroot path - if(file.Exists == false) + if (file.Exists == false) { - output.SuppressOutput(); - return; + return null; } using var reader = new StreamReader(file.CreateReadStream()); @@ -120,12 +201,31 @@ public override void Process(TagHelperContext context, TagHelperOutput output) @"syntax:error:", RegexOptions.IgnoreCase | RegexOptions.Singleline); - // Remove the src attribute or media-item from the - output.Attributes.RemoveAll("src"); - output.Attributes.RemoveAll("media-item"); + if ((EnsureViewBox || (_globalSettings.OurSVG.EnsureViewBox && !IgnoreAppSettings)) || !string.IsNullOrEmpty(CssClass)) + { + HtmlDocument doc = new HtmlDocument(); + doc.LoadHtml(cleanedFileContents); + var svgs = doc.DocumentNode.SelectNodes("//svg"); + foreach (var svgNode in svgs) + { + if (!string.IsNullOrEmpty(CssClass)) + { + svgNode.AddClass(CssClass); + } + if ((EnsureViewBox || (_globalSettings.OurSVG.EnsureViewBox && !IgnoreAppSettings)) && svgNode.Attributes.Contains("width") && svgNode.Attributes.Contains("height") && !svgNode.Attributes.Contains("viewbox")) + { + var width = StringUtils.GetDecimal(svgNode.GetAttributeValue("width", "0")); + var height = StringUtils.GetDecimal(svgNode.GetAttributeValue("height", "0")); + svgNode.SetAttributeValue("viewbox", $"0 0 {width} {height}"); + + svgNode.Attributes.Remove("width"); + svgNode.Attributes.Remove("height"); + } + } + cleanedFileContents = doc.DocumentNode.OuterHtml; + } - output.TagName = null; // Remove - output.Content.SetHtmlContent(cleanedFileContents); - } + return cleanedFileContents; + } } } diff --git a/Our.Umbraco.TagHelpers/Notifications/InlineSvgTagHelperNotifications.cs b/Our.Umbraco.TagHelpers/Notifications/InlineSvgTagHelperNotifications.cs new file mode 100644 index 0000000..2fc6ed0 --- /dev/null +++ b/Our.Umbraco.TagHelpers/Notifications/InlineSvgTagHelperNotifications.cs @@ -0,0 +1,41 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Umbraco.Cms.Core.Cache; +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Notifications; +using Umbraco.Extensions; + +namespace Our.Umbraco.TagHelpers.Notifications +{ + public class InlineSvgTagHelperNotifications : INotificationHandler + { + private readonly ILogger _logger; + private readonly AppCaches _appCaches; + + public InlineSvgTagHelperNotifications(ILogger logger, AppCaches appCaches) + { + _logger = logger; + _appCaches = appCaches; + } + + public void Handle(MediaSavedNotification notification) + { + foreach (var mediaItem in notification.SavedEntities) + { + if (mediaItem.ContentType.Alias.Equals("umbracoMediaVectorGraphics", StringComparison.InvariantCultureIgnoreCase)) + { + var cacheKey = string.Concat("MediaItem-SvgContents (", mediaItem.Key.ToString(), ")"); + if (_appCaches.RuntimeCache.SearchByKey(cacheKey).Any()) + { + _appCaches.RuntimeCache.ClearByKey(cacheKey); + _logger.LogDebug($"Removed {mediaItem.Name} from RuntimeCache"); + } + } + } + } + } +} diff --git a/README.md b/README.md index dfba851..37848ef 100644 --- a/README.md +++ b/README.md @@ -182,11 +182,44 @@ If you do not specify a template and use `` it will use the This tag helper element `` will read the file contents of an SVG file and output it as an inline SVG in the DOM. It can be used in one of two ways, either by specifying the `src` attribute to a physcial static file served from wwwRoot or by specifying the `media-item` attribute to use a picked IPublishedContent Media Item. +### Basic usage: ```cshtml ``` +### Advanced usage: *(as of version 1.x.x) + +Additional properties have been added to cache the output and also to ensure the SVG contains a viewbox property instead of the width & height properties to aid in making the vector image responsive within a parent HTML element. +```cshtml + + +``` + +- `class` - Allows for a CSS class upon the SVG element. This is a `string` value. +- `ensure-viewbox` - Enables the feature to "fix" the output SVG which always ensures the SVG utilises a viewbox rather than width & height. This is a `boolean` value. +- `cache` - Enables the feature to cache the output at runtime level. This is a `boolean` value. +- `cache-minutes` - Defines the amount of time (in minutes) to cache the output. To be used in conjunction with the `cache` property. This is an `integer` value. +- `ignore-appsettings` - When enabled, the all settings appropiate to this tag helper which are defined within `appsettings.json` are completely ignored. For example, if global caching is enabled we can simply disable caching of individual elements (unless the `cache` property is `true`). This is a `boolean` value. + +### Global settings via appsettings.json + +Applying any of the below configurations within your `appsettings.json` file will apply global settings to all elements using this tag helper. See the `ignore-appsettings` to override these global settings at element level. The values shown below are the hard-coded default values. + + { + "Our.Umbraco.TagHelpers": { + "OurSVG": { + "EnsureViewBox": false, + "Cache": false, + "CacheMinutes": 180 + } + } + } + +> **Note:** SVG caches are cleared on application restart, or by resaving the media in the media library. + + + ## `` This tag helper element `` uses the same fallback mode logic that is only available on the `Value()` method of the `IPublishedContent` interface that uses a string for the property name to lookup. In addition if the fallback value from a language or ancestors is not available we are still able to fallback to the content inside the tag. From 1222c659bdb9e74a1563368f5ef90afef888ae87 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Fri, 14 Oct 2022 11:12:50 +0100 Subject: [PATCH 2/5] Update Our.Umbraco.TagHelpers/Notifications/InlineSvgTagHelperNotifications.cs --- .../Notifications/InlineSvgTagHelperNotifications.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Our.Umbraco.TagHelpers/Notifications/InlineSvgTagHelperNotifications.cs b/Our.Umbraco.TagHelpers/Notifications/InlineSvgTagHelperNotifications.cs index 2fc6ed0..93a27c0 100644 --- a/Our.Umbraco.TagHelpers/Notifications/InlineSvgTagHelperNotifications.cs +++ b/Our.Umbraco.TagHelpers/Notifications/InlineSvgTagHelperNotifications.cs @@ -32,7 +32,7 @@ public void Handle(MediaSavedNotification notification) if (_appCaches.RuntimeCache.SearchByKey(cacheKey).Any()) { _appCaches.RuntimeCache.ClearByKey(cacheKey); - _logger.LogDebug($"Removed {mediaItem.Name} from RuntimeCache"); + _logger.LogDebug("Removed {MediaItemName} from RuntimeCache", mediaItem.Name); } } } From a6c84094665af19327025933cf93d920c4b6ef51 Mon Sep 17 00:00:00 2001 From: AndyBoot <82056925+AndyBoot@users.noreply.github.com> Date: Sat, 15 Oct 2022 22:39:40 +0100 Subject: [PATCH 3/5] Added missing StringUtils. Also, resolved the errors in the InlineSvgTagHelperTests file by adding nulls in place of the newly added dependencies within the constructor. --- .../InlineSvgTagHelperTests.cs | 24 +++--- Our.Umbraco.TagHelpers/Utils/StringUtils.cs | 83 +++++++++++++++++++ 2 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 Our.Umbraco.TagHelpers/Utils/StringUtils.cs diff --git a/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs b/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs index e5732ae..d7d9d3c 100644 --- a/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs +++ b/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs @@ -41,7 +41,7 @@ public void Setup() [Test] public void NoOutputIfNoMediaOrFileSet() { - var tagHelper = new InlineSvgTagHelper(null, null, null); + var tagHelper = new InlineSvgTagHelper(null, null, null, null, null); tagHelper.Process(_context, _output); @@ -52,7 +52,7 @@ public void NoOutputIfNoMediaOrFileSet() public void NoOutputIfBothMediaAndFileSet() { var umbContent = Mock.Of(c => c.ContentType.ItemType == PublishedItemType.Media); - var tagHelper = new InlineSvgTagHelper(null, null, null) + var tagHelper = new InlineSvgTagHelper(null, null, null, null, null) { FileSource = "test.svg", MediaItem = umbContent @@ -66,7 +66,7 @@ public void NoOutputIfBothMediaAndFileSet() [Test] public void NoOutputIfFileNotSvg() { - var tagHelper = new InlineSvgTagHelper(null, null, null) + var tagHelper = new InlineSvgTagHelper(null, null, null, null, null) { FileSource = "test.notsvg" }; @@ -82,7 +82,7 @@ public void NoOutputIfFileNotFound() var fileProvider = new Mock(); fileProvider.Setup(p => p.GetFileInfo(It.IsAny())).Returns(Mock.Of(f => !f.Exists)); var hostEnv = Mock.Of(e => e.WebRootFileProvider == fileProvider.Object); - var tagHelper = new InlineSvgTagHelper(null, hostEnv, null) + var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, null, null) { FileSource = "test.svg" }; @@ -98,7 +98,7 @@ public void ExpectedOutputIfValidFile() var fileProvider = new Mock(); fileProvider.Setup(p => p.GetFileInfo(It.IsAny())).Returns(Mock.Of(f => f.Exists && f.CreateReadStream() == new MemoryStream(Encoding.UTF8.GetBytes("test svg")))); var hostEnv = Mock.Of(e => e.WebRootFileProvider == fileProvider.Object); - var tagHelper = new InlineSvgTagHelper(null, hostEnv, null) + var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, null, null) { FileSource = "test.svg" }; @@ -116,7 +116,7 @@ public void NoOutputIfMediaUrlNull() { var urlProvider = new Mock(); urlProvider.Setup(p => p.GetMediaUrl(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns((string)null!); - var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object) + var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object, null, null) { MediaItem = Mock.Of(c => c.ContentType.ItemType == PublishedItemType.Media) }; @@ -132,7 +132,7 @@ public void NoOutputIfMediaNotSvg() var umbContent = Mock.Of(c => c.ContentType.ItemType == PublishedItemType.Media); var urlProvider = new Mock(); urlProvider.Setup(p => p.GetMediaUrl(umbContent, It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns("test.notsvg"); - var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object) + var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object, null, null) { MediaItem = umbContent }; @@ -152,7 +152,9 @@ public void NoOutputIfMediaNotFound() var tagHelper = new InlineSvgTagHelper( new MediaFileManager(fileSystem, null, null, null, null, Mock.Of>()), null, - urlProvider.Object) + urlProvider.Object, + null, + null) { MediaItem = umbContent }; @@ -172,7 +174,9 @@ public void ExpectedOutputIfValidMedia() var tagHelper = new InlineSvgTagHelper( new MediaFileManager(fileSystem, null, null, null, null, Mock.Of>()), null, - urlProvider.Object) + urlProvider.Object, + null, + null) { MediaItem = umbContent }; @@ -193,7 +197,7 @@ public void SanitizesJavascript() .Setup(p => p.GetFileInfo(It.IsAny())) .Returns(Mock.Of(f => f.Exists && f.CreateReadStream() == new MemoryStream(Encoding.UTF8.GetBytes("Click hereend")))); var hostEnv = Mock.Of(e => e.WebRootFileProvider == fileProvider.Object); - var tagHelper = new InlineSvgTagHelper(null, hostEnv, null) + var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, null, null) { FileSource = "test.svg" }; diff --git a/Our.Umbraco.TagHelpers/Utils/StringUtils.cs b/Our.Umbraco.TagHelpers/Utils/StringUtils.cs new file mode 100644 index 0000000..016e3b2 --- /dev/null +++ b/Our.Umbraco.TagHelpers/Utils/StringUtils.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Our.Umbraco.TagHelpers.Utils +{ + internal static class StringUtils + { + internal static double GetDouble(object value, double defaultValue = 0) + { + double num; + if (!IsDouble(value)) + { + return defaultValue; + } + try + { + num = Convert.ToDouble(value); + } + catch + { + num = defaultValue; + } + return num; + } + + internal static decimal GetDecimal(object input, decimal defaultValue = 0) + { + decimal value = decimal.MinValue; + decimal.TryParse(input != null ? input.ToString().Replace("£", "") : "", out value); + + if (value > decimal.MinValue) + { + return value; + } + + return defaultValue; + } + + internal static int GetInteger(object input, int defaultValue = 0) + { + var value = 0; + int.TryParse(input != null ? input.ToString() : "", out value); + + if (value > 0) + { + return value; + } + + return defaultValue; + } + + internal static bool IsDouble(object value) + { + double num; + if (IsNull(value)) + { + return false; + } + if (value is double) + { + return true; + } + string str = Convert.ToString(value); + if (double.TryParse(str, out num)) + { + return true; + } + return false; + } + + internal static bool IsNull(object value) + { + if (value == null) + { + return true; + } + return value == DBNull.Value; + } + } +} From 81186c19c043ced754f7c32467ae99ad8e8638f6 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Mon, 17 Oct 2022 11:30:37 +0100 Subject: [PATCH 4/5] Updated one test to help show Andy - mayu want to add more tests based on the different config set --- .../InlineSvgTagHelperTests.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs b/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs index d7d9d3c..d736f23 100644 --- a/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs +++ b/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Options; using Moq; using NUnit.Framework; +using Our.Umbraco.TagHelpers.Configuration; using System; using System.Collections.Generic; using System.IO; @@ -41,7 +42,18 @@ public void Setup() [Test] public void NoOutputIfNoMediaOrFileSet() { - var tagHelper = new InlineSvgTagHelper(null, null, null, null, null); + var settings = new OurUmbracoTagHelpersConfiguration() + { + OurSVG = + { + Cache = false, + EnsureViewBox = false, + CacheMinutes = 180 + } + }; + IOptions tagHelperSettings = Options.Create(settings); + + var tagHelper = new InlineSvgTagHelper(null, null, null, tagHelperSettings, null); tagHelper.Process(_context, _output); From 4bd085dfa089bcb9b8a62cd7c8429b403cb0fa39 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Mon, 17 Oct 2022 11:53:04 +0100 Subject: [PATCH 5/5] Fixes up all the tests for now - we can revisit more tests to test with the different config options --- .../InlineSvgTagHelperTests.cs | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs b/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs index d736f23..c2c993d 100644 --- a/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs +++ b/Our.Umbraco.TagHelpers.Tests/InlineSvgTagHelperTests.cs @@ -21,6 +21,7 @@ public class InlineSvgTagHelperTests { private TagHelperContext _context = null!; private TagHelperOutput _output = null!; + private IOptions _settings = null!; [SetUp] public void Setup() @@ -37,11 +38,7 @@ public void Setup() content.SetContent("Something else"); return Task.FromResult(content); }); - } - [Test] - public void NoOutputIfNoMediaOrFileSet() - { var settings = new OurUmbracoTagHelpersConfiguration() { OurSVG = @@ -51,9 +48,15 @@ public void NoOutputIfNoMediaOrFileSet() CacheMinutes = 180 } }; - IOptions tagHelperSettings = Options.Create(settings); - - var tagHelper = new InlineSvgTagHelper(null, null, null, tagHelperSettings, null); + _settings = Options.Create(settings); + + } + + [Test] + public void NoOutputIfNoMediaOrFileSet() + { + + var tagHelper = new InlineSvgTagHelper(null, null, null, _settings, null); tagHelper.Process(_context, _output); @@ -64,7 +67,7 @@ public void NoOutputIfNoMediaOrFileSet() public void NoOutputIfBothMediaAndFileSet() { var umbContent = Mock.Of(c => c.ContentType.ItemType == PublishedItemType.Media); - var tagHelper = new InlineSvgTagHelper(null, null, null, null, null) + var tagHelper = new InlineSvgTagHelper(null, null, null, _settings, null) { FileSource = "test.svg", MediaItem = umbContent @@ -78,7 +81,7 @@ public void NoOutputIfBothMediaAndFileSet() [Test] public void NoOutputIfFileNotSvg() { - var tagHelper = new InlineSvgTagHelper(null, null, null, null, null) + var tagHelper = new InlineSvgTagHelper(null, null, null, _settings, null) { FileSource = "test.notsvg" }; @@ -94,7 +97,7 @@ public void NoOutputIfFileNotFound() var fileProvider = new Mock(); fileProvider.Setup(p => p.GetFileInfo(It.IsAny())).Returns(Mock.Of(f => !f.Exists)); var hostEnv = Mock.Of(e => e.WebRootFileProvider == fileProvider.Object); - var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, null, null) + var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, _settings, null) { FileSource = "test.svg" }; @@ -110,7 +113,7 @@ public void ExpectedOutputIfValidFile() var fileProvider = new Mock(); fileProvider.Setup(p => p.GetFileInfo(It.IsAny())).Returns(Mock.Of(f => f.Exists && f.CreateReadStream() == new MemoryStream(Encoding.UTF8.GetBytes("test svg")))); var hostEnv = Mock.Of(e => e.WebRootFileProvider == fileProvider.Object); - var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, null, null) + var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, _settings, null) { FileSource = "test.svg" }; @@ -128,7 +131,7 @@ public void NoOutputIfMediaUrlNull() { var urlProvider = new Mock(); urlProvider.Setup(p => p.GetMediaUrl(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns((string)null!); - var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object, null, null) + var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object, _settings, null) { MediaItem = Mock.Of(c => c.ContentType.ItemType == PublishedItemType.Media) }; @@ -144,7 +147,7 @@ public void NoOutputIfMediaNotSvg() var umbContent = Mock.Of(c => c.ContentType.ItemType == PublishedItemType.Media); var urlProvider = new Mock(); urlProvider.Setup(p => p.GetMediaUrl(umbContent, It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns("test.notsvg"); - var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object, null, null) + var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object, _settings, null) { MediaItem = umbContent }; @@ -164,8 +167,8 @@ public void NoOutputIfMediaNotFound() var tagHelper = new InlineSvgTagHelper( new MediaFileManager(fileSystem, null, null, null, null, Mock.Of>()), null, - urlProvider.Object, - null, + urlProvider.Object, + _settings, null) { MediaItem = umbContent @@ -186,8 +189,8 @@ public void ExpectedOutputIfValidMedia() var tagHelper = new InlineSvgTagHelper( new MediaFileManager(fileSystem, null, null, null, null, Mock.Of>()), null, - urlProvider.Object, - null, + urlProvider.Object, + _settings, null) { MediaItem = umbContent @@ -209,7 +212,7 @@ public void SanitizesJavascript() .Setup(p => p.GetFileInfo(It.IsAny())) .Returns(Mock.Of(f => f.Exists && f.CreateReadStream() == new MemoryStream(Encoding.UTF8.GetBytes("Click hereend")))); var hostEnv = Mock.Of(e => e.WebRootFileProvider == fileProvider.Object); - var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, null, null) + var tagHelper = new InlineSvgTagHelper(null, hostEnv, null, _settings, null) { FileSource = "test.svg" };