Compress/compact HTML during rendering

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Compress/compact HTML during rendering

    I'm writing some custom controls, and I'd like to be able to remove all
    unnecessary whitespace, etc. from my output. Ideally, I would just render
    as normal, then remove this extra whitespace as the last step in the
    rendering process. I'm sure I saw code to do this, but now I can't find it.
    Is this something I have to do as I go, or can I somehow do this in a
    single, last step of rendering?

    Thanks,
    cpnet


    cpnet Guest

  2. Similar Questions and Discussions

    1. Rendering HTML within an InDesign document
      Hi all, I'm trying to use InDesign to create User Experience specs for a e-commerce company. Specifically, I want to be able to prototype in a...
    2. CF 7 rendering HTML differently
      I'm reposting this as I now understand the problem better and I can reproduce it. You are going to think I am nuts, but hear me out.....If I save...
    3. ScrollPane, HTML Rendering with CSS
      Alright, so I am loading an swf into the scrollpane with a dynamic textfield as the only content. I am then setting the textfield to render html...
    4. rendering html from perl
      On Friday, Nov 14, 2003, at 02:29 US/Pacific, john@shortstay-london.com wrote: From: drieux Sent: 15 November 2003 17:13 I'm not sure I...
    5. [Q] HTML rendering?
      Hello, Does anyone know if any of the current, cross-platform, GUI toolkits of Ruby has an HTML rendering widget? Cheers, -- Daniel Carrera...
  3. #2

    Default RE: Compress/compact HTML during rendering

    Hi Cpnet,

    As for the problem you mentioned, here is my understanding:

    if your custom control is a composite control which use the
    "CreateChildControls" to generate the whole controls hierarchy, I think its
    ok for use to override the "Render" method and do some modification on the
    control's final output. We just need to create HtmlTextWriter through a
    StringBuilder and call the
    Control's base.Render to get the original Rendered html. Then, we can do
    any modification on the stringbuilder as we like. For exmaple:

    =====================================
    protected override void Render(HtmlTextWriter output)
    {
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    HtmlTextWriter writer = new HtmlTextWriter(new System.IO.StringWriter(sb));
    base.Render(writer);

    //the output has been retrieved in the stringbuilder

    //do the modification here

    output.Write(sb.ToString());
    }
    ======================================

    HOpe helps. Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    [url]http://msdn.microsoft.com/asp.net/whidbey/default.aspx[/url]


    Steven Cheng[MSFT] Guest

  4. #3

    Default Re: Compress/compact HTML during rendering

    I thought I remembered there being a property on some class I could set that
    would automatically take care of the whitespace removal, but your method
    below will work well for me.

    Thanks.


    cpnet Guest

  5. #4

    Default Re: Compress/compact HTML during rendering

    As I looked at this a little more, I think maybe I need to change it a
    little in case we end up getting an Html32TextWriter instead of a 'regular'
    HtmlTextWriter from the output argument? I also found that if I called
    "base.Render( tw)" then the output got rendered twice!

    Here's what I came up with as an alternative:


    protected override void Render(HtmlTextWriter writer)
    {
    StringBuilder sb = new StringBuilder();
    object[] createArgs = {new StringWriter(sb)};

    //Make sure we're creating an instance of the
    //same type of HtmlTextWriter as the original
    HtmlTextWriter tw =
    (HtmlTextWriter)Activator.CreateInstance(
    writer.GetType(), createArgs);

    try
    {
    //Calling base.Render here causes the
    //output to be rendered twice?!?
    //base.Render( tw);

    RenderChildren( tw);
    tw.Flush();

    //Do stuff to remove whitespace

    writer.Write( sb.ToString());
    }
    finally
    {
    tw.Close();
    }
    }


    cpnet Guest

  6. #5

    Default Re: Compress/compact HTML during rendering

    Hi Cpnet,

    Thanks a lot for your followup and your additional suggestions. I really
    appreciate. In addition, as for the
    ===========
    "base.Render( tw)" then the output got rendered twice!
    ===========

    problem you mentioned, I'm not sure whether there will be anything else
    cause it. I've try both
    base.Render(tw) or base.RenderChildren(tw) and didn't encounter this
    problem. I simplely add some input controls in my test custom control in
    CreateChildControl and they are only rendered once.
    Thanks

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    [url]http://msdn.microsoft.com/asp.net/whidbey/default.aspx[/url]

    Steven Cheng[MSFT] Guest

  7. #6

    Lightbulb Re: Compress/compact HTML during rendering

    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))
    {
    base.Render(htmlwriter);
    string html = htmlwriter.InnerWriter.ToString();
    html = Regex.Replace(html, @"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}", "");
    html = Regex.Replace(html, @"[ \f\r\t\v]?([\n\xFE\xFF/{}[\];,<>*%&|^!~?:=])[\f\r\t\v]?", "$1");
    html = html.Replace(";\n", ";");
    writer.Write(html);
    }
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139