Exemplo n.º 1
0
        public void InjectsScriptTagReferencingAssemblyAndDependencies()
        {
            // Arrange
            var htmlTemplatePrefix = @"
                <html>
                <body>
                    <h1>Hello</h1>
                    Some text
                    <script>alert(1)</script>";
            var htmlTemplateSuffix = @"
                </body>
                </html>";
            var htmlTemplate       =
                $@"{htmlTemplatePrefix}
                    <script type='blazor-boot' custom1 custom2=""value"">some text that should be removed</script>
                {htmlTemplateSuffix}";
            var assemblyReferences = new string[] { "System.Abc.dll", "MyApp.ClassLib.dll", };
            var jsReferences       = new string[] { "some/file.js", "another.js" };
            var cssReferences      = new string[] { "my/styles.css" };
            var instance           = IndexHtmlWriter.GetIndexHtmlContents(
                htmlTemplate,
                "MyApp.Entrypoint",
                "MyNamespace.MyType::MyMethod",
                assemblyReferences,
                jsReferences,
                cssReferences,
                linkerEnabled: true);

            // Act & Assert: Start and end is not modified (including formatting)
            Assert.StartsWith(htmlTemplatePrefix, instance);
            Assert.EndsWith(htmlTemplateSuffix, instance);

            // Assert: Boot tag is correct
            var scriptTagText = instance.Substring(htmlTemplatePrefix.Length, instance.Length - htmlTemplatePrefix.Length - htmlTemplateSuffix.Length);
            var parsedHtml    = new HtmlParser().Parse("<html><body>" + scriptTagText + "</body></html>");
            var scriptElems   = parsedHtml.Body.QuerySelectorAll("script");
            var linkElems     = parsedHtml.Body.QuerySelectorAll("link");
            var scriptElem    = scriptElems[0];

            Assert.False(scriptElem.HasChildNodes);
            Assert.Equal("_framework/blazor.js", scriptElem.GetAttribute("src"));
            Assert.Equal("MyApp.Entrypoint.dll", scriptElem.GetAttribute("main"));
            Assert.Equal("MyNamespace.MyType::MyMethod", scriptElem.GetAttribute("entrypoint"));
            Assert.Equal("System.Abc.dll,MyApp.ClassLib.dll", scriptElem.GetAttribute("references"));
            Assert.False(scriptElem.HasAttribute("type"));
            Assert.Equal(string.Empty, scriptElem.Attributes["custom1"].Value);
            Assert.Equal("value", scriptElem.Attributes["custom2"].Value);
            Assert.Equal("true", scriptElem.Attributes["linker-enabled"].Value);

            // Assert: Also contains script tags referencing JS files
            Assert.Equal(
                scriptElems.Skip(1).Select(tag => tag.GetAttribute("src")),
                jsReferences);

            // Assert: Also contains link tags referencing CSS files
            Assert.Equal(
                linkElems.Select(tag => tag.GetAttribute("href")),
                cssReferences);
        }
Exemplo n.º 2
0
        public static void Command(CommandLineApplication command)
        {
            var clientPage = command.Option("--html-page",
                                            "Path to the HTML Page containing the Blazor bootstrap script tag.",
                                            CommandOptionType.SingleValue);

            var references = command.Option("--reference",
                                            "The path from the _bin folder to a given referenced dll file (Typically just the dll name)",
                                            CommandOptionType.MultipleValue);

            var jsReferences = command.Option("--js",
                                              "Adds a <script> tag with the specified 'src' value",
                                              CommandOptionType.MultipleValue);

            var cssReferences = command.Option("--css",
                                               "Adds a <link rel=stylesheet> tag with the specified 'href' value",
                                               CommandOptionType.MultipleValue);

            var outputPath = command.Option("--output",
                                            "Path to the output file",
                                            CommandOptionType.SingleValue);

            var mainAssemblyPath = command.Argument("assembly",
                                                    "Path to the assembly containing the entry point of the application.");

            var linkerEnabledFlag = command.Option("--linker-enabled",
                                                   "If set, specifies that the application is being built with linking enabled.",
                                                   CommandOptionType.NoValue);

            command.OnExecute(() =>
            {
                if (string.IsNullOrEmpty(mainAssemblyPath.Value) ||
                    !clientPage.HasValue() || !references.HasValue() || !outputPath.HasValue())
                {
                    command.ShowHelp(command.Name);
                    return(1);
                }

                try
                {
                    IndexHtmlWriter.UpdateIndex(
                        clientPage.Value(),
                        mainAssemblyPath.Value,
                        references.Values.ToArray(),
                        jsReferences.Values.ToArray(),
                        cssReferences.Values.ToArray(),
                        linkerEnabledFlag.HasValue(),
                        outputPath.Value());
                    return(0);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"ERROR: {ex.Message}");
                    Console.WriteLine(ex.StackTrace);
                    return(1);
                }
            });
        }
