Skip to content

Add support for v-bind:content shorthand, including namespaced attributes (supersedes #2858) #2877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/compiler/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
checkComponentAttr,
findRef,
defineReactive,
getAttr
getAttr,
camelize
} from '../util/index'

// special binding prefixes
Expand Down Expand Up @@ -716,6 +717,11 @@ function compileDirectives (attrs, options) {
pushDir(dirName, internalDirectives[dirName])
} else {
arg = dirName
// support for shorthand expression (#2843)
// <div v-bind:content> => <div v-bind:content="content">
if (!value) {
value = camelize(dirName, true)
}
pushDir('bind', publicDirectives.bind)
}
} else
Expand Down
5 changes: 3 additions & 2 deletions src/util/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 25 additions & 1 deletion test/unit/specs/compiler/compile_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ describe('Compile', function () {
el.setAttribute(':class', 'a')
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
Expand All @@ -135,6 +138,27 @@ describe('Compile', function () {
expression: 'c',
arg: 'title',
def: publicDirectives.bind
},
':content': {
name: 'bind',
attr: ':content',
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) {
Expand All @@ -143,7 +167,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(6)

expects.forEach(function (e, i) {
var args = vm._bindDir.calls.argsFor(i)
Expand Down