Skip to content

Commit 6490384

Browse files
committed
Handle other tag helper setting content
1 parent c0960f3 commit 6490384

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,27 +242,31 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
242242

243243
if (string.Equals(Type, "importmap", StringComparison.OrdinalIgnoreCase))
244244
{
245+
// Do not update the content if another tag helper targeting this element has already done so.
246+
if (output.IsContentModified)
247+
{
248+
return;
249+
}
250+
245251
// This is an importmap script, check if there's existing content first.
246252
var childContent = await output.GetChildContentAsync();
247253
if (!childContent.IsEmptyOrWhiteSpace)
248254
{
249255
// User provided existing content; preserve it.
250256
output.Content.SetHtmlContent(childContent);
257+
return;
251258
}
252-
else
253-
{
254-
// No existing content, so we can apply import map logic.
255-
var importMap = ImportMap ?? ViewContext.HttpContext.GetEndpoint()?.Metadata.GetMetadata<ImportMapDefinition>();
256-
if (importMap == null)
257-
{
258-
// No importmap found, nothing to do.
259-
output.SuppressOutput();
260-
return;
261-
}
262259

263-
output.Content.SetHtmlContent(importMap.ToString());
260+
// No existing content, so we can apply import map logic.
261+
var importMap = ImportMap ?? ViewContext.HttpContext.GetEndpoint()?.Metadata.GetMetadata<ImportMapDefinition>();
262+
if (importMap == null)
263+
{
264+
// No importmap found, nothing to do.
265+
output.SuppressOutput();
266+
return;
264267
}
265268

269+
output.Content.SetHtmlContent(importMap.ToString());
266270
output.TagName = "script";
267271
output.TagMode = TagMode.StartTagAndEndTag;
268272
output.Attributes.SetAttribute("type", "importmap");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ public async Task ScriptTagHelper_PreservesExplicitImportMapContent_WhenUserProv
804804
var childContent = new DefaultTagHelperContent();
805805
childContent.SetHtmlContent(@"{""imports"":{""jquery"":""https://code.jquery.com/jquery.js""}}");
806806

807-
var output = MakeTagHelperOutput("script", attributes: [], childContent: childContent);
807+
var output = MakeTagHelperOutput("script", attributes: [new TagHelperAttribute("type", "importmap")], childContent: childContent);
808808

809809
var helper = GetHelper();
810810
helper.Type = "importmap";

0 commit comments

Comments
 (0)