Last year my colleague asked me help to implement an insertion of
html-entities. This task was interesting but the searching was attractive because there are many copies of the manual parsing of the datum list of entities -
phpjs.org :: html_entity_decode. It is bombastic code, in our opinion is unreasonable - we was looking for the compact and easy code using internal features of browsers. We found the solution completely satisfying our requests - it is compact, easy and uses internal parsing algorithms of browsers.
text in Russian -
http://with-love-from-siberia.blogsp...-entities.html
source code -
http://code.google.com/p/jsxt/source.../web/Entity.js
Code:
var Entity = {
encode: function(value)
{
var div = document.createElement('div');
var text = document.createTextNode(value);
div.appendChild(text);
return div.innerHTML.replace(/"/g, '"').replace(/'/g, ''');
},
decode: function(value)
{
var textarea = document.createElement('textarea');
textarea.innerHTML = value;
return textarea.value;
}
};
var original = '& is <ampersand>';
var encoded = Entity.encode(original);
var decoded = Entity.decode(encoded);
alert([original, encoded, decoded].join('\n'));
// will output
// & is <ampersand>
// & is <ampersand>
// & is <ampersand>