-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
OSOE-338: Javascript Module Support
- Loading branch information
Showing
11 changed files
with
341 additions
and
12 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
Lombiq.HelpfulLibraries.AspNetCore/Extensions/JsonHelperExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.AspNetCore.Html; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using System.IO; | ||
using System.Web; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Rendering; | ||
|
||
public static class JsonHelperExtensions | ||
{ | ||
/// <summary> | ||
/// Returns a full HTML element attribute with the given <paramref name="name"/> prefixed with <c>data-</c> and the | ||
/// value appropriately encoded to prevent XSS attacks. | ||
/// </summary> | ||
public static IHtmlContent DataAttribute(this IJsonHelper helper, string name, object value) | ||
{ | ||
using var stringWriter = new StringWriter(); | ||
helper.Serialize(value).WriteTo(stringWriter, NullHtmlEncoder.Default); | ||
|
||
return new HtmlString($"data-{name}=\"{HttpUtility.HtmlAttributeEncode(stringWriter.ToString())}\""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
Lombiq.HelpfulLibraries.OrchardCore/ResourceManagement/ResourceTypes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Lombiq.HelpfulLibraries.OrchardCore.ResourceManagement; | ||
|
||
public static class ResourceTypes | ||
{ | ||
public const string ScriptModule = "script-module"; | ||
} |
55 changes: 55 additions & 0 deletions
55
Lombiq.HelpfulLibraries.OrchardCore/ResourceManagement/ScriptModuleResourceFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Lombiq.HelpfulLibraries.OrchardCore.Contents; | ||
using Microsoft.AspNetCore.Html; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.DisplayManagement.Implementation; | ||
using OrchardCore.DisplayManagement.Layout; | ||
using OrchardCore.ResourceManagement; | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Lombiq.HelpfulLibraries.OrchardCore.ResourceManagement; | ||
|
||
// Don't replace the "script-module" there with <c>script-module</c> as that will cause the DOC105UseParamref analyzer | ||
// to throw NullReferenceException. The same doesn't seem to happen in other files, for example the | ||
// ResourceManagerExtensions.cs in this directory. | ||
|
||
/// <summary> | ||
/// A filter that looks for the required "script-module" resources. If there were any, it injects the input map | ||
/// (used for mapping module names to URLs) of all registered module resources and the script blocks of the currently | ||
/// required resource. | ||
/// </summary> | ||
public record ScriptModuleResourceFilter(ILayoutAccessor LayoutAccessor) : IAsyncResultFilter | ||
{ | ||
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) | ||
{ | ||
var shape = await context.HttpContext.RequestServices.CreateAdHocShapeForCurrentThemeAsync( | ||
nameof(ScriptModuleResourceFilter), | ||
displayContext => Task.FromResult(DisplayScriptModuleResources(displayContext.ServiceProvider))); | ||
|
||
await LayoutAccessor.AddShapeToZoneAsync("Content", shape, "After"); | ||
await next(); | ||
} | ||
|
||
// We can't safely inject resources from the constructor because some resources may get disposed by the time this | ||
// display action takes place, leading to potential access of disposed objects. Instead, the DisplayContext's | ||
// service provider is used. | ||
private static IHtmlContent DisplayScriptModuleResources(IServiceProvider serviceProvider) | ||
{ | ||
// Won't work correctly with injected resources, the scriptElements below will be empty. Possibly related to the | ||
// IResourceManager.InlineManifest being different. | ||
var resourceManager = serviceProvider.GetRequiredService<IResourceManager>(); | ||
var options = serviceProvider.GetRequiredService<IOptions<ResourceManagementOptions>>().Value; | ||
|
||
var scriptElements = resourceManager.GetRequiredScriptModuleTags(options.ContentBasePath).ToList(); | ||
if (scriptElements.Count == 0) return null; | ||
|
||
var importMap = serviceProvider.GetScriptModuleImportMap(); | ||
var content = new HtmlContentBuilder(capacity: scriptElements.Count + 1).AppendHtml(importMap); | ||
foreach (var script in scriptElements) content.AppendHtml(script); | ||
|
||
return content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.