While working on ASP.NET MVC 3, I implemented an Action Filter to remove white spaces from the entire HTML. It has been functioning as expected most of the time but now I need to tailor the RegEx so that it does not affect content inside the pre
element.
I sourced the RegEx logic from the insightful blog of the talented Mads Kristensen, however, I am unsure how to adapt it for this specific purpose.
Here is the current logic:
public override void Write(byte[] buffer, int offset, int count) {
string HTML = Encoding.UTF8.GetString(buffer, offset, count);
Regex reg = new Regex(@"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}");
HTML = reg.Replace(HTML, string.Empty);
buffer = System.Text.Encoding.UTF8.GetBytes(HTML);
this.Base.Write(buffer, 0, buffer.Length);
}
The complete code for the filter can be found here:
Any suggestions?
EDIT:
BIG NOTE:
Please note that my primary goal is not to speed up response time. In fact, this may potentially slow things down. I have GZiped the pages and this minification only saves me approximately 4 - 5 kb per page which is negligible in the grand scheme of things.