Skip to content

Commit e02fb12

Browse files
defccyyx990803
authored andcommitted
preserve the only whitespace child (vuejs#4760)
1 parent c9fbcaf commit e02fb12

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/compiler/parser/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export function parse (
197197
// remove trailing whitespace
198198
const element = stack[stack.length - 1]
199199
const lastNode = element.children[element.children.length - 1]
200-
if (lastNode && lastNode.type === 3 && lastNode.text === ' ') {
200+
if (lastNode && lastNode.type === 3 && lastNode.text === ' ' && !inPre) {
201201
element.children.pop()
202202
}
203203
// pop stack
@@ -242,7 +242,7 @@ export function parse (
242242
expression,
243243
text
244244
})
245-
} else if (text !== ' ' || children[children.length - 1].text !== ' ') {
245+
} else if (text !== ' ' || !children.length || children[children.length - 1].text !== ' ') {
246246
currentParent.children.push({
247247
type: 3,
248248
text

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,16 @@ describe('parser', () => {
481481

482482
it('preserve whitespace in <pre> tag', function () {
483483
const options = extend({}, baseOptions)
484-
const ast = parse('<pre><code> \n<span>hi</span>\n </code></pre>', options)
484+
const ast = parse('<pre><code> \n<span>hi</span>\n </code><span> </span></pre>', options)
485485
const code = ast.children[0]
486486
expect(code.children[0].type).toBe(3)
487487
expect(code.children[0].text).toBe(' \n')
488488
expect(code.children[2].type).toBe(3)
489489
expect(code.children[2].text).toBe('\n ')
490+
491+
const span = ast.children[1]
492+
expect(span.children[0].type).toBe(3)
493+
expect(span.children[0].text).toBe(' ')
490494
})
491495

492496
it('forgivingly handle < in plain text', () => {

0 commit comments

Comments
 (0)