Skip to content

Commit 0f1fab8

Browse files
JustineoLostlover
authored andcommitted
fix(compiler): whitespace: 'condense' should honor pre tag as well (vuejs#9660)
1 parent 743e920 commit 0f1fab8

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/compiler/parser/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ export function parse (
351351
text = preserveWhitespace ? ' ' : ''
352352
}
353353
if (text) {
354-
if (whitespaceOption === 'condense') {
354+
if (!inPre && whitespaceOption === 'condense') {
355355
// condense consecutive whitespaces into single space
356356
text = text.replace(whitespaceRE, ' ')
357357
}

test/unit/modules/compiler/parser.spec.js

+44-5
Original file line numberDiff line numberDiff line change
@@ -820,12 +820,14 @@ describe('parser', () => {
820820
expect(ast.children[3].children[0].text).toBe('.\n Have fun!\n')
821821
})
822822

823+
const condenseOptions = extend({
824+
whitespace: 'condense',
825+
// should be ignored when whitespace is specified
826+
preserveWhitespace: false
827+
}, baseOptions)
828+
823829
it(`whitespace: 'condense'`, () => {
824-
const options = extend({
825-
whitespace: 'condense',
826-
// should be ignored when whitespace is specified
827-
preserveWhitespace: false
828-
}, baseOptions)
830+
const options = extend({}, condenseOptions)
829831
const ast = parse('<p>\n Welcome to <b>Vue.js</b> <i>world</i> \n <span>.\n Have fun!\n</span></p>', options)
830832
expect(ast.tag).toBe('p')
831833
expect(ast.children.length).toBe(5)
@@ -842,4 +844,41 @@ describe('parser', () => {
842844
expect(ast.children[4].tag).toBe('span')
843845
expect(ast.children[4].children[0].text).toBe('. Have fun! ')
844846
})
847+
848+
it(`preserve whitespace in <pre> tag with whitespace: 'condense'`, function () {
849+
const options = extend({}, condenseOptions)
850+
const ast = parse('<pre><code> \n<span>hi</span>\n </code><span> </span></pre>', options)
851+
const code = ast.children[0]
852+
expect(code.children[0].type).toBe(3)
853+
expect(code.children[0].text).toBe(' \n')
854+
expect(code.children[2].type).toBe(3)
855+
expect(code.children[2].text).toBe('\n ')
856+
857+
const span = ast.children[1]
858+
expect(span.children[0].type).toBe(3)
859+
expect(span.children[0].text).toBe(' ')
860+
})
861+
862+
it(`ignore the first newline in <pre> tag with whitespace: 'condense'`, function () {
863+
const options = extend({}, condenseOptions)
864+
const ast = parse('<div><pre>\nabc</pre>\ndef<pre>\n\nabc</pre></div>', options)
865+
const pre = ast.children[0]
866+
expect(pre.children[0].type).toBe(3)
867+
expect(pre.children[0].text).toBe('abc')
868+
const text = ast.children[1]
869+
expect(text.type).toBe(3)
870+
expect(text.text).toBe(' def')
871+
const pre2 = ast.children[2]
872+
expect(pre2.children[0].type).toBe(3)
873+
expect(pre2.children[0].text).toBe('\nabc')
874+
})
875+
876+
it(`keep first newline after unary tag in <pre> with whitespace: 'condense'`, () => {
877+
const options = extend({}, condenseOptions)
878+
const ast = parse('<pre>abc<input>\ndef</pre>', options)
879+
expect(ast.children[1].type).toBe(1)
880+
expect(ast.children[1].tag).toBe('input')
881+
expect(ast.children[2].type).toBe(3)
882+
expect(ast.children[2].text).toBe('\ndef')
883+
})
845884
})

0 commit comments

Comments
 (0)