Skip to content

Commit

Permalink
Support depend-on when using script and style inline (OrchardCMS#17349)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
  • Loading branch information
MikeAlhayek and Piedone authored Jan 15, 2025
1 parent ebc7b11 commit 1c8ae25
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu

if (!hasName && hasSource)
{
// <script asp-src="~/TheBlogTheme/js/clean-blog.min.js"></script>
// <script asp-src="~/TheBlogTheme/js/clean-blog.min.js" at="Foot"></script>
RequireSettings setting;

if (string.IsNullOrEmpty(DependsOn))
{
// Include custom script url
// Include custom script url.
setting = _resourceManager.RegisterUrl("script", Src, DebugSrc);
}
else
{
// Anonymous declaration with dependencies, then display
// Anonymous declaration with dependencies, then display.

// Using the source as the name to prevent duplicate references to the same file
// Using the source as the name to prevent duplicate references to the same file.
var name = Src.ToLowerInvariant();

PopulateResourceDefinition(_resourceManager.InlineManifest.DefineScript(name));
Expand All @@ -88,8 +88,8 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
}
else if (hasName && !hasSource)
{
// Resource required
// <script asp-name="bootstrap"></script>
// Resource required.
// <script asp-name="bootstrap" at="Foot"></script>

var setting = _resourceManager.RegisterResource("script", Name);

Expand All @@ -108,7 +108,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
// This allows additions to the pre registered scripts dependencies.
if (!string.IsNullOrEmpty(DependsOn))
{
setting.SetDependencies(DependsOn.Split(_separator, StringSplitOptions.RemoveEmptyEntries));
setting.SetDependencies(DependsOn.Split(_separator, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
}

// Allow Inline to work with both named scripts, and named inline scripts.
Expand All @@ -118,7 +118,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
var childContent = await output.GetChildContentAsync();
if (!childContent.IsEmptyOrWhiteSpace)
{
// Inline content definition
// Inline content definition.
_resourceManager.InlineManifest.DefineScript(Name)
.SetInnerContent(childContent.GetContent());
}
Expand All @@ -135,11 +135,11 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
}
else if (hasName && hasSource)
{
// Inline declaration
// Inline declaration.

PopulateResourceDefinition(_resourceManager.InlineManifest.DefineScript(Name));

// If At is specified then we also render it
// If At is specified then we also render it.
if (At != ResourceLocation.Unspecified)
{
var setting = _resourceManager.RegisterResource("script", Name);
Expand All @@ -154,10 +154,32 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
}
else
{
// Custom script content
// Custom script content.
// <script at="Foot"> /* example JavaScript code*/ </script>

var childContent = await output.GetChildContentAsync();

if (!string.IsNullOrEmpty(DependsOn))
{
var dependencies = DependsOn.Split(_separator, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);

foreach (var dependency in dependencies)
{
var versionParts = dependency.Split(':');

var resourceName = versionParts[0];

var script = _resourceManager.RegisterResource("script", resourceName);

if (versionParts.Length == 2)
{
script.Version = versionParts[1];
}

script.AtLocation(At);
}
}

var builder = new TagBuilder("script");
builder.InnerHtml.AppendHtml(childContent);
builder.TagRenderMode = TagRenderMode.Normal;
Expand Down Expand Up @@ -198,7 +220,7 @@ private void PopulateResourceDefinition(ResourceDefinition definition)

if (!string.IsNullOrEmpty(DependsOn))
{
definition.SetDependencies(DependsOn.Split(_separator, StringSplitOptions.RemoveEmptyEntries));
definition.SetDependencies(DependsOn.Split(_separator, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
}

if (AppendVersion.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
{
output.SuppressOutput();

if (string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Src))
var hasName = !string.IsNullOrEmpty(Name);
var hasSource = !string.IsNullOrEmpty(Src);

if (!hasName && hasSource)
{
// Include custom style
// Include custom style.
// <style asp-src="~/example.css" at="Head"></style>

var setting = _resourceManager.RegisterUrl("stylesheet", Src, DebugSrc);

foreach (var attribute in output.Attributes)
Expand Down Expand Up @@ -89,7 +94,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu

if (!string.IsNullOrEmpty(DependsOn))
{
setting.SetDependencies(DependsOn.Split(_splitSeparators, StringSplitOptions.RemoveEmptyEntries));
setting.SetDependencies(DependsOn.Split(_splitSeparators, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
}

if (At == ResourceLocation.Inline)
Expand All @@ -99,9 +104,10 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
output.Content.AppendHtml(sw.ToString());
}
}
else if (!string.IsNullOrEmpty(Name) && string.IsNullOrEmpty(Src))
else if (hasName && !hasSource)
{
// Resource required
// Resource required.
// <style asp-name="example" at="Head"></style>

var setting = _resourceManager.RegisterResource("stylesheet", Name);

Expand Down Expand Up @@ -152,13 +158,13 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
// This allows additions to the pre registered style dependencies.
if (!string.IsNullOrEmpty(DependsOn))
{
setting.SetDependencies(DependsOn.Split(_splitSeparators, StringSplitOptions.RemoveEmptyEntries));
setting.SetDependencies(DependsOn.Split(_splitSeparators, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
}

var childContent = await output.GetChildContentAsync();
if (!childContent.IsEmptyOrWhiteSpace)
{
// Inline named style definition
// Inline named style definition.
_resourceManager.InlineManifest.DefineStyle(Name)
.SetInnerContent(childContent.GetContent());
}
Expand All @@ -170,9 +176,9 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
output.Content.AppendHtml(sw.ToString());
}
}
else if (!string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Src))
else if (hasName && hasSource)
{
// Inline declaration
// Inline declaration.

var definition = _resourceManager.InlineManifest.DefineStyle(Name);
definition.SetUrl(Src, DebugSrc);
Expand All @@ -194,12 +200,12 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu

if (!string.IsNullOrEmpty(Culture))
{
definition.SetCultures(Culture.Split(',', StringSplitOptions.RemoveEmptyEntries));
definition.SetCultures(Culture.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
}

if (!string.IsNullOrEmpty(DependsOn))
{
definition.SetDependencies(DependsOn.Split(',', StringSplitOptions.RemoveEmptyEntries));
definition.SetDependencies(DependsOn.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
}

// Also include the style.
Expand Down Expand Up @@ -241,12 +247,34 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
output.Content.AppendHtml(sw.ToString());
}
}
else if (string.IsNullOrEmpty(Name) && string.IsNullOrEmpty(Src))
else
{
// Custom style content
// Custom style content.
// <style at="Head"> /* example css code*/ </style>

var childContent = await output.GetChildContentAsync();

if (!string.IsNullOrEmpty(DependsOn))
{
var dependencies = DependsOn.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);

foreach (var dependency in dependencies)
{
var versionParts = dependency.Split(':');

var resourceName = versionParts[0];

var style = _resourceManager.RegisterResource("stylesheet", resourceName);

if (versionParts.Length == 2)
{
style.Version = versionParts[1];
}

style.AtLocation(At);
}
}

var builder = new TagBuilder("style");
builder.InnerHtml.AppendHtml(childContent);
builder.TagRenderMode = TagRenderMode.Normal;
Expand All @@ -256,7 +284,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
builder.Attributes.Add(attribute.Name, attribute.Value.ToString());
}

// If no type was specified, define a default one
// If no type was specified, define a default one.
if (!builder.Attributes.ContainsKey("type"))
{
builder.Attributes.Add("type", "text/css");
Expand Down

0 comments on commit 1c8ae25

Please sign in to comment.