Browse Source

fix v-bind for enumerated attributes that expect true and false values (fix #2407)

Evan You 10 years ago
parent
commit
54fcb5cf83
2 changed files with 16 additions and 1 deletions
  1. 6 1
      src/directives/public/bind.js
  2. 10 0
      test/unit/specs/directives/public/bind_spec.js

+ 6 - 1
src/directives/public/bind.js

@@ -12,6 +12,9 @@ const disallowedInterpAttrRE = /^v-|^:|^@|^(?:is|transition|transition-mode|debo
 // these attributes should also set their corresponding properties
 // these attributes should also set their corresponding properties
 // because they only affect the initial state of the element
 // because they only affect the initial state of the element
 const attrWithPropsRE = /^(?:value|checked|selected|muted)$/
 const attrWithPropsRE = /^(?:value|checked|selected|muted)$/
+// these attributes expect enumrated values of "true" or "false"
+// but are not boolean attributes
+const enumeratedAttrRE = /^(?:draggable|contenteditable|spellcheck)$/
 
 
 // these attributes should set a hidden property for
 // these attributes should set a hidden property for
 // binding v-model to object values
 // binding v-model to object values
@@ -126,7 +129,9 @@ export default {
       return
       return
     }
     }
     // update attribute
     // update attribute
-    if (value != null && value !== false) {
+    if (enumeratedAttrRE.test(attr)) {
+      el.setAttribute(attr, value ? 'true' : 'false')
+    } else if (value != null && value !== false) {
       if (attr === 'class') {
       if (attr === 'class') {
         // handle edge case #1960:
         // handle edge case #1960:
         // class interpolation should not overwrite Vue transition class
         // class interpolation should not overwrite Vue transition class

+ 10 - 0
test/unit/specs/directives/public/bind_spec.js

@@ -80,4 +80,14 @@ describe('v-bind', function () {
     dir.update('0 0 1500 1000')
     dir.update('0 0 1500 1000')
     expect(dir.el.getAttribute('viewBox')).toBe('0 0 1500 1000')
     expect(dir.el.getAttribute('viewBox')).toBe('0 0 1500 1000')
   })
   })
+
+  it('enumrated non-boolean attributes', function () {
+    ['draggable', 'contenteditable', 'spellcheck'].forEach(function (attr) {
+      dir.arg = attr
+      dir.update(true)
+      expect(el.getAttribute(attr)).toBe('true')
+      dir.update(false)
+      expect(el.getAttribute(attr)).toBe('false')
+    })
+  })
 })
 })