123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- /**
- * Simple sanitization. It is not intended to sanitize
- * malicious element values.
- *
- * character | escaped
- * < <
- * > >
- * ( (
- * ) )
- * # #
- * & &
- * " "
- * ' '
- */
- var chars = {
- '&': '&',
- '#': '#',
- '<': '<',
- '>': '>',
- '(': '(',
- ')': ')',
- '"': '"',
- "'": '''
- };
- function escapeRegExp(string) {
- return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
- }
- exports.sanitize = function sanitize(value, reverse) {
- if (typeof value !== 'string') {
- return value;
- }
- Object.keys(chars).forEach(function(key) {
- if (reverse) {
- value = value.replace(new RegExp(escapeRegExp(chars[key]), 'g'), key);
- } else {
- value = value.replace(new RegExp(escapeRegExp(key), 'g'), chars[key]);
- }
- });
- return value;
- }
|