Skip to content

Commit 92dd92a

Browse files
committed
fix exp-parser so it correctly access variables that start with $ in expressions
1 parent 315cfe8 commit 92dd92a

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/compiler.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,12 @@ CompilerProto.bindDirective = function (directive) {
413413
}
414414

415415
// set initial value
416-
if (binding.isComputed) {
417-
directive.refresh(value)
418-
} else {
419-
directive.update(value, true)
416+
if (value !== undefined) {
417+
if (binding.isComputed) {
418+
directive.refresh(value)
419+
} else {
420+
directive.update(value, true)
421+
}
420422
}
421423
}
422424

src/directives/repeat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ module.exports = {
130130
this.buildItem()
131131
this.initiated = true
132132
}
133-
this.collection = collection || []
133+
collection = this.collection = collection || []
134134
this.vms = []
135135

136136
// listen for collection mutation events

src/exp-parser.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ function getRel (path, compiler) {
6868
}
6969
}
7070
compiler = vm.$compiler
71-
if (!hasOwn.call(compiler.bindings, path)) {
71+
if (
72+
!hasOwn.call(compiler.bindings, path) &&
73+
path.charAt(0) !== '$'
74+
) {
7275
compiler.createBinding(path)
7376
}
7477
return rel
@@ -90,6 +93,15 @@ function makeGetter (exp, raw) {
9093
return fn
9194
}
9295

96+
/**
97+
* Escape a leading dollar sign for regex construction
98+
*/
99+
function escapeDollar (v) {
100+
return v.charAt(0) === '$'
101+
? '\\' + v
102+
: v
103+
}
104+
93105
module.exports = {
94106

95107
/**
@@ -105,11 +117,22 @@ module.exports = {
105117
}
106118
vars = utils.unique(vars)
107119
var accessors = '',
108-
pathRE = new RegExp("\\b(" + vars.join('|') + ")[$\\w\\.]*\\b", 'g'),
109-
body = 'return ' + exp.replace(pathRE, function (path) {
120+
// construct a regex to extract all valid variable paths
121+
// ones that begin with "$" are particularly tricky
122+
// because we can't use \b for them
123+
pathRE = new RegExp(
124+
"[^$\\w\\.](" +
125+
vars.map(escapeDollar).join('|') +
126+
")[$\\w\\.]*\\b", 'g'
127+
),
128+
body = ('return ' + exp).replace(pathRE, function (path) {
129+
// keep track of the first char
130+
var c = path.charAt(0)
131+
path = path.slice(1)
110132
var val = 'this.' + getRel(path, compiler) + path
111133
accessors += val + ';'
112-
return val
134+
// don't forget to put that first char back
135+
return c + val
113136
})
114137
body = accessors + body
115138
return makeGetter(body, exp)

0 commit comments

Comments
 (0)