Skip to content

Commit

Permalink
When rewriting urls in css, and avoiding those which are data-uris, l…
Browse files Browse the repository at this point in the history
…ike data:, keep their surrounding quotations chars in case they are needed, e.g. non-base64 encoded svg.

#8
  • Loading branch information
benmccallum committed Jul 27, 2015
1 parent 73a3ee5 commit 817090d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
6 changes: 6 additions & 0 deletions AspNetBundling.TestWebsite/Content/Site.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ select,
textarea {
max-width: 280px;
}

#non-base64-data-uri-test {
height: 100px;
width: 100px;
background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='100%' height='1'><rect fill='#c8c7cc' x='0' y='0' width='100%' height='0.5'/></svg>");
}
3 changes: 3 additions & 0 deletions AspNetBundling.TestWebsite/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
</div>
<div class="container body-content">
@RenderBody()

<div id="non-base64-data-uri-test"></div>

<hr />
<footer>
<p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
Expand Down
16 changes: 12 additions & 4 deletions AspNetBundling/CssRewriteUrlTransformFixed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ namespace System.Web.Optimization
/// </summary>
public class CssRewriteUrlTransformFixed : IItemTransform
{
private static string RebaseUrlToAbsolute(string baseUrl, string url)
private static string RebaseUrlToAbsolute(string baseUrl, string url, string prefix, string suffix)
{
if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(baseUrl) || url.StartsWith("/", StringComparison.OrdinalIgnoreCase)
|| url.StartsWith("data:") || url.StartsWith("http://") || url.StartsWith("https://"))
|| url.StartsWith("http://") || url.StartsWith("https://"))
{
return url;
}

if (url.StartsWith("data:"))
{
// Keep the prefix and suffix quotation chars as is in case they are needed (e.g. non-base64 encoded svg)
return prefix + url + suffix;
}

if (!baseUrl.EndsWith("/", StringComparison.OrdinalIgnoreCase))
{
baseUrl += "/";
}

return VirtualPathUtility.ToAbsolute(baseUrl + url);
}
private static string ConvertUrlsToAbsolute(string baseUrl, string content)
Expand All @@ -27,8 +35,8 @@ private static string ConvertUrlsToAbsolute(string baseUrl, string content)
{
return content;
}
var regex = new Regex("url\\(['\"]?(?<url>[^)]+?)['\"]?\\)");
return regex.Replace(content, (Match match) => "url(" + CssRewriteUrlTransformFixed.RebaseUrlToAbsolute(baseUrl, match.Groups["url"].Value) + ")");
var regex = new Regex("url\\((?<prefix>['\"]?)(?<url>[^)]+?)(?<suffix>['\"]?)\\)");
return regex.Replace(content, (Match match) => "url(" + CssRewriteUrlTransformFixed.RebaseUrlToAbsolute(baseUrl, match.Groups["url"].Value, match.Groups["prefix"].Value, match.Groups["suffix"].Value) + ")");
}
public string Process(string includedVirtualPath, string input)
{
Expand Down
4 changes: 2 additions & 2 deletions AspNetBundling/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.1.0")]
[assembly: AssemblyFileVersion("2.0.1.0")]
[assembly: AssemblyVersion("2.0.2.0")]
[assembly: AssemblyFileVersion("2.0.2.0")]
2 changes: 1 addition & 1 deletion AspNetBundling/build-nupkg.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
W:\OneDrive\Utils\nuget.exe pack -Prop Configuration=Release
nuget pack -Prop Configuration=Release
PAUSE

0 comments on commit 817090d

Please sign in to comment.