Skip to content

Commit ad2ac50

Browse files
committed
improve class interpoations (fix #1960)
1 parent a4cfd2b commit ad2ac50

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"build": "node build/build.js",
2828
"install-hook": "ln -s ../../build/git-hooks/pre-commit .git/hooks/pre-commit",
2929
"dev": "webpack --watch --config build/webpack.dev.config.js & npm run serve-test",
30-
"serve-test": "webpack-dev-server --config build/webpack.test.config.js",
30+
"serve-test": "webpack-dev-server --config build/webpack.test.config.js --host 0.0.0.0",
3131
"build-test": "webpack --config build/webpack.test.config.js",
3232
"lint": "eslint src/** test/e2e/** test/unit/specs/** build/**.js",
3333
"e2e": "casperjs test --concise ./test/e2e",

src/compiler/compile.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,12 @@ function compileDirectives (attrs, options) {
652652
// attribute interpolations
653653
if (tokens) {
654654
value = tokensToExp(tokens)
655-
arg = name
656-
pushDir('bind', publicDirectives.bind, true)
655+
if (name === 'class') {
656+
pushDir('class', internalDirectives['class'], true)
657+
} else {
658+
arg = name
659+
pushDir('bind', publicDirectives.bind, true)
660+
}
657661
// warn against mixing mustaches with v-bind
658662
if (process.env.NODE_ENV !== 'production') {
659663
if (name === 'class' && Array.prototype.some.call(attrs, function (attr) {

src/directive.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
warn
1111
} from './util/index'
1212
import Watcher from './watcher'
13+
import { removeTags } from './parsers/text'
1314
import { parseExpression, isSimplePath } from './parsers/expression'
1415

1516
function noop () {}
@@ -82,7 +83,15 @@ Directive.prototype._bind = function () {
8283
this.el && this.el.removeAttribute
8384
) {
8485
var attr = descriptor.attr || ('v-' + name)
85-
this.el.removeAttribute(attr)
86+
if (attr !== 'class') {
87+
this.el.removeAttribute(attr)
88+
} else {
89+
// for class interpolations, only remove the parts that
90+
// need to be interpolated.
91+
this.el.className = removeTags(this.el.className)
92+
.trim()
93+
.replace(/\s+/g, ' ')
94+
}
8695
}
8796

8897
// copy def properties

src/parsers/text.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,14 @@ function inlineFilters (exp, single) {
159159
}
160160
}
161161
}
162+
163+
/**
164+
* Replace all interpolation tags in a piece of text.
165+
*
166+
* @param {String} text
167+
* @return {String}
168+
*/
169+
170+
export function removeTags (text) {
171+
return text.replace(tagRE, '')
172+
}

test/unit/specs/compiler/compile_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,12 @@ describe('Compile', function () {
516516
}
517517
})
518518
expect(el.firstChild.id).toBe('aaa')
519-
expect(el.firstChild.className).toBe('b ccc d')
519+
expect(el.firstChild.className).toBe('b d ccc')
520520
vm.a = 'aa'
521521
vm.c = 'cc'
522522
_.nextTick(function () {
523523
expect(el.firstChild.id).toBe('aa')
524-
expect(el.firstChild.className).toBe('b cc d')
524+
expect(el.firstChild.className).toBe('b d cc')
525525
done()
526526
})
527527
})

0 commit comments

Comments
 (0)