Exemplo n.º 3
0
        public static void Command(CommandLineApplication command)
        {
            var clientPage = command.Option("--html-page",
                                            "Path to the HTML Page containing the Blazor bootstrap script tag.",
                                            CommandOptionType.SingleValue);

            var references = command.Option("--reference",
                                            "The path from the _bin folder to a given referenced dll file (typically just the dll name)",
                                            CommandOptionType.MultipleValue);

            var embeddedResourcesFile = command.Option("--embedded-resources",
                                                       "The path to a file that lists the paths of .NET assemblies that may contain embedded resources (typically, referenced assemblies in their pre-linked states)",
                                                       CommandOptionType.SingleValue);

            var outputPath = command.Option("--output",
                                            "Path to the output file",
                                            CommandOptionType.SingleValue);

            var mainAssemblyPath = command.Argument("assembly",
                                                    "Path to the assembly containing the entry point of the application.");

            var linkerEnabledFlag = command.Option("--linker-enabled",
                                                   "If set, specifies that the application is being built with linking enabled.",
                                                   CommandOptionType.NoValue);

            command.OnExecute(() =>
            {
                if (string.IsNullOrEmpty(mainAssemblyPath.Value) ||
                    !clientPage.HasValue() || !references.HasValue() || !outputPath.HasValue())
                {
                    command.ShowHelp(command.Name);
                    return(1);
                }

                try
                {
                    var embeddedResourcesSources = embeddedResourcesFile.HasValue()
                        ? File.ReadAllLines(embeddedResourcesFile.Value())
                        : Array.Empty <string>();

                    IndexHtmlWriter.UpdateIndex(
                        clientPage.Value(),
                        mainAssemblyPath.Value,
                        references.Values.ToArray(),
                        embeddedResourcesSources,
                        linkerEnabledFlag.HasValue(),
                        outputPath.Value());
                    return(0);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"ERROR: {ex.Message}");
                    Console.WriteLine(ex.StackTrace);
                    return(1);
                }
            });
        }
Exemplo n.º 4
0
        public void SuppliesHtmlTemplateUnchangedIfNoBootScriptPresent()
        {
            // Arrange
            var htmlTemplate       = "<!DOCTYPE html><html><body><h1 style='color:red'>Hello</h1>Some text<script type='irrelevant'>blah</script></body></html>";
            var assemblyReferences = new string[] { "System.Abc.dll", "MyApp.ClassLib.dll" };
            var jsReferences       = new string[] { "some/file.js", "another.js" };
            var cssReferences      = new string[] { "my/styles.css" };

            var content = IndexHtmlWriter.GetIndexHtmlContents(
                htmlTemplate, "MyApp.Entrypoint", "MyNamespace.MyType::MyMethod", assemblyReferences, jsReferences, cssReferences, linkerEnabled: true, reloadUri: "/my/reload");

            // Assert
            Assert.Equal(htmlTemplate, content);
        }
        public void SuppliesHtmlTemplateUnchangedIfNoBootScriptPresent()
        {
            // Arrange
            var htmlTemplate = "<!DOCTYPE html><html><body><h1 style='color:red'>Hello</h1>Some text<script type='irrelevant'>blah</script></body></html>";
            var dependencies = new string[]
            {
                "System.Abc.dll",
                "MyApp.ClassLib.dll",
            };

            var content = IndexHtmlWriter.GetIndexHtmlContents(
                htmlTemplate, "MyApp.Entrypoint", "MyNamespace.MyType::MyMethod", dependencies);

            // Assert
            Assert.Equal(htmlTemplate, content);
        }
