Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn when template contains text outside root element #5164

Merged
merged 3 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export function parse (
let inPre = false
let warned = false

function warnOnce (msg) {
if (!warned) {
warned = true
warn(msg)
}
}

function endPre (element) {
// check pre state
if (element.pre) {
Expand Down Expand Up @@ -144,17 +151,15 @@ export function parse (
}

function checkRootConstraints (el) {
if (process.env.NODE_ENV !== 'production' && !warned) {
if (process.env.NODE_ENV !== 'production') {
if (el.tag === 'slot' || el.tag === 'template') {
warned = true
warn(
warnOnce(
`Cannot use <${el.tag}> as component root element because it may ` +
'contain multiple nodes.'
)
}
if (el.attrsMap.hasOwnProperty('v-for')) {
warned = true
warn(
warnOnce(
'Cannot use v-for on stateful component root element because ' +
'it renders multiple elements.'
)
Expand All @@ -174,9 +179,8 @@ export function parse (
exp: element.elseif,
block: element
})
} else if (process.env.NODE_ENV !== 'production' && !warned) {
warned = true
warn(
} else if (process.env.NODE_ENV !== 'production') {
warnOnce(
`Component template should contain exactly one root element. ` +
`If you are using v-if on multiple elements, ` +
`use v-else-if to chain them instead.`
Expand Down Expand Up @@ -222,11 +226,16 @@ export function parse (

chars (text: string) {
if (!currentParent) {
if (process.env.NODE_ENV !== 'production' && !warned && text === template) {
warned = true
warn(
'Component template requires a root element, rather than just text.'
)
if (process.env.NODE_ENV !== 'production') {
if (text === template) {
warnOnce(
'Component template requires a root element, rather than just text.'
)
} else if ((text = text.trim())) {
warnOnce(
`text "${text}" outside root element will be ignored.`
)
}
}
return
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/modules/compiler/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ describe('parser', () => {
expect('Component template requires a root element, rather than just text').toHaveBeenWarned()
})

it('warn text before root element', () => {
parse('before root {{ interpolation }}<div></div>', baseOptions)
expect('text "before root {{ interpolation }}" outside root element will be ignored.').toHaveBeenWarned()
})

it('warn text after root element', () => {
parse('<div></div>after root {{ interpolation }}', baseOptions)
expect('text "after root {{ interpolation }}" outside root element will be ignored.').toHaveBeenWarned()
})

it('warn multiple root elements', () => {
parse('<div></div><div></div>', baseOptions)
expect('Component template should contain exactly one root element').toHaveBeenWarned()
Expand Down