Skip to content

Commit

Permalink
Fixes #153
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazwazza committed May 4, 2022
1 parent a962307 commit e6fb98c
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 66 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ _❤️ If you use and like Smidge please consider [becoming a GitHub Sponsor](h
```
1. Render your bundle:
```html
<script src="my-application" type="text/javascript"></script>
<script src="my-application"></script>
```

__[See Installation](https://github.com/Shazwazza/Smidge/wiki/installation) for full configuration details__
Expand Down Expand Up @@ -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
<script src="my-awesome-js-bundle" type="text/javascript"></script>
<script src="my-awesome-js-bundle"></script>
<link rel="stylesheet" href="my-cool-css-bundle"/>
```

Expand Down
10 changes: 5 additions & 5 deletions src/Smidge.Web/Views/Home/AllDebug.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using System.Diagnostics
@using System.Diagnostics
@using Smidge
@using Smidge.Models;
@using Smidge.FileProcessors;
Expand Down Expand Up @@ -26,8 +26,8 @@
SmidgeHelper.RequiresCss("~/Css/Folder/*.css");
}

<script src="inline-js-bundle" type="text/javascript" debug="true"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css" />
<script src="inline-js-bundle" debug="true"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css" />
<link rel="stylesheet" href="inline-css-bundle" debug="true" />
<link rel="stylesheet" href="test-bundle-4" debug="true" />
</head>
Expand All @@ -41,7 +41,7 @@
@await SmidgeHelper.JsHereAsync("test-bundle-2", debug: true)
@await SmidgeHelper.JsHereAsync("test-bundle-10", debug: true)

<script src="~/Js/non-bundled.js" type="text/javascript"></script>
<script src="~/Js/non-bundled.js"></script>

</body>
</html>
</html>
12 changes: 6 additions & 6 deletions src/Smidge.Web/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using Smidge
@using Smidge
@using Smidge.Models;
@using Smidge.FileProcessors;

Expand Down Expand Up @@ -30,10 +30,10 @@
SmidgeHelper.RequiresCss("~/Css/Folder/*.css");
}

<script src="inline-js-bundle" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css" />
<script src="inline-js-bundle" async="true"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css" />
<link rel="stylesheet" href="inline-css-bundle" />
<link rel="stylesheet" href="test-bundle-4" />
<link rel="stylesheet" href="test-bundle-4" onload="window.alert('hello')" />
</head>
<body>

Expand All @@ -46,6 +46,6 @@
@await SmidgeHelper.JsHereAsync("no-files", debug: false)
@await SmidgeHelper.JsHereAsync("test-bundle-10")

<script src="~/Js/non-bundled.js" type="text/javascript"></script>
<script src="~/Js/non-bundled.js" asp-append-version="true"></script>
</body>
</html>
</html>
10 changes: 5 additions & 5 deletions src/Smidge.Web/Views/Home/SubFolder.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using Smidge
@using Smidge
@using Smidge.Models;
@using Smidge.FileProcessors;

Expand All @@ -24,8 +24,8 @@
SmidgeHelper.RequiresCss("~/Css/Folder/*.css");
}

<script src="inline-js-bundle" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css" />
<script src="inline-js-bundle"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css" />
<link rel="stylesheet" href="inline-css-bundle"/>
<link rel="stylesheet" href="test-bundle-4" />
</head>
Expand All @@ -38,7 +38,7 @@
@await SmidgeHelper.JsHereAsync("test-bundle-1")
@await SmidgeHelper.JsHereAsync("test-bundle-2")

<script src="~/Js/non-bundled.js" type="text/javascript"></script>
<script src="~/Js/non-bundled.js"></script>

</body>
</html>
</html>
6 changes: 3 additions & 3 deletions src/Smidge.Web/appsettings.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
84 changes: 61 additions & 23 deletions src/Smidge/TagHelpers/SmidgeLinkTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> _invalid = new()
{
HrefIncludeAttributeName,
HrefExcludeAttributeName,
FallbackHrefAttributeName,
FallbackHrefIncludeAttributeName,
FallbackTestClassAttributeName,
FallbackTestPropertyAttributeName,
SuppressFallbackIntegrityAttributeName,
FallbackHrefExcludeAttributeName,
FallbackTestValueAttributeName,
AppendVersionAttributeName
};
private readonly SmidgeHelper _smidgeHelper;
private readonly IBundleManager _bundleManager;
private readonly HtmlEncoder _encoder;
Expand All @@ -31,45 +58,56 @@ public SmidgeLinkTagHelper(SmidgeHelper smidgeHelper, IBundleManager bundleManag
/// </summary>
public override int Order => -2000;

[HtmlAttributeName("href")]
[HtmlAttributeName(HrefAttributeName)]
public string Source { get; set; }

[HtmlAttributeName("debug")]
public bool Debug { get; set; }

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;
}
}
}
}
79 changes: 57 additions & 22 deletions src/Smidge/TagHelpers/SmidgeScriptTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> _invalid = new()
{
SrcIncludeAttributeName,
SrcExcludeAttributeName,
FallbackSrcAttributeName,
FallbackSrcIncludeAttributeName,
SuppressFallbackIntegrityAttributeName,
FallbackSrcExcludeAttributeName,
FallbackTestExpressionAttributeName,
AppendVersionAttributeName
};
private readonly SmidgeHelper _smidgeHelper;
private readonly IBundleManager _bundleManager;
private readonly HtmlEncoder _encoder;
Expand Down Expand Up @@ -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;
}
}
}
}

0 comments on commit e6fb98c

Please sign in to comment.