File tree Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -4,13 +4,18 @@ export const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine
4
4
const ENCODED_ENTITIES = / [ & < > " ] / ;
5
5
6
6
export function encodeEntities ( str ) {
7
+ // Ensure we're always parsing and returning a string:
8
+ str += '' ;
9
+
7
10
// Skip all work for strings with no entities needing encoding:
8
11
if ( ENCODED_ENTITIES . test ( str ) === false ) return str ;
9
12
10
- let start = 0 ,
13
+ let last = 0 ,
11
14
i = 0 ,
12
15
out = '' ,
13
16
ch = '' ;
17
+
18
+ // Seek forward in str until the next entity char:
14
19
for ( ; i < str . length ; i ++ ) {
15
20
switch ( str . charCodeAt ( i ) ) {
16
21
case 60 : ch = '<' ; break ;
@@ -19,11 +24,13 @@ export function encodeEntities(str) {
19
24
case 38 : ch = '&' ; break ;
20
25
default : continue ;
21
26
}
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 ) ;
23
29
out += ch ;
24
- start = i + 1 ;
30
+ // Start the next seek/buffer after the entity's offset:
31
+ last = i + 1 ;
25
32
}
26
- return out + str . slice ( start , i ) ;
33
+ return out + str . slice ( last , i ) ;
27
34
}
28
35
29
36
export let indent = ( s , char ) =>
You can’t perform that action at this time.
0 commit comments