Created
February 27, 2014 13:52
-
-
Save dhont/9250426 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
using System.Web; | |
using System.Web.Optimization; | |
namespace Web.Extensions | |
{ | |
/// <summary> | |
/// ref: http://aspnetoptimization.codeplex.com/workitem/83 | |
/// </summary> | |
public class AbsolutePathCssRewriteUrlTransform : IItemTransform | |
{ | |
internal static string RebaseUrlToAbsolute(string baseUrl, string url) | |
{ | |
if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(baseUrl) || url.StartsWith("/", StringComparison.OrdinalIgnoreCase)) | |
return url; | |
if (!baseUrl.EndsWith("/", StringComparison.OrdinalIgnoreCase)) | |
baseUrl = baseUrl + "/"; | |
return VirtualPathUtility.ToAbsolute(baseUrl + url); | |
} | |
internal static string ConvertUrlsToAbsolute(string baseUrl, string content) | |
{ | |
if (string.IsNullOrWhiteSpace(content)) | |
return content; | |
else | |
return new Regex("url\\(['\"]?(?<url>[^)]+?)['\"]?\\)").Replace(content, (MatchEvaluator)(match => "url(" + RebaseUrlToAbsolute(baseUrl, match.Groups["url"].Value) + ")")); | |
} | |
public string Process(string includedVirtualPath, string input) | |
{ | |
if (includedVirtualPath == null) | |
throw new ArgumentNullException("includedVirtualPath"); | |
else | |
return ConvertUrlsToAbsolute(VirtualPathUtility.GetDirectory(includedVirtualPath), input); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment