github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/vue-1.0.24/src/directives/public/bind.js (about)

     1  import { warn, setClass, camelize } from '../../util/index'
     2  import { BIND } from '../priorities'
     3  import vStyle from '../internal/style'
     4  import { tokensToExp } from '../../parsers/text'
     5  
     6  // xlink
     7  const xlinkNS = 'http://www.w3.org/1999/xlink'
     8  const xlinkRE = /^xlink:/
     9  
    10  // check for attributes that prohibit interpolations
    11  const disallowedInterpAttrRE = /^v-|^:|^@|^(?:is|transition|transition-mode|debounce|track-by|stagger|enter-stagger|leave-stagger)$/
    12  // these attributes should also set their corresponding properties
    13  // because they only affect the initial state of the element
    14  const attrWithPropsRE = /^(?:value|checked|selected|muted)$/
    15  // these attributes expect enumrated values of "true" or "false"
    16  // but are not boolean attributes
    17  const enumeratedAttrRE = /^(?:draggable|contenteditable|spellcheck)$/
    18  
    19  // these attributes should set a hidden property for
    20  // binding v-model to object values
    21  const modelProps = {
    22    value: '_value',
    23    'true-value': '_trueValue',
    24    'false-value': '_falseValue'
    25  }
    26  
    27  export default {
    28  
    29    priority: BIND,
    30  
    31    bind () {
    32      var attr = this.arg
    33      var tag = this.el.tagName
    34      // should be deep watch on object mode
    35      if (!attr) {
    36        this.deep = true
    37      }
    38      // handle interpolation bindings
    39      const descriptor = this.descriptor
    40      const tokens = descriptor.interp
    41      if (tokens) {
    42        // handle interpolations with one-time tokens
    43        if (descriptor.hasOneTime) {
    44          this.expression = tokensToExp(tokens, this._scope || this.vm)
    45        }
    46  
    47        // only allow binding on native attributes
    48        if (
    49          disallowedInterpAttrRE.test(attr) ||
    50          (attr === 'name' && (tag === 'PARTIAL' || tag === 'SLOT'))
    51        ) {
    52          process.env.NODE_ENV !== 'production' && warn(
    53            attr + '="' + descriptor.raw + '": ' +
    54            'attribute interpolation is not allowed in Vue.js ' +
    55            'directives and special attributes.',
    56            this.vm
    57          )
    58          this.el.removeAttribute(attr)
    59          this.invalid = true
    60        }
    61  
    62        /* istanbul ignore if */
    63        if (process.env.NODE_ENV !== 'production') {
    64          var raw = attr + '="' + descriptor.raw + '": '
    65          // warn src
    66          if (attr === 'src') {
    67            warn(
    68              raw + 'interpolation in "src" attribute will cause ' +
    69              'a 404 request. Use v-bind:src instead.',
    70              this.vm
    71            )
    72          }
    73  
    74          // warn style
    75          if (attr === 'style') {
    76            warn(
    77              raw + 'interpolation in "style" attribute will cause ' +
    78              'the attribute to be discarded in Internet Explorer. ' +
    79              'Use v-bind:style instead.',
    80              this.vm
    81            )
    82          }
    83        }
    84      }
    85    },
    86  
    87    update (value) {
    88      if (this.invalid) {
    89        return
    90      }
    91      var attr = this.arg
    92      if (this.arg) {
    93        this.handleSingle(attr, value)
    94      } else {
    95        this.handleObject(value || {})
    96      }
    97    },
    98  
    99    // share object handler with v-bind:class
   100    handleObject: vStyle.handleObject,
   101  
   102    handleSingle (attr, value) {
   103      const el = this.el
   104      const interp = this.descriptor.interp
   105      if (this.modifiers.camel) {
   106        attr = camelize(attr)
   107      }
   108      if (
   109        !interp &&
   110        attrWithPropsRE.test(attr) &&
   111        attr in el
   112      ) {
   113        var attrValue = attr === 'value'
   114          ? value == null // IE9 will set input.value to "null" for null...
   115            ? ''
   116            : value
   117          : value
   118  
   119        if (el[attr] !== attrValue) {
   120          el[attr] = attrValue
   121        }
   122      }
   123      // set model props
   124      var modelProp = modelProps[attr]
   125      if (!interp && modelProp) {
   126        el[modelProp] = value
   127        // update v-model if present
   128        var model = el.__v_model
   129        if (model) {
   130          model.listener()
   131        }
   132      }
   133      // do not set value attribute for textarea
   134      if (attr === 'value' && el.tagName === 'TEXTAREA') {
   135        el.removeAttribute(attr)
   136        return
   137      }
   138      // update attribute
   139      if (enumeratedAttrRE.test(attr)) {
   140        el.setAttribute(attr, value ? 'true' : 'false')
   141      } else if (value != null && value !== false) {
   142        if (attr === 'class') {
   143          // handle edge case #1960:
   144          // class interpolation should not overwrite Vue transition class
   145          if (el.__v_trans) {
   146            value += ' ' + el.__v_trans.id + '-transition'
   147          }
   148          setClass(el, value)
   149        } else if (xlinkRE.test(attr)) {
   150          el.setAttributeNS(xlinkNS, attr, value === true ? '' : value)
   151        } else {
   152          el.setAttribute(attr, value === true ? '' : value)
   153        }
   154      } else {
   155        el.removeAttribute(attr)
   156      }
   157    }
   158  }