Skip to content

Commit c2d870c

Browse files
committed
support .= literal syntax
1 parent cbfd778 commit c2d870c

File tree

6 files changed

+48
-21
lines changed

6 files changed

+48
-21
lines changed

src/compiler/compile.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,23 @@ function makeTerminalNodeLinkFn (el, dirName, value, options, def) {
538538
function compileDirectives (attrs, options) {
539539
var i = attrs.length
540540
var dirs = []
541-
var attr, name, value, dir, dirName, dirDef, arg
541+
var attr, name, value, dir, dirName, dirDef, isLiteral, arg
542542
while (i--) {
543543
attr = attrs[i]
544544
name = attr.name
545545
value = attr.value
546546
// Core directive
547547
if (name.indexOf(config.prefix) === 0) {
548548
dirName = name.slice(config.prefix.length)
549+
550+
// check literal
551+
if (dirName.charAt(dirName.length - 1) === '.') {
552+
isLiteral = true
553+
dirName = dirName.slice(0, -1)
554+
} else {
555+
isLiteral = false
556+
}
557+
549558
dirDef = resolveAsset(options, 'directives', dirName)
550559
if (process.env.NODE_ENV !== 'production') {
551560
_.assertAsset(dirDef, 'directive', dirName)
@@ -570,7 +579,8 @@ function compileDirectives (attrs, options) {
570579
dirs.push({
571580
name: dirName,
572581
descriptors: dirParser.parse(value),
573-
def: dirDef
582+
def: dirDef,
583+
literal: isLiteral
574584
})
575585
}
576586
} else
@@ -661,7 +671,7 @@ function makeNodeLinkFn (directives) {
661671
k = dir.descriptors.length
662672
for (j = 0; j < k; j++) {
663673
vm._bindDir(dir.name, el,
664-
dir.descriptors[j], dir.def, host, scope, frag, dir.arg)
674+
dir.descriptors[j], dir.def, host, scope, frag, dir.arg, dir.literal)
665675
}
666676
}
667677
}

src/deprecations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ if (process.env.NODE_ENV !== 'production') {
217217

218218
LITERAL: function () {
219219
warn(
220-
'Literal directives will be deprecated in 1.0.0. Just add quotes if ' +
221-
'you want to pass a literal string to the directive.'
220+
'It is no longer necessary to declare literal directives in 1.0.0. Just ' +
221+
'use the dot-equal syntax (v-dir.="string") to indicate a literal value.'
222222
)
223223
}
224224

src/directive.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var expParser = require('./parsers/expression')
2626

2727
// TODO: 1.0.0 cleanup the arguments
2828

29-
function Directive (name, el, vm, descriptor, def, host, scope, frag, arg) {
29+
function Directive (name, el, vm, descriptor, def, host, scope, frag, arg, literal) {
3030
// public
3131
this.name = name
3232
this.el = el
@@ -45,6 +45,8 @@ function Directive (name, el, vm, descriptor, def, host, scope, frag, arg) {
4545
this._host = host
4646
this._scope = scope
4747
this._frag = frag
48+
// literal
49+
this._literal = literal
4850
}
4951

5052
/**
@@ -92,10 +94,14 @@ Directive.prototype._bind = function () {
9294
if (this.bind) {
9395
this.bind()
9496
}
95-
if (this._watcherExp &&
96-
(this.update || this.twoWay) &&
97-
(!this.isLiteral || this._isDynamicLiteral) &&
98-
!this._checkStatement()) {
97+
if (this._literal) {
98+
this.update && this.update(this.expression)
99+
} else if (
100+
this._watcherExp &&
101+
(this.update || this.twoWay) &&
102+
(!this.isLiteral || this._isDynamicLiteral) &&
103+
!this._checkStatement()
104+
) {
99105
// wrapped updater for context
100106
var dir = this
101107
var update = this._update = this.update

src/instance/lifecycle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ exports._initElement = function (el) {
107107
* @param {Fragment} [frag] - owner fragment
108108
*/
109109

110-
exports._bindDir = function (name, node, desc, def, host, scope, frag, arg) {
110+
exports._bindDir = function (name, node, desc, def, host, scope, frag, arg, literal) {
111111
this._directives.push(
112-
new Directive(name, node, this, desc, def, host, scope, frag, arg)
112+
new Directive(name, node, this, desc, def, host, scope, frag, arg, literal)
113113
)
114114
}
115115

test/unit/specs/compiler/compile_spec.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ if (_.inBrowser) {
5050

5151
it('normal directives', function () {
5252
el.setAttribute('v-a', 'b')
53-
el.innerHTML = '<p v-a="a" v-b="b">hello</p><div v-b="b"></div>'
53+
el.innerHTML = '<p v-a="a" v-b="b">hello</p><div v-b.="b"></div>'
5454
var defA = { priority: 1 }
5555
var defB = { priority: 2 }
5656
var descriptorA = dirParser.parse('a')[0]
@@ -66,10 +66,10 @@ if (_.inBrowser) {
6666
linker(vm, el)
6767
expect(directiveBind.calls.count()).toBe(4)
6868
expect(vm._bindDir.calls.count()).toBe(4)
69-
expect(vm._bindDir).toHaveBeenCalledWith('a', el, descriptorB, defA, undefined, undefined, undefined, undefined)
70-
expect(vm._bindDir).toHaveBeenCalledWith('a', el.firstChild, descriptorA, defA, undefined, undefined, undefined, undefined)
71-
expect(vm._bindDir).toHaveBeenCalledWith('b', el.firstChild, descriptorB, defB, undefined, undefined, undefined, undefined)
72-
expect(vm._bindDir).toHaveBeenCalledWith('b', el.lastChild, descriptorB, defB, undefined, undefined, undefined, undefined)
69+
expect(vm._bindDir).toHaveBeenCalledWith('a', el, descriptorB, defA, undefined, undefined, undefined, undefined, false)
70+
expect(vm._bindDir).toHaveBeenCalledWith('a', el.firstChild, descriptorA, defA, undefined, undefined, undefined, undefined, false)
71+
expect(vm._bindDir).toHaveBeenCalledWith('b', el.firstChild, descriptorB, defB, undefined, undefined, undefined, undefined, false)
72+
expect(vm._bindDir).toHaveBeenCalledWith('b', el.lastChild, descriptorB, defB, undefined, undefined, undefined, undefined, true)
7373
// check the priority sorting
7474
// the "b"s should be called first!
7575
expect(directiveBind.calls.argsFor(0)[0]).toBe('b')
@@ -88,9 +88,9 @@ if (_.inBrowser) {
8888
var linker = compile(el, Vue.options)
8989
linker(vm, el)
9090
expect(vm._bindDir.calls.count()).toBe(3)
91-
expect(vm._bindDir).toHaveBeenCalledWith('class', el, descA, Vue.options.directives.class, undefined, undefined, undefined, undefined)
92-
expect(vm._bindDir).toHaveBeenCalledWith('style', el, descB, Vue.options.directives.style, undefined, undefined, undefined, undefined)
93-
expect(vm._bindDir).toHaveBeenCalledWith('attr', el, descC, Vue.options.directives.attr, undefined, undefined, undefined, 'title')
91+
expect(vm._bindDir).toHaveBeenCalledWith('class', el, descA, Vue.options.directives.class, undefined, undefined, undefined, undefined, undefined)
92+
expect(vm._bindDir).toHaveBeenCalledWith('style', el, descB, Vue.options.directives.style, undefined, undefined, undefined, undefined, undefined)
93+
expect(vm._bindDir).toHaveBeenCalledWith('attr', el, descC, Vue.options.directives.attr, undefined, undefined, undefined, 'title', undefined)
9494
})
9595

9696
it('on- syntax', function () {
@@ -99,7 +99,7 @@ if (_.inBrowser) {
9999
var linker = compile(el, Vue.options)
100100
linker(vm, el)
101101
expect(vm._bindDir.calls.count()).toBe(1)
102-
expect(vm._bindDir).toHaveBeenCalledWith('on', el, desc, Vue.options.directives.on, undefined, undefined, undefined, 'click')
102+
expect(vm._bindDir).toHaveBeenCalledWith('on', el, desc, Vue.options.directives.on, undefined, undefined, undefined, 'click', undefined)
103103
})
104104

105105
it('text interpolation', function () {

test/unit/specs/directive_spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ describe('Directive', function () {
7171
expect(d.update).not.toHaveBeenCalled()
7272
})
7373

74+
it('static literal (new syntax)', function () {
75+
var d = new Directive('test', el, vm, {
76+
expression: 'a'
77+
}, def, null, null, null, null, true)
78+
d._bind()
79+
expect(d._watcher).toBeUndefined()
80+
expect(d.expression).toBe('a')
81+
expect(d.bind).toHaveBeenCalled()
82+
expect(d.update).toHaveBeenCalledWith('a')
83+
})
84+
7485
it('static literal, interpolate with no update', function () {
7586
def.isLiteral = true
7687
delete def.update

0 commit comments

Comments
 (0)