1.31.07 True ColdFusion whitespace removal + CFAJAX support

To go on my other article, I added two lines to my onRequestEnd function as set in the Application.cfc. These lines will parse out preceding and trailing whitespace for all CFAJAX responses if your CF server is still having trouble doing proper “whitespace management”.

<cffunction name="onRequestEnd" output="Yes"><cfsetting enablecfoutputonly="Yes">
    <!--- forceful strip of whitespaces from CFMX Java output buffer - idea thanks to Dmitry Namiot --->
    <cfset pageContent = getPageContext().getOut().getString()>
    <cfset getPageContext().getOut().clearBuffer()>
    <!--- AJAX whitespace stripping --->
    <cfset pageContent = REReplace(pageContent, "^[[:space:]]+\*\*\*\*\/", " ****/", "all" )>
    <cfset pageContent = REReplace(pageContent, "\*\/[[:space:]]+$", "*/", "all" )>
    <cfset pageContent = REReplace(pageContent, "^[[:space:]]+<", "<", "all" )>
    <cfset pageContent = REReplace(pageContent, ">[[:space:]]+$", ">", "all" )>
    <!--- capture pure whitespace pages for those CFLOCATION tags in the middle of a logic block --->
    <cfset pageContent = REReplace(pageContent, "^[[:space:]]+$", "", "all" )>
    <cfset writeOutput(pageContent)>
    <cfset getPageContext().getOut().flush()>
<cfsetting enablecfoutputonly="No"></cffunction>

Download this code: onrequestend-whitespace-ajax


5 Comments
Jonathan van Zuijlekom @ 10.27.07 10am

The holy grail in coldfusion whitespace management. I added the following lines to remove tabs, carriage returns and line feeds:

#341 217chars
Jonathan van Zuijlekom @ 10.27.07 10am

The lines got filtered, another try:

cfset pageContent = REReplace(pageContent, chr(9), “”, “all” )
cfset pageContent = REReplace(pageContent, chr(13), “”, “all” )
cfset pageContent = REReplace(pageContent, chr(10), “”, “all” )

#342 232chars
Jonathan van Zuijlekom @ 10.27.07 11am

it better to replace them with a space and the compress al whitespace to a single space

cfset pageContent = Replace(pageContent, chr(9), ” “, “all” )
cfset pageContent = Replace(pageContent, chr(13), ” “, “all” )
cfset pageContent = Replace(pageContent, chr(10), ” “, “all” )
cfset pageContent = REReplace(pageContent, “[[:space:]]+”, ” “, “all” )

#343 353chars
Jonathan van Zuijlekom @ 10.27.07 11am

Its not necessary to remove the tabs chr(9) ’cause [:space:] includes tabs.

Only use this when not using pre tags!

#344 118chars
Jim Palmer @ 10.31.07 2pm

Jonathan,

Using the Chr() function makes more sense than using the [[::]] regex aliases! Thanks for adding that!

Also, very good call on NOT using ANY

 tags with the [[:space:]] seeing as it the tabs are stripped. I honestly never thought of that seeing as I rarely use the 
 tag. I was on a mission to remove characters from being sent to the user seeing as I wrote this at the same time as fixing HTTP compression - so "Less Is Better" was the mindset at the time.

Any other characters we should strip?
#348 522chars



monetary contributions are always accepted $
0.547s