From 11c8dbf9ca5be0020a1e7a168c72dd49db7b921e Mon Sep 17 00:00:00 2001 From: An Phan Date: Thu, 12 May 2016 15:39:52 +0800 Subject: [PATCH 1/2] Add support for v-bind:content shorthand (fixes #2843) This commit aims to add support for `v-bind:content` shorthand. With this, v-bind with an empty expression will imply the expression to be the directive name, i.e. `v-bind :content` will have the same effect as `v-bind:content="content"`, `:href` will have the same effect as `:href="href"` and so on so forth. Only attribute bindings are affected by this change. Event, transition, class/style bindings remain unchanged. --- src/compiler/compile.js | 5 +++++ test/unit/specs/compiler/compile_spec.js | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/compiler/compile.js b/src/compiler/compile.js index 45a6b890dc2..482787395bb 100644 --- a/src/compiler/compile.js +++ b/src/compiler/compile.js @@ -716,6 +716,11 @@ function compileDirectives (attrs, options) { pushDir(dirName, internalDirectives[dirName]) } else { arg = dirName + // support for shorthand expression (#2843) + //
=>
+ if (!value) { + value = dirName + } pushDir('bind', publicDirectives.bind) } } else diff --git a/test/unit/specs/compiler/compile_spec.js b/test/unit/specs/compiler/compile_spec.js index fb8a934e4ab..fb7f78230d0 100644 --- a/test/unit/specs/compiler/compile_spec.js +++ b/test/unit/specs/compiler/compile_spec.js @@ -112,6 +112,7 @@ describe('Compile', function () { el.setAttribute(':class', 'a') el.setAttribute(':style', 'b') el.setAttribute(':title', 'c') + el.setAttribute(':content', '') // The order of setAttribute is not guaranteed to be the same with // the order of attribute enumberation, therefore we need to save @@ -135,6 +136,13 @@ describe('Compile', function () { expression: 'c', arg: 'title', def: publicDirectives.bind + }, + ':content': { + name: 'bind', + attr: ':content', + expression: 'content', + arg: 'content', + def: publicDirectives.bind } } var expects = [].map.call(el.attributes, function (attr) { @@ -143,7 +151,7 @@ describe('Compile', function () { var linker = compile(el, Vue.options) linker(vm, el) - expect(vm._bindDir.calls.count()).toBe(3) + expect(vm._bindDir.calls.count()).toBe(4) expects.forEach(function (e, i) { var args = vm._bindDir.calls.argsFor(i) From e49113de19d648f4aaf9117c814b118b78db258a Mon Sep 17 00:00:00 2001 From: Bob Date: Thu, 12 May 2016 12:13:00 +0200 Subject: [PATCH 2/2] camelize and clean up default v-bind experessions --- src/compiler/compile.js | 5 +++-- src/util/lang.js | 5 +++-- test/unit/specs/compiler/compile_spec.js | 18 +++++++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/compiler/compile.js b/src/compiler/compile.js index 482787395bb..bf258cb7ab6 100644 --- a/src/compiler/compile.js +++ b/src/compiler/compile.js @@ -14,7 +14,8 @@ import { checkComponentAttr, findRef, defineReactive, - getAttr + getAttr, + camelize } from '../util/index' // special binding prefixes @@ -719,7 +720,7 @@ function compileDirectives (attrs, options) { // support for shorthand expression (#2843) //
=>
if (!value) { - value = dirName + value = camelize(dirName, true) } pushDir('bind', publicDirectives.bind) } diff --git a/src/util/lang.js b/src/util/lang.js index 29e450fa5a2..fe2ca88b5a5 100644 --- a/src/util/lang.js +++ b/src/util/lang.js @@ -174,8 +174,9 @@ export function stripQuotes (str) { */ var camelizeRE = /-(\w)/g -export function camelize (str) { - return str.replace(camelizeRE, toUpper) +var camelizeCleanupRE = /\W+(\w)?/ +export function camelize (str, cleanup) { + return str.replace(cleanup ? camelizeCleanupRE : camelizeRE, toUpper) } function toUpper (_, c) { diff --git a/test/unit/specs/compiler/compile_spec.js b/test/unit/specs/compiler/compile_spec.js index fb7f78230d0..537af31b838 100644 --- a/test/unit/specs/compiler/compile_spec.js +++ b/test/unit/specs/compiler/compile_spec.js @@ -113,6 +113,8 @@ describe('Compile', function () { el.setAttribute(':style', 'b') el.setAttribute(':title', 'c') el.setAttribute(':content', '') + el.setAttribute(':data-content', '') + el.setAttribute(':ns:attr', '') // The order of setAttribute is not guaranteed to be the same with // the order of attribute enumberation, therefore we need to save @@ -143,6 +145,20 @@ describe('Compile', function () { expression: 'content', arg: 'content', def: publicDirectives.bind + }, + ':data-content': { + name: 'bind', + attr: ':data-content', + expression: 'dataContent', + arg: 'data-content', + def: publicDirectives.bind + }, + ':ns:attr': { + name: 'bind', + attr: ':ns:attr', + expression: 'nsAttr', + arg: 'ns:attr', + def: publicDirectives.bind } } var expects = [].map.call(el.attributes, function (attr) { @@ -151,7 +167,7 @@ describe('Compile', function () { var linker = compile(el, Vue.options) linker(vm, el) - expect(vm._bindDir.calls.count()).toBe(4) + expect(vm._bindDir.calls.count()).toBe(6) expects.forEach(function (e, i) { var args = vm._bindDir.calls.argsFor(i)