@@ -2,7 +2,7 @@ var Cache = require('../cache')
2
2
var config = require ( '../config' )
3
3
var dirParser = require ( './directive' )
4
4
var regexEscapeRE = / [ - . * + ? ^ $ { } ( ) | [ \] \/ \\ ] / g
5
- var cache , tagRE , htmlRE , firstChar , lastChar
5
+ var cache , tagRE , htmlRE
6
6
7
7
/**
8
8
* Escape a string so it can be used in a RegExp
@@ -15,32 +15,18 @@ function escapeRegex (str) {
15
15
return str . replace ( regexEscapeRE , '\\$&' )
16
16
}
17
17
18
- /**
19
- * Compile the interpolation tag regex.
20
- *
21
- * @return {RegExp }
22
- */
23
-
24
- function compileRegex ( ) {
25
- config . _delimitersChanged = false
26
- var open = config . delimiters [ 0 ]
27
- var close = config . delimiters [ 1 ]
28
- firstChar = open . charAt ( 0 )
29
- lastChar = close . charAt ( close . length - 1 )
30
- var firstCharRE = escapeRegex ( firstChar )
31
- var lastCharRE = escapeRegex ( lastChar )
32
- var openRE = escapeRegex ( open )
33
- var closeRE = escapeRegex ( close )
18
+ exports . compileRegex = function ( ) {
19
+ var open = escapeRegex ( config . delimiters [ 0 ] )
20
+ var close = escapeRegex ( config . delimiters [ 1 ] )
21
+ var unsafeOpen = escapeRegex ( config . unsafeDelimiters [ 0 ] )
22
+ var unsafeClose = escapeRegex ( config . unsafeDelimiters [ 1 ] )
34
23
tagRE = new RegExp (
35
- firstCharRE + '?' + openRE +
36
- '(.+?)' +
37
- closeRE + lastCharRE + '?' ,
24
+ unsafeOpen + '(.+?)' + unsafeClose + '|' +
25
+ open + '(.+?)' + close ,
38
26
'g'
39
27
)
40
28
htmlRE = new RegExp (
41
- '^' + firstCharRE + openRE +
42
- '.*' +
43
- closeRE + lastCharRE + '$'
29
+ '^' + unsafeOpen + '.*' + unsafeClose + '$'
44
30
)
45
31
// reset cache
46
32
cache = new Cache ( 1000 )
@@ -58,8 +44,8 @@ function compileRegex () {
58
44
*/
59
45
60
46
exports . parse = function ( text ) {
61
- if ( config . _delimitersChanged ) {
62
- compileRegex ( )
47
+ if ( ! cache ) {
48
+ exports . compileRegex ( )
63
49
}
64
50
var hit = cache . get ( text )
65
51
if ( hit ) {
@@ -71,7 +57,7 @@ exports.parse = function (text) {
71
57
}
72
58
var tokens = [ ]
73
59
var lastIndex = tagRE . lastIndex = 0
74
- var match , index , value , first , oneTime , twoWay
60
+ var match , index , html , value , first , oneTime , twoWay
75
61
/* eslint-disable no-cond-assign */
76
62
while ( match = tagRE . exec ( text ) ) {
77
63
/* eslint-enable no-cond-assign */
@@ -83,16 +69,18 @@ exports.parse = function (text) {
83
69
} )
84
70
}
85
71
// tag token
86
- first = match [ 1 ] . charCodeAt ( 0 )
72
+ html = htmlRE . test ( match [ 0 ] )
73
+ value = html ? match [ 1 ] : match [ 2 ]
74
+ first = value . charCodeAt ( 0 )
87
75
oneTime = first === 42 // *
88
76
twoWay = first === 64 // @
89
77
value = oneTime || twoWay
90
- ? match [ 1 ] . slice ( 1 )
91
- : match [ 1 ]
78
+ ? value . slice ( 1 )
79
+ : value
92
80
tokens . push ( {
93
81
tag : true ,
94
82
value : value . trim ( ) ,
95
- html : htmlRE . test ( match [ 0 ] ) ,
83
+ html : html ,
96
84
oneTime : oneTime ,
97
85
twoWay : twoWay
98
86
} )
0 commit comments