///<summary>Normalizes all URLs in a CSS parse tree to be relative to the specified directory.</summary> ///<param name="tree">The CSS parse tree to read.</param> ///<param name="targetFile">The new filename to make URLs relative to.</param> ///<param name="oldBasePath">The previous filename to resolve existing relative paths from. If null, relative paths will be left unchanged.</param> ///<returns>The rewritten CSS source.</returns> ///<remarks> /// This is used to combine CSS files from different folders into a single /// bundle and to fix absolute URLs from the hacked browser LESS compiler. /// Fully absolute paths (starting with a drive letter) will be changed to /// relative URLs using targetFile (fix for LESS compiler). /// Host-relative paths (starting with a /) will be unchanged. /// Normal relative paths (starting with . or a name) will be re-resolved. ///</remarks> public static string NormalizeUrls(BlockItem tree, string targetFile, string oldBasePath) { var normalizer = new CssUrlNormalizer(targetFile, oldBasePath); tree.Accept(normalizer); var retVal = new StringBuilder(tree.Text); for (int i = normalizer.replacements.Count - 1; i >= 0; i--) { var range = normalizer.replacements[i].Item1; var url = normalizer.replacements[i].Item2; retVal.Remove(range.Start, range.Length); retVal.Insert(range.Start, HttpUtility.UrlPathEncode(url)); } return retVal.ToString(); }
///<summary>Normalizes all URLs in a CSS parse tree to be relative to the specified directory.</summary> ///<param name="tree">The CSS parse tree to read.</param> ///<param name="targetFile">The new filename to make URLs relative to.</param> ///<param name="oldBasePath">The previous filename to resolve existing relative paths from. If null, relative paths will be left unchanged.</param> ///<returns>The rewritten CSS source.</returns> ///<remarks> /// This is used to combine CSS files from different folders into a single /// bundle and to fix absolute URLs from the hacked browser LESS compiler. /// Fully absolute paths (starting with a drive letter) will be changed to /// relative URLs using targetFile (fix for LESS compiler). /// Host-relative paths (starting with a /) will be unchanged. /// Normal relative paths (starting with . or a name) will be re-resolved. ///</remarks> public static string NormalizeUrls(BlockItem tree, string targetFile, string oldBasePath) { var normalizer = new CssUrlNormalizer(targetFile, oldBasePath); tree.Accept(normalizer); var retVal = new StringBuilder(tree.Text); for (int i = normalizer.replacements.Count - 1; i >= 0; i--) { var range = normalizer.replacements[i].Item1; var url = normalizer.replacements[i].Item2; retVal.Remove(range.Start, range.Length); retVal.Insert(range.Start, HttpUtility.UrlPathEncode(url)); } return(retVal.ToString()); }