Skip to main content
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: Jason Dean
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: Jason Dean

Using Canonicalize() To Render Emoji In ColdFusion

By
Published in Comments (1)

In ColdFusion, the canonicalize() function is used to reduce a given string down to its simplest form. This is typically used during user input sanitization and validation; but, this normalization process can also be used to convert HTML entities into their associated characters. In other words, we can use the canonicalize() function to convert encoded emoji characters into native emoji glyphs.

To see this in action, let's take the police siren emoji and render it to the browser using both HTML entities and canonicalized strings:

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
</head>
<body>

	<h1>
		Using Canonicalize to Render Emoji In ColdFusion
	</h1>

	<ul>
		<li>
			HTML Entity, hex: &#x1F6A8;
		</li>
		<li>
			HTML Entity, code point: &#128680;
		</li>
		<li>
			Canonicalize hex:
			<cfoutput>#canonicalize( "&##x1F6A8;", true, true )#</cfoutput>
		</li>
		<li>
			Canonicalize code point:
			<cfoutput>#canonicalize( "&##128680;", true, true )#</cfoutput>
		</li>
	</ul>

</body>
</html>

Notice that I'm using two instances of the canonicalize() function: one that takes a hex-based HTML entity and one that takes a decimal-based HTML entity. In both cases, the canonicalize() function will simplify the string by converting the encoded value into the native character.

Now, if we render this ColdFusion page and inspect the source code, here's what we see:

Screen and source rendering of a ColdFusion page with two HTML encoded emoji and two native emoji.

As you can see, the two canonicalize() calls return actual, native emoji characters in the page response.

In most cases, there's no inherent need to return a native emoji character instead of an encoded emoji character. But, there are context in which encoded characters aren't supported (such as email subject lines). In such cases, using the canonicalize() function can help. This is much simpler than my previous exploration that used the java.lang.Character class for decoding emoji.

Want to use code from this post? Check out the license.

Reader Comments

Post A Comment — I'd Love To Hear From You!

Post a Comment

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel