public void WontRewriteAbsolutePaths()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .header {
                                                                background-image: url(http://www.somewhere.com/img/something.jpg);
                                                        }

                                                        .footer {
                                                                background-image: url(http://www.somewhere.com/img/blah/somethingelse.jpg);
                                                        }
                                                    ";
            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\someothersubpath\evendeeper\output.css");
            string result     = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher);

            string expected =
                @"
                                                        .header {
                                                                background-image: url(http://www.somewhere.com/img/something.jpg);
                                                        }

                                                        .footer {
                                                                background-image: url(http://www.somewhere.com/img/blah/somethingelse.jpg);
                                                        }
                                                    ";

            Assert.AreEqual(expected, result);
        }
        public void CanRewritePathsInCssWithUppercaseUrlStatement()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .header {
                                                                background-image: URL(""../img/something.jpg"");
                                                        }

                                                        .footer {
                                                                background-image: uRL(""../img/blah/somethingelse.jpg"");
                                                        }
                                                    ";
            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\output.css");
            string result     = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher);

            string expected =
                @"
                                                        .header {
                                                                background-image: URL(""img/something.jpg"");
                                                        }

                                                        .footer {
                                                                background-image: uRL(""img/blah/somethingelse.jpg"");
                                                        }
                                                    ";

            Assert.AreEqual(expected, result);
        }
        public void CanRewritePathsInCssWhenMultipleOccurencesOfSameRelativePathAppearInOneCssFile()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .ui-icon { background-image: url(images/ui-icons_222222_256x240.png); }
                                                        .ui-widget-content .ui-icon {background-image: url(images/ui-icons_222222_256x240.png); }
                                                        .ui-widget-header .ui-icon {background-image: url(images/ui-icons_222222_256x240.png); }
                                                    ";
            //real example from jquery ui-generated custom css file

            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\someothersubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");

            string result = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher);

            string expected =
                @"
                                                        .ui-icon { background-image: url(someothersubpath/images/ui-icons_222222_256x240.png); }
                                                        .ui-widget-content .ui-icon {background-image: url(someothersubpath/images/ui-icons_222222_256x240.png); }
                                                        .ui-widget-header .ui-icon {background-image: url(someothersubpath/images/ui-icons_222222_256x240.png); }
                                                    ";

            //if it fails, it will look like this: url(someothersubpath/someothersubpath/someothersubpath/images/

            Assert.AreEqual(expected, result);
        }
        public void CanRewritePathsInCssWhenDifferentFoldersAtSameDepth()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .header {
                                                                background-image: url(../img/something.jpg);
                                                        }

                                                        .footer {
                                                                background-image: url(../img/blah/somethingelse.jpg);
                                                        }
                                                    ";
            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\someothersubpath\output.css");

            string result = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher);

            string expected =
                @"
                                                        .header {
                                                                background-image: url(../img/something.jpg);
                                                        }

                                                        .footer {
                                                                background-image: url(../img/blah/somethingelse.jpg);
                                                        }
                                                    ";

            Assert.AreEqual(expected, result);
        }
Example #5
0
        public void CanRewritePathsInCssWithSingleQuotes()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .header {
                                                                background-image: url('../img/something.jpg');
                                                        }

                                                        .footer {
                                                                background-image: url('../img/blah/somethingelse.jpg');
                                                        }
                                                    ";
            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\output.css");
            string result     = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher, new PathTranslator());

            string expected =
                @"
                                                        .header {
                                                                background-image: url('img/something.jpg');
                                                        }

                                                        .footer {
                                                                background-image: url('img/blah/somethingelse.jpg');
                                                        }
                                                    ";

            Assert.AreEqual(expected, result);
        }
Example #6
0
        public void CanRewritePathsInCssWhenRelativePathsInsideOfSourceFolder()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .header {
                                                                background-image: url(img/something.jpg);
                                                        }

                                                        .footer {
                                                                background-image: url(img/blah/somethingelse.jpg);
                                                        }
                                                    ";
            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\someothersubpath\evendeeper\output.css");
            string result     = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher, new PathTranslator());

            string expected =
                @"
                                                        .header {
                                                                background-image: url(../../somesubpath/img/something.jpg);
                                                        }

                                                        .footer {
                                                                background-image: url(../../somesubpath/img/blah/somethingelse.jpg);
                                                        }
                                                    ";

            Assert.AreEqual(expected, result);
        }
