Skip to content

Commit

Permalink
Properly rewrite CSS Url expressions with spaces
Browse files Browse the repository at this point in the history
- ex: background-image: url( "url.jpg" )
- fix jetheredge#207
  • Loading branch information
AlexCuse committed Aug 14, 2012
1 parent cbd7065 commit 87761d4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions SquishIt.Framework/Css/CssPathRewriter.cs
Expand Up @@ -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);
Expand Down
32 changes: 31 additions & 1 deletion 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
Expand Down Expand Up @@ -283,6 +282,37 @@ public void CanRewritePathsInCssWithSingleQuotes()
Assert.AreEqual(expected, result);
}

[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()
{
Expand Down

0 comments on commit 87761d4

Please sign in to comment.