We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18397
    • 3,250 Posts
    Obviously been able to clearly see the generated page code is useful when designing and bug fixing but the rest of the time, during normal production, the site visitor doesn’t require lots of comments and white space which are taking extra bandwitdh and reducing efficiency.

    This plugin is the answer to this feature request: FS#248 — Verbose / cleaned up code rendering.

    The origional author of the "Strip HTML Comments via REGEX" has this to say:


    The final regex here will not be very kind to any code that uses HTML comments inside script or style tags for example (as dreamweaver and anyone else wanting to hide scripts / styles from older browsers will put..)


    Make sure you remove the <?php line from the plugin and give it the OnWebPagePrerender event.

    <?php
    // +----------------------------------------------------------------------+
    // | CleanOutput                                                          |
    // +----------------------------------------------------------------------+
    // | Created February 2006                                                |
    // +----------------------------------------------------------------------+
    // | Author: Mark Kaplan                                                  |
    // +----------------------------------------------------------------------+
    
    $e = $modx -> Event;
    $content = "";
    switch ($e -> name) {
        case "OnWebPagePrerender":
            $content = $modx -> documentOutput;
            $content = preg_replace('/\n|\r/', '', $content);
            $content = preg_replace('/<!--(.|\s)*?-->/', '', $content);
            $modx -> documentOutput = $content;
            break;
        default :
            return; // stop here - this is very important.
            break;
            }
    
    
      • 31337
      • 258 Posts
      It’s critical to note that this code will remove any Javascript you have in your code. Also, if you have nested comments, everything goes "Boom!"

      Here’s a trivial example:

      <?php
      $content = '<script type="text/javascript">
      <!--
      $foo = "bar";
      <!-- comment on Javascript code here -->
      -->
      <html stuff here>
      ';
      $content = preg_replace('/<!--(.|\s)*?-->/s', '', $content);
      echo $content;
      ?>
      ~       
        • 31337
        • 258 Posts
        As a follow up, I think the only reliable, clean way to manage is this is either:
        1) Write your own HTML parser to strip out the comments or
        2) Convert $content into a DOM tree and walk the tree looking for "comment" elements and remove them


        Having said that, this gets you almost there for 90% of the cases:
        $content = preg_replace('/<!(.*?)(--.*?--\s*)+(.*?)>/s', '', $content);
        $content = preg_replace('/-->/', '', $content);


        This one does 2 things differently:
        1) It tries to be a little smarter about where and how it finds the beginning and end of comments and
        2) it cleans up after itself for any nested comments that got missed.

        Again, I would not recommend this for production code as it will break on any badly formatted HTML or unbalanced comments or other similiar silly things. Also, this does get rid of any Javascript script code that’s inside of comments (to hide it from older browsers). Whether this is a feature or a bug is debatable grin

          • 32241
          • 1,495 Posts
          Nice one though. I would love to see more powerful regexp that won’t strip out javascript, but hey, this alone is good enough. I’ll surely give this a try, when I built a site that needs faster download time.

          Thanks for the great plugin Mark, and thanks for the great input victor.
            Wendy Novianto
            [font=Verdana]PT DJAMOER Technology Media
            [font=Verdana]Xituz Media
          • Is it possible to run that plugin to clean the code, then have it (or another plugin) load javascript via regClientScript and/or regClientStartupScript after the cleanup?
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 31337
              • 258 Posts
              Quote from: sottwell at Feb 06, 2006, 11:58 AM

              Is it possible to run that plugin to clean the code, then have it (or another plugin) load javascript via regClientScript and/or regClientStartupScript after the cleanup?

              Well...tell me how to differentiate between HTML comments and Javascript inside comment tags, that’s there for the purpose of hiding it from older browser grin
              • Actually, there’s very little if any javascript that has to be in the html file, it can all be in external files and included as needed. In fact, if you want valid XHTML it almost has to be, since a few perfecty normal javascript constructs cause validation to fail. And since the browser will cache the .js files, you won’t have the code taking up space in every page.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 32241
                  • 1,495 Posts
                  That does make sense. External JS will be the way to go I believe. Same thing with CSS
                    Wendy Novianto
                    [font=Verdana]PT DJAMOER Technology Media
                    [font=Verdana]Xituz Media
                    • 31337
                    • 258 Posts
                    Yep. I am not arguing about where JS code should be located, I agree with you.

                    My point was that *if* there was any Javascript stored in $content, this regexp would wipe it out along with HTML comments