Skip to content

Commit c6ab2e0

Browse files
javoskiyyx990803
authored andcommitted
warn when template contains text outside root element (#5164)
* warn when template contains text outside root element * fix warned flag * make warn once a function
1 parent 025e763 commit c6ab2e0

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/compiler/parser/index.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ export function parse (
6363
let inPre = false
6464
let warned = false
6565

66+
function warnOnce (msg) {
67+
if (!warned) {
68+
warned = true
69+
warn(msg)
70+
}
71+
}
72+
6673
function endPre (element) {
6774
// check pre state
6875
if (element.pre) {
@@ -146,17 +153,15 @@ export function parse (
146153
}
147154

148155
function checkRootConstraints (el) {
149-
if (process.env.NODE_ENV !== 'production' && !warned) {
156+
if (process.env.NODE_ENV !== 'production') {
150157
if (el.tag === 'slot' || el.tag === 'template') {
151-
warned = true
152-
warn(
158+
warnOnce(
153159
`Cannot use <${el.tag}> as component root element because it may ` +
154160
'contain multiple nodes.'
155161
)
156162
}
157163
if (el.attrsMap.hasOwnProperty('v-for')) {
158-
warned = true
159-
warn(
164+
warnOnce(
160165
'Cannot use v-for on stateful component root element because ' +
161166
'it renders multiple elements.'
162167
)
@@ -176,9 +181,8 @@ export function parse (
176181
exp: element.elseif,
177182
block: element
178183
})
179-
} else if (process.env.NODE_ENV !== 'production' && !warned) {
180-
warned = true
181-
warn(
184+
} else if (process.env.NODE_ENV !== 'production') {
185+
warnOnce(
182186
`Component template should contain exactly one root element. ` +
183187
`If you are using v-if on multiple elements, ` +
184188
`use v-else-if to chain them instead.`
@@ -224,11 +228,16 @@ export function parse (
224228

225229
chars (text: string) {
226230
if (!currentParent) {
227-
if (process.env.NODE_ENV !== 'production' && !warned && text === template) {
228-
warned = true
229-
warn(
230-
'Component template requires a root element, rather than just text.'
231-
)
231+
if (process.env.NODE_ENV !== 'production') {
232+
if (text === template) {
233+
warnOnce(
234+
'Component template requires a root element, rather than just text.'
235+
)
236+
} else if ((text = text.trim())) {
237+
warnOnce(
238+
`text "${text}" outside root element will be ignored.`
239+
)
240+
}
232241
}
233242
return
234243
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ describe('parser', () => {
7777
expect('Component template requires a root element, rather than just text').toHaveBeenWarned()
7878
})
7979

80+
it('warn text before root element', () => {
81+
parse('before root {{ interpolation }}<div></div>', baseOptions)
82+
expect('text "before root {{ interpolation }}" outside root element will be ignored.').toHaveBeenWarned()
83+
})
84+
85+
it('warn text after root element', () => {
86+
parse('<div></div>after root {{ interpolation }}', baseOptions)
87+
expect('text "after root {{ interpolation }}" outside root element will be ignored.').toHaveBeenWarned()
88+
})
89+
8090
it('warn multiple root elements', () => {
8191
parse('<div></div><div></div>', baseOptions)
8292
expect('Component template should contain exactly one root element').toHaveBeenWarned()

0 commit comments

Comments
 (0)