CFLib.org – Common Function Library Project

HtmlCompressFormat(sInput)

Last updated November 19, 2002
Download UDF

author

Jordan Clark                                      Jordan Clark

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 4 time(s). Average Rating: 4.5

Description:
This function is useful to reduce download time of your entire webpage. Client's will load pages noticably faster. Also very useful to compress a page before caching it for storage. Code is very useful in the fusebox 3 methodology, wrap this around your #fusebox.layout# variable to compress the html output. Code works on 3 levels of compression, and can easily be customized for more or different ways. Level 1: Replaces excessive spaces with no side effects. Level 2: (default) Removes more blank space but keeps html tags from wrapping into each other, so the code is still readable (maybe even more so) Level 3: Replaces all comments and any spaces possible, the side effect is space between html tags is removed, so alignment changes are possible.

Return Values:
Returns a string.

Example:

<cfoutput>
<cfsavecontent variable="html">
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td colspan="2">
        <b>Table Title</b>
    </td>
</tr>
<tr>
<td>
        Column 1
    </td>
<td>
        Column 2
    </td>
</tr>
</table>
</cfsavecontent>
<b>Before:</b><br>
#HtmlCodeFormat(html)#
<p>
<b>After:</b><br>
#HtmlCodeFormat(htmlCompressFormat(html, 2))#

</cfoutput>

Parameters:

Name Description Required
sInput HTML you wish to compress. Yes

Full UDF Source:

<cfscript>
/**
* Replaces a huge amount of unnecessary whitespace from your HTML code.
*
* @param sInput      HTML you wish to compress. (Required)
* @return Returns a string.
* @author Jordan Clark (JordanClark@Telus.net)
* @version 1, November 19, 2002
*/

function HtmlCompressFormat(sInput)
{
var level = 2;
if( arrayLen( arguments ) GTE 2 AND isNumeric(arguments[2]))
{
level = arguments[2];
}
// just take off the useless stuff
sInput = trim(sInput);
switch(level)
{
case "3":
{
// extra compression can screw up a few little pieces of HTML, doh
sInput = reReplace( sInput, "[[:space:]]{2,}", " ", "all" );
sInput = replace( sInput, "> <", "><", "all" );
sInput = reReplace( sInput, "<!--[^>]+>", "", "all" );
break;
}
case "2":
{
sInput = reReplace( sInput, "[[:space:]]{2,}", chr( 13 ), "all" );
break;
}
case "1":
{
// only compresses after a line break
sInput = reReplace( sInput, "
(" & chr( 10 ) & "|" & chr( 13 ) & ")+[[:space:]]{2,}", chr( 13 ), "all" );
break;
}
}
return sInput;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson