فهرست منبع

preserve the only whitespace child (#4760)

chengchao 9 سال پیش
والد
کامیت
e02fb1294d
2فایلهای تغییر یافته به همراه7 افزوده شده و 3 حذف شده
  1. 2 2
      src/compiler/parser/index.js
  2. 5 1
      test/unit/modules/compiler/parser.spec.js

+ 2 - 2
src/compiler/parser/index.js

@@ -197,7 +197,7 @@ export function parse (
       // remove trailing whitespace
       const element = stack[stack.length - 1]
       const lastNode = element.children[element.children.length - 1]
-      if (lastNode && lastNode.type === 3 && lastNode.text === ' ') {
+      if (lastNode && lastNode.type === 3 && lastNode.text === ' ' && !inPre) {
         element.children.pop()
       }
       // pop stack
@@ -242,7 +242,7 @@ export function parse (
             expression,
             text
           })
-        } else if (text !== ' ' || children[children.length - 1].text !== ' ') {
+        } else if (text !== ' ' || !children.length || children[children.length - 1].text !== ' ') {
           currentParent.children.push({
             type: 3,
             text

+ 5 - 1
test/unit/modules/compiler/parser.spec.js

@@ -481,12 +481,16 @@ describe('parser', () => {
 
   it('preserve whitespace in <pre> tag', function () {
     const options = extend({}, baseOptions)
-    const ast = parse('<pre><code>  \n<span>hi</span>\n  </code></pre>', options)
+    const ast = parse('<pre><code>  \n<span>hi</span>\n  </code><span> </span></pre>', options)
     const code = ast.children[0]
     expect(code.children[0].type).toBe(3)
     expect(code.children[0].text).toBe('  \n')
     expect(code.children[2].type).toBe(3)
     expect(code.children[2].text).toBe('\n  ')
+
+    const span = ast.children[1]
+    expect(span.children[0].type).toBe(3)
+    expect(span.children[0].text).toBe(' ')
   })
 
   it('forgivingly handle < in plain text', () => {