diff --git a/src/compiler/parser/index.ts b/src/compiler/parser/index.ts index 148cc0110f5..511b683dee6 100644 --- a/src/compiler/parser/index.ts +++ b/src/compiler/parser/index.ts @@ -109,7 +109,7 @@ export function parse(template: string, options: CompilerOptions): ASTElement { let root let currentParent let inVPre = false - let inPre = false + let inPre = 0 let warned = false function warnOnce(msg, range) { @@ -173,7 +173,7 @@ export function parse(template: string, options: CompilerOptions): ASTElement { inVPre = false } if (platformIsPreTag(element.tag)) { - inPre = false + inPre = Math.max(inPre - 1, 0) } // apply post-transforms for (let i = 0; i < postTransforms.length; i++) { @@ -287,7 +287,7 @@ export function parse(template: string, options: CompilerOptions): ASTElement { } } if (platformIsPreTag(element.tag)) { - inPre = true + inPre += 1 } if (inVPre) { processRawAttrs(element) diff --git a/test/unit/modules/compiler/parser.spec.ts b/test/unit/modules/compiler/parser.spec.ts index 1efba124146..b1840c1de50 100644 --- a/test/unit/modules/compiler/parser.spec.ts +++ b/test/unit/modules/compiler/parser.spec.ts @@ -1113,6 +1113,18 @@ describe('parser', () => { expect(pre2.children[0].text).toBe('\nabc') }) + it(`preserve whitespace after nested
tags with whitespace: 'condense'`, () => {
+ const options = extend({}, condenseOptions)
+ const ast = parse(
+ '1\n\n2\n\naaa\nbbb\nc\n
\n3\n
',
+ options
+ )
+ expect(ast.tag).toBe('pre')
+ expect(ast.children[1].tag).toBe('pre')
+ expect(ast.children[2].type).toBe(3)
+ expect(ast.children[2].text).toBe('\n3\n')
+ })
+
it(`keep first newline after unary tag in with whitespace: 'condense'`, () => {
const options = extend({}, condenseOptions)
const ast = parse('abc\ndef
', options)