tags:

views:

37

answers:

1

Hey guys, I am making an application using C# in Visual Studio that minifies CSS code. Now I came across a piece of code that strips all prefixes for the # ID selector.

strCSS = Regex.Replace(strCSS, @"[a-zA-Z]+#", "#");

However, this would also remove any class names in front the ID selector. For example,

#interior .field#user-comment
{}

when passed through the Regex, will become -

#interior .#user-comment
{}

How do I prevent this from happening? Should I use a ? condition in the Regex, or a Match?

+1  A: 

IMO you should not do this at all.

Can't you imagine a situation where you might inject, say, <span class="InputReplacement" id="Email">[email protected]</span> in one case, then in another, <input id="Email" type="email" />? And perhaps you want to have styles that only apply to input#Email, but not span#Email? Or different styles for each?

You are changing the meaning of your CSS by doing this, so that it can give different results on the same input as the original CSS. That is not CSS minification; it is CSS modification.

Domenic
You are correct, I hadn't thought about that. I found a .NET port of the Yahoo YUI compressor. I guess I'd just use that!
GPX