From 4623651086a1e99658ef29136f8971ffd7c15fa4 Mon Sep 17 00:00:00 2001 From: defcc Date: Fri, 20 Jan 2017 21:23:15 +0800 Subject: [PATCH] preserve the only whitespace child --- src/compiler/parser/index.js | 4 ++-- test/unit/modules/compiler/parser.spec.js | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/compiler/parser/index.js b/src/compiler/parser/index.js index fde176e7016..4febe1ce7fe 100644 --- a/src/compiler/parser/index.js +++ b/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 diff --git a/test/unit/modules/compiler/parser.spec.js b/test/unit/modules/compiler/parser.spec.js index 566c637950e..751edd1326c 100644 --- a/test/unit/modules/compiler/parser.spec.js +++ b/test/unit/modules/compiler/parser.spec.js @@ -481,12 +481,16 @@ describe('parser', () => { it('preserve whitespace in
 tag', function () {
     const options = extend({}, baseOptions)
-    const ast = parse('
  \nhi\n  
', options) + const ast = parse('
  \nhi\n   
', 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', () => {