Skip to content

Commit 515467a

Browse files
royedenposva
andauthored
feat(compiler): condenses staticClass whitespace (fix #12113) (#12195)
* 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 #12113 * refactor(refactor(web-compiler): removed ignore in regex): Removed ignore flag in regex fix #12113 * test(ssr-string.spec.js): Removed newline character, as whitespace is purged in static classes There's no need to escape newlines in static classes, as they're now replaced with a single whitespace character fix #12113 * test(directives/class.spec.js): Added whitespace to test fix #12113 * Apply suggestions from code review Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent 4f6f39a commit 515467a

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

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

+1-1
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+/g, ' ').trim())
2727
}
2828
const classBinding = getBindingAttr(el, 'class', false /* getStatic */)
2929
if (classBinding) {

test/ssr/ssr-string.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ describe('SSR: renderToString', () => {
13511351
</div>
13521352
`
13531353
}, result => {
1354-
expect(result).toContain(`<div class="a\nb"></div>`)
1354+
expect(result).toContain(`<div class="a b"></div>`)
13551355
done()
13561356
})
13571357
})

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

+33
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('condenses 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('condenses whitespace in staticClass merge in a component', done => {
167+
const vm = new Vue({
168+
template: `
169+
<component1 class="\n\t staticClass \t\n" :class="componentClass1">
170+
</component1>
171+
`,
172+
data: {
173+
componentClass1: 'componentClass1',
174+
},
175+
components: {
176+
component1: {
177+
template: '<div class="\n\t test \t\n"></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)