Exemplo n.º 6
0
        public void InjectsAdditionalTagsForEmbeddedContent()
        {
            // Arrange
            var htmlTemplate    = "Start <script id='testboot' type='blazor-boot'></script> End";
            var embeddedContent = new[]
            {
                new EmbeddedResourceInfo(EmbeddedResourceKind.Static, "my/static/file"),
                new EmbeddedResourceInfo(EmbeddedResourceKind.Css, "css/first.css"),
                new EmbeddedResourceInfo(EmbeddedResourceKind.JavaScript, "javascript/first.js"),
                new EmbeddedResourceInfo(EmbeddedResourceKind.Css, "css/second.css"),
                new EmbeddedResourceInfo(EmbeddedResourceKind.JavaScript, "javascript/second.js"),
            };

            // Act
            var resultHtml = IndexHtmlWriter.GetIndexHtmlContents(
                htmlTemplate,
                "MyApp.Entrypoint",
                "MyNamespace.MyType::MyMethod",
                assemblyReferences: new[] { "Something.dll" },
                embeddedContent: embeddedContent,
                linkerEnabled: true);

            // Assert
            var parsedHtml       = new HtmlParser().Parse(resultHtml);
            var blazorBootScript = parsedHtml.GetElementById("testboot");

            Assert.NotNull(blazorBootScript);
            Assert.Equal(
                "Start "
                + blazorBootScript.OuterHtml
                // First we insert the CSS file tags in order
                + Environment.NewLine + "<link rel=\"stylesheet\" href=\"css/first.css\" />"
                + Environment.NewLine + "<link rel=\"stylesheet\" href=\"css/second.css\" />"
                // Then the JS file tags in order, each with 'defer'
                + Environment.NewLine + "<script src=\"javascript/first.js\" defer></script>"
                + Environment.NewLine + "<script src=\"javascript/second.js\" defer></script>"
                + " End",
                resultHtml);
        }
Exemplo n.º 7
0
        public static void Command(CommandLineApplication command)
        {
            var clientPage = command.Option("-hp|--html-page",
                                            "Path to the HTML Page containing the Blazor bootstrap script tag.",
                                            CommandOptionType.SingleValue);

            var references = command.Option("-r|--reference",
                                            "The path from the _bin folder to a given referenced dll file (Typically just the dll name)",
                                            CommandOptionType.MultipleValue);

            var outputPath = command.Option("-o|--output",
                                            "Path to the output file",
                                            CommandOptionType.SingleValue);

            var mainAssemblyPath = command.Argument("assembly",
                                                    "Path to the assembly containing the entry point of the application.");

            command.OnExecute(() =>
            {
                if (string.IsNullOrEmpty(mainAssemblyPath.Value) ||
                    !clientPage.HasValue() || !references.HasValue() || !outputPath.HasValue())
                {
                    command.ShowHelp(command.Name);
                    return(1);
                }

                try
                {
                    IndexHtmlWriter.UpdateIndex(clientPage.Value(), mainAssemblyPath.Value, references.Values.ToArray(), outputPath.Value());
                    return(0);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"ERROR: {ex.Message}");
                    Console.WriteLine(ex.StackTrace);
                    return(1);
                }
            });
        }
Exemplo n.º 8
0
        public void OmitsReloadAttributeIfNoReloadUriSpecified()
        {
            // Arrange
            var htmlTemplate       = "<script type='blazor-boot'></script>";
            var assemblyReferences = new string[] { "System.Abc.dll", "MyApp.ClassLib.dll", };

            // Act
            var fileContents = IndexHtmlWriter.GetIndexHtmlContents(
                htmlTemplate,
                "MyApp.Entrypoint",
                "MyNamespace.MyType::MyMethod",
                assemblyReferences,
                /* js references */ new string[] {},
                /* js references */ new string[] {},
                /* linkerEnabled */ true,
                /* reloadUri */ null);

            // Assert
            var parsedHtml = new HtmlParser().Parse(fileContents);
            var scriptElem = parsedHtml.QuerySelector("script");

            Assert.False(scriptElem.HasAttribute("reload"));
        }