Kaynağa Gözat

simplify IE bind attr workaround by adjusting check order

Evan You 10 yıl önce
ebeveyn
işleme
9a66ece882
3 değiştirilmiş dosya ile 52 ekleme ve 87 silme
  1. 51 67
      src/compiler/compile-props.js
  2. 0 11
      src/util/env.js
  3. 1 9
      test/unit/specs/misc_spec.js

+ 51 - 67
src/compiler/compile-props.js

@@ -21,7 +21,7 @@ module.exports = function compileProps (el, propOptions) {
   var props = []
   var props = []
   var names = Object.keys(propOptions)
   var names = Object.keys(propOptions)
   var i = names.length
   var i = names.length
-  var options, name, attr, value, path, parsed, prop, hasBinding
+  var options, name, attr, value, path, parsed, prop
   while (i--) {
   while (i--) {
     name = names[i]
     name = names[i]
     options = propOptions[name] || empty
     options = propOptions[name] || empty
@@ -47,89 +47,73 @@ module.exports = function compileProps (el, propOptions) {
       name: name,
       name: name,
       path: path,
       path: path,
       options: options,
       options: options,
-      mode: propBindingModes.ONE_WAY
+      mode: propBindingModes.ONE_WAY,
+      raw: null
     }
     }
 
 
     attr = _.hyphenate(name)
     attr = _.hyphenate(name)
-    hasBinding = _.preferBinding && hasBindingAttr(el, attr)
-
-    // first check literal version
-    value = prop.raw = _.attr(el, attr)
-    if (value === null || hasBinding) {
-      // then check dynamic version
-      if ((value = _.getBindAttr(el, attr)) === null) {
-        if ((value = _.getBindAttr(el, attr + '.sync')) !== null) {
-          prop.mode = propBindingModes.TWO_WAY
-        } else if ((value = _.getBindAttr(el, attr + '.once')) !== null) {
-          prop.mode = propBindingModes.ONE_TIME
-        }
+    // first check dynamic version
+    if ((value = _.getBindAttr(el, attr)) === null) {
+      if ((value = _.getBindAttr(el, attr + '.sync')) !== null) {
+        prop.mode = propBindingModes.TWO_WAY
+      } else if ((value = _.getBindAttr(el, attr + '.once')) !== null) {
+        prop.mode = propBindingModes.ONE_TIME
       }
       }
+    }
+    if (value !== null) {
+      // has dynamic binding!
       prop.raw = value
       prop.raw = value
-      if (value !== null) {
-        parsed = dirParser.parse(value)
-        value = parsed.expression
-        prop.filters = parsed.filters
-        // check binding type
-        if (_.isLiteral(value)) {
-          // for expressions containing literal numbers and
-          // booleans, there's no need to setup a prop binding,
-          // so we can optimize them as a one-time set.
-          prop.optimizedLiteral = true
-        } else {
-          prop.dynamic = true
-          // check non-settable path for two-way bindings
-          if (process.env.NODE_ENV !== 'production' &&
-              prop.mode === propBindingModes.TWO_WAY &&
-              !settablePathRE.test(value)) {
-            prop.mode = propBindingModes.ONE_WAY
-            _.warn(
-              'Cannot bind two-way prop with non-settable ' +
-              'parent path: ' + value
-            )
-          }
-        }
-        prop.parentPath = value
-
-        // warn required two-way
-        if (
-          process.env.NODE_ENV !== 'production' &&
-          options.twoWay &&
-          prop.mode !== propBindingModes.TWO_WAY
-        ) {
+      parsed = dirParser.parse(value)
+      value = parsed.expression
+      prop.filters = parsed.filters
+      // check binding type
+      if (_.isLiteral(value)) {
+        // for expressions containing literal numbers and
+        // booleans, there's no need to setup a prop binding,
+        // so we can optimize them as a one-time set.
+        prop.optimizedLiteral = true
+      } else {
+        prop.dynamic = true
+        // check non-settable path for two-way bindings
+        if (process.env.NODE_ENV !== 'production' &&
+            prop.mode === propBindingModes.TWO_WAY &&
+            !settablePathRE.test(value)) {
+          prop.mode = propBindingModes.ONE_WAY
           _.warn(
           _.warn(
-            'Prop "' + name + '" expects a two-way binding type.'
+            'Cannot bind two-way prop with non-settable ' +
+            'parent path: ' + value
           )
           )
         }
         }
-
-      } else if (options.required) {
-        // warn missing required
-        process.env.NODE_ENV !== 'production' && _.warn(
-          'Missing required prop: ' + name
+      }
+      prop.parentPath = value
+
+      // warn required two-way
+      if (
+        process.env.NODE_ENV !== 'production' &&
+        options.twoWay &&
+        prop.mode !== propBindingModes.TWO_WAY
+      ) {
+        _.warn(
+          'Prop "' + name + '" expects a two-way binding type.'
         )
         )
       }
       }
+    /* eslint-disable no-cond-assign */
+    } else if (value = _.attr(el, attr)) {
+    /* eslint-enable no-cond-assign */
+      // has literal binding!
+      prop.raw = value
+    } else if (options.required) {
+      // warn missing required
+      process.env.NODE_ENV !== 'production' && _.warn(
+        'Missing required prop: ' + name
+      )
     }
     }
-
     // push prop
     // push prop
     props.push(prop)
     props.push(prop)
   }
   }
   return makePropsLinkFn(props)
   return makePropsLinkFn(props)
 }
 }
 
 
-/**
- * Check existance of an attribute with binding syntax.
- *
- * @param {Element} el
- * @return {String} attr
- */
-
-function hasBindingAttr (el, attr) {
-  /* istanbul ignore next */
-  return attr !== 'class' && (
-    el.hasAttribute(':' + attr) ||
-    el.hasAttribute('v-bind:' + attr)
-  )
-}
-
 /**
 /**
  * Build a function that applies props to a vm.
  * Build a function that applies props to a vm.
  *
  *

+ 0 - 11
src/util/env.js

@@ -83,14 +83,3 @@ exports.nextTick = (function () {
     timerFunc(nextTickHandler, 0)
     timerFunc(nextTickHandler, 0)
   }
   }
 })()
 })()
-
-// feature detect browsers (IE) that have trouble
-// with binding syntax on certain attributes
-var div
-var preferBinding = false
-if (inBrowser) {
-  div = document.createElement('div')
-  div.setAttribute(':title', '')
-  preferBinding = div.getAttribute('title') !== null
-}
-exports.preferBinding = preferBinding

+ 1 - 9
test/unit/specs/misc_spec.js

@@ -288,14 +288,6 @@ describe('Misc', function () {
       }
       }
     }
     }
 
 
-    document.body.appendChild(el)
-    el.setAttribute(':title', '')
-    if (el.getAttribute('title') === null) {
-      // this browser does not need this test
-      done()
-      return
-    }
-
     new Vue({
     new Vue({
       el: el,
       el: el,
       template:
       template:
@@ -312,7 +304,7 @@ describe('Misc', function () {
       components: {
       components: {
         comp: {
         comp: {
           props: ['title'],
           props: ['title'],
-          ready: function () {
+          created: function () {
             check(this.title)
             check(this.title)
           }
           }
         }
         }