views:

51

answers:

3

function works perfectly. it makes source code single line. but problem is about Google Adsense ads. their locations get messed up. what might be the reason ?

programming language is c# asp.net 4.0

here the function

    protected override void Render(HtmlTextWriter writer)
{
    if (this.Request.Headers["X-MicrosoftAjax"] != "Delta=true")
    {
        System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"<script[^>]*>[\w|\t|\r|\W]*?</script>");
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        base.Render(hw);
        string html = sb.ToString();
        System.Text.RegularExpressions.MatchCollection mymatch = reg.Matches(html);
        html = reg.Replace(html, string.Empty);
        reg = new System.Text.RegularExpressions.Regex(@"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}|(?=[\r])\s{2,}");
        html = reg.Replace(html, string.Empty);
        reg = new System.Text.RegularExpressions.Regex(@"</body>");
        string str = string.Empty;
        foreach (System.Text.RegularExpressions.Match match in mymatch)
        {
            str += match.ToString();
        }
        html = reg.Replace(html, str + "</body>");
        writer.Write(html);
    }
    else
        base.Render(writer);
}
+2  A: 

Writing your own minifier in a few lines of code and expecting consistent results strikes me as a little thoughtless. I could think of several ways to break your minifier off the cuff. Doubtless there's many, many more. This is a well trodden, and difficult path. Use something tried and tested, because getting it right takes lots of effort, a very long time and lots and lots of testing. Your code demonstrates none of these attributes.

edit

I mistook your question to be concerning Javascript minification. Don't bother. Turn on gzip at the server.

spender
+1  A: 

I think the problem might be that it makes the source a single line.

First, it could remove significant whitespace - spaces within block elements are significant but collapsable, so while you can replace them all with a single whitespace character, you can't remove them.

Secondly, sometimes insignificant whitespace is treated as significant due to browser bugs (though these are much fewer than they used to be).

Even without the second case, the first suffices that there's no good reason to expect the code to still work correctly after as extreme a reduction as you have here.

Finally, there's not much point in it anyway. Even with a large file it won't have much effect on render time, so it's only really going to improve download time, but since most whitespace in source is quite amenable to the deflate algorithm, it's going to be of little effect after you gzip or deflate it for transmission anyway (assuming you are using content-encoding to compress; if not, why waste time mangling source when you'll get much better tried-and-tested improvements with that?)

Jon Hanna
+1  A: 

Maybe it messes up adsense locations because it moves all the <script> tags to the end of the body.

Les