From: Kato Kazuyoshi Date: Wed, 4 May 2011 20:06:27 +0000 (+0900) Subject: Datepicker: Greedy matching in month name. Fixed #7062 - $.datepicker.parseDate does... X-Git-Tag: 1.8.13~14 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f9faa0ab5fb2b9f332de93fd9d8fb97e9957ff6d;p=jquery-ui.git Datepicker: Greedy matching in month name. Fixed #7062 - $.datepicker.parseDate does not work for some locale date strings. (cherry picked from commit a891e81e06c7ce491ae9058aea2fa7fbd51eddb6) --- diff --git a/tests/unit/datepicker/datepicker.html b/tests/unit/datepicker/datepicker.html index d3c6f0676..db1f0a55f 100644 --- a/tests/unit/datepicker/datepicker.html +++ b/tests/unit/datepicker/datepicker.html @@ -12,6 +12,7 @@ + diff --git a/tests/unit/datepicker/datepicker_options.js b/tests/unit/datepicker/datepicker_options.js index 33b07d694..b14c87b8b 100644 --- a/tests/unit/datepicker/datepicker_options.js +++ b/tests/unit/datepicker/datepicker_options.js @@ -827,6 +827,10 @@ test('parseDate', function() { equalsDate($.datepicker.parseDate('\'jour\' d \'de\' MM (\'\'DD\'\'), yy', 'jour 9 de Avril (\'Lundi\'), 2001', settings), new Date(2001, 4 - 1, 9), 'Parse date \'jour\' d \'de\' MM (\'\'DD\'\'), yy with settings'); + + var zh = $.datepicker.regional['zh-CN']; + equalsDate($.datepicker.parseDate('yy M d', '2011 十一 22', zh), + new Date(2011, 11 - 1, 22), 'Parse date yy M d with zh-CN'); }); test('parseDateErrors', function() { diff --git a/ui/jquery.ui.datepicker.js b/ui/jquery.ui.datepicker.js index d6419e2e3..fe24d695a 100644 --- a/ui/jquery.ui.datepicker.js +++ b/ui/jquery.ui.datepicker.js @@ -992,14 +992,24 @@ $.extend(Datepicker.prototype, { }; // Extract a name from the string value and convert to an index var getName = function(match, shortNames, longNames) { - var names = (lookAhead(match) ? longNames : shortNames); - for (var i = 0; i < names.length; i++) { - if (value.substr(iValue, names[i].length).toLowerCase() == names[i].toLowerCase()) { - iValue += names[i].length; - return i + 1; + var names = $.map(lookAhead(match) ? longNames : shortNames, function (v, k) { + return [ [k, v] ]; + }).sort(function (a, b) { + return -(a[1].length - b[1].length); + }); + var index = -1; + $.each(names, function (i, pair) { + var name = pair[1]; + if (value.substr(iValue, name.length).toLowerCase() == name.toLowerCase()) { + index = pair[0]; + iValue += name.length; + return false; } - } - throw 'Unknown name at position ' + iValue; + }); + if (index != -1) + return index + 1; + else + throw 'Unknown name at position ' + iValue; }; // Confirm that a literal character matches the string value var checkLiteral = function() {