Permalink
Browse files

Properly rewrite CSS Url expressions with spaces

- ex: background-image: url( "url.jpg" )
- fix #207
  • Loading branch information...
1 parent cbd7065 commit 87761d4e15c217d50f8af76925280f249b6eca0c @AlexCuse committed Aug 14, 2012
Showing with 33 additions and 3 deletions.
  1. +2 −2 SquishIt.Framework/Css/CssPathRewriter.cs
  2. +31 −1 SquishIt.Tests/CssPathRewriterTests.cs
View
4 SquishIt.Framework/Css/CssPathRewriter.cs
@@ -57,15 +57,15 @@ public static string RewriteCssPaths (string outputPath, string sourcePath, stri
static string ReplaceRelativePathsIn (string css, string oldPath, string newPath)
{
- var regex = new Regex (@"url\([""']{0,1}" + Regex.Escape (oldPath) + @"[""']{0,1}\)", RegexOptions.IgnoreCase);
+ var regex = new Regex (@"url\(\s*[""']{0,1}" + Regex.Escape (oldPath) + @"[""']{0,1}\s*\)", RegexOptions.IgnoreCase);
return regex.Replace (css, match => {
var path = match.Value.Replace (oldPath, newPath);
return path;
});
}
- static readonly Regex pathsRegex = new Regex(@"(?<!.*behavior\s*:\s*)url\([""']?(.*?)[""']?\)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
+ static readonly Regex pathsRegex = new Regex(@"(?<!.*behavior\s*:\s*)url\(\s*(?:[""']?)(.*?)(?:[""']?)\s*\)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
static IEnumerable<string> FindDistinctRelativePathsIn (string css)
{
var matches = pathsRegex.Matches(css);
View
32 SquishIt.Tests/CssPathRewriterTests.cs
@@ -1,7 +1,6 @@
using NUnit.Framework;
using SquishIt.Framework.Css;
using SquishIt.Framework.Utilities;
-using System.Text.RegularExpressions;
using SquishIt.Tests.Helpers;
namespace SquishIt.Tests
@@ -284,6 +283,37 @@ public void CanRewritePathsInCssWithSingleQuotes()
}
[Test]
+ public void CanRewritePathsInCssWithSpaces()
+ {
+ 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);
+ }
+
+ [Test]
public void CanRewritePathsInCssWithUppercaseUrlStatement()
{
ICssAssetsFileHasher cssAssetsFileHasher = null;

0 comments on commit 87761d4

Please sign in to comment.