Skip to content

Commit 2fbe331

Browse files
github-actions[bot]CopilotMackinnonBuckjaviercn
authored
[release/10.0] Fix ScriptTagHelper regression by checking for existing content before processing importmaps (#63631)
* Initial plan * Fix script tag importmap regression - preserve user content when no asp-importmap Co-authored-by: MackinnonBuck <[email protected]> * Fix ScriptTagHelper importmap regression by checking for existing content Co-authored-by: javiercn <[email protected]> * Manual fixes * Handle other tag helper setting content * Update PublicAPI.Unshipped.txt * Use `ProcessAsync()` in tests --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: MackinnonBuck <[email protected]> Co-authored-by: javiercn <[email protected]> Co-authored-by: Mackinnon Buck <[email protected]>
1 parent f940be1 commit 2fbe331

File tree

3 files changed

+119
-50
lines changed

3 files changed

+119
-50
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
*REMOVED*~override Microsoft.AspNetCore.Mvc.TagHelpers.ScriptTagHelper.Process(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext context, Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput output) -> void
3+
~override Microsoft.AspNetCore.Mvc.TagHelpers.ScriptTagHelper.ProcessAsync(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext context, Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput output) -> System.Threading.Tasks.Task

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,29 @@ private StringWriter StringWriter
235235
}
236236

237237
/// <inheritdoc />
238-
public override void Process(TagHelperContext context, TagHelperOutput output)
238+
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
239239
{
240240
ArgumentNullException.ThrowIfNull(context);
241241
ArgumentNullException.ThrowIfNull(output);
242242

243243
if (string.Equals(Type, "importmap", StringComparison.OrdinalIgnoreCase))
244244
{
245-
// This is an importmap script, we'll write out the import map and
246-
// stop processing.
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+
251+
// This is an importmap script, check if there's existing content first.
252+
var childContent = await output.GetChildContentAsync();
253+
if (!childContent.IsEmptyOrWhiteSpace)
254+
{
255+
// User provided existing content; preserve it.
256+
output.Content.SetHtmlContent(childContent);
257+
return;
258+
}
259+
260+
// No existing content, so we can apply import map logic.
247261
var importMap = ImportMap ?? ViewContext.HttpContext.GetEndpoint()?.Metadata.GetMetadata<ImportMapDefinition>();
248262
if (importMap == null)
249263
{
@@ -252,10 +266,10 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
252266
return;
253267
}
254268

269+
output.Content.SetHtmlContent(importMap.ToString());
255270
output.TagName = "script";
256271
output.TagMode = TagMode.StartTagAndEndTag;
257272
output.Attributes.SetAttribute("type", "importmap");
258-
output.Content.SetHtmlContent(importMap.ToString());
259273
return;
260274
}
261275

0 commit comments

Comments
 (0)