Using Canonicalize() To Render Emoji In ColdFusion
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: 🚨
</li>
<li>
HTML Entity, code point: 🚨
</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:
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
In the outro above, I mention that emojis don't work well in subject lines. Here's a fast-follow post to demonstrate that:
www.bennadel.com/blog/4709-using-canonicalize-to-embed-emoji-in-email-subject-lines-in-coldfusion.htm
And, how the
canonicalize()
function can be used to render HTML entities and emojis in email subject lines.Post A Comment — ❤️ I'd Love To Hear From You! ❤️
Post a Comment →