From b38718d77f5fb0615fab2ea65647606588513c08 Mon Sep 17 00:00:00 2001 From: Eugene Bekker Date: Fri, 9 Feb 2018 09:33:16 -0500 Subject: [PATCH] Require diff --git a/samples/MonoSanity/wwwroot/index.html b/samples/MonoSanity/wwwroot/index.html index dc01913f85..3dd60e581d 100644 --- a/samples/MonoSanity/wwwroot/index.html +++ b/samples/MonoSanity/wwwroot/index.html @@ -57,6 +57,7 @@

Loading...

+ diff --git a/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Boot.ts b/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Boot.ts index dcbe130563..d1e59ffd78 100644 --- a/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Boot.ts +++ b/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Boot.ts @@ -6,10 +6,10 @@ import './GlobalExports'; async function boot() { // Read startup config from the ) - // and then let the developer manually place the tag that loads blazor.js - // wherever they want (adding their own async/defer if they want). - return htmlTemplate - .Replace("", "\n" + CreateBootMarkup(assemblyName, binFiles)); + var parser = new HtmlParser(); + var dom = parser.Parse(htmlTemplate); + + // First see if the user has declared a 'boot' script, + // then it's their responsibility to load blazor.js + var bootScript = dom.Body?.QuerySelectorAll("script") + .Where(x => x.Attributes["type"]?.Value == "blazor-boot").FirstOrDefault(); + + // If we find a script tag that is decorated with a type="blazor-boot" + // this will be the point at which we start the Blazor boot process + if (bootScript != null) + { + // We need to remove the 'type="blazor-boot"' so that + // it reverts to being processed as JS by the browser + bootScript.RemoveAttribute("type"); + + // Leave any user-specified attributes on the tag as-is + // and add/overwrite the config data needed to boot Blazor + InjectBootConfig(bootScript, assemblyName, binFiles); + } + + // If no blazor-boot script tag was found, we skip it and + // leave it up to the user to handle kicking off the boot + + using (var writer = new StringWriter()) + { + dom.ToHtml(writer, new AutoSelectedMarkupFormatter()); + return writer.ToString(); + } } - private static string CreateBootMarkup(string assemblyName, IEnumerable binFiles) + private static void InjectBootConfig(IElement script, string assemblyName, IEnumerable binFiles) { var assemblyNameWithExtension = $"{assemblyName}.dll"; var referenceNames = binFiles @@ -54,9 +89,9 @@ namespace Microsoft.AspNetCore.Blazor.Build.Core.FileSystem .Select(file => file.Name); var referencesAttribute = string.Join(",", referenceNames.ToArray()); - return $""; + script.SetAttribute("src", "/_framework/blazor.js"); + script.SetAttribute("main", assemblyNameWithExtension); + script.SetAttribute("references", referencesAttribute); } } } diff --git a/test/Microsoft.AspNetCore.Blazor.Build.Test/IndexHtmlFileProviderTest.cs b/test/Microsoft.AspNetCore.Blazor.Build.Test/IndexHtmlFileProviderTest.cs index 803f5c5aac..c86632eb0b 100644 --- a/test/Microsoft.AspNetCore.Blazor.Build.Test/IndexHtmlFileProviderTest.cs +++ b/test/Microsoft.AspNetCore.Blazor.Build.Test/IndexHtmlFileProviderTest.cs @@ -32,6 +32,7 @@ namespace Microsoft.AspNetCore.Blazor.Server.Test { // Arrange var htmlTemplate = "test"; + var htmlOutput = $"{htmlTemplate}"; var instance = new IndexHtmlFileProvider( htmlTemplate, "fakeassembly", Enumerable.Empty()); @@ -43,8 +44,8 @@ namespace Microsoft.AspNetCore.Blazor.Server.Test Assert.False(file.IsDirectory); Assert.Equal("/index.html", file.PhysicalPath); Assert.Equal("index.html", file.Name); - Assert.Equal(htmlTemplate, ReadString(file)); - Assert.Equal(htmlTemplate.Length, file.Length); + Assert.Equal(htmlOutput, ReadString(file)); + Assert.Equal(htmlOutput.Length, file.Length); } [Fact] @@ -65,7 +66,36 @@ namespace Microsoft.AspNetCore.Blazor.Server.Test } [Fact] - public void InsertsScriptTagReferencingAssemblyAndDependencies() + public void InjectsScriptTagReferencingAssemblyAndDependencies() + { + // Arrange + var htmlTemplate = "

Hello

Some text"; + var dependencies = new IFileInfo[] + { + new TestFileInfo("System.Abc.dll"), + new TestFileInfo("MyApp.ClassLib.dll"), + }; + var instance = new IndexHtmlFileProvider( + htmlTemplate, "MyApp.Entrypoint", dependencies); + + // Act + var file = instance.GetFileInfo("/index.html"); + var parsedHtml = new HtmlParser().Parse(ReadString(file)); + var firstElem = parsedHtml.Body.FirstElementChild; + var scriptElem = parsedHtml.Body.QuerySelector("script"); + + // Assert + Assert.Equal("h1", firstElem.TagName.ToLowerInvariant()); + Assert.Equal("script", scriptElem.TagName.ToLowerInvariant()); + Assert.False(scriptElem.HasChildNodes); + Assert.Equal("/_framework/blazor.js", scriptElem.GetAttribute("src")); + Assert.Equal("MyApp.Entrypoint.dll", scriptElem.GetAttribute("main")); + Assert.Equal("System.Abc.dll,MyApp.ClassLib.dll", scriptElem.GetAttribute("references")); + Assert.False(scriptElem.HasAttribute("type")); + } + + [Fact] + public void MissingBootScriptTagReferencingAssemblyAndDependencies() { // Arrange var htmlTemplate = "

Hello

Some text"; @@ -80,14 +110,13 @@ namespace Microsoft.AspNetCore.Blazor.Server.Test // Act var file = instance.GetFileInfo("/index.html"); var parsedHtml = new HtmlParser().Parse(ReadString(file)); - var scriptElem = parsedHtml.Body.FirstElementChild; + var firstElem = parsedHtml.Body.FirstElementChild; + var scriptElem = parsedHtml.Body.QuerySelector("script"); + // Assert - Assert.Equal("script", scriptElem.TagName.ToLowerInvariant()); - Assert.False(scriptElem.HasChildNodes); - Assert.Equal("/_framework/blazor.js", scriptElem.GetAttribute("src")); - Assert.Equal("MyApp.Entrypoint.dll", scriptElem.GetAttribute("main")); - Assert.Equal("System.Abc.dll,MyApp.ClassLib.dll", scriptElem.GetAttribute("references")); + Assert.Equal("h1", firstElem.TagName.ToLowerInvariant()); + Assert.Null(scriptElem); } private static string ReadString(IFileInfo file) diff --git a/test/testapps/BasicTestApp/wwwroot/index.html b/test/testapps/BasicTestApp/wwwroot/index.html index c8858528a2..b5d5ef0fab 100644 --- a/test/testapps/BasicTestApp/wwwroot/index.html +++ b/test/testapps/BasicTestApp/wwwroot/index.html @@ -7,6 +7,7 @@ Loading... +