Skip to content

Commit 2c7e3ad

Browse files
committed
fix(compiler): fix the bug 'root level <script> will rendered'
fix vuejs#11483
1 parent 8ead9d2 commit 2c7e3ad

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/compiler/codegen/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export function generate (
4545
options: CompilerOptions
4646
): CodegenResult {
4747
const state = new CodegenState(options)
48-
const code = ast ? genElement(ast, state) : '_c("div")'
48+
// fix #11483, Root level <script> tags should not be rendered.
49+
const code = ast ? (ast.tag === 'script' ? 'null' : genElement(ast, state)) : '_c("div")'
4950
return {
5051
render: `with(this){return ${code}}`,
5152
staticRenderFns: state.staticRenderFns

test/unit/features/component/component.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,17 @@ describe('Component', () => {
426426
vm.$destroy()
427427
}).then(done)
428428
})
429+
430+
it('render vnode with <script> tag as root element', () => {
431+
const vm = new Vue({
432+
template: '<scriptTest></scriptTest>',
433+
components: {
434+
scriptTest: {
435+
template: '<script>console.log(1)</script>'
436+
}
437+
}
438+
}).$mount()
439+
expect(vm.$el.nodeName).toBe('#comment')
440+
expect('Templates should only be responsible for mapping the state').toHaveBeenWarned()
441+
})
429442
})

0 commit comments

Comments
 (0)