Esempio n. 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);
    }
Esempio n. 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);
    }
Esempio n. 3
0
        private void BuildFallbackBlock(TagHelperContent builder)
        {
            EnsureGlobbingUrlBuilder();
            var fallbackHrefs =
                GlobbingUrlBuilder.BuildUrlList(FallbackHref, FallbackHrefInclude, FallbackHrefExclude).ToArray();

            if (fallbackHrefs.Length > 0)
            {
                if (AppendVersion == true)
                {
                    for (var i = 0; i < fallbackHrefs.Length; i++)
                    {
                        // fallbackHrefs come from bound attributes and globbing. Must always be non-null.
                        Debug.Assert(fallbackHrefs[i] != null);

                        fallbackHrefs[i] = _fileVersionProvider.AddFileVersionToPath(fallbackHrefs[i]);
                    }
                }

                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("\",")
                .AppendHtml(JavaScriptStringArrayEncoder.Encode(JavaScriptEncoder, fallbackHrefs))
                .AppendHtml(");</script>");
            }
        }
Esempio n. 4
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);
        });
    }
Esempio n. 5
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);
    }
Esempio n. 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 and read interface .Count once rather than per iteration
        var attributesCount = attributes.Count;

        for (var i = 0; i < attributesCount; 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>");
    }