Spaces:
Running
Running
File size: 1,336 Bytes
30c32c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
const log = require('./log');
/**
* 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 | !Array.<string>} unsafe Unsafe string.
* @return {string} XML-escaped string, for use within an XML tag.
*/
const xmlEscape = function (unsafe) {
if (typeof unsafe !== 'string') {
if (Array.isArray(unsafe)) {
// This happens when we have hacked blocks from 2.0
// See #1030
unsafe = String(unsafe);
} else {
log.error(`Unexptected type ${typeof unsafe} in xmlEscape at: ${new Error().stack}`);
return unsafe;
}
}
return unsafe.replace(/[<>&'"]/g, c => {
switch (c) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '\'': return ''';
case '"': return '"';
}
});
};
/**
* creates escaped text suitible for attributes
* @param {string} unsafe the contents to escape
* @returns {string} escaped contents
*/
const escapeAttribute = unsafe => {
const escaped = xmlEscape(unsafe);
return JSON.stringify(escaped).slice(1, -1);
};
module.exports = xmlEscape;
module.exports.escapeAttribute = escapeAttribute;
|