diff --git a/src/compiler/parser/index.js b/src/compiler/parser/index.js index 0689eb87..fb258752 100644 --- a/src/compiler/parser/index.js +++ b/src/compiler/parser/index.js @@ -351,7 +351,7 @@ export function parse ( text = preserveWhitespace ? ' ' : '' } if (text) { - if (whitespaceOption === 'condense') { + if (!inPre && whitespaceOption === 'condense') { // condense consecutive whitespaces into single space text = text.replace(whitespaceRE, ' ') } diff --git a/test/unit/modules/compiler/parser.spec.js b/test/unit/modules/compiler/parser.spec.js index 13f1c0d3..d6521bbf 100644 --- a/test/unit/modules/compiler/parser.spec.js +++ b/test/unit/modules/compiler/parser.spec.js @@ -820,12 +820,14 @@ describe('parser', () => { expect(ast.children[3].children[0].text).toBe('.\n Have fun!\n') }) + const condenseOptions = extend({ + whitespace: 'condense', + // should be ignored when whitespace is specified + preserveWhitespace: false + }, baseOptions) + it(`whitespace: 'condense'`, () => { - const options = extend({ - whitespace: 'condense', - // should be ignored when whitespace is specified - preserveWhitespace: false - }, baseOptions) + const options = extend({}, condenseOptions) const ast = parse('
\n Welcome to Vue.js world \n .\n Have fun!\n
', options) expect(ast.tag).toBe('p') expect(ast.children.length).toBe(5) @@ -842,4 +844,41 @@ describe('parser', () => { expect(ast.children[4].tag).toBe('span') expect(ast.children[4].children[0].text).toBe('. Have fun! ') }) + + it(`preserve whitespace intag with whitespace: 'condense'`, function () { + const options = extend({}, condenseOptions) + const ast = parse('', 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(`ignore the first newline in\nhi\n
tag with whitespace: 'condense'`, function () { + const options = extend({}, condenseOptions) + const ast = parse('', options) + const pre = ast.children[0] + expect(pre.children[0].type).toBe(3) + expect(pre.children[0].text).toBe('abc') + const text = ast.children[1] + expect(text.type).toBe(3) + expect(text.text).toBe(' def') + const pre2 = ast.children[2] + expect(pre2.children[0].type).toBe(3) + expect(pre2.children[0].text).toBe('\nabc') + }) + + it(`keep first newline after unary tag in\nabc\ndef\n\nabcwith whitespace: 'condense'`, () => { + const options = extend({}, condenseOptions) + const ast = parse('abc\ndef', options) + expect(ast.children[1].type).toBe(1) + expect(ast.children[1].tag).toBe('input') + expect(ast.children[2].type).toBe(3) + expect(ast.children[2].text).toBe('\ndef') + }) })