Skip to content

Commit 01f533d

Browse files
committed
avoid duplicate whitespace nodes caused by comments
1 parent c779819 commit 01f533d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/compiler/parser/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,20 @@ export function parse (
229229
currentParent.attrsMap.placeholder === text) {
230230
return
231231
}
232+
const children = currentParent.children
232233
text = inPre || text.trim()
233234
? decodeHTMLCached(text)
234235
// only preserve whitespace if its not right after a starting tag
235-
: preserveWhitespace && currentParent.children.length ? ' ' : ''
236+
: preserveWhitespace && children.length ? ' ' : ''
236237
if (text) {
237238
let expression
238239
if (!inVPre && text !== ' ' && (expression = parseText(text, delimiters))) {
239-
currentParent.children.push({
240+
children.push({
240241
type: 2,
241242
expression,
242243
text
243244
})
244-
} else {
245+
} else if (text !== ' ' || children[children.length - 1].text !== ' ') {
245246
currentParent.children.push({
246247
type: 3,
247248
text

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ describe('parser', () => {
8282
expect('Component template should contain exactly one root element:\n\n<div></div><div></div>').toHaveBeenWarned()
8383
})
8484

85+
it('remove duplicate whitespace text nodes caused by comments', () => {
86+
const ast = parse(`<div><a></a> <!----> <a></a></div>`, baseOptions)
87+
expect(ast.children.length).toBe(3)
88+
expect(ast.children[0].tag).toBe('a')
89+
expect(ast.children[1].text).toBe(' ')
90+
expect(ast.children[2].tag).toBe('a')
91+
})
92+
8593
it('remove text nodes between v-if conditions', () => {
8694
const ast = parse(`<div><div v-if="1"></div> <div v-else-if="2"></div> <div v-else></div> <span></span></div>`, baseOptions)
8795
expect(ast.children.length).toBe(3)

0 commit comments

Comments
 (0)