mirror of https://github.com/jkjoy/sunpeiwen.git
26 lines
513 B
JavaScript
26 lines
513 B
JavaScript
'use strict';
|
|
|
|
const unescapeHTML = require('./unescape_html');
|
|
|
|
const htmlEntityMap = {
|
|
'&': '&',
|
|
'<': '<',
|
|
'>': '>',
|
|
'"': '"',
|
|
'\'': ''',
|
|
'`': '`',
|
|
'/': '/',
|
|
'=': '='
|
|
};
|
|
|
|
function escapeHTML(str) {
|
|
if (typeof str !== 'string') throw new TypeError('str must be a string!');
|
|
|
|
str = unescapeHTML(str);
|
|
|
|
// http://stackoverflow.com/a/12034334
|
|
return str.replace(/[&<>"'`/=]/g, a => htmlEntityMap[a]);
|
|
}
|
|
|
|
module.exports = escapeHTML;
|