in src/OrchardCore/OrchardCore.ResourceManagement/TagHelpers/ScriptTagHelper.cs [48:301]
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
output.SuppressOutput();
if (String.IsNullOrEmpty(Name) && !String.IsNullOrEmpty(Src))
{
RequireSettings setting;
if (String.IsNullOrEmpty(DependsOn))
{
// Include custom script url
setting = _resourceManager.RegisterUrl("script", Src, DebugSrc);
}
else
{
// Anonymous declaration with dependencies, then display
// Using the source as the name to prevent duplicate references to the same file
var name = Src.ToLowerInvariant();
var definition = _resourceManager.InlineManifest.DefineScript(name);
definition.SetUrl(Src, DebugSrc);
if (!String.IsNullOrEmpty(Version))
{
definition.SetVersion(Version);
}
if (!String.IsNullOrEmpty(CdnSrc))
{
definition.SetCdn(CdnSrc, DebugCdnSrc);
}
if (!String.IsNullOrEmpty(Culture))
{
definition.SetCultures(Culture.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries));
}
if (!String.IsNullOrEmpty(DependsOn))
{
definition.SetDependencies(DependsOn.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries));
}
if (AppendVersion.HasValue)
{
definition.ShouldAppendVersion(AppendVersion);
}
if (!String.IsNullOrEmpty(Version))
{
definition.SetVersion(Version);
}
setting = _resourceManager.RegisterResource("script", name);
}
if (At != ResourceLocation.Unspecified)
{
setting.AtLocation(At);
}
if (!String.IsNullOrEmpty(Condition))
{
setting.UseCondition(Condition);
}
if (Debug != null)
{
setting.UseDebugMode(Debug.Value);
}
if (!String.IsNullOrEmpty(Culture))
{
setting.UseCulture(Culture);
}
if (AppendVersion.HasValue)
{
setting.ShouldAppendVersion(AppendVersion);
}
foreach (var attribute in output.Attributes)
{
setting.SetAttribute(attribute.Name, attribute.Value.ToString());
}
if (At == ResourceLocation.Unspecified)
{
_resourceManager.RenderLocalScript(setting, output.Content);
}
}
else if (!String.IsNullOrEmpty(Name) && String.IsNullOrEmpty(Src))
{
// Resource required
var setting = _resourceManager.RegisterResource("script", Name);
if (At != ResourceLocation.Unspecified)
{
setting.AtLocation(At);
}
if (UseCdn != null)
{
setting.UseCdn(UseCdn.Value);
}
if (!String.IsNullOrEmpty(Condition))
{
setting.UseCondition(Condition);
}
if (Debug != null)
{
setting.UseDebugMode(Debug.Value);
}
if (!String.IsNullOrEmpty(Culture))
{
setting.UseCulture(Culture);
}
if (AppendVersion.HasValue)
{
setting.ShouldAppendVersion(AppendVersion);
}
if (!String.IsNullOrEmpty(Version))
{
setting.UseVersion(Version);
}
// This allows additions to the pre registered scripts dependencies.
if (!String.IsNullOrEmpty(DependsOn))
{
setting.SetDependencies(DependsOn.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries));
}
if (At == ResourceLocation.Unspecified)
{
_resourceManager.RenderLocalScript(setting, output.Content);
}
else
{
var childContent = await output.GetChildContentAsync();
if (!childContent.IsEmptyOrWhiteSpace)
{
// Inline content definition
_resourceManager.InlineManifest.DefineScript(Name)
.SetInnerContent(childContent.GetContent());
}
}
}
else if (!String.IsNullOrEmpty(Name) && !String.IsNullOrEmpty(Src))
{
// Inline declaration
var definition = _resourceManager.InlineManifest.DefineScript(Name);
definition.SetUrl(Src, DebugSrc);
if (!String.IsNullOrEmpty(Version))
{
definition.SetVersion(Version);
}
if (!String.IsNullOrEmpty(CdnSrc))
{
definition.SetCdn(CdnSrc, DebugCdnSrc);
}
if (!String.IsNullOrEmpty(Culture))
{
definition.SetCultures(Culture.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries));
}
if (!String.IsNullOrEmpty(DependsOn))
{
definition.SetDependencies(DependsOn.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries));
}
if (AppendVersion.HasValue)
{
definition.ShouldAppendVersion(AppendVersion);
}
if (!String.IsNullOrEmpty(Version))
{
definition.SetVersion(Version);
}
// If At is specified then we also render it
if (At != ResourceLocation.Unspecified)
{
var setting = _resourceManager.RegisterResource("script", Name);
setting.AtLocation(At);
if (UseCdn != null)
{
setting.UseCdn(UseCdn.Value);
}
if (!String.IsNullOrEmpty(Condition))
{
setting.UseCondition(Condition);
}
if (Debug != null)
{
setting.UseDebugMode(Debug.Value);
}
if (!String.IsNullOrEmpty(Culture))
{
setting.UseCulture(Culture);
}
foreach (var attribute in output.Attributes)
{
setting.SetAttribute(attribute.Name, attribute.Value.ToString());
}
}
}
else if (String.IsNullOrEmpty(Name) && String.IsNullOrEmpty(Src))
{
// Custom script content
var childContent = await output.GetChildContentAsync();
var builder = new TagBuilder("script");
builder.InnerHtml.AppendHtml(childContent);
builder.TagRenderMode = TagRenderMode.Normal;
foreach (var attribute in output.Attributes)
{
builder.Attributes.Add(attribute.Name, attribute.Value.ToString());
}
// If no type was specified, define a default one
if (!builder.Attributes.ContainsKey("type"))
{
builder.Attributes.Add("type", "text/javascript");
}
if (At == ResourceLocation.Head)
{
_resourceManager.RegisterHeadScript(builder);
}
else
{
_resourceManager.RegisterFootScript(builder);
}
}
}