Skip to content
Permalink
Browse files

Datepicker: changed setDate not to modify the passed date argument. F…

…ixes #6671 - setDate manipulates its argument
  • Loading branch information
pheiberg authored and scottgonzalez committed Nov 22, 2010
1 parent 3361e8f commit d69f2ecb1273f382d83b13c349a8c76b17bee2a6
Showing with 20 additions and 16 deletions.
  1. +4 −0 tests/unit/datepicker/datepicker_options.js
  2. +16 −16 ui/jquery.ui.datepicker.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() {
@@ -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. */

0 comments on commit d69f2ec

Please sign in to comment.
You can’t perform that action at this time.