Browse Source

Fix multiple attr in select with undefined value (#4859)

Fix #4855
Eduardo San Martin Morote 9 years ago
parent
commit
ec7fca8495

+ 2 - 1
src/platforms/web/runtime/node-ops.js

@@ -7,7 +7,8 @@ export function createElement (tagName: string, vnode: VNode): Element {
   if (tagName !== 'select') {
     return elm
   }
-  if (vnode.data && vnode.data.attrs && 'multiple' in vnode.data.attrs) {
+  // false or null will remove the attribute but undefined will not
+  if (vnode.data && vnode.data.attrs && vnode.data.attrs.multiple !== undefined) {
     elm.setAttribute('multiple', 'multiple')
   }
   return elm

+ 16 - 0
test/unit/features/directives/model-select.spec.js

@@ -287,6 +287,22 @@ describe('Directive v-model select', () => {
     }).then(done)
   })
 
+  it('should not have multiple attr with falsy values except \'\'', () => {
+    const vm = new Vue({
+      template:
+        '<div>' +
+          '<select id="undefined" :multiple="undefined"></select>' +
+          '<select id="null" :multiple="null"></select>' +
+          '<select id="false" :multiple="false"></select>' +
+          '<select id="string" :multiple="\'\'"></select>' +
+        '</div>'
+    }).$mount()
+    expect(vm.$el.querySelector('#undefined').multiple).toEqual(false)
+    expect(vm.$el.querySelector('#null').multiple).toEqual(false)
+    expect(vm.$el.querySelector('#false').multiple).toEqual(false)
+    expect(vm.$el.querySelector('#string').multiple).toEqual(true)
+  })
+
   it('multiple with static template', () => {
     const vm = new Vue({
       template: