I found the best answer to this question before and accepted the code posted, set it up and made it mine, but I can't find it anywhere.
Anyway, here is the solution I made from it.
Now the encoder only supports , & , " , < and > , but itβs very easy to add support for more HTML objects.
First of all, here is Encoder:
var Encoder = { encode: (function() { var translate_re = /&(nbsp|amp|quot|lt|gt);/g, translate = { 'nbsp': String.fromCharCode(160), 'amp' : '&', 'quot': '"', 'lt' : '<', 'gt' : '>' }, translator = function($0, $1) { return translate[$1]; }; return function(s) { if(typeof s === 'string') return s.replace(translate_re, translator); else return s; }; })(), decode: (function() { var reg_str = '(<|>|"|&|' + String.fromCharCode(160) + ')'; var translate_re = new RegExp(reg_str, 'g'); var translate = { '&' : '&', '"': '"', '<' : '<', '>' : '>' }; translate[String.fromCharCode(160)] = ' '; var translator = function($0, $1) { return translate[$1]; }; return function(s) { if(typeof s === 'string') return s.replace(translate_re, translator); else return s; }; })() };
var Encoder = { encode: (function() { var translate_re = /&(nbsp|amp|quot|lt|gt);/g, translate = { 'nbsp': String.fromCharCode(160), 'amp' : '&', 'quot': '"', 'lt' : '<', 'gt' : '>' }, translator = function($0, $1) { return translate[$1]; }; return function(s) { if(typeof s === 'string') return s.replace(translate_re, translator); else return s; }; })(), decode: (function() { var reg_str = '(<|>|"|&|' + String.fromCharCode(160) + ')'; var translate_re = new RegExp(reg_str, 'g'); var translate = { '&' : '&', '"': '"', '<' : '<', '>' : '>' }; translate[String.fromCharCode(160)] = ' '; var translator = function($0, $1) { return translate[$1]; }; return function(s) { if(typeof s === 'string') return s.replace(translate_re, translator); else return s; }; })() };
* { font: 13.2px "Courier New", Arial, sans-serif; } body { font-size: 100%; } .row { width:100%; height:auto; padding: 8px 6px; }
With HTML Entities: <div id="output_not_endcoded" class="row" ></div> <br/> With HTML Entities Decoded: <div id="output_endcoded" class="row" ></div>
It is very easy to add support for other HTML objects.
Looking at the encoder, you will see our translation section. One part contains the regular expression and the other part contains our translation fields.
Regex:
var translate_re = /&(nbsp|amp|quot|lt|gt);/g
Translations:
translate = { 'nbsp': String.fromCharCode(160), 'amp' : '&', 'quot': '"', 'lt' : '<', 'gt' : '>' }
Say you wanted to add support for the copyright symbol "Β©". The object name for this character is ©. To add support for this character, simply add it to the regular expression and translation:
Regex:
var translate_re = /&(nbsp|amp|quot|lt|gt|copy);/g
Translations:
translate = { 'nbsp': String.fromCharCode(160), 'amp' : '&', 'quot': '"', 'lt' : '<', 'gt' : '>', 'copy': 'Β©', }
You will need to make sure that you add support for both encoding and decoding functions if you want full support for encoding and decoding.
And this! Hope this helps!