Example #7
0
        public static string RewriteCssPaths(string outputPath, string sourcePath, string css, ICSSAssetsFileHasher cssAssetsFileHasher, bool asImport = false)
        {
            //see http://stackoverflow.com/questions/3692818/uri-makerelativeuri-behavior-on-mono
            if (FileSystem.Unix)
            {
                outputPath += "/";
            }

            var sourceDirectory = Path.GetDirectoryName(sourcePath) + "/";
            var outputUri = new Uri(Path.GetDirectoryName(outputPath) + "/", UriKind.Absolute);

            var relativePaths = FindDistinctRelativePathsIn(css);

            foreach (string relativePath in relativePaths)
            {
                var firstIndexOfHashOrQuestionMark = relativePath.IndexOfAny(new[] { '?', '#' });

                var segmentAfterHashOrQuestionMark = firstIndexOfHashOrQuestionMark >= 0
                    ? relativePath.Substring(firstIndexOfHashOrQuestionMark)
                    : string.Empty;

                var capturedRelativePath = segmentAfterHashOrQuestionMark != string.Empty
                    ? relativePath.Substring(0, firstIndexOfHashOrQuestionMark)
                    : relativePath;

                var resolvedSourcePathString = Path.Combine(sourceDirectory, capturedRelativePath);

                var resolvedSourcePath = new Uri(resolvedSourcePathString);

                var resolvedOutput = outputUri.MakeRelativePathTo(resolvedSourcePath);

                var newRelativePath = asImport ? "squishit://" + resolvedOutput : (resolvedOutput + segmentAfterHashOrQuestionMark);

                css = ReplaceRelativePathsIn(css, relativePath, newRelativePath);
            }

            if (!asImport)
            {
                css = css.Replace("squishit://", "");
            }

            if (cssAssetsFileHasher != null)
            {
                var localRelativePathsThatExist = FindDistinctLocalRelativePathsThatExist(css);

                foreach (string localRelativePathThatExist in localRelativePathsThatExist)
                {
                    var localRelativePathThatExistWithFileHash = cssAssetsFileHasher.AppendFileHash(outputPath, localRelativePathThatExist);

                    if (localRelativePathThatExist != localRelativePathThatExistWithFileHash)
                    {
                        css = css.Replace(localRelativePathThatExist, localRelativePathThatExistWithFileHash);
                    }
                }
            }
            return css;
        }
        public static string RewriteCssPaths(string outputPath, string sourcePath, string css, ICSSAssetsFileHasher cssAssetsFileHasher, IPathTranslator pathTranslator, bool asImport = false)
        {
            var relativePaths = FindDistinctRelativePathsIn(css);

            if (relativePaths.Any())
            {
                var relativeOutputPath = GetWebPath(outputPath, pathTranslator);
                var relativeSourcePath = GetWebPath(sourcePath, pathTranslator);

                var relativePathAdapter = RelativePathAdapter.Between(relativeOutputPath, relativeSourcePath);

                foreach (var relativePath in relativePaths)
                {
                    var firstIndexOfHashOrQuestionMark = relativePath.IndexOfAny(new[] {'?', '#'});

                    var segmentAfterHashOrQuestionMark = firstIndexOfHashOrQuestionMark >= 0
                                                             ? relativePath.Substring(firstIndexOfHashOrQuestionMark)
                                                             : string.Empty;

                    var capturedRelativePath = segmentAfterHashOrQuestionMark != string.Empty
                                                   ? relativePath.Substring(0, firstIndexOfHashOrQuestionMark)
                                                   : relativePath;

                    var resolvedOutput = relativePathAdapter.Adapt(capturedRelativePath);

                    var newRelativePath = (asImport ? "squishit://" : "") +  (resolvedOutput + segmentAfterHashOrQuestionMark);

                    css = ReplaceRelativePathsIn(css, relativePath, newRelativePath);
                }
            }

            //moved out of if block above so that root-relative paths can be hashed as well
            if(cssAssetsFileHasher != null)
            {
                var hashableAssetPaths = FindHashableAssetPaths(css);

                foreach(var hashableAssetPath in hashableAssetPaths)
                {
                    var assetPathWithHash = cssAssetsFileHasher.AppendFileHash(outputPath, hashableAssetPath);

                    if(hashableAssetPath != assetPathWithHash)
                    {
                        css = ReplaceRelativePathsIn(css, hashableAssetPath, assetPathWithHash);
                    }
                }
            }

            if(!asImport)
            {
                css = css.Replace("squishit://", "");
            }

            return css;
        }
