1
1
var utils = require ( './utils' ) ,
2
- hasOwn = Object . prototype . hasOwnProperty
2
+ hasOwn = Object . prototype . hasOwnProperty ,
3
+ stringSaveRE = / " (?: [ ^ " \\ ] | \\ .) * " | ' (?: [ ^ ' \\ ] | \\ .) * ' / g,
4
+ stringRestoreRE = / " ( \d + ) " / g
3
5
4
6
// Variable extraction scooped from https://github.com/RubyLouvre/avalon
5
7
@@ -122,6 +124,8 @@ module.exports = {
122
124
}
123
125
vars = utils . unique ( vars )
124
126
var accessors = '' ,
127
+ has = utils . hash ( ) ,
128
+ strings = [ ] ,
125
129
// construct a regex to extract all valid variable paths
126
130
// ones that begin with "$" are particularly tricky
127
131
// because we can't use \b for them
@@ -130,16 +134,35 @@ module.exports = {
130
134
vars . map ( escapeDollar ) . join ( '|' ) +
131
135
")[$\\w\\.]*\\b" , 'g'
132
136
) ,
133
- body = ( 'return ' + exp ) . replace ( pathRE , function ( path ) {
134
- // keep track of the first char
135
- var c = path . charAt ( 0 )
136
- path = path . slice ( 1 )
137
- var val = 'this.' + getRel ( path , compiler ) + path
138
- accessors += val + ';'
139
- // don't forget to put that first char back
140
- return c + val
141
- } )
137
+ body = ( 'return ' + exp )
138
+ . replace ( stringSaveRE , saveStrings )
139
+ . replace ( pathRE , replacePath )
140
+ . replace ( stringRestoreRE , restoreStrings )
142
141
body = accessors + body
142
+
143
+ function saveStrings ( str ) {
144
+ var i = strings . length
145
+ strings [ i ] = str
146
+ return '"' + i + '"'
147
+ }
148
+
149
+ function replacePath ( path ) {
150
+ // keep track of the first char
151
+ var c = path . charAt ( 0 )
152
+ path = path . slice ( 1 )
153
+ var val = 'this.' + getRel ( path , compiler ) + path
154
+ if ( ! has [ path ] ) {
155
+ accessors += val + ';'
156
+ has [ path ] = 1
157
+ }
158
+ // don't forget to put that first char back
159
+ return c + val
160
+ }
161
+
162
+ function restoreStrings ( str , i ) {
163
+ return strings [ i ]
164
+ }
165
+
143
166
return makeGetter ( body , exp )
144
167
}
145
168
}
0 commit comments