Просмотр исходного кода

avoid duplicate whitespace nodes caused by comments

Evan You 9 лет назад
Родитель
Сommit
01f533db1a
2 измененных файлов с 12 добавлено и 3 удалено
  1. 4 3
      src/compiler/parser/index.js
  2. 8 0
      test/unit/modules/compiler/parser.spec.js

+ 4 - 3
src/compiler/parser/index.js

@@ -229,19 +229,20 @@ export function parse (
           currentParent.attrsMap.placeholder === text) {
         return
       }
+      const children = currentParent.children
       text = inPre || text.trim()
         ? decodeHTMLCached(text)
         // only preserve whitespace if its not right after a starting tag
-        : preserveWhitespace && currentParent.children.length ? ' ' : ''
+        : preserveWhitespace && children.length ? ' ' : ''
       if (text) {
         let expression
         if (!inVPre && text !== ' ' && (expression = parseText(text, delimiters))) {
-          currentParent.children.push({
+          children.push({
             type: 2,
             expression,
             text
           })
-        } else {
+        } else if (text !== ' ' || children[children.length - 1].text !== ' ') {
           currentParent.children.push({
             type: 3,
             text

+ 8 - 0
test/unit/modules/compiler/parser.spec.js

@@ -82,6 +82,14 @@ describe('parser', () => {
     expect('Component template should contain exactly one root element:\n\n<div></div><div></div>').toHaveBeenWarned()
   })
 
+  it('remove duplicate whitespace text nodes caused by comments', () => {
+    const ast = parse(`<div><a></a> <!----> <a></a></div>`, baseOptions)
+    expect(ast.children.length).toBe(3)
+    expect(ast.children[0].tag).toBe('a')
+    expect(ast.children[1].text).toBe(' ')
+    expect(ast.children[2].tag).toBe('a')
+  })
+
   it('remove text nodes between v-if conditions', () => {
     const ast = parse(`<div><div v-if="1"></div> <div v-else-if="2"></div> <div v-else></div> <span></span></div>`, baseOptions)
     expect(ast.children.length).toBe(3)