Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support depend-on when using script and style inline #17349

Merged
merged 7 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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