Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping and unescaping HTML

In a function I do not control, data is being returned via

return xmlFormat(rc.content)

I later want to do a

<cfoutput>#resultsofreturn#</cfoutput>

The problem is all the HTML tags are escaped.

I have considered

<cfoutput>#DecodeForHTML(resultsofreturn)#</cfoutput>

But I am not sure these are inverses of each other

like image 728
James A Mohler Avatar asked Apr 25 '26 03:04

James A Mohler


1 Answers

As of CF 10, you should be using the newer encodeFor functions. These functions account for high ASCII characters as well as UTF-8 characters.

Old and Busted

  • XmlFormat()
  • HTMLEditFormat()
  • JSStringFormat()

New Hotness

  • encodeForXML()
  • encodeForXMLAttribute()
  • encodeForHTML()
  • encodeForHTMLAttribute()
  • encodeForJavaScript()
  • encodeForCSS()

The output from these functions differs by context.

Then, if you're only getting escaped HTML, you can convert it back using Jsouo or the Jakarta Commons Lang library. There are some examples in a related SO answer.

Obviously, the best solution would be to update the existing function to return either version of the content. Is there a way to copy that function in order to return the unescaped content? Or can you just call it from a new function that uses the Java solution to convert the HTML?

like image 143
Adrian J. Moreno Avatar answered May 05 '26 19:05

Adrian J. Moreno