Showing posts with label render. Show all posts
Showing posts with label render. Show all posts

Saturday, March 24, 2012

Strip space before page rendering

Is there any benefit to stripping all the space from the page before
rendering by overriding the page render and using the htmltextwriter and
stringbuilder to strip linefeeds, tabs and extra space etc. I have noticed
that may site's source code is like this. Is this because it is better for
the browser or harder for someone to be able to read it. Or maybe there is a
method or command to do this automatically?

Any thoughts are appreciated.

Mikesaves bandwidth mainly...

"vMike" <Michael.George@.gewarren.com.nospam> wrote in message
news:bnpsnk$rfa$1@.ngspool-d02.news.aol.com...
> Is there any benefit to stripping all the space from the page before
> rendering by overriding the page render and using the htmltextwriter and
> stringbuilder to strip linefeeds, tabs and extra space etc. I have noticed
> that may site's source code is like this. Is this because it is better for
> the browser or harder for someone to be able to read it. Or maybe there is
a
> method or command to do this automatically?
> Any thoughts are appreciated.
> Mike
I don't think there is a way to do it automatically, but it's simple enough
to implement.

This is the approach I use:

I have a base class derived from System.Web.UI.Page and override Render

namespace DoNot.Invade.MySpace
{
class Page : System.Web.UI.Page
{
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);

base.Render (hw);

string html = sb.ToString();

html = html.Replace(Environment.NewLine, string.Empty);
html = html.Replace("\n", string.Empty); // This may be redundant
html = html.Replace("\t", string.Empty);

writer.Write(html);
}
}
}

Hope this helps
Brian W

"vMike" <Michael.George@.gewarren.com.nospam> wrote in message
news:bnpsnk$rfa$1@.ngspool-d02.news.aol.com...
> Is there any benefit to stripping all the space from the page before
> rendering by overriding the page render and using the htmltextwriter and
> stringbuilder to strip linefeeds, tabs and extra space etc. I have noticed
> that may site's source code is like this. Is this because it is better for
> the browser or harder for someone to be able to read it. Or maybe there is
a
> method or command to do this automatically?
> Any thoughts are appreciated.
> Mike
Thanks, I had something similar to that. The only thing I had to do was do
remove the <!-- that asp puts before the javascipt for postback because when
I removed the rest of the line is caused an error. But I wonder is stripping
the space make the page display any quicker in the browser as I image there
is a small amount of overhead on the server side.

"Brian W" <brianw@.gold_death_2_spam_rush.com> wrote in message
news:%23zf8QjxnDHA.1488@.TK2MSFTNGP12.phx.gbl...
> I don't think there is a way to do it automatically, but it's simple
enough
> to implement.
> This is the approach I use:
> I have a base class derived from System.Web.UI.Page and override Render
> namespace DoNot.Invade.MySpace
> {
> class Page : System.Web.UI.Page
> {
> protected override void Render(HtmlTextWriter writer)
> {
> StringBuilder sb = new StringBuilder();
> StringWriter sw = new StringWriter(sb);
> HtmlTextWriter hw = new HtmlTextWriter(sw);
> base.Render (hw);
> string html = sb.ToString();
> html = html.Replace(Environment.NewLine, string.Empty);
> html = html.Replace("\n", string.Empty); // This may be redundant
> html = html.Replace("\t", string.Empty);
> writer.Write(html);
> }
> }
> }
> Hope this helps
> Brian W
> "vMike" <Michael.George@.gewarren.com.nospam> wrote in message
> news:bnpsnk$rfa$1@.ngspool-d02.news.aol.com...
> > Is there any benefit to stripping all the space from the page before
> > rendering by overriding the page render and using the htmltextwriter and
> > stringbuilder to strip linefeeds, tabs and extra space etc. I have
noticed
> > that may site's source code is like this. Is this because it is better
for
> > the browser or harder for someone to be able to read it. Or maybe there
is
> a
> > method or command to do this automatically?
> > Any thoughts are appreciated.
> > Mike
Brian W wrote:

> I don't think there is a way to do it automatically, but it's simple enough
> to implement.
> This is the approach I use:
> I have a base class derived from System.Web.UI.Page and override Render
> namespace DoNot.Invade.MySpace
> {
> class Page : System.Web.UI.Page
> {
> protected override void Render(HtmlTextWriter writer)
> {
> StringBuilder sb = new StringBuilder();
> StringWriter sw = new StringWriter(sb);
> HtmlTextWriter hw = new HtmlTextWriter(sw);
> base.Render (hw);
> string html = sb.ToString();
> html = html.Replace(Environment.NewLine, string.Empty);
> html = html.Replace("\n", string.Empty); // This may be redundant
> html = html.Replace("\t", string.Empty);
> writer.Write(html);
> }
> }
> }

You'll want to be careful with simplistic code like this. There are
times when whitespace and newlines are significant, such as text inside
of a <pre> element or script code.

> Hope this helps
> Brian W
> "vMike" <Michael.George@.gewarren.com.nospam> wrote in message
> news:bnpsnk$rfa$1@.ngspool-d02.news.aol.com...
>>Is there any benefit to stripping all the space from the page before
>>rendering by overriding the page render and using the htmltextwriter and
>>stringbuilder to strip linefeeds, tabs and extra space etc. I have noticed
>>that may site's source code is like this. Is this because it is better for
>>the browser or harder for someone to be able to read it. Or maybe there is
> a
>>method or command to do this automatically?
>>
>>Any thoughts are appreciated.
>>
>>Mike
>>
>>

--
mikeb
True, I guess I should have mentioned that. Since the site I'm currently
working on doesn't use <pre> I don't worry about it. And what I have written
can be expanded upon to handle these situations.

There are other cases where this is a problem too. Such as the following:

