@@ -10,6 +10,8 @@ function safe(context, input, config) {
10
10
var index = - 1
11
11
var positions = [ ]
12
12
var result = [ ]
13
+ var infos = { }
14
+ var info
13
15
var position
14
16
var character
15
17
var pattern
@@ -36,12 +38,30 @@ function safe(context, input, config) {
36
38
continue
37
39
}
38
40
39
- expression = toExpression ( pattern )
41
+ expression =
42
+ pattern . _compiled || ( pattern . _compiled = toExpression ( pattern ) )
40
43
41
44
while ( ( match = expression . exec ( value ) ) ) {
42
- positions . push (
45
+ position =
43
46
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
+ }
45
65
}
46
66
}
47
67
@@ -54,22 +74,35 @@ function safe(context, input, config) {
54
74
55
75
while ( ++ index < length ) {
56
76
position = positions [ index ]
77
+ info = infos [ position ]
57
78
58
79
if (
59
80
// Character before or after matched:
60
81
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
64
96
) {
65
97
continue
66
98
}
67
99
100
+ character = value . charAt ( position )
101
+
68
102
if ( start !== position ) {
69
103
result . push ( value . slice ( start , position ) )
70
104
}
71
105
72
- character = value . charAt ( position )
73
106
start = position
74
107
75
108
if (
0 commit comments