Beispiel #1
0
        public void GetEmbeddedJavaScript_PreparesJavaScriptCorrectly(string resource)
        {
            // Arrange
            var expected = resource.Substring(0, resource.Length - 2);
            var stream   = new MemoryStream(Encoding.UTF8.GetBytes(resource));
            var getManifestResourceStream = new Func <string, Stream>(name => stream);
            var cache = new ConcurrentDictionary <string, string>();

            // Act
            var result = JavaScriptResources.GetEmbeddedJavaScript("test.js", getManifestResourceStream, cache);

            // Assert
            Assert.Equal(expected, result);
        }
Beispiel #2
0
        public void GetEmbeddedJavaScript_LoadsEmbeddedResourceFromManifestStream()
        {
            // Arrange
            var resource = "window.alert('An alert');";
            var expected = resource.Substring(0, resource.Length - 2);
            var stream   = new MemoryStream(Encoding.UTF8.GetBytes(resource));
            var getManifestResourceStream = new Func <string, Stream>(name => stream);
            var cache = new ConcurrentDictionary <string, string>();

            // Act
            var result = JavaScriptResources.GetEmbeddedJavaScript("test.js", getManifestResourceStream, cache);

            // Assert
            Assert.Equal(expected, result);
        }
Beispiel #3
0
        public void GetEmbeddedJavaScript_AddsResourceToCacheWhenRead()
        {
            // Arrange
            var resource = "window.alert('An alert');";
            var expected = resource.Substring(0, resource.Length - 2);
            var stream   = new MemoryStream(Encoding.UTF8.GetBytes(resource));
            var getManifestResourceStream = new Func <string, Stream>(name => stream);
            var cache = new ConcurrentDictionary <string, string>();

            // Act
            var result = JavaScriptResources.GetEmbeddedJavaScript("test.js", getManifestResourceStream, cache);

            // Assert
            Assert.Collection(cache, kvp =>
            {
                Assert.Equal("test.js", kvp.Key);
                Assert.Equal(expected, kvp.Value);
            });
        }
Beispiel #4
0
        public void GetEmbeddedJavaScript_LoadsResourceFromCacheAfterInitialCall()
        {
            // Arrange
            var resource  = "window.alert('An alert');";
            var stream    = new MemoryStream(Encoding.UTF8.GetBytes(resource));
            var callCount = 0;
            var getManifestResourceStream = new Func <string, Stream>(name =>
            {
                callCount++;
                return(stream);
            });
            var cache = new ConcurrentDictionary <string, string>();

            // Act
            var result = JavaScriptResources.GetEmbeddedJavaScript("test.js", getManifestResourceStream, cache);

            result = JavaScriptResources.GetEmbeddedJavaScript("test.js", getManifestResourceStream, cache);

            // Assert
            Assert.Equal(1, callCount);
        }
        private void BuildFallbackBlock(TagHelperContent builder)
        {
            EnsureGlobbingUrlBuilder();
            var fallbackHrefs = GlobbingUrlBuilder.BuildUrlList(
                FallbackHref,
                FallbackHrefInclude,
                FallbackHrefExclude);

            if (fallbackHrefs.Count == 0)
            {
                return;
            }

            builder.AppendHtml(HtmlString.NewLine);

            // Build the <meta /> tag that's used to test for the presence of the stylesheet
            builder
            .AppendHtml("<meta name=\"x-stylesheet-fallback-test\" content=\"\" class=\"")
            .Append(FallbackTestClass)
            .AppendHtml("\" />");

            // Build the <script /> tag that checks the effective style of <meta /> tag above and renders the extra
            // <link /> tag to load the fallback stylesheet if the test CSS property value is found to be false,
            // indicating that the primary stylesheet failed to load.
            // GetEmbeddedJavaScript returns JavaScript to which we add '"{0}","{1}",{2});'
            builder
            .AppendHtml("<script>")
            .AppendHtml(JavaScriptResources.GetEmbeddedJavaScript(FallbackJavaScriptResourceName))
            .AppendHtml("\"")
            .AppendHtml(JavaScriptEncoder.Encode(FallbackTestProperty))
            .AppendHtml("\",\"")
            .AppendHtml(JavaScriptEncoder.Encode(FallbackTestValue))
            .AppendHtml("\",");

            AppendFallbackHrefs(builder, fallbackHrefs);
            builder.AppendHtml("</script>");
        }
Beispiel #6
0
        private void BuildFallbackBlock(TagHelperAttributeList attributes, TagHelperContent builder)
        {
            EnsureGlobbingUrlBuilder();
            var fallbackHrefs = GlobbingUrlBuilder.BuildUrlList(
                FallbackHref,
                FallbackHrefInclude,
                FallbackHrefExclude);

            if (fallbackHrefs.Count == 0)
            {
                return;
            }

            builder.AppendHtml(HtmlString.NewLine);

            // Build the <meta /> tag that's used to test for the presence of the stylesheet
            builder
            .AppendHtml("<meta name=\"x-stylesheet-fallback-test\" content=\"\" class=\"")
            .Append(FallbackTestClass)
            .AppendHtml("\" />");

            // Build the <script /> tag that checks the effective style of <meta /> tag above and renders the extra
            // <link /> tag to load the fallback stylesheet if the test CSS property value is found to be false,
            // indicating that the primary stylesheet failed to load.
            // GetEmbeddedJavaScript returns JavaScript to which we add '"{0}","{1}",{2});'
            builder
            .AppendHtml("<script>")
            .AppendHtml(JavaScriptResources.GetEmbeddedJavaScript(FallbackJavaScriptResourceName))
            .AppendHtml("\"")
            .AppendHtml(JavaScriptEncoder.Encode(FallbackTestProperty))
            .AppendHtml("\",\"")
            .AppendHtml(JavaScriptEncoder.Encode(FallbackTestValue))
            .AppendHtml("\",");

            AppendFallbackHrefs(builder, fallbackHrefs);

            builder.AppendHtml(", \"");

            // Perf: Avoid allocating enumerator
            for (var i = 0; i < attributes.Count; i++)
            {
                var attribute = attributes[i];
                if (string.Equals(attribute.Name, HrefAttributeName, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (SuppressFallbackIntegrity && string.Equals(attribute.Name, IntegrityAttributeName, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                attribute.WriteTo(StringWriter, HtmlEncoder);
                StringWriter.Write(' ');
            }

            var stringBuilder = StringWriter.GetStringBuilder();
            var scriptTags    = stringBuilder.ToString();

            stringBuilder.Clear();
            var encodedScriptTags = JavaScriptEncoder.Encode(scriptTags);

            builder.AppendHtml(encodedScriptTags);

            builder.AppendHtml("\");</script>");
        }