Наследование: ICSSAssetsFileHasher
Пример #1
0
        public void DoesNotAppendHashIfFileDoesNotExists()
        {
            var hashQueryStringKeyName = "v";
            var fileResolver = new FileSystemResolver();
            var hasher = new StubHasher("hash");
            var pathTranslator = new PathTranslator();

            var cssFilePath = TestUtilities.PreparePath(@"C:\somepath\output.css");
            var url = "/doesnotexist.jpg";

            var assetsFileHasher = new CSSAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher, pathTranslator);

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(url));
        }
Пример #2
0
        public void DoesNotAppendHashIfFileIsRemote()
        {
            var hashQueryStringKeyName = "v";
            var fileResolver = new FileSystemResolver();
            var hasher = new StubHasher("hash");
            var pathTranslator = new PathTranslator();

            var cssFilePath = @"C:\somepath\output.css";
            var url = "http://www.test.com/image.jpg";

            var assetsFileHasher = new CSSAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher, pathTranslator);

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(url));
        }
Пример #3
0
        public void CanAppendFileHashToRelativeUri()
        {
            var hashQueryStringKeyName = "v";
            var hashValue = "hashValue";
            var hasher = new StubHasher(hashValue);
            var fileResolver = new FileSystemResolver();
            var uri = Assembly.GetExecutingAssembly().CodeBase;
            var cssFilePath = Path.GetDirectoryName(uri) + TestUtilities.PreparePath(@"\subdirectory\output.css");
            var url = "../" + Path.GetFileName(uri);
            var assetsFileHasher = new CSSAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);

            var expectedUrl = url + "?" + hashQueryStringKeyName + "=" + hashValue;

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(expectedUrl));
        }
Пример #4
0
        public void CanAppendFileHashToAbsoluteUriWithAnExistingQueryString()
        {
            var hashQueryStringKeyName = "v";
            var hashValue = "hashValue";
            var hasher = new StubHasher(hashValue);
            var uri = Assembly.GetExecutingAssembly().CodeBase;
            var cssFilePath = Path.GetDirectoryName(uri) + @"\output.css";
            var url = "/" + Path.GetFileName(uri) + "?test=value";
            var pathToResolveTo = Assembly.GetExecutingAssembly().Location;
            var fileResolver = StubResolver.ForFile(pathToResolveTo);
            var assetsFileHasher = new CSSAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher);

            var expectedUrl = url + "&" + hashQueryStringKeyName + "=" + hashValue;

            var rewrittenUrl = assetsFileHasher.AppendFileHash(cssFilePath, url);

            Assert.That(rewrittenUrl, Is.EqualTo(expectedUrl));
        }
Пример #5
0
        string ProcessCssFile(string file, string outputFile, string fileForCssRewriter, bool asImport = false)
        {
            var preprocessors = FindPreprocessors(file);

            var css = preprocessors.NullSafeAny()
                ? PreprocessFile(file, preprocessors)
                : ReadFile(file);

            if (ShouldImport)
            {
                css = ProcessImport(file, outputFile, css);
            }

            ICSSAssetsFileHasher fileHasher = null;

            if (ShouldAppendHashForAssets)
            {
                var fileResolver = new FileSystemResolver();
                fileHasher = new CSSAssetsFileHasher(bundleState.HashKeyName, fileResolver, hasher, pathTranslator);
            }

            return(CSSPathRewriter.RewriteCssPaths(outputFile, fileForCssRewriter, css, fileHasher, asImport));
        }
        public void WontThrowPathTooLongExceptionForLongDataUrls()
        {
            //
            // Base64 images can throw Windows PathTooLong exceptions (limit of 260 characters)
            // when present in a CSS file with non-base64 relative URLs.
            //

            var cssAssetsFileHasher = new CSSAssetsFileHasher(string.Empty, new FileSystemResolver(), null, null);
            
            string css =
                @"
                                                        .header {
                                                                background: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/PgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2ZlZmZmZiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNmMmYyZjIiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+') no-repeat 0 0;
                                                                background: url('fake.png');
                                                        }
                                                    ";
            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\output.css");
            string result = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher, new PathTranslator());

            string expected =
                @"
                                                        .header {
                                                                background: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/PgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2ZlZmZmZiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNmMmYyZjIiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+') no-repeat 0 0;
                                                                background: url('fake.png');
                                                        }
                                                    ";
            Assert.AreEqual(expected, result);
        }