Skip to content

Commit de199f9

Browse files
chrisvfritzyyx990803
authored andcommitted
allow 2 root nodes with v-if and v-else (#3887)
* allow 2 root nodes with v-if and v-else * fix compiler-options test with less specific text
1 parent f59e903 commit de199f9

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

Diff for: src/compiler/parser/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ export function parse (
153153
checkRootConstraints(root)
154154
} else if (process.env.NODE_ENV !== 'production' && !stack.length && !warned) {
155155
// allow 2 root elements with v-if and v-else
156-
if ((root.attrsMap.hasOwnProperty('v-if') && element.attrsMap.hasOwnProperty('v-else'))) {
156+
if (root.if && element.else) {
157157
checkRootConstraints(element)
158+
root.elseBlock = element
158159
} else {
159160
warned = true
160161
warn(
@@ -201,10 +202,10 @@ export function parse (
201202

202203
chars (text: string) {
203204
if (!currentParent) {
204-
if (process.env.NODE_ENV !== 'production' && !warned) {
205+
if (process.env.NODE_ENV !== 'production' && !warned && text === template) {
205206
warned = true
206207
warn(
207-
'Component template should contain exactly one root element:\n\n' + template
208+
'Component template requires a root element, rather than just text:\n\n' + template
208209
)
209210
}
210211
return

Diff for: test/unit/modules/compiler/compiler-options.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe('compile options', () => {
118118
it('should collect errors', () => {
119119
let compiled = compile('hello')
120120
expect(compiled.errors.length).toBe(1)
121-
expect(compiled.errors[0]).toContain('should contain exactly one root element')
121+
expect(compiled.errors[0]).toContain('root element')
122122

123123
compiled = compile('<div v-if="a----">{{ b++++ }}</div>')
124124
expect(compiled.errors.length).toBe(2)

Diff for: test/unit/modules/compiler/parser.spec.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('parser', () => {
7373

7474
it('not contain root element', () => {
7575
parse('hello world', baseOptions)
76-
expect('Component template should contain exactly one root element').toHaveBeenWarned()
76+
expect('Component template requires a root element, rather than just text').toHaveBeenWarned()
7777
})
7878

7979
it('warn multiple root elements', () => {
@@ -83,10 +83,28 @@ describe('parser', () => {
8383

8484
it('not warn 2 root elements with v-if and v-else', () => {
8585
parse('<div v-if="1"></div><div v-else></div>', baseOptions)
86-
expect('Component template should contain exactly one root element:\n\n<div v-if="1"></div><div v-else></div>')
86+
expect('Component template should contain exactly one root element')
8787
.not.toHaveBeenWarned()
8888
})
8989

90+
it('not warn 2 root elements with v-if and v-else on separate lines', () => {
91+
parse(`
92+
<div v-if="1"></div>
93+
<div v-else></div>
94+
`, baseOptions)
95+
expect('Component template should contain exactly one root element')
96+
.not.toHaveBeenWarned()
97+
})
98+
99+
it('generate correct ast for 2 root elements with v-if and v-else on separate lines', () => {
100+
const ast = parse(`
101+
<div v-if="1"></div>
102+
<p v-else></p>
103+
`, baseOptions)
104+
expect(ast.tag).toBe('div')
105+
expect(ast.elseBlock.tag).toBe('p')
106+
})
107+
90108
it('warn 2 root elements with v-if', () => {
91109
parse('<div v-if="1"></div><div v-if="2"></div>', baseOptions)
92110
expect('Component template should contain exactly one root element:\n\n<div v-if="1"></div><div v-if="2"></div>')

0 commit comments

Comments
 (0)