• Home
  • Flailing Wildly
  • Résumé
  • Design
  • Code
  • Wishlist
  • About Me

Flailing Wildly
Too much straw, not enough camel.

document.createTextNode and entities

by Ryan Parman • February 25, 2006 • Code • 2 comments

One problem that I’ve had when working with dynamic DOM nodes is the inability to use entities with document.createTextElement(). I’ve read suggestions about using utf-16/ucs-2 values, but how do you find them and how do you convert them?

This led me on a journey to figure out how to handle this. I ended up digging through some TinyMCE source code, and found a gem of a function that I then added a ‘stupefy’ mode to.

Here’s the code:

function entity(str, mode) {
	var str = (str) ? str : '';
	var mode = (mode) ? mode : 'string';

	var e=document.createElement("div");
	e.innerHTML=str;

	if (mode=='numeric') {
		return'&#'+e.innerHTML.charCodeAt(0)+';';
	}
	else if (mode=='utf16') {
		var un=e.innerHTML.charCodeAt(0).toString(16);
		while(un.length<4) un="0"+un;
		return"\\u"+un;
	}
	else return e.innerHTML;
}

entity() has two parameters:

  1. entity: is a string which can be either a named entity (&raquo;) or a numeric entity (&#187;)
  2. mode: is an optional value that accepts ‘numeric’, ‘utf16′, or ‘string’. Defaults to ‘string’.

You’d use it like this:

// Normal mode
var div = document.createElement('div');
var text = document.createTextNode('Parent '+[b]entity([/b]'»'[b])[/b]+' Child');
div.appendChild(text);

// Stupefy mode
var num = "The numeric entity for » is "+entity('»', 'numeric');
var utf = "The UTF-16 entity for » is "+entity('»', 'utf16');

I hope this can help other people out there who’ve run into the same problem as many times as I have!

Ryan Parman

Ryan Parman is an entrepreneur, open source evangelist and passionate usability advocate currently living in Seattle. He is the founder and visionary behind SimplePie and CloudFusion, co-founder of WarpShare, member of the RSS Advisory Board, and is currently with Amazon. Ryan's aptly-named blog, Flailing Wildly, is where he writes about ideas longer than 140 characters.

« Camino has left the building
TinyMCE Issue »

Discussion

Andrew K.

February 26, 2006

I think you’ll be interested in this:
http://slayeroffice.com/tools/unicode_lookup/
:)

Ryan Parman

February 26, 2006

Ooooh… that’s nifty! I’ll have to find a way to work that in, because that’s awfully useful. Thanks!

Comments for this post are now closed.

Blog search

Archives
  • 2010 (7)
  • 2009 (7)
  • 2008 (12)
  • 2007 (8)
  • 2006 (18)
  • 2005 (57)
  • 2004 (104)
  • 2003 (103)
Categories
  • Apple (43)
  • Browsers (55)
  • Code (57)
  • Creating Websites (31)
  • Design (5)
  • Digital Media (3)
  • Family Life (10)
  • Just for Fun (25)
  • Music (5)
  • Personal (32)
  • Political (12)
  • Projects (44)
  • Software (60)
  • Syndication (28)
  • Technology (75)
  • Tutorials (5)
  • TV and Movies (16)
  • Video Games (5)
  • Website (62)
  • Work and Business (3)
  • Writing (4)
Socially-aware
  • Twitter
  • Facebook
  • Flickr
  • Last.fm
  • Glue
  • YouTube
  • Delicious
  • LinkedIn
Claim to fame
  • Amazon Web Services
  • WarpShare
  • CloudFusion
  • SimplePie
Legal mumbo-jumbo

All ideas, opinions and comments I post are my own and are in no way affiliated with anybody I work with. If you quote and/or reprint something I've written or said, please direct folks back to this site as a form of attribution. I promise I'll do the same for you. Unless otherwise noted, all content on this site is copyright © 1979–2010 Ryan Parman. Powered by Rocket Sauce.