Skip to content

Commit

Permalink
preserve the only whitespace child (#4760)
Browse files Browse the repository at this point in the history
  • Loading branch information
defcc authored and yyx990803 committed Jan 20, 2017
1 parent c9fbcaf commit e02fb12
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/unit/modules/compiler/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit e02fb12

Please sign in to comment.