Skip to content

Commit f844b1c

Browse files
committed
feat(compiler): template staticClass no longer preserves whitespace
Template static classes used to preserve whitespace after compilation, resulting in builds that had bigger file outputs with whitespace in component's staticClass attributes fix vuejs#12113
1 parent 8a219e3 commit f844b1c

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/platforms/web/compiler/modules/class.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function transformNode (el: ASTElement, options: CompilerOptions) {
2323
}
2424
}
2525
if (staticClass) {
26-
el.staticClass = JSON.stringify(staticClass)
26+
el.staticClass = JSON.stringify(staticClass.replace(/\s+/gi, " ").trim())
2727
}
2828
const classBinding = getBindingAttr(el, 'class', false /* getStatic */)
2929
if (classBinding) {

test/unit/features/directives/class.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,39 @@ describe('Directive v-bind:class', () => {
152152
}).then(done)
153153
})
154154

155+
// css static classes should only contain a single space in between,
156+
// as all the text inside of classes is shipped as a JS string
157+
// and this could lead to useless spacing in static classes
158+
it('corrects whitespace in staticClass', done => {
159+
const vm = new Vue({
160+
template: '<div class=" test1\ntest2\ttest3 test4 test5 \n \n \ntest6\t"></div>',
161+
}).$mount()
162+
expect(vm.$el.className).toBe('test1 test2 test3 test4 test5 test6')
163+
done()
164+
})
165+
166+
it('corrects whitespace in staticClass merge in a component', done => {
167+
const vm = new Vue({
168+
template: `
169+
<component1 class="staticClass" :class="componentClass1">
170+
</component1>
171+
`,
172+
data: {
173+
componentClass1: 'componentClass1',
174+
},
175+
components: {
176+
component1: {
177+
template: '<div class="test"></div>'
178+
},
179+
}
180+
}).$mount()
181+
expect(vm.$el.className).toBe('test staticClass componentClass1')
182+
vm.componentClass1 = 'c1'
183+
waitForUpdate(() => {
184+
expect(vm.$el.className).toBe('test staticClass c1')
185+
}).then(done)
186+
})
187+
155188
// a vdom patch edge case where the user has several un-keyed elements of the
156189
// same tag next to each other, and toggling them.
157190
it('properly remove staticClass for toggling un-keyed children', done => {

0 commit comments

Comments
 (0)