From f5500eb040c637923d51f24273ada14117638b77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 12 Jul 2016 22:01:30 +0200 Subject: [PATCH] Refactor `fn.val()` implementation --- src/zepto.js | 13 +++++++------ test/zepto.html | 5 ++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/zepto.js b/src/zepto.js index 84694de58..670fe6cac 100644 --- a/src/zepto.js +++ b/src/zepto.js @@ -655,15 +655,16 @@ var Zepto = (function() { return data !== null ? deserializeValue(data) : undefined }, val: function(value){ - if (value == null) value = "" - return 0 in arguments ? - this.each(function(idx){ + if (0 in arguments) { + if (value == null) value = "" + return this.each(function(idx){ this.value = funcArg(this, value, idx, this.value) - }) : - (this[0] && (this[0].multiple ? + }) + } else { + return this[0] && (this[0].multiple ? $(this[0]).find('option').filter(function(){ return this.selected }).pluck('value') : this[0].value) - ) + } }, offset: function(coordinates){ if (coordinates) return this.each(function(index){ diff --git a/test/zepto.html b/test/zepto.html index ddad18570..1c6664350 100644 --- a/test/zepto.html +++ b/test/zepto.html @@ -1978,7 +1978,10 @@

Zepto Core unit tests

t.assertEqual('Hello World', input.val()) - input.val(undefined); + input.val(undefined) + t.assertEqual('', input.val()) + + input.val(null) t.assertEqual('', input.val()) input.val('')