Skip to content

Commit 85da5b6

Browse files
committed
remove classList polyfill requirement for IE9
1 parent d95b521 commit 85da5b6

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

src/directives/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ module.exports = {
2929

3030
'class': function (value) {
3131
if (this.arg) {
32-
this.el.classList[value ? 'add' : 'remove'](this.arg)
32+
utils[value ? 'addClass' : 'removeClass'](this.el, this.arg)
3333
} else {
3434
if (this.lastVal) {
35-
this.el.classList.remove(this.lastVal)
35+
utils.removeClass(this.el, this.lastVal)
3636
}
3737
if (value) {
38-
this.el.classList.add(value)
38+
utils.addClass(this.el, value)
3939
this.lastVal = value
4040
}
4141
}

src/transition.js

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ function applyTransitionClass (el, stage, changeState) {
6666
return codes.CSS_SKIP
6767
}
6868

69+
// if the browser supports transition,
70+
// it must have classList...
6971
var classList = el.classList,
7072
lastLeaveCallback = el.vue_trans_cb
7173

src/utils.js

+30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var config = require('./config'),
33
toString = Object.prototype.toString,
44
join = Array.prototype.join,
55
console = window.console,
6+
7+
hasClassList = 'classList' in document.documentElement,
68
ViewModel // late def
79

810
var defer =
@@ -195,5 +197,33 @@ var utils = module.exports = {
195197
*/
196198
nextTick: function (cb) {
197199
defer(cb, 0)
200+
},
201+
202+
/**
203+
* add class for IE9
204+
* uses classList if available
205+
*/
206+
addClass: function (el, cls) {
207+
if (hasClassList) {
208+
el.classList.add(cls)
209+
} else {
210+
var cur = ' ' + el.className + ' '
211+
if (cur.indexOf(' ' + cls + ' ') < 0) {
212+
el.className = (cur + cls).trim()
213+
}
214+
}
215+
},
216+
217+
/**
218+
* remove class for IE9
219+
*/
220+
removeClass: function (el, cls) {
221+
if (hasClassList) {
222+
el.classList.remove(cls)
223+
} else {
224+
el.className = (' ' + el.className + ' ')
225+
.replace(' ' + cls + ' ', '')
226+
.trim()
227+
}
198228
}
199229
}

test/unit/specs/utils.js

+29
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,33 @@ describe('UNIT: Utils', function () {
319319

320320
})
321321

322+
describe('addClass', function () {
323+
324+
var el = document.createElement('div')
325+
326+
it('should work', function () {
327+
utils.addClass(el, 'hihi')
328+
assert.strictEqual(el.className, 'hihi')
329+
utils.addClass(el, 'hi')
330+
assert.strictEqual(el.className, 'hihi hi')
331+
})
332+
333+
it('should not add duplicate', function () {
334+
utils.addClass(el, 'hi')
335+
assert.strictEqual(el.className, 'hihi hi')
336+
})
337+
338+
})
339+
340+
describe('removeClass', function () {
341+
342+
it('should work', function () {
343+
var el = document.createElement('div')
344+
el.className = 'hihi hi'
345+
utils.removeClass(el, 'hi')
346+
assert.strictEqual(el.className, 'hihi')
347+
})
348+
349+
})
350+
322351
})

0 commit comments

Comments
 (0)