Example #9
0
        public static string RewriteCssPaths(string outputPath, string sourcePath, string css, ICSSAssetsFileHasher cssAssetsFileHasher, bool asImport = false)
        {
            var relativePaths = FindDistinctRelativePathsIn(css);

            if (relativePaths.Any())
            {
                var relativePathAdapter = RelativePathAdapter.Between(outputPath, sourcePath);

                foreach (var relativePath in relativePaths)
                {
                    var firstIndexOfHashOrQuestionMark = relativePath.IndexOfAny(new[] {'?', '#'});

                    var segmentAfterHashOrQuestionMark = firstIndexOfHashOrQuestionMark >= 0
                                                             ? relativePath.Substring(firstIndexOfHashOrQuestionMark)
                                                             : string.Empty;

                    var capturedRelativePath = segmentAfterHashOrQuestionMark != string.Empty
                                                   ? relativePath.Substring(0, firstIndexOfHashOrQuestionMark)
                                                   : relativePath;

                    var resolvedOutput = relativePathAdapter.Adapt(capturedRelativePath);

                    var newRelativePath = (asImport ? "squishit://" : "") +  (resolvedOutput + segmentAfterHashOrQuestionMark);

                    css = ReplaceRelativePathsIn(css, relativePath, newRelativePath);
                }

                if (cssAssetsFileHasher != null)
                {
                    var localRelativePathsThatExist = FindDistinctLocalRelativePathsThatExist(css);

                    foreach (var localRelativePathThatExist in localRelativePathsThatExist)
                    {
                        var localRelativePathThatExistWithFileHash = cssAssetsFileHasher.AppendFileHash(outputPath,
                                                                                                        localRelativePathThatExist);

                        if (localRelativePathThatExist != localRelativePathThatExistWithFileHash)
                        {
                            css = css.Replace(localRelativePathThatExist, localRelativePathThatExistWithFileHash);
                        }
                    }
                }
            }

            if(!asImport)
            {
                css = css.Replace("squishit://", "");
            }

            return css;
        }
        public void DontThrowIfPathContainsRegexMetacharacters()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .header {
                                                                background-image: URL(""*../img/something.jpg"");
                                                                background-image: url(c:\documents\usr8\local\temp\1\d1b73b93-5ff0-11de-9339-0017317c60aa); 
                                                        }

                                                        .footer {
                                                                background-image: uRL(""..c:\1\2\somethingelse.jpg"");
                                                        }
                                                    ";
            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\output.css");

            //concrete result doesn't matter here (since input isn't valid)
            //the only important thing is that it shouldn't throw exceptions
            Assert.DoesNotThrow(() => CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher));
        }
        public void WontRewriteBehaviorUrls()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .header {
                                                                behavior: url('somethingorother') no-repeat 0 0;
                                                        }
                                                    ";
            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\someothersubpath\evendeeper\output.css");
            string result     = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher);

            string expected =
                @"
                                                        .header {
                                                                behavior: url('somethingorother') no-repeat 0 0;
                                                        }
                                                    ";

            Assert.AreEqual(expected, result);
        }
        public void WontRewriteDataUrls()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .header {
                                                                background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...') no-repeat 0 0;
                                                        }
                                                    ";
            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\someothersubpath\evendeeper\output.css");
            string result     = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher);

            string expected =
                @"
                                                        .header {
                                                                background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...') no-repeat 0 0;
                                                        }
                                                    ";

            Assert.AreEqual(expected, result);
        }
        public void DontThrowIfPathIsEmpty()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .header {
                                                                background-image: URL("""");
                                                                background-image: url(); 
                                                        }

                                                        .footer {
                                                                background-image: uRL("""");
                                                                background-image: UrL('');
                                                        }
                                                    ";
            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\output.css");

            //concrete result doesn't matter here (since input isn't valid)
            //the only important thing is that it shouldn't throw exceptions
            Assert.DoesNotThrow(() => CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher));
        }
