]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler): should only strip leading newline directly in pre tag
authorEvan You <yyx990803@gmail.com>
Wed, 8 Apr 2020 20:44:32 +0000 (16:44 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 8 Apr 2020 20:44:32 +0000 (16:44 -0400)
packages/compiler-core/src/parse.ts
packages/compiler-dom/__tests__/parse.spec.ts

index 1e94d64511643cac5570f6f6b2b296936b2771a3..1fbd7d5c24d14193307872e41961f8beae9219d1 100644 (file)
@@ -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]
index 58e37753040dd85de836bd04b21fe14e84a823ad..8e88d14954c3cc70d387b5ce590644552183ade6 100644 (file)
@@ -141,12 +141,24 @@ describe('DOM parser', () => {
 
     // #908
     test('<pre> tag should remove leading newline', () => {
-      const rawText = `\nhello`
+      const rawText = `\nhello<div>\nbye</div>`
       const ast = parse(`<pre>${rawText}</pre>`, 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`
+            }
+          ]
+        }
+      ])
     })
   })