chanjs 2.0.14 → 2.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/helper/html.js +22 -2
- package/middleware/setBody.js +1 -0
- package/package.json +1 -1
package/helper/html.js
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 处理最常用的HTML特殊字符实体
|
|
2
|
+
* 处理最常用的HTML特殊字符实体 - 编码
|
|
3
|
+
* @param {string} str - 需要编码的字符串
|
|
4
|
+
* @returns {string} 编码后的字符串
|
|
5
|
+
*/
|
|
6
|
+
export const htmlEncode = (str) => {
|
|
7
|
+
// 非字符串直接返回,避免报错
|
|
8
|
+
if (typeof str !== 'string') return str;
|
|
9
|
+
|
|
10
|
+
// 一次性替换所有特殊字符,减少函数调用
|
|
11
|
+
return str.replace(/[&<>"' ]/g, match => ({
|
|
12
|
+
'&': '&',
|
|
13
|
+
'<': '<',
|
|
14
|
+
'>': '>',
|
|
15
|
+
'"': '"',
|
|
16
|
+
"'": ''',
|
|
17
|
+
' ': ' '
|
|
18
|
+
}[match]));
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 处理最常用的HTML特殊字符实体 - 解码
|
|
3
23
|
* @param {string} str - 需要解码的字符串
|
|
4
24
|
* @returns {string} 解码后的字符串
|
|
5
25
|
*/
|
|
@@ -16,4 +36,4 @@ export const htmlDecode = (str) => {
|
|
|
16
36
|
''': "'",
|
|
17
37
|
'"': '"'
|
|
18
38
|
}[match]));
|
|
19
|
-
}
|
|
39
|
+
};
|
package/middleware/setBody.js
CHANGED