From d69f2ecb1273f382d83b13c349a8c76b17bee2a6 Mon Sep 17 00:00:00 2001 From: pheiberg Date: Sun, 21 Nov 2010 22:06:23 +0100 Subject: [PATCH] Datepicker: changed setDate not to modify the passed date argument. Fixes #6671 - setDate manipulates its argument --- tests/unit/datepicker/datepicker_options.js | 4 +++ ui/jquery.ui.datepicker.js | 32 ++++++++++----------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/tests/unit/datepicker/datepicker_options.js b/tests/unit/datepicker/datepicker_options.js index 5b5f252cc..af561a783 100644 --- a/tests/unit/datepicker/datepicker_options.js +++ b/tests/unit/datepicker/datepicker_options.js @@ -470,6 +470,10 @@ test('setDate', function() { equalsDate(inp.datepicker('getDate'), minDate, 'Set date min/max - setDate < min'); inp.datepicker('setDate', date2); equalsDate(inp.datepicker('getDate'), maxDate, 'Set date min/max - setDate > max'); + var dateAndTimeToSet = new Date(2008, 3 - 1, 28, 1, 11, 0); + var dateAndTimeClone = new Date(2008, 3 - 1, 28, 1, 11, 0); + inp.datepicker('setDate', dateAndTimeToSet); + equals(dateAndTimeToSet.getTime(), dateAndTimeClone.getTime(), 'Date object passed should not be changed by setDate'); }); test('altField', function() { diff --git a/ui/jquery.ui.datepicker.js b/ui/jquery.ui.datepicker.js index 961e1780d..739db1007 100644 --- a/ui/jquery.ui.datepicker.js +++ b/ui/jquery.ui.datepicker.js @@ -1315,16 +1315,16 @@ $.extend(Datepicker.prototype, { } return new Date(year, month, day); }; - date = (date == null ? defaultDate : (typeof date == 'string' ? offsetString(date) : - (typeof date == 'number' ? (isNaN(date) ? defaultDate : offsetNumeric(date)) : date))); - date = (date && date.toString() == 'Invalid Date' ? defaultDate : date); - if (date) { - date.setHours(0); - date.setMinutes(0); - date.setSeconds(0); - date.setMilliseconds(0); + var newDate = (date == null ? defaultDate : (typeof date == 'string' ? offsetString(date) : + (typeof date == 'number' ? (isNaN(date) ? defaultDate : offsetNumeric(date)) : new Date(date.getTime())))); + newDate = (newDate && newDate.toString() == 'Invalid Date' ? defaultDate : newDate); + if (newDate) { + newDate.setHours(0); + newDate.setMinutes(0); + newDate.setSeconds(0); + newDate.setMilliseconds(0); } - return this._daylightSavingAdjust(date); + return this._daylightSavingAdjust(newDate); }, /* Handle switch to/from daylight saving. @@ -1344,10 +1344,10 @@ $.extend(Datepicker.prototype, { var clear = !(date); var origMonth = inst.selectedMonth; var origYear = inst.selectedYear; - date = this._restrictMinMax(inst, this._determineDate(inst, date, new Date())); - inst.selectedDay = inst.currentDay = date.getDate(); - inst.drawMonth = inst.selectedMonth = inst.currentMonth = date.getMonth(); - inst.drawYear = inst.selectedYear = inst.currentYear = date.getFullYear(); + var newDate = this._restrictMinMax(inst, this._determineDate(inst, date, new Date())); + inst.selectedDay = inst.currentDay = newDate.getDate(); + inst.drawMonth = inst.selectedMonth = inst.currentMonth = newDate.getMonth(); + inst.drawYear = inst.selectedYear = inst.currentYear = newDate.getFullYear(); if ((origMonth != inst.selectedMonth || origYear != inst.selectedYear) && !noChange) this._notifyChange(inst); this._adjustInstDate(inst); @@ -1625,9 +1625,9 @@ $.extend(Datepicker.prototype, { _restrictMinMax: function(inst, date) { var minDate = this._getMinMaxDate(inst, 'min'); var maxDate = this._getMinMaxDate(inst, 'max'); - date = (minDate && date < minDate ? minDate : date); - date = (maxDate && date > maxDate ? maxDate : date); - return date; + var newDate = (minDate && date < minDate ? minDate : date); + newDate = (maxDate && newDate > maxDate ? maxDate : newDate); + return newDate; }, /* Notify change of month/year. */ -- 2.39.5