diff --git a/README.md b/README.md index 8c6e426..587d140 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ _❤️ If you use and like Smidge please consider [becoming a GitHub Sponsor](h ``` 1. Render your bundle: ```html - + ``` __[See Installation](https://github.com/Shazwazza/Smidge/wiki/installation) for full configuration details__ @@ -109,7 +109,7 @@ __[See Declarations](https://github.com/Shazwazza/Smidge/wiki/Declarations) for The easiest way to render bundles is simply by the bundle name: ```html - + ``` diff --git a/src/Smidge.Web/Views/Home/AllDebug.cshtml b/src/Smidge.Web/Views/Home/AllDebug.cshtml index c9cf83a..c9ba0b8 100644 --- a/src/Smidge.Web/Views/Home/AllDebug.cshtml +++ b/src/Smidge.Web/Views/Home/AllDebug.cshtml @@ -1,4 +1,4 @@ -@using System.Diagnostics +@using System.Diagnostics @using Smidge @using Smidge.Models; @using Smidge.FileProcessors; @@ -26,8 +26,8 @@ SmidgeHelper.RequiresCss("~/Css/Folder/*.css"); } - - + + @@ -41,7 +41,7 @@ @await SmidgeHelper.JsHereAsync("test-bundle-2", debug: true) @await SmidgeHelper.JsHereAsync("test-bundle-10", debug: true) - + - \ No newline at end of file + diff --git a/src/Smidge.Web/Views/Home/Index.cshtml b/src/Smidge.Web/Views/Home/Index.cshtml index 39f9308..393d56b 100644 --- a/src/Smidge.Web/Views/Home/Index.cshtml +++ b/src/Smidge.Web/Views/Home/Index.cshtml @@ -1,4 +1,4 @@ -@using Smidge +@using Smidge @using Smidge.Models; @using Smidge.FileProcessors; @@ -30,10 +30,10 @@ SmidgeHelper.RequiresCss("~/Css/Folder/*.css"); } - - + + - + @@ -46,6 +46,6 @@ @await SmidgeHelper.JsHereAsync("no-files", debug: false) @await SmidgeHelper.JsHereAsync("test-bundle-10") - + - \ No newline at end of file + diff --git a/src/Smidge.Web/Views/Home/SubFolder.cshtml b/src/Smidge.Web/Views/Home/SubFolder.cshtml index e10f2ab..fef9db5 100644 --- a/src/Smidge.Web/Views/Home/SubFolder.cshtml +++ b/src/Smidge.Web/Views/Home/SubFolder.cshtml @@ -1,4 +1,4 @@ -@using Smidge +@using Smidge @using Smidge.Models; @using Smidge.FileProcessors; @@ -24,8 +24,8 @@ SmidgeHelper.RequiresCss("~/Css/Folder/*.css"); } - - + + @@ -38,7 +38,7 @@ @await SmidgeHelper.JsHereAsync("test-bundle-1") @await SmidgeHelper.JsHereAsync("test-bundle-2") - + - \ No newline at end of file + diff --git a/src/Smidge.Web/appsettings.json b/src/Smidge.Web/appsettings.json index 1045efa..51b7e33 100644 --- a/src/Smidge.Web/appsettings.json +++ b/src/Smidge.Web/appsettings.json @@ -1,6 +1,6 @@ -{ +{ "smidge": { "dataFolder": "Smidge", //where the cache files are stored - "version": "123" //can be any string + "version": "125" //can be any string } -} \ No newline at end of file +} diff --git a/src/Smidge/TagHelpers/SmidgeLinkTagHelper.cs b/src/Smidge/TagHelpers/SmidgeLinkTagHelper.cs index 85cab8a..239031a 100644 --- a/src/Smidge/TagHelpers/SmidgeLinkTagHelper.cs +++ b/src/Smidge/TagHelpers/SmidgeLinkTagHelper.cs @@ -6,12 +6,39 @@ using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Razor.TagHelpers; using Microsoft.AspNetCore.Mvc.TagHelpers; +using System.Collections.Generic; +using System; namespace Smidge.TagHelpers { - [HtmlTargetElement("link", Attributes = "href", TagStructure = TagStructure.WithoutEndTag)] + [HtmlTargetElement("link", Attributes = HrefAttributeName, TagStructure = TagStructure.WithoutEndTag)] public class SmidgeLinkTagHelper : TagHelper { + private const string HrefIncludeAttributeName = "asp-href-include"; + private const string HrefExcludeAttributeName = "asp-href-exclude"; + private const string FallbackHrefAttributeName = "asp-fallback-href"; + private const string SuppressFallbackIntegrityAttributeName = "asp-suppress-fallback-integrity"; + private const string FallbackHrefIncludeAttributeName = "asp-fallback-href-include"; + private const string FallbackHrefExcludeAttributeName = "asp-fallback-href-exclude"; + private const string FallbackTestClassAttributeName = "asp-fallback-test-class"; + private const string FallbackTestPropertyAttributeName = "asp-fallback-test-property"; + private const string FallbackTestValueAttributeName = "asp-fallback-test-value"; + private const string AppendVersionAttributeName = "asp-append-version"; + private const string HrefAttributeName = "href"; + + private readonly HashSet _invalid = new() + { + HrefIncludeAttributeName, + HrefExcludeAttributeName, + FallbackHrefAttributeName, + FallbackHrefIncludeAttributeName, + FallbackTestClassAttributeName, + FallbackTestPropertyAttributeName, + SuppressFallbackIntegrityAttributeName, + FallbackHrefExcludeAttributeName, + FallbackTestValueAttributeName, + AppendVersionAttributeName + }; private readonly SmidgeHelper _smidgeHelper; private readonly IBundleManager _bundleManager; private readonly HtmlEncoder _encoder; @@ -31,7 +58,7 @@ public SmidgeLinkTagHelper(SmidgeHelper smidgeHelper, IBundleManager bundleManag /// public override int Order => -2000; - [HtmlAttributeName("href")] + [HtmlAttributeName(HrefAttributeName)] public string Source { get; set; } [HtmlAttributeName("debug")] @@ -39,37 +66,48 @@ public SmidgeLinkTagHelper(SmidgeHelper smidgeHelper, IBundleManager bundleManag public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { + if (string.IsNullOrWhiteSpace(Source)) + { + return; + } + + var exists = _bundleManager.Exists(Source); + // Pass through attribute that is also a well-known HTML attribute. // this is required to make sure that other tag helpers executing against this element have // the value copied across - if (Source != null) + output.Attributes.SetAttribute("href", Source); + + if (!exists) { - output.CopyHtmlAttribute("href", context); + return; } - if (_bundleManager.Exists(Source)) + if (context.AllAttributes.Any(x => _invalid.Contains(x.Name))) { - var result = (await _smidgeHelper.GenerateCssUrlsAsync(Source, Debug)).ToArray(); - var currAttr = output.Attributes.ToDictionary(x => x.Name, x => x.Value); - using (var writer = new StringWriter()) + throw new InvalidOperationException("Smidge tag helpers do not support the ASP.NET tag helpers: " + string.Join(", ", _invalid)); + } + + var result = (await _smidgeHelper.GenerateCssUrlsAsync(Source, Debug)).ToArray(); + var currAttr = output.Attributes.ToDictionary(x => x.Name, x => x.Value); + using (var writer = new StringWriter()) + { + foreach (var s in result) { - foreach (var s in result) + var builder = new TagBuilder(output.TagName) { - var builder = new TagBuilder(output.TagName) - { - TagRenderMode = TagRenderMode.SelfClosing - }; - builder.MergeAttributes(currAttr); - builder.Attributes["href"] = s; + TagRenderMode = TagRenderMode.SelfClosing + }; + builder.MergeAttributes(currAttr); + builder.Attributes["href"] = s; - builder.WriteTo(writer, _encoder); - } - writer.Flush(); - output.PostElement.SetHtmlContent(new HtmlString(writer.ToString())); + builder.WriteTo(writer, _encoder); } - //This ensures the original tag is not written. - output.TagName = null; - } + writer.Flush(); + output.PostElement.SetHtmlContent(new HtmlString(writer.ToString())); + } + //This ensures the original tag is not written. + output.TagName = null; } } -} \ No newline at end of file +} diff --git a/src/Smidge/TagHelpers/SmidgeScriptTagHelper.cs b/src/Smidge/TagHelpers/SmidgeScriptTagHelper.cs index a01b9e2..75ed561 100644 --- a/src/Smidge/TagHelpers/SmidgeScriptTagHelper.cs +++ b/src/Smidge/TagHelpers/SmidgeScriptTagHelper.cs @@ -6,13 +6,37 @@ using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Razor.TagHelpers; using Microsoft.AspNetCore.Mvc.TagHelpers; +using System.Collections.Generic; +using System; namespace Smidge.TagHelpers { - [HtmlTargetElement("script", Attributes = "src")] + [HtmlTargetElement("script", Attributes = SrcAttributeName)] public class SmidgeScriptTagHelper : TagHelper { + private const string SrcIncludeAttributeName = "asp-src-include"; + private const string SrcExcludeAttributeName = "asp-src-exclude"; + private const string FallbackSrcAttributeName = "asp-fallback-src"; + private const string FallbackSrcIncludeAttributeName = "asp-fallback-src-include"; + private const string SuppressFallbackIntegrityAttributeName = "asp-suppress-fallback-integrity"; + private const string FallbackSrcExcludeAttributeName = "asp-fallback-src-exclude"; + private const string FallbackTestExpressionAttributeName = "asp-fallback-test"; + private const string AppendVersionAttributeName = "asp-append-version"; + + private const string SrcAttributeName = "src"; + + private readonly HashSet _invalid = new() + { + SrcIncludeAttributeName, + SrcExcludeAttributeName, + FallbackSrcAttributeName, + FallbackSrcIncludeAttributeName, + SuppressFallbackIntegrityAttributeName, + FallbackSrcExcludeAttributeName, + FallbackTestExpressionAttributeName, + AppendVersionAttributeName + }; private readonly SmidgeHelper _smidgeHelper; private readonly IBundleManager _bundleManager; private readonly HtmlEncoder _encoder; @@ -40,37 +64,48 @@ public SmidgeScriptTagHelper(SmidgeHelper smidgeHelper, IBundleManager bundleMan public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { + if (string.IsNullOrWhiteSpace(Source)) + { + return; + } + + var exists = _bundleManager.Exists(Source); + // Pass through attribute that is also a well-known HTML attribute. // this is required to make sure that other tag helpers executing against this element have // the value copied across - if (Source != null) + output.Attributes.SetAttribute(SrcAttributeName, Source); + + if (!exists) + { + return; + } + + if (context.AllAttributes.Any(x => _invalid.Contains(x.Name))) { - output.CopyHtmlAttribute("src", context); + throw new InvalidOperationException("Smidge tag helpers do not support the ASP.NET tag helpers: " + string.Join(", ", _invalid)); } - if (_bundleManager.Exists(Source)) + var result = (await _smidgeHelper.GenerateJsUrlsAsync(Source, Debug)).ToArray(); + var currAttr = output.Attributes.ToDictionary(x => x.Name, x => x.Value); + using (var writer = new StringWriter()) { - var result = (await _smidgeHelper.GenerateJsUrlsAsync(Source, Debug)).ToArray(); - var currAttr = output.Attributes.ToDictionary(x => x.Name, x => x.Value); - using (var writer = new StringWriter()) + foreach (var s in result) { - foreach (var s in result) + var builder = new TagBuilder(output.TagName) { - var builder = new TagBuilder(output.TagName) - { - TagRenderMode = TagRenderMode.Normal - }; - builder.MergeAttributes(currAttr); - builder.Attributes["src"] = s; + TagRenderMode = TagRenderMode.Normal + }; + builder.MergeAttributes(currAttr); + builder.Attributes[SrcAttributeName] = s; - builder.WriteTo(writer, _encoder); - } - writer.Flush(); - output.PostElement.SetHtmlContent(new HtmlString(writer.ToString())); + builder.WriteTo(writer, _encoder); } - //This ensures the original tag is not written. - output.TagName = null; - } + writer.Flush(); + output.PostElement.SetHtmlContent(new HtmlString(writer.ToString())); + } + //This ensures the original tag is not written. + output.TagName = null; } } -} \ No newline at end of file +}