	/*
http://docs.emmet.io/
	* template:
	* https://github.com/Shopify/liquid/wiki/Liquid-for-Designers
	* https://github.com/paularmstrong/swig
	* http://jade-lang.com/reference/#plaintext
	* https://github.com/slim-template/slim
	* https://docs.djangoproject.com/en/1.6/topics/templates/
	* http://bits.shutterstock.com/2013/03/07/mustache-vs-swig-templating-shootout/
	* http://paularmstrong.github.io/node-templates/benchmarks.html
	* http://garann.github.io/template-chooser/
	* http://engineering.linkedin.com/frontend/client-side-templating-throwdown-mustache-handlebars-dustjs-and-more
	* https://github.com/joyent/node/wiki/modules#templating
	*
	*
	* i18n:
	* http://slexaxton.github.io/Jed/
	*
	* plural:
	* http://stackoverflow.com/questions/1353408/messageformat-in-javascript-parameters-in-localized-ui-strings
	* https://docs.google.com/presentation/d/1ZyN8-0VXmod5hbHveq-M1AeQ61Ga3BmVuahZjbmbBxo/pub?start=false&loop=false&delayms=3000#slide=id.g1bc43a82_2_83
	* https://gist.github.com/kjantzer/4957176
	*
	* Backbone is event-driven, not callback driven framework
	*/



Notes
-----

-   createTextNode is supposed to take any text input 
    and insert it into the DOM exactly like it is. 
    This makes it impossible to insert for example HTML elements, 
    and HTML entities. 
    It’s actually a feature, so you don’t need to escape these first. 
    Instead you just operate on the DOM to insert text nodes.






Example

var text = document.createTextNode('Mmm ... something');
The example above creates a new Text node and saves it to the variable text. The node can then be added to an element like this:
element.appendChild(text);
The argument to createTextNode can only be plain text — you cannot pass entity references (for that you would need the createEntityReference method, which is poorly supported) — however you can pass unicode characters, such as \u00a0 for a non-breaking space.
Since the createTextNode method returns the created Text node, you can also use it directly as the argument to appendChild, which slightly reduces the amount of code (and typing!) involved:
element.appendChild(document.createTextNode('Mmm ... something'));





You can't create nodes with HTML entities. Your alternatives would be to use unicode values

var dropdownTriggerText = document.createTextNode('blabla \u0026');
or set innerHTML of the element. You can of course directly input &...









Nope, it's not specific to the XML (Core) DOM - a TextNode can only contain pure text. If you want to create an entity, you have to use Document.createEntityReference which to my knowledge isn't implemented anywhere (or at least wasn't working even in moz last time I tried to use it).

It takes a DOMString with UTF-16 characters, so I guess you can use a JavaScript string with U+00bb to get your result. ("\u00bb")






Entity references are necessary in the HTML or XML markup. For example, &lt; in a text file tells a parser that the '<' character is part of the data, not the start of a new tag.

But once a document is parsed into a DOM representation, you no longer need to distiguish between the markup and the data. Programming object represent nodes, rather than syntactic strings of text. The DOM doesn't need entity references because there's no mistaking between what's structure and what's data. That's why it gives you the literal text.

How you define characters within the DOM depends on the particular DOM API. liorean's suggestion works in JavaScript because that language uses the \u escape sequence in string literals to define arbitrary Unicode characters. I think C and Java use that too, but other languages might use other methods. It's all dependent on the language of the DOM implementation, not on the DOM or XML standards.

I hadn't heard of document.createEntityReference before but I suspect it's a convenience function that will maps an entity reference to a native character code for you. That would be a nice alternative to looking up the codes.





Entities are a bit more than that. An entity in HTML is usually a single character - it needn't be. And entity can be a strings of longer content, composed of other entities and/or characters. They are defined by the DTD, though, and that's where the problem lies: our browsers are not validating XML parsers, even if expat and msxml have that ability - they don't parse the DTD, they merely checks if one they recognise exists, and use that one.
In other words, they don't "get" entities. They resolve the standard html set of them, but they don't let the user define new ones.

An entity reference is what it sounds like, a reference to an entity - the entity itself is what tells us what content it's to be replaced with. (Look on entities as the declaration of a variable - and entity references as the usage of that variable. It's the closest analogue that I can think of.)







createTextNode and entities

 
Modified with a newer version since it was first posted. Some comments may refer to older versions.

Code:
function entity(str, mode) {
    str = (str) ? str : "";
    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:
- entity: is a string which can be either a named entity (&raquo;), numeric entity (&#38;#187;), UTF-16 value (\u00bb), or even the character itself (&#187;).
- mode: is an optional value that can be 'string', 'numeric', or 'utf16'. This tells the function what to return the value as. Defaults to 'string'.

You'd use it like this:
Code:
// String mode
var div = document.createElement('div');
var text = document.createTextNode('Parent '+entity('&raquo;')+' Child');
div.appendChild(text);

// Numeric mode
var description = "The entity for &#187; is "+entity('&#187;', 'numeric');

// UTF-16 mode
var description = "The UTF-16 value for &#187; is "+entity('&#187;', 'utf16');
Hope this helps!

function entity(str,stupefy){
    var
        e=document.createElement("div");
    e.innerHTML=String(str);

    if (stupefy)
        return '&#'+e.innerHTML.charCodeAt(0)+';';
    return e.innerHTML;
}


