Skip to content

Commit 3b2038d

Browse files
committed
fix IE9 class retrieval when both class and :class are present
1 parent aed53c8 commit 3b2038d

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/util/dom.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ export function off (el, event, cb) {
179179
el.removeEventListener(event, cb)
180180
}
181181

182+
/**
183+
* For IE9 compat: when both class and :class are present
184+
* getAttribute('class') returns wrong value...
185+
*
186+
* @param {Element} el
187+
* @return {String}
188+
*/
189+
190+
function getClass (el) {
191+
var classname = el.className
192+
if (typeof classname === 'object') {
193+
classname = classname.baseVal || ''
194+
}
195+
return classname
196+
}
197+
182198
/**
183199
* In IE9, setAttribute('class') will result in empty class
184200
* if the element also has the :class attribute; However in
@@ -209,7 +225,7 @@ export function addClass (el, cls) {
209225
if (el.classList) {
210226
el.classList.add(cls)
211227
} else {
212-
var cur = ' ' + (el.getAttribute('class') || '') + ' '
228+
var cur = ' ' + getClass(el) + ' '
213229
if (cur.indexOf(' ' + cls + ' ') < 0) {
214230
setClass(el, (cur + cls).trim())
215231
}
@@ -227,7 +243,7 @@ export function removeClass (el, cls) {
227243
if (el.classList) {
228244
el.classList.remove(cls)
229245
} else {
230-
var cur = ' ' + (el.getAttribute('class') || '') + ' '
246+
var cur = ' ' + getClass(el) + ' '
231247
var tar = ' ' + cls + ' '
232248
while (cur.indexOf(tar) >= 0) {
233249
cur = cur.replace(tar, ' ')

test/unit/specs/misc_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,11 @@ describe('Misc', function () {
396396
components: {
397397
test: {
398398
replace: true,
399-
template: '<div :class="{\'inner\': true}"></div>'
399+
template: '<div class="static-inner" :class="{\'inner\': true}"></div>'
400400
}
401401
}
402402
})
403-
expect(vm.$el.firstChild.className).toBe('outer inner')
403+
expect(vm.$el.firstChild.className).toBe('static-inner outer inner')
404404
})
405405

406406
it('SVG class interpolation', function () {

0 commit comments

Comments
 (0)