From 515467a618479792abedf01a7b1dcef2ac2a17ed Mon Sep 17 00:00:00 2001 From: Roy Eden Date: Wed, 8 Sep 2021 06:23:49 -0300 Subject: [PATCH] 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 --- src/platforms/web/compiler/modules/class.js | 2 +- test/ssr/ssr-string.spec.js | 2 +- test/unit/features/directives/class.spec.js | 33 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/platforms/web/compiler/modules/class.js b/src/platforms/web/compiler/modules/class.js index dcb5a229..378c959c 100644 --- a/src/platforms/web/compiler/modules/class.js +++ b/src/platforms/web/compiler/modules/class.js @@ -23,7 +23,7 @@ function transformNode (el: ASTElement, options: CompilerOptions) { } } if (staticClass) { - el.staticClass = JSON.stringify(staticClass) + el.staticClass = JSON.stringify(staticClass.replace(/\s+/g, ' ').trim()) } const classBinding = getBindingAttr(el, 'class', false /* getStatic */) if (classBinding) { diff --git a/test/ssr/ssr-string.spec.js b/test/ssr/ssr-string.spec.js index b4f96287..33094be9 100644 --- a/test/ssr/ssr-string.spec.js +++ b/test/ssr/ssr-string.spec.js @@ -1351,7 +1351,7 @@ describe('SSR: renderToString', () => { ` }, result => { - expect(result).toContain(`
`) + expect(result).toContain(`
`) done() }) }) diff --git a/test/unit/features/directives/class.spec.js b/test/unit/features/directives/class.spec.js index 0ee3380c..38a25988 100644 --- a/test/unit/features/directives/class.spec.js +++ b/test/unit/features/directives/class.spec.js @@ -152,6 +152,39 @@ describe('Directive v-bind:class', () => { }).then(done) }) + // css static classes should only contain a single space in between, + // as all the text inside of classes is shipped as a JS string + // and this could lead to useless spacing in static classes + it('condenses whitespace in staticClass', done => { + const vm = new Vue({ + template: '
', + }).$mount() + expect(vm.$el.className).toBe('test1 test2 test3 test4 test5 test6') + done() + }) + + it('condenses whitespace in staticClass merge in a component', done => { + const vm = new Vue({ + template: ` + + + `, + data: { + componentClass1: 'componentClass1', + }, + components: { + component1: { + template: '
' + }, + } + }).$mount() + expect(vm.$el.className).toBe('test staticClass componentClass1') + vm.componentClass1 = 'c1' + waitForUpdate(() => { + expect(vm.$el.className).toBe('test staticClass c1') + }).then(done) + }) + // a vdom patch edge case where the user has several un-keyed elements of the // same tag next to each other, and toggling them. it('properly remove staticClass for toggling un-keyed children', done => {