Example #14
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 CanRewritePathsInCssWhenMultipleOccurencesOfSameRelativePathAppearInOneCssFileWithDifferentCasing()
        {
            ICSSAssetsFileHasher cssAssetsFileHasher = null;
            string css =
                @"
                                                        .ui-icon { background-image: url(images/ui-icons_222222_256x240.png); }
                                                        .ui-widget-content .ui-icon {background-image: url(Images/ui-icons_222222_256x240.png); }
                                                        .ui-widget-header .ui-icon {background-image: url(iMages/ui-icons_222222_256x240.png); }
                                                    ";

            string sourceFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\someothersubpath\myfile.css");
            string targetFile = TestUtilities.PreparePath(@"C:\somepath\somesubpath\myfile.css");

            string result = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher);

            string expected =
                @"
                                                        .ui-icon { background-image: url(someothersubpath/images/ui-icons_222222_256x240.png); }
                                                        .ui-widget-content .ui-icon {background-image: url(someothersubpath/Images/ui-icons_222222_256x240.png); }
                                                        .ui-widget-header .ui-icon {background-image: url(someothersubpath/iMages/ui-icons_222222_256x240.png); }
                                                    ";

            Assert.AreEqual(expected, result);
        }
        public static string RewriteCssPaths(string outputPath, string sourcePath, string css, ICSSAssetsFileHasher cssAssetsFileHasher, IPathTranslator pathTranslator, bool asImport = false)
        {
            var relativePaths = FindDistinctRelativePathsIn(css);

            if (relativePaths.Any())
            {
                var relativeOutputPath = GetWebPath(outputPath, pathTranslator);
                var relativeSourcePath = GetWebPath(sourcePath, pathTranslator);

                var relativePathAdapter = RelativePathAdapter.Between(relativeOutputPath, relativeSourcePath);

                foreach (var relativePath in relativePaths)
                {
                    var firstIndexOfHashOrQuestionMark = relativePath.IndexOfAny(new[] { '?', '#' });

                    var segmentAfterHashOrQuestionMark = firstIndexOfHashOrQuestionMark >= 0
                                                             ? relativePath.Substring(firstIndexOfHashOrQuestionMark)
                                                             : string.Empty;

                    var capturedRelativePath = segmentAfterHashOrQuestionMark != string.Empty
                                                   ? relativePath.Substring(0, firstIndexOfHashOrQuestionMark)
                                                   : relativePath;

                    var resolvedOutput = relativePathAdapter.Adapt(capturedRelativePath);

                    var newRelativePath = (asImport ? "squishit://" : "") + (resolvedOutput + segmentAfterHashOrQuestionMark);

                    css = ReplaceRelativePathsIn(css, relativePath, newRelativePath);
                }
            }

            //moved out of if block above so that root-relative paths can be hashed as well
            if (cssAssetsFileHasher != null)
            {
                var hashableAssetPaths = FindHashableAssetPaths(css);

                foreach (var hashableAssetPath in hashableAssetPaths)
                {
                    var assetPathWithHash = cssAssetsFileHasher.AppendFileHash(outputPath, hashableAssetPath);

                    if (hashableAssetPath != assetPathWithHash)
                    {
                        css = ReplaceRelativePathsIn(css, hashableAssetPath, assetPathWithHash);
                    }
                }
            }

            if (!asImport)
            {
                css = css.Replace("squishit://", "");
            }

            return(css);
        }
