Skip to content

Commit d82475f

Browse files
authored
Optimize HTML entity encoding
1 parent fe749fb commit d82475f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/util.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
// DOM properties that should NOT have "px" added when numeric
22
export const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|^--/i;
33

4-
export let encodeEntities = s => String(s)
5-
.replace(/&/g, '&')
6-
.replace(/</g, '&lt;')
7-
.replace(/>/g, '&gt;')
8-
.replace(/"/g, '&quot;');
4+
export function encodeEntities(s) {
5+
let out = '';
6+
for (let i=0; i<s.length; i++) {
7+
let ch = s[i];
8+
switch (ch) {
9+
case '<': ch = '&lt;';
10+
case '>': ch = '&gt;';
11+
case '"': ch = '&quot;';
12+
case '&': ch = '&amp;';
13+
}
14+
out += ch;
15+
}
16+
return out;
17+
}
18+
919

1020
export let indent = (s, char) => String(s).replace(/(\n+)/g, '$1' + (char || '\t'));
1121

0 commit comments

Comments
 (0)