<p>
This is
some text
</p
will produce the following output:

This issome text

For me, though, I just edit the html and make sure there is also a space
before the newline.

Regards
Brian W

"mikeb" <mailbox.google@.mailnull.com> wrote in message
news:%23YdMB$xnDHA.1672@.TK2MSFTNGP09.phx.gbl...
> Brian W wrote:
> > I don't think there is a way to do it automatically, but it's simple
enough
> > to implement.
> > This is the approach I use:
> > I have a base class derived from System.Web.UI.Page and override Render
> > namespace DoNot.Invade.MySpace
> > {
> > class Page : System.Web.UI.Page
> > {
> > protected override void Render(HtmlTextWriter writer)
> > {
> > StringBuilder sb = new StringBuilder();
> > StringWriter sw = new StringWriter(sb);
> > HtmlTextWriter hw = new HtmlTextWriter(sw);
> > base.Render (hw);
> > string html = sb.ToString();
> > html = html.Replace(Environment.NewLine, string.Empty);
> > html = html.Replace("\n", string.Empty); // This may be redundant
> > html = html.Replace("\t", string.Empty);
> > writer.Write(html);
> > }
> > }
> > }
> You'll want to be careful with simplistic code like this. There are
> times when whitespace and newlines are significant, such as text inside
> of a <pre> element or script code.
>
> > Hope this helps
> > Brian W
> > "vMike" <Michael.George@.gewarren.com.nospam> wrote in message
> > news:bnpsnk$rfa$1@.ngspool-d02.news.aol.com...
> >>Is there any benefit to stripping all the space from the page before
> >>rendering by overriding the page render and using the htmltextwriter and
> >>stringbuilder to strip linefeeds, tabs and extra space etc. I have
noticed
> >>that may site's source code is like this. Is this because it is better
for
> >>the browser or harder for someone to be able to read it. Or maybe there
is
> > a
> >>method or command to do this automatically?
> >>
> >>Any thoughts are appreciated.
> >>
> >>Mike
> >>
> >>
> --
> mikeb

stripping tags from source on render

Hi,
What way could I strip certain tags (like HTML comments) from the HTML being
delivered to the client? I don't mean what regexp to use, but where do I
put this stripping code? I'm thinking something in the Global.asax, but I
can't find any reference to it having a Render or PreRender event or how to
tie into them if I did! I'd ideally like some help along the lines of:

1 - Where to put the code
2 - example of how to alter the source being delivered to the client

Thanks,
LanceHi Lance:

You could use a Response.Filter, like the one in this article:
http://www.codeproject.com/aspnet/R...pacesAspNet.asp

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 17 Jun 2005 11:43:50 +0100, "Lance"
<lance@.[nospam]keayweb.com> wrote:

>Hi,
>What way could I strip certain tags (like HTML comments) from the HTML being
>delivered to the client? I don't mean what regexp to use, but where do I
>put this stripping code? I'm thinking something in the Global.asax, but I
>can't find any reference to it having a Render or PreRender event or how to
>tie into them if I did! I'd ideally like some help along the lines of:
>1 - Where to put the code
>2 - example of how to alter the source being delivered to the client
>Thanks,
>Lance
Hi Lance,

I'd look at the Page.Render event. This takes an HtmlTextWriter parameter
where you can modify the contents before calling the base.Render method.

HTH
--
Ian Lane

"Lance" wrote:

> Hi,
> What way could I strip certain tags (like HTML comments) from the HTML being
> delivered to the client? I don't mean what regexp to use, but where do I
> put this stripping code? I'm thinking something in the Global.asax, but I
> can't find any reference to it having a Render or PreRender event or how to
> tie into them if I did! I'd ideally like some help along the lines of:
> 1 - Where to put the code
> 2 - example of how to alter the source being delivered to the client
> Thanks,
> Lance
>
Thanks! I thought i had seen it somewhere on good 'ol codeproject!

"Scott Allen" <scott@.nospam.odetocode.com> wrote in message
news:k3s5b1drg28qbudj23ggjs1l8r861cuqhh@.4ax.com...
> Hi Lance:
> You could use a Response.Filter, like the one in this article:
> http://www.codeproject.com/aspnet/R...pacesAspNet.asp
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
> On Fri, 17 Jun 2005 11:43:50 +0100, "Lance"
> <lance@.[nospam]keayweb.com> wrote:
> >Hi,
> >What way could I strip certain tags (like HTML comments) from the HTML
being
> >delivered to the client? I don't mean what regexp to use, but where do I
> >put this stripping code? I'm thinking something in the Global.asax, but
I
> >can't find any reference to it having a Render or PreRender event or how
to
> >tie into them if I did! I'd ideally like some help along the lines of:
> >1 - Where to put the code
> >2 - example of how to alter the source being delivered to the client
> >Thanks,
> >Lance
I'd have to call the code from every page using this method, right? If I
could call it from one location (global.asax) that would be great!

"Ian Lane .enizin.net>" <ian@.<nospam> wrote in message
news:329AC863-65C0-41DC-B99E-2C47F83EA16F@.microsoft.com...
> Hi Lance,
> I'd look at the Page.Render event. This takes an HtmlTextWriter parameter
> where you can modify the contents before calling the base.Render method.
> HTH
> --
> Ian Lane
>
> "Lance" wrote:
> > Hi,
> > What way could I strip certain tags (like HTML comments) from the HTML
being
> > delivered to the client? I don't mean what regexp to use, but where do
I
> > put this stripping code? I'm thinking something in the Global.asax, but
I
> > can't find any reference to it having a Render or PreRender event or how
to
> > tie into them if I did! I'd ideally like some help along the lines of:
> > 1 - Where to put the code
> > 2 - example of how to alter the source being delivered to the client
> > Thanks,
> > Lance