Merge pull request #4924 from sbwalker/dev

refactor Static Blazor script processing
This commit is contained in:
Shaun Walker
2024-12-18 10:09:20 -05:00
committed by GitHub
4 changed files with 160 additions and 193 deletions

View File

@ -58,27 +58,22 @@
var index = headcontent.IndexOf("<script");
while (index >= 0)
{
var script = headcontent.Substring(index, headcontent.IndexOf("</script>") + 9 - index);
var script = headcontent.Substring(index, headcontent.IndexOf("</script>", index) - index + 9);
if (RenderMode == RenderModes.Interactive)
{
// remove scripts when interactive rendering
headcontent = headcontent.Remove(index, script.Length);
index = headcontent.IndexOf("<script");
}
else
{
// transform scripts into page-scripts when static rendering
var pagescript = script.Replace("script", "page-script");
if (!pagescript.Contains("><"))
if (!script.Contains("><") && !script.Contains("data-reload"))
{
// convert inline script body to content attribute
var content = pagescript.Substring(pagescript.IndexOf(">") + 1, pagescript.LastIndexOf("<") - pagescript.IndexOf(">") - 1);
pagescript = pagescript.Replace(">" + content, " content=\"" + content.Replace("\"","'") + "\">");
// add data-reload attribute to inline script
headcontent = headcontent.Replace(script, script.Replace("<script", "<script data-reload=\"true\""));
}
// move page-script to end of headcontent as Blazor will move anything after a page-script element to the top of the body
headcontent = headcontent.Replace(script, "");
headcontent = headcontent + pagescript;
index = headcontent.IndexOf("<script", index + 1);
}
index = headcontent.IndexOf("<script");
}
}
return headcontent;