@@ -10,6 +10,8 @@ function safe(context, input, config) {
1010 var index = - 1
1111 var positions = [ ]
1212 var result = [ ]
13+ var infos = { }
14+ var info
1315 var position
1416 var character
1517 var pattern
@@ -36,12 +38,30 @@ function safe(context, input, config) {
3638 continue
3739 }
3840
39- expression = toExpression ( pattern )
41+ expression =
42+ pattern . _compiled || ( pattern . _compiled = toExpression ( pattern ) )
4043
4144 while ( ( match = expression . exec ( value ) ) ) {
42- positions . push (
45+ position =
4346 match . index + ( pattern . before || pattern . atBreak ? match [ 1 ] . length : 0 )
44- )
47+
48+ info = {
49+ before : pattern . atBreak || 'before' in pattern ,
50+ after : 'after' in pattern
51+ }
52+
53+ if ( positions . indexOf ( position ) === - 1 ) {
54+ positions . push ( position )
55+ infos [ position ] = info
56+ } else {
57+ if ( infos [ position ] . before && ! info . before ) {
58+ infos [ position ] . before = false
59+ }
60+
61+ if ( infos [ position ] . after && ! info . after ) {
62+ infos [ position ] . after = false
63+ }
64+ }
4565 }
4666 }
4767
@@ -54,22 +74,35 @@ function safe(context, input, config) {
5474
5575 while ( ++ index < length ) {
5676 position = positions [ index ]
77+ info = infos [ position ]
5778
5879 if (
5980 // Character before or after matched:
6081 position < start ||
61- position >= end ||
62- // Character matched multiple times:
63- position === positions [ index + 1 ]
82+ position >= end
83+ ) {
84+ continue
85+ }
86+
87+ // If this character is supposed to be escaped, but only because it has a
88+ // condition on the next character, and the next character is definitly
89+ // being escaped), then skip this escape.
90+ if (
91+ info . after &&
92+ position + 1 < end &&
93+ positions [ index + 1 ] === position + 1 &&
94+ ! infos [ position + 1 ] . before &&
95+ ! infos [ position + 1 ] . after
6496 ) {
6597 continue
6698 }
6799
100+ character = value . charAt ( position )
101+
68102 if ( start !== position ) {
69103 result . push ( value . slice ( start , position ) )
70104 }
71105
72- character = value . charAt ( position )
73106 start = position
74107
75108 if (
0 commit comments