From: Rudy Date: Tue, 8 Nov 2022 14:50:43 +0000 (+0800) Subject: fix(compiler-core): keep whitespaces between interpolation and comment (#6828) X-Git-Tag: v3.2.42~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48876182dbe5ef88a65a0aa7377e882c735b6104;p=thirdparty%2Fvuejs%2Fcore.git fix(compiler-core): keep whitespaces between interpolation and comment (#6828) fix #6352 --- diff --git a/packages/compiler-core/__tests__/parse.spec.ts b/packages/compiler-core/__tests__/parse.spec.ts index 4154e306c7..8979e8fb0e 100644 --- a/packages/compiler-core/__tests__/parse.spec.ts +++ b/packages/compiler-core/__tests__/parse.spec.ts @@ -1980,6 +1980,17 @@ foo expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION) }) + it('should NOT remove whitespaces w/ newline between interpolation and comment', () => { + const ast = parse(` \n {{msg}}`) + expect(ast.children.length).toBe(3) + expect(ast.children[0].type).toBe(NodeTypes.COMMENT) + expect(ast.children[1]).toMatchObject({ + type: NodeTypes.TEXT, + content: ' ' + }) + expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION) + }) + it('should NOT remove whitespaces w/o newline between elements', () => { const ast = parse(`
`) expect(ast.children.length).toBe(5) diff --git a/packages/compiler-core/src/parse.ts b/packages/compiler-core/src/parse.ts index 2f904098e6..642f3edafd 100644 --- a/packages/compiler-core/src/parse.ts +++ b/packages/compiler-core/src/parse.ts @@ -264,14 +264,19 @@ function parseChildren( const next = nodes[i + 1] // Remove if: // - the whitespace is the first or last node, or: - // - (condense mode) the whitespace is adjacent to a comment, or: + // - (condense mode) the whitespace is between twos comments, or: + // - (condense mode) the whitespace is between comment and element, or: // - (condense mode) the whitespace is between two elements AND contains newline if ( !prev || !next || (shouldCondense && - (prev.type === NodeTypes.COMMENT || - next.type === NodeTypes.COMMENT || + ((prev.type === NodeTypes.COMMENT && + next.type === NodeTypes.COMMENT) || + (prev.type === NodeTypes.COMMENT && + next.type === NodeTypes.ELEMENT) || + (prev.type === NodeTypes.ELEMENT && + next.type === NodeTypes.COMMENT) || (prev.type === NodeTypes.ELEMENT && next.type === NodeTypes.ELEMENT && /[\r\n]/.test(node.content))))