Преглед изворни кода

fix(vdom): avoid executing root level script tags (#11487)

fix #11483
shadowings-zy пре 5 година
родитељ
комит
fb16d7bfa1
2 измењених фајлова са 15 додато и 1 уклоњено
  1. 2 1
      src/compiler/codegen/index.js
  2. 13 0
      test/unit/features/component/component.spec.js

+ 2 - 1
src/compiler/codegen/index.js

@@ -45,7 +45,8 @@ export function generate (
   options: CompilerOptions
 ): CodegenResult {
   const state = new CodegenState(options)
-  const code = ast ? genElement(ast, state) : '_c("div")'
+  // fix #11483, Root level <script> tags should not be rendered.
+  const code = ast ? (ast.tag === 'script' ? 'null' : genElement(ast, state)) : '_c("div")'
   return {
     render: `with(this){return ${code}}`,
     staticRenderFns: state.staticRenderFns

+ 13 - 0
test/unit/features/component/component.spec.js

@@ -426,4 +426,17 @@ describe('Component', () => {
       vm.$destroy()
     }).then(done)
   })
+
+  it('render vnode with <script> tag as root element', () => {
+    const vm = new Vue({
+      template: '<scriptTest></scriptTest>',
+      components: {
+        scriptTest: {
+          template: '<script>console.log(1)</script>'
+        }
+      }
+    }).$mount()
+    expect(vm.$el.nodeName).toBe('#comment')
+    expect('Templates should only be responsible for mapping the state').toHaveBeenWarned()
+  })
 })