Skip to content

Commit 9a91938

Browse files
Fix script tag importmap regression - preserve user content when no asp-importmap
Co-authored-by: MackinnonBuck <10456961+MackinnonBuck@users.noreply.github.com>
1 parent 96d658b commit 9a91938

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

src/Mvc/Mvc.TagHelpers/src/ScriptTagHelper.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,25 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
247247
var importMap = ImportMap ?? ViewContext.HttpContext.GetEndpoint()?.Metadata.GetMetadata<ImportMapDefinition>();
248248
if (importMap == null)
249249
{
250-
// No importmap found, nothing to do.
251-
output.SuppressOutput();
250+
// No importmap found. Only suppress output if this was intended to be
251+
// an automatically generated importmap (i.e., when asp-importmap was used).
252+
// If the user provided explicit content without asp-importmap, let it render as-is.
253+
if (ImportMap != null || context.AllAttributes.ContainsName(ImportMapAttributeName))
254+
{
255+
output.SuppressOutput();
256+
return;
257+
}
258+
// Let the tag render as-is by continuing with normal processing
259+
// Don't return here, let normal attribute copying happen
260+
}
261+
else
262+
{
263+
output.TagName = "script";
264+
output.TagMode = TagMode.StartTagAndEndTag;
265+
output.Attributes.SetAttribute("type", "importmap");
266+
output.Content.SetHtmlContent(importMap.ToString());
252267
return;
253268
}
254-
255-
output.TagName = "script";
256-
output.TagMode = TagMode.StartTagAndEndTag;
257-
output.Attributes.SetAttribute("type", "importmap");
258-
output.Content.SetHtmlContent(importMap.ToString());
259-
return;
260269
}
261270

262271
// Pass through attribute that is also a well-known HTML attribute.

src/Mvc/Mvc.TagHelpers/test/ScriptTagHelperTest.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,59 @@ public void ScriptTagHelper_RendersImportMap_FromEndpoint()
794794
Assert.Equal(importMap.ToJson(), output.Content.GetContent());
795795
}
796796

797+
[Fact]
798+
public void ScriptTagHelper_PreservesExplicitImportMapContent_WhenNoImportMapDefinition()
799+
{
800+
// Arrange - this simulates the user's scenario where they provide explicit importmap content
801+
// without using asp-importmap attribute
802+
var context = MakeTagHelperContext(
803+
attributes: new TagHelperAttributeList
804+
{
805+
new TagHelperAttribute("type", "importmap"),
806+
});
807+
808+
var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());
809+
810+
var helper = GetHelper();
811+
helper.Type = "importmap";
812+
// No endpoint with ImportMapDefinition and no asp-importmap attribute
813+
// This should NOT suppress the output, allowing user content to render
814+
815+
// Act
816+
helper.Process(context, output);
817+
818+
// Assert
819+
Assert.Equal("script", output.TagName); // Tag should not be suppressed
820+
Assert.Equal("importmap", output.Attributes["type"].Value);
821+
// The output should not be suppressed, allowing user's explicit content to render
822+
Assert.False(output.IsContentModified); // Content should remain as user provided
823+
}
824+
825+
[Fact]
826+
public void ScriptTagHelper_SuppressesOutput_WhenAspImportMapAttributeUsedButNoDefinition()
827+
{
828+
// Arrange - this simulates using asp-importmap attribute but having no ImportMapDefinition
829+
var context = MakeTagHelperContext(
830+
attributes: new TagHelperAttributeList
831+
{
832+
new TagHelperAttribute("type", "importmap"),
833+
new TagHelperAttribute("asp-importmap", null), // asp-importmap used but no value
834+
});
835+
836+
var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());
837+
838+
var helper = GetHelper();
839+
helper.Type = "importmap";
840+
// No endpoint with ImportMapDefinition but asp-importmap attribute is present
841+
// This should suppress the output since it was intended to be auto-generated
842+
843+
// Act
844+
helper.Process(context, output);
845+
846+
// Assert - output should be suppressed when asp-importmap is used but no definition found
847+
Assert.Null(output.TagName); // Tag should be suppressed
848+
}
849+
797850
private Endpoint CreateEndpoint(ImportMapDefinition importMap = null)
798851
{
799852
return new Endpoint(

0 commit comments

Comments
 (0)