Example #17
0
        public static string RewriteCssPaths(string outputPath, string sourcePath, string css, ICSSAssetsFileHasher cssAssetsFileHasher, bool asImport = false)
        {
            //see http://stackoverflow.com/questions/3692818/uri-makerelativeuri-behavior-on-mono
            if (FileSystem.Unix)
            {
                outputPath += "/";
            }

            var sourceDirectory = Path.GetDirectoryName(sourcePath) + "/";
            var outputUri       = new Uri(Path.GetDirectoryName(outputPath) + "/", UriKind.Absolute);

            var relativePaths = FindDistinctRelativePathsIn(css);

            foreach (string relativePath in relativePaths)
            {
                var firstIndexOfHashOrQuestionMark = relativePath.IndexOfAny(new[] { '?', '#' });

                var segmentAfterHashOrQuestionMark = firstIndexOfHashOrQuestionMark >= 0
                    ? relativePath.Substring(firstIndexOfHashOrQuestionMark)
                    : string.Empty;

                var capturedRelativePath = segmentAfterHashOrQuestionMark != string.Empty
                    ? relativePath.Substring(0, firstIndexOfHashOrQuestionMark)
                    : relativePath;

                var resolvedSourcePathString = Path.Combine(sourceDirectory, capturedRelativePath);

                var resolvedSourcePath = new Uri(resolvedSourcePathString);

                var resolvedOutput = outputUri.MakeRelativePathTo(resolvedSourcePath);

                var newRelativePath = asImport ? "squishit://" + resolvedOutput : (resolvedOutput + segmentAfterHashOrQuestionMark);

                css = ReplaceRelativePathsIn(css, relativePath, newRelativePath);
            }

            if (!asImport)
            {
                css = css.Replace("squishit://", "");
            }

            if (cssAssetsFileHasher != null)
            {
                var localRelativePathsThatExist = FindDistinctLocalRelativePathsThatExist(css);

                foreach (string localRelativePathThatExist in localRelativePathsThatExist)
                {
                    var localRelativePathThatExistWithFileHash = cssAssetsFileHasher.AppendFileHash(outputPath, localRelativePathThatExist);

                    if (localRelativePathThatExist != localRelativePathThatExistWithFileHash)
                    {
                        css = css.Replace(localRelativePathThatExist, localRelativePathThatExistWithFileHash);
                    }
                }
            }
            return(css);
        }
Example #18
0
        public static string RewriteCssPaths(string outputPath, string sourcePath, string css, ICSSAssetsFileHasher cssAssetsFileHasher, bool asImport = false)
        {
            var relativePaths = FindDistinctRelativePathsIn(css);

            if (relativePaths.Any())
            {
                var relativePathAdapter = RelativePathAdapter.Between(outputPath, sourcePath);

                foreach (var relativePath in relativePaths)
                {
                    var firstIndexOfHashOrQuestionMark = relativePath.IndexOfAny(new[] { '?', '#' });

                    var segmentAfterHashOrQuestionMark = firstIndexOfHashOrQuestionMark >= 0
                                                             ? relativePath.Substring(firstIndexOfHashOrQuestionMark)
                                                             : string.Empty;

                    var capturedRelativePath = segmentAfterHashOrQuestionMark != string.Empty
                                                   ? relativePath.Substring(0, firstIndexOfHashOrQuestionMark)
                                                   : relativePath;

                    var resolvedOutput = relativePathAdapter.Adapt(capturedRelativePath);

                    var newRelativePath = (asImport ? "squishit://" : "") + (resolvedOutput + segmentAfterHashOrQuestionMark);

                    css = ReplaceRelativePathsIn(css, relativePath, newRelativePath);
                }

                if (cssAssetsFileHasher != null)
                {
                    var localRelativePathsThatExist = FindDistinctLocalRelativePathsThatExist(css);

                    foreach (var localRelativePathThatExist in localRelativePathsThatExist)
                    {
                        var localRelativePathThatExistWithFileHash = cssAssetsFileHasher.AppendFileHash(outputPath,
                                                                                                        localRelativePathThatExist);

                        if (localRelativePathThatExist != localRelativePathThatExistWithFileHash)
                        {
                            css = css.Replace(localRelativePathThatExist, localRelativePathThatExistWithFileHash);
                        }
                    }
                }
            }

            if (!asImport)
            {
                css = css.Replace("squishit://", "");
            }

            return(css);
        }