From be666ebd59027eb2fc96595c1a6054ecf62832e8 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 8 Apr 2020 16:44:32 -0400 Subject: [PATCH] fix(compiler): should only strip leading newline directly in pre tag --- packages/compiler-core/src/parse.ts | 2 +- packages/compiler-dom/__tests__/parse.spec.ts | 22 ++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/compiler-core/src/parse.ts b/packages/compiler-core/src/parse.ts index 1e94d645116..1fbd7d5c24d 100644 --- a/packages/compiler-core/src/parse.ts +++ b/packages/compiler-core/src/parse.ts @@ -223,7 +223,7 @@ function parseChildren( } } } - } else { + } else if (parent && context.options.isPreTag(parent.tag)) { // remove leading newline per html spec // https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element const first = nodes[0] diff --git a/packages/compiler-dom/__tests__/parse.spec.ts b/packages/compiler-dom/__tests__/parse.spec.ts index 58e37753040..8e88d14954c 100644 --- a/packages/compiler-dom/__tests__/parse.spec.ts +++ b/packages/compiler-dom/__tests__/parse.spec.ts @@ -141,12 +141,24 @@ describe('DOM parser', () => { // #908 test('
 tag should remove leading newline', () => {
-      const rawText = `\nhello`
+      const rawText = `\nhello
\nbye
` const ast = parse(`
${rawText}
`, parserOptions) - expect((ast.children[0] as ElementNode).children[0]).toMatchObject({ - type: NodeTypes.TEXT, - content: rawText.slice(1) - }) + expect((ast.children[0] as ElementNode).children).toMatchObject([ + { + type: NodeTypes.TEXT, + content: `hello` + }, + { + type: NodeTypes.ELEMENT, + children: [ + { + type: NodeTypes.TEXT, + // should not remove the leading newline for nested elements + content: `\nbye` + } + ] + } + ]) }) })