<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1783</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1783" rel="self"/>
  <title>Buu Nguyen</title>
  <updated>Sun Nov 01 06:52:47 -0800 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor351853</id>
    <published>2009-11-01T06:52:47-08:00</published>
    <title>[C#] On Modify relative urls in a css file</title>
    <content type="html">&lt;p&gt;@Ants: that's an interesting observation.  I am not aware of any source saying &amp;quot;../&amp;quot; is not allowed in the middle/end either.  I'm thinking whether this is typical enough to tackle it in the code though.&lt;/p&gt;

&lt;p&gt;Thanks for the link, it's a super cool technique.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Buu Nguyen</name>
      <email>nqbuu111@yahoo.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1083-modify-relative-urls-in-a-css-file/refactors/351853" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor351017</id>
    <published>2009-10-31T09:52:21-07:00</published>
    <title>[C#] On Modify relative urls in a css file</title>
    <content type="html">&lt;p&gt;@Ants: Thanks.  You're right about the bug in line 21.  Re. 22-29, I don't think &amp;quot;path/to/../to/image.gif&amp;quot; is a valid CSS URL.  How do you come up with that in the first place :)?&lt;/p&gt;

&lt;p&gt;The newly refactored code is below.&lt;/p&gt;

&lt;pre&gt;private static string FixUrl(string cssPath, Match match)
{
	try
	{
		const string template = &amp;quot;url(\&amp;quot;{0}\&amp;quot;)&amp;quot;;
		var url = match.Groups[&amp;quot;url&amp;quot;].Value.Trim('\&amp;quot;', '\'');
		if (url.StartsWith(&amp;quot;/&amp;quot;))
			return string.Format(template, url);

		var cssFolder = cssPath.Substring(0, cssPath.LastIndexOf(&amp;quot;/&amp;quot;));
		var backFolderCount = Regex.Matches(url, @&amp;quot;\.\./&amp;quot;).Count;
		for (int i = 0; i &amp;lt; backFolderCount; i++)
		{
			url = url.Substring(3); // skip 1 '../'
			cssFolder = cssFolder.Substring(0, cssFolder.LastIndexOf(&amp;quot;/&amp;quot;)); // move back 1 folder
		}
		return string.Format(template, cssFolder + &amp;quot;/&amp;quot; + url);
	}
	catch (Exception ex)
	{
		return match.Value;
	}
}&lt;/pre&gt;</content>
    <author>
      <name>Buu Nguyen</name>
      <email>nqbuu111@yahoo.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1083-modify-relative-urls-in-a-css-file/refactors/351017" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1083</id>
    <published>2009-10-30T16:39:57-07:00</published>
    <updated>2009-11-01T11:54:36-08:00</updated>
    <title>[C#] Modify relative urls in a css file</title>
    <content type="html">&lt;p&gt;This function changes all URLs inside a CSS file which are relative to the path of that CSS file into URLs which are relative to the web application.  For instance, if the CSS file path is /content/site.css, then:
&lt;br /&gt;- Any occurence of url(path/to/image.gif) will become url(/content/path/to/image.gif)
&lt;br /&gt;- Any occurence of url(../path/to/image.gif) will become url(/path/to/image.gif)
&lt;br /&gt;- ...&lt;/p&gt;

&lt;pre&gt;public string Transform(string cssPath, string cssContent)
{
        return Regex.Replace(cssContent, @&amp;quot;url\((?&amp;lt;url&amp;gt;.*?)\)&amp;quot;, 
			match =&amp;gt; FixUrl(cssPath, match),
			RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
}

/// &amp;lt;summary&amp;gt;
/// Assume cssPath is /content/site.css:
///     * in: path/to/image.gif     -&amp;gt; out: /content/path/to/image.gif
///     * in: ../path/to/image.gif  -&amp;gt; out: /path/to/image.gif
///     * in: /path/to/image.gif    -&amp;gt; out: /path/to/image.gif
/// &amp;lt;/summary&amp;gt;
private static string FixUrl(string cssPath, Match match)
{
	try
	{
		var url = match.Groups[&amp;quot;url&amp;quot;].Value;
		const string template = &amp;quot;url({0})&amp;quot;;
		if (url.StartsWith(&amp;quot;/&amp;quot;))
			return url;
		var adjustedResourceFolder = cssPath.Substring(0, resourcePath.LastIndexOf(&amp;quot;/&amp;quot;));
		var backFolderCount = Regex.Matches(url, @&amp;quot;\.\./&amp;quot;).Count;
		for (int i = 0; i &amp;lt; backFolderCount; i++)
		{
			url = url.Substring(3);
			adjustedResourceFolder = adjustedResourceFolder.Substring(0,
				adjustedResourceFolder.LastIndexOf(&amp;quot;/&amp;quot;));
		}
		return string.Format(template, (adjustedResourceFolder + &amp;quot;/&amp;quot; + url));
	}
	catch (Exception ex)
	{
		return match.Value;
	}
}&lt;/pre&gt;</content>
    <author>
      <name>Buu Nguyen</name>
      <email>nqbuu111@yahoo.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1083-modify-relative-urls-in-a-css-file" rel="alternate"/>
  </entry>
</feed>

