cleanHtml.js
895 Bytes
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
'use strict';
const re = /(\r\n)|["\'<>]/g;
const htmlEntity = {
'&': '\u0026',
'"': '\u0022',
''': '\u0027',
'<': '\u003c',
'>': '\u003e'
};
exports.htmlDecode = function(txt) {
txt = txt || '';
return txt.replace(/((&(([a-z][a-z0-9]*)|(#[0-9]+)|(#x[0-9a-f]+));)|["'<>&])/gi, function(s) {
s = s || '';
const s1 = htmlEntity[s.toLowerCase()];
if (s1) {
s = s1;
}
return s;
});
};
exports.htmlEncode = function(str) {
str = str || '';
return str.replace(re, function(s) {
switch (s) {
case '"':
return '"';
case '\'':
return ''';
case '<':
return '<';
case '>':
return '>';
default:
return s;
}
});
};