Skip to content

Commit 67e27b6

Browse files
authored
comments
1 parent 35a2e84 commit 67e27b6

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/util.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ export const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine
44
const ENCODED_ENTITIES = /[&<>"]/;
55

66
export function encodeEntities(str) {
7+
// Ensure we're always parsing and returning a string:
8+
str += '';
9+
710
// Skip all work for strings with no entities needing encoding:
811
if (ENCODED_ENTITIES.test(str) === false) return str;
912

10-
let start = 0,
13+
let last = 0,
1114
i = 0,
1215
out = '',
1316
ch = '';
17+
18+
// Seek forward in str until the next entity char:
1419
for (; i<str.length; i++) {
1520
switch (str.charCodeAt(i)) {
1621
case 60: ch = '&lt;'; break;
@@ -19,11 +24,13 @@ export function encodeEntities(str) {
1924
case 38: ch = '&amp;'; break;
2025
default: continue;
2126
}
22-
if (i > start) out += str.slice(start, i);
27+
// Append skipped/buffered characters and the encoded entity:
28+
if (i > last) out += str.slice(last, i);
2329
out += ch;
24-
start = i + 1;
30+
// Start the next seek/buffer after the entity's offset:
31+
last = i + 1;
2532
}
26-
return out + str.slice(start, i);
33+
return out + str.slice(last, i);
2734
}
2835

2936
export let indent = (s, char) =>

0 commit comments

Comments
 (0)