mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-26 01:19:51 -05:00
21 lines
607 B
JavaScript
21 lines
607 B
JavaScript
/**
|
|
* Escape a string to be safe to use in XML content.
|
|
* CC-BY-SA: hgoebl
|
|
* https://stackoverflow.com/questions/7918868/
|
|
* how-to-escape-xml-entities-in-javascript
|
|
* @param {!string} unsafe Unsafe string.
|
|
* @return {string} XML-escaped string, for use within an XML tag.
|
|
*/
|
|
const xmlEscape = function (unsafe) {
|
|
return unsafe.replace(/[<>&'"]/g, c => {
|
|
switch (c) {
|
|
case '<': return '<';
|
|
case '>': return '>';
|
|
case '&': return '&';
|
|
case '\'': return ''';
|
|
case '"': return '"';
|
|
}
|
|
});
|
|
};
|
|
|
|
module.exports = xmlEscape;
|