Parcourir la source

forbid tags with side-effects in templates

Evan You il y a 10 ans
Parent
commit
f123bd397c
1 fichiers modifiés avec 20 ajouts et 1 suppressions
  1. 20 1
      src/compiler/parser/index.js

+ 20 - 1
src/compiler/parser/index.js

@@ -70,6 +70,15 @@ export function parse (template, options) {
         children: []
       }
 
+      if (isForbiddenTag(element)) {
+        element.forbidden = true
+        process.env.NODE_ENV !== 'production' && warn(
+          'Templates should only be responsbile for mapping the state to the ' +
+          'UI. Avoid placing tags with side-effects in your templates, such as ' +
+          `<${tag}>.`
+        )
+      }
+
       // check namespace.
       // inherit parent ns if there is one
       let ns
@@ -111,7 +120,7 @@ export function parse (template, options) {
           `Component template should contain exactly one root element:\n\n${template}`
         )
       }
-      if (currentParent) {
+      if (currentParent && !element.forbidden) {
         if (element.else) {
           processElse(element, currentParent)
         } else {
@@ -384,3 +393,13 @@ function findPrevElement (children) {
     if (children[i].tag) return children[i]
   }
 }
+
+function isForbiddenTag (el) {
+  return (
+    el.tag === 'style' ||
+    (el.tag === 'script' && (
+      !el.attrsMap.type ||
+      el.attrsMap.type === 'text/javascript'
+    ))
+  )
+}