I have a situation where I'm generating xml for a SOAP request and receiving the data that goes into this xml from a 3rd party. This code is running on a server and doesn't have access to functions in the DOM. Sometimes the data will have xml entities already encoded and other times it will not.
For example sometimes I will receive this:
Billy & Joe's Garage
And other times I will receive this:
Billy & Joe's Garage
I know there are solutions to handling the first example like those found on this post: how to escape xml entities in javascript?
But if I apply those solutions to the second example I will get something like:
function escapeXml(unsafe) {
return unsafe.replace(/[<>&'"]/g, function (c) {
switch (c) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '\'': return ''';
case '"': return '"';
}
});
}
escapeXml("Billy & Joe's Garage")
// Returns "Billy &amp; Joe&apos;s Garage"
So for the second example the desired output would be the same as the input.