开发者

Using conditional replace in C# Regular Expressions for CSS minification

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 开发者_Python百科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?


IMO you should not do this at all.

Can't you imagine a situation where you might inject, say, <span class="InputReplacement" id="Email">user@example.com</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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