public static string RewriteCssPaths(string outputPath, string sourcePath, string css, ICssAssetsFileHasher cssAssetsFileHasher) { var sourceUri = new Uri(Path.GetDirectoryName(sourcePath) + "/", UriKind.Absolute); var outputUri = new Uri(Path.GetDirectoryName(outputPath) + "/", UriKind.Absolute); var relativePaths = FindDistinctRelativePathsIn(css); foreach (string relativePath in relativePaths) { var resolvedSourcePath = new Uri(sourceUri + relativePath ); var resolvedOutput = outputUri.MakeRelativeUri(resolvedSourcePath); css = css.Replace(relativePath , resolvedOutput.OriginalString); } 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 void CanRewritePathsInCssWithSingleQuotes() { ICssAssetsFileHasher cssAssetsFileHasher = null; string css = @" .header { background-image: url('../img/something.jpg'); } .footer { background-image: url('../img/blah/somethingelse.jpg'); } "; string sourceFile = @"C:\somepath\somesubpath\myfile.css"; string targetFile = @"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 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); }
string ProcessCssFile(string file, string outputFile, bool asImport = false) { string css = null; var preprocessors = FindPreprocessors(file); if (preprocessors != null) { css = PreprocessFile(file, preprocessors); } else { css = 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); } return(CSSPathRewriter.RewriteCssPaths(outputFile, file, css, fileHasher, asImport)); }
private string CompressCss(string outputFilePath, List <string> files, ICssCompressor compressor, bool appendHashToAssets) { var outputCss = new StringBuilder(); foreach (string file in files) { string css; if (file.ToLower().EndsWith(".less") || file.ToLower().EndsWith(".less.css")) { css = ProcessLess(file); } else { css = ReadFile(file); } if (processImports) { css = ProcessImport(css); } ICssAssetsFileHasher fileHasher = null; if (appendHashToAssets) { var fileResolver = new FileResolver(); fileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, fileResolver, hasher); } css = CssPathRewriter.RewriteCssPaths(outputFilePath, file, css, fileHasher); outputCss.Append(css + "\n"); } return(compressor.CompressContent(outputCss.ToString())); }
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 = @"C:\somepath\somesubpath\myfile.css"; string targetFile = @"C:\somepath\someothersubpath\evendeeper\output.css"; string result = CssPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher); string expected = @" .header { background-image: url(../../somesubpath/img/something.jpg); } .footer { background-image: url(../../somesubpath/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 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"); string result = CSSPathRewriter.RewriteCssPaths(targetFile, sourceFile, css, cssAssetsFileHasher); //concrete result doesn't matter here (since input isn't valid) //the only important thing is that it shouldn't throw exceptions string expected = @" .header { background-image: URL(""somesubpath/*/img/something.jpg""); background-image: url(../documents/usr8/local/temp/1/d1b73b93-5ff0-11de-9339-0017317c60aa); } .footer { background-image: uRL(""somesubpath/..c:/1/2/somethingelse.jpg""); } "; Assert.AreEqual(expected, result); }
protected override string BeforeMinify(string outputFile, List <string> filePaths) { var outputCss = new StringBuilder(); foreach (string file in filePaths) { string css; if (file.ToLower().EndsWith(".less") || file.ToLower().EndsWith(".less.css")) { css = ProcessLess(file); } else { css = ReadFile(file); } if (ShouldImport) { css = ProcessImport(css); } ICssAssetsFileHasher fileHasher = null; if (ShouldAppendHashForAssets) { var fileResolver = new FileResolver(); fileHasher = new CssAssetsFileHasher(HashKeyName, fileResolver, hasher); } css = CSSPathRewriter.RewriteCssPaths(outputFile, file, css, fileHasher); outputCss.Append(css + "\n"); } return(outputCss.ToString()); }
string ProcessCssFile(string file, string outputFile, bool asImport = false) { string css = null; if (file.ToLower().EndsWith(".less") || file.ToLower().EndsWith(".less.css")) { css = ProcessLess(file); } else { css = ReadFile(file); } if (ShouldImport) { css = ProcessImport(file, outputFile, css); } ICssAssetsFileHasher fileHasher = null; if (ShouldAppendHashForAssets) { var fileResolver = new FileSystemResolver(); fileHasher = new CssAssetsFileHasher(HashKeyName, fileResolver, hasher); } return(CSSPathRewriter.RewriteCssPaths(outputFile, file, css, fileHasher, asImport)); }
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); }
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 resolvedSourcePathString = Path.Combine (sourceDirectory, relativePath); var resolvedSourcePath = new Uri (resolvedSourcePathString); var resolvedOutput = outputUri.MakeRelativePathTo(resolvedSourcePath); var newRelativePath = asImport ? "squishit://" + resolvedOutput : resolvedOutput; 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 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 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 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)); }
public static string RewriteCssPaths(string outputPath, string sourcePath, string css, ICssAssetsFileHasher cssAssetsFileHasher) { //see http://stackoverflow.com/questions/3692818/uri-makerelativeuri-behavior-on-mono if (FileSystem.Unix) { outputPath += "/"; } var sourceUri = new Uri(Path.GetDirectoryName(sourcePath) + "/", UriKind.Absolute); var outputUri = new Uri(Path.GetDirectoryName(outputPath) + "/", UriKind.Absolute); var relativePaths = FindDistinctRelativePathsIn(css); foreach (string relativePath in relativePaths) { var resolvedSourcePath = new Uri(sourceUri + relativePath); var resolvedOutput = outputUri.MakeRelativeUri(resolvedSourcePath); css = css.Replace(relativePath, resolvedOutput.OriginalString); } 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 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) { var sourceUri = new Uri(Path.GetDirectoryName(sourcePath) + "/", UriKind.Absolute); var outputUri = new Uri(Path.GetDirectoryName(outputPath) + "/", UriKind.Absolute); var relativePaths = FindDistinctRelativePathsIn(css); foreach (string relativePath in relativePaths) { var resolvedSourcePath = new Uri(sourceUri + relativePath); var resolvedOutput = outputUri.MakeRelativeUri(resolvedSourcePath); css = css.Replace(relativePath, resolvedOutput.OriginalString); } 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) { //see http://stackoverflow.com/questions/3692818/uri-makerelativeuri-behavior-on-mono if (FileSystem.Unix) { outputPath += "/"; } var sourceUri = new Uri(Path.GetDirectoryName(sourcePath) + "/", UriKind.Absolute); var outputUri = new Uri(Path.GetDirectoryName(outputPath) + "/", UriKind.Absolute); var relativePaths = FindDistinctRelativePathsIn(css); foreach (string relativePath in relativePaths) { var resolvedSourcePath = new Uri(sourceUri + relativePath, true); var resolvedOutput = outputUri.MakeRelativeUri(resolvedSourcePath); css = css.Replace(relativePath, resolvedOutput.OriginalString); } 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 CssPathRewriter(ICssAssetsFileHasher cssAssetsFileHasher, IPathProvider pathProvider) { CssAssetsFileHasher = cssAssetsFileHasher; PathProvider = pathProvider; }
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 resolvedSourcePathString = Path.Combine(sourceDirectory, relativePath); var resolvedSourcePath = new Uri(resolvedSourcePathString); var resolvedOutput = outputUri.MakeRelativePathTo(resolvedSourcePath); var newRelativePath = asImport ? "squishit://" + resolvedOutput : resolvedOutput; 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); }