HTMLCodeFormat Difference Between Adobe CF and Railo

Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

Today I came across a small but annoying difference between the way Adobe CF and Railo implement the HTMLCodeFormat() function that I want to share.

A few days ago I turned on whitespace management on my Railo server and noticed that the code blocks in my blog entries were losing all their indenting. I checked the database and all the carraige returns and tabs were still in their so I am fairly confident that the whitespace management was to blame. Now I could have simply turned it back off but I didnt really want to so I decided to try code around the problem.

Solution On Railo

So after a little research I came up with the simple code block to convert carraige returns and tabs to html. I tested and deployed it all on Railo and life was good.

<!--- Escape all the code I want to display in my code block --->
<cfset formatedCode = htmlCodeFormat(rawCode)>            

<!--- Convert carriage returns to html <br/> --->
<cfset formatedCode = REReplaceNoCase(formatedCode,"\r","<br/>","all")>

<!--- Convert tabs to 5 * html &nbsp; --->
<cfset formatedCode = REReplaceNoCase(formatedCode,"\t","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","all")>

Adobe CF Is Different

So today I come along and decide to do some cross platform testing on my site. I load up my local copy in railo and everything looks fine, but when I load it in Adobe CF all my code blocks are missing their indenting and carraige returns.

Needless to say this had me scratching my head for a while until I noticed a small difference between the Adobe livedocs and the Railo wiki for the HTMLCodeFormat() function. Note the following highlighted difference in the function documentation.

Railo HTMLCodeFormat()

Replaces special characters in a string with their HTML-escaped equivalents and inserts <pre> and </pre> tags at the beginning and end of the string.

Adobe HTMLCodeFormat()

HTML-escaped string string, enclosed in <pre> and </pre> tags. Return characters are removed; line feed characters are preserved. Characters with special meanings in HTML are converted to HTML character entities such as >.

The Solution

Now I really didn't need to do anything to fix this as it is unlikely I will every run my blog on Adobe CF, but just on the off chance I decided I might as well fix it while I know what the issue is.

<!--- Replace all carriage returns with a temporary placeholder --->
<cfset formatedCode = REReplaceNoCase(formatedCode,"(\r)","
"
,"all")>


<!--- Escape all the code I want to display in my code block --->
<cfset formatedCode = htmlCodeFormat(rawCode)>            

<!--- Convert marked carriage return placeholders to html <br/> --->
<cfset formatedCode = REReplaceNoCase(formatedCode,"
"
,"<br/>","all")>


<!--- Convert tabs to 5 * html &nbsp; --->
<cfset formatedCode = REReplaceNoCase(formatedCode,"\t","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","all")>

As you can see my solution was to replace all the carriage returns with a place holder string which I knew would be unique before escaping my code. Then once I had escaped my code I could safely convert the place holder to actual html <br/> tags.

Now there might be a more elegant solution than this (I'd love to hear about it), but for now this code works consistently on Railo and Adobe CF and gives me my desired output.

Reader Comments

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing