When you want to convert htmlspecialchars in javascript to not so dangerous text and decode those html entities back again, you may have some convenient methods on a dom entity (like mootools .get('html') and .get('text')).
If you want to do that simple work on simple strings, I use the following functions:
var xml_special_to_escaped_one_map = {
'&': '&',
'"': '"',
'<': '<',
'>': '>'
};
var escaped_one_to_xml_special_map = {
'&': '&',
'"': '"',
'<': '<',
'>': '>'
};
function encodeXml(string) {
return string.replace(/([\&"<>])/g, function(str, item) {
return xml_special_to_escaped_one_map[item];
});
};
function decodeXml(string) {
return string.replace(/("|<|>|&)/g,
function(str, item) {
return escaped_one_to_xml_special_map[item];
});
}