]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): consistently remove comment nodes for pre tags in production
authorEvan You <yyx990803@gmail.com>
Mon, 5 Oct 2020 21:53:17 +0000 (17:53 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 5 Oct 2020 21:53:34 +0000 (17:53 -0400)
close #2217

packages/compiler-core/__tests__/parse.spec.ts
packages/compiler-core/src/parse.ts

index eabbbcaec24c86faf68f8d23b6c5b05ccdbfabec..b73609ee194752cd16f8abb15f9f0491f2048dd5 100644 (file)
@@ -384,6 +384,25 @@ describe('compiler: parse', () => {
       expect(astNoComment.children).toHaveLength(0)
       expect(astWithComments.children).toHaveLength(1)
     })
+
+    // #2217
+    test('comments in the <pre> tag should be removed in production mode', () => {
+      __DEV__ = false
+      const rawText = `<p/><!-- foo --><p/>`
+      const ast = baseParse(`<pre>${rawText}</pre>`)
+      __DEV__ = true
+
+      expect((ast.children[0] as ElementNode).children).toMatchObject([
+        {
+          type: NodeTypes.ELEMENT,
+          tag: 'p'
+        },
+        {
+          type: NodeTypes.ELEMENT,
+          tag: 'p'
+        }
+      ])
+    })
   })
 
   describe('Element', () => {
index 60d6042713c42800fee1444a71cc9853f9de52e5..2d66e84ae5bb1f4d87b1751f8aaf5e876a8b6679 100644 (file)
@@ -198,48 +198,48 @@ function parseChildren(
   // (same as v2 whitespace: 'condense')
   let removedWhitespace = false
   if (mode !== TextModes.RAWTEXT) {
-    if (!context.inPre) {
-      for (let i = 0; i < nodes.length; i++) {
-        const node = nodes[i]
-        if (node.type === NodeTypes.TEXT) {
-          if (!/[^\t\r\n\f ]/.test(node.content)) {
-            const prev = nodes[i - 1]
-            const next = nodes[i + 1]
-            // If:
-            // - the whitespace is the first or last node, or:
-            // - the whitespace is adjacent to a comment, or:
-            // - the whitespace is between two elements AND contains newline
-            // Then the whitespace is ignored.
-            if (
-              !prev ||
-              !next ||
-              prev.type === NodeTypes.COMMENT ||
-              next.type === NodeTypes.COMMENT ||
-              (prev.type === NodeTypes.ELEMENT &&
-                next.type === NodeTypes.ELEMENT &&
-                /[\r\n]/.test(node.content))
-            ) {
-              removedWhitespace = true
-              nodes[i] = null as any
-            } else {
-              // Otherwise, condensed consecutive whitespace inside the text
-              // down to a single space
-              node.content = ' '
-            }
+    for (let i = 0; i < nodes.length; i++) {
+      const node = nodes[i]
+      if (!context.inPre && node.type === NodeTypes.TEXT) {
+        if (!/[^\t\r\n\f ]/.test(node.content)) {
+          const prev = nodes[i - 1]
+          const next = nodes[i + 1]
+          // If:
+          // - the whitespace is the first or last node, or:
+          // - the whitespace is adjacent to a comment, or:
+          // - the whitespace is between two elements AND contains newline
+          // Then the whitespace is ignored.
+          if (
+            !prev ||
+            !next ||
+            prev.type === NodeTypes.COMMENT ||
+            next.type === NodeTypes.COMMENT ||
+            (prev.type === NodeTypes.ELEMENT &&
+              next.type === NodeTypes.ELEMENT &&
+              /[\r\n]/.test(node.content))
+          ) {
+            removedWhitespace = true
+            nodes[i] = null as any
           } else {
-            node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
+            // Otherwise, condensed consecutive whitespace inside the text
+            // down to a single space
+            node.content = ' '
           }
-        } else if (
-          !__DEV__ &&
-          node.type === NodeTypes.COMMENT &&
-          !context.options.comments
-        ) {
-          // remove comment nodes in prod by default
-          removedWhitespace = true
-          nodes[i] = null as any
+        } else {
+          node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
         }
       }
-    } else if (parent && context.options.isPreTag(parent.tag)) {
+      // also remove comment nodes in prod by default
+      if (
+        !__DEV__ &&
+        node.type === NodeTypes.COMMENT &&
+        !context.options.comments
+      ) {
+        removedWhitespace = true
+        nodes[i] = null as any
+      }
+    }
+    if (context.inPre && 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]