Просмотр исходного кода

check invalid v-bind in production build as well

Evan You 10 лет назад
Родитель
Сommit
97754de83b
2 измененных файлов с 48 добавлено и 48 удалено
  1. 2 1
      src/compiler/compile.js
  2. 46 47
      src/directives/public/bind.js

+ 2 - 1
src/compiler/compile.js

@@ -560,9 +560,10 @@ function compileDirectives (attrs, options) {
     attr = attrs[i]
     name = attr.name
     raw = value = attr.value
+    tokens = textParser.parse(value)
 
     // attribute interpolations
-    if (tokens = textParser.parse(value)) {
+    if (tokens) {
       value = textParser.tokensToExp(tokens)
       pushDir('bind', publicDirectives.bind, {
         arg: name,

+ 46 - 47
src/directives/public/bind.js

@@ -24,6 +24,52 @@ module.exports = {
 
   priority: 850,
 
+  bind: function () {
+    var attr = this.arg
+    // handle interpolation bindings
+    if (this.descriptor.interp) {
+      // only allow binding on native attributes
+      if (!(
+        // class is allowed globally
+        attr === 'class' ||
+        // data attributes are allowed globally
+        /^data-/.test(attr) ||
+        // for available
+        (attr === 'for' && 'htmlFor' in this.el) ||
+        // camelized prop available
+        _.camelize(attr) in this.el
+      )) {
+        process.env.NODE_ENV !== 'production' && _.warn(
+          attr + '="' + this.descriptor.raw + '": ' +
+          'attribute interpolation is allowed only ' +
+          'in valid native attributes. "' + attr + '" ' +
+          'is not a valid attribute on <' + this.el.tagName.toLowerCase() + '>.'
+        )
+        this.invalid = true
+      }
+
+      if (process.env.NODE_ENV !== 'production') {
+        var raw = attr + '="' + this.descriptor.raw + '": '
+        // warn src
+        if (attr === 'src') {
+          _.warn(
+            raw + 'interpolation in "src" attribute will cause ' +
+            'a 404 request. Use v-bind:src instead.'
+          )
+        }
+
+        // warn style
+        if (attr === 'style') {
+          _.warn(
+            raw + 'interpolation in "style" attribtue will cause ' +
+            'the attribtue to be discarded in Internet Explorer. ' +
+            'Use v-bind:style instead.'
+          )
+        }
+      }
+    }
+  },
+
   update: function (value) {
     if (this.invalid) return
     var attr = this.arg
@@ -49,50 +95,3 @@ module.exports = {
     }
   }
 }
-
-if (process.env.NODE_ENV !== 'production') {
-  module.exports.bind = function () {
-    var attr = this.arg
-    // handle interpolation bindings
-    if (this.descriptor.interp) {
-      var raw = attr + '="' + this.descriptor.raw + '": '
-      // only allow binding on native attributes
-      if (
-        // data attributes are allowed
-        !(/^data-/.test(attr)) &&
-        // class is allowed
-        !(attr === 'class') &&
-        (
-          // label for
-          (attr === 'for' && !('htmlFor' in this.el)) ||
-          // other native attributes
-          !(_.camelize(attr) in this.el)
-        )
-      ) {
-        _.warn(
-          raw + 'attribute interpolation is allowed only ' +
-          'in valid native attributes. "' + attr + '" ' +
-          'is not a valid attribute on <' + this.el.tagName.toLowerCase() + '>.'
-        )
-        this.invalid = true
-      }
-
-      // warn src
-      if (attr === 'src') {
-        _.warn(
-          raw + 'interpolation in "src" attribute will cause ' +
-          'a 404 request. Use v-bind:src instead.'
-        )
-      }
-
-      // warn style
-      if (attr === 'style') {
-        _.warn(
-          raw + 'interpolation in "style" attribtue will cause ' +
-          'the attribtue to be discarded in Internet Explorer. ' +
-          'Use v-bind:style instead.'
-        )
-      }
-    }
-  }
-}