rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
37 lines (36 loc) • 791 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const replace = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
'\'': '''
};
/**
* Parse HTML Content to remove XSS (if used properly)
* @example
* ```
* const userInput = '<script>alert("OOps")</script>'
*
* const insecure = `
* <p>Message:</p>
* <p>${userInput}</p>
* `
*
* const secure = html`
* <p>Message:</p>
* <p>${userInput}</p>
* `
* ```
* @since 8.7.0
*/ function html(parts, ...variables) {
let result = '';
for (let i = 0; i < parts.length; i++) {
result += parts[i];
if (variables[i])
result += String(variables[i]).replace(/[&<>"']/g, (m) => replace[m]);
}
return result;
}
exports.default = html;