Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datepicker: Support parsing, formatting, and selecting four-digit years before year 100 #1908

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions tests/unit/datepicker/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ QUnit.test( "widget method", function( assert ) {

QUnit.test( "baseStructure", function( assert ) {
var ready = assert.async();
assert.expect( 58 );
assert.expect( 60 );
var header, title, table, thead, week, panel, inl, child,
inp = testHelper.initNewInput(),
inp = testHelper.initNewInput( {
defaultDate: $.datepicker._newDate( 1, 2 - 1, 3 )
} ),
dp = $( "#ui-datepicker-div" );

function step1() {
Expand All @@ -61,7 +63,7 @@ QUnit.test( "baseStructure", function( assert ) {
assert.ok( title.is( "div.ui-datepicker-title" ) && title.html() !== "", "Structure - title division" );
assert.equal( title.children().length, 2, "Structure - title child count" );
assert.ok( title.children().first().is( "span.ui-datepicker-month" ) && title.children().first().text() !== "", "Structure - month text" );
assert.ok( title.children().last().is( "span.ui-datepicker-year" ) && title.children().last().text() !== "", "Structure - year text" );
assert.ok( title.children().last().is( "span.ui-datepicker-year" ) && title.children().last().text() === "0001", "Structure - year text" );

table = dp.children().eq( 1 );
assert.ok( table.is( "table.ui-datepicker-calendar" ), "Structure - month table" );
Expand Down Expand Up @@ -90,12 +92,15 @@ QUnit.test( "baseStructure", function( assert ) {
inp = testHelper.initNewInput( {
changeMonth: true,
changeYear: true,
showButtonPanel: true
showButtonPanel: true,
defaultDate: $.datepicker._newDate( 1, 2 - 1, 3 )
} );
testHelper.onFocus( inp, function() {
title = dp.find( "div.ui-datepicker-title" );
assert.ok( title.children().first().is( "select.ui-datepicker-month" ), "Structure - month selector" );
assert.ok( title.children().last().is( "select.ui-datepicker-year" ), "Structure - year selector" );
assert.equal( title.children().last().children().first().text(), "-9" );
assert.equal( title.children().last().children().last().text(), "0011" );

panel = dp.children().last();
assert.ok( panel.is( "div.ui-datepicker-buttonpane" ), "Structure - button panel division" );
Expand Down Expand Up @@ -457,7 +462,7 @@ QUnit.test( "keystrokes", function( assert ) {
} );

QUnit.test( "mouse", function( assert ) {
assert.expect( 15 );
assert.expect( 16 );
var inl,
inp = testHelper.init( "#inp" ),
dp = $( "#ui-datepicker-div" ),
Expand All @@ -470,6 +475,10 @@ QUnit.test( "mouse", function( assert ) {
$( ".ui-datepicker-calendar tbody a:contains(12)", dp ).simulate( "click", {} );
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), new Date( 2008, 2 - 1, 12 ),
"Mouse click - preset" );
inp.val( "02/04/0001" ).datepicker( "show" );
$( ".ui-datepicker-calendar tbody a:contains(12)", dp ).simulate( "click", {} );
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), $.datepicker._newDate( 1, 2 - 1, 12 ),
"Mouse click - year 0-99" );
inp.val( "02/04/2008" ).datepicker( "show" );
inp.val( "" ).datepicker( "show" );
$( "button.ui-datepicker-close", dp ).simulate( "click", {} );
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/datepicker/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ return $.extend( helper, {
assert.ok( false, message + " - missing date" );
return;
}
d1 = new Date( d1.getFullYear(), d1.getMonth(), d1.getDate() );
d2 = new Date( d2.getFullYear(), d2.getMonth(), d2.getDate() );
d1 = $.datepicker._newDate( d1.getFullYear(), d1.getMonth(), d1.getDate() );
d2 = $.datepicker._newDate( d2.getFullYear(), d2.getMonth(), d2.getDate() );
assert.equal( d1.toString(), d2.toString(), message );
},

Expand Down
34 changes: 29 additions & 5 deletions tests/unit/datepicker/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ QUnit.test( "otherMonths", function( assert ) {
} );

QUnit.test( "defaultDate", function( assert ) {
assert.expect( 16 );
assert.expect( 18 );
var inp = testHelper.init( "#inp" ),
date = new Date();
inp.val( "" ).datepicker( "show" ).
Expand Down Expand Up @@ -350,6 +350,11 @@ QUnit.test( "defaultDate", function( assert ) {
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
date = new Date( 2007, 7 - 1, 4 );
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date, "Default date 07/04/2007" );
inp.datepicker( "option", { defaultDate: "07/04/0001" } ).
datepicker( "hide" ).val( "" ).datepicker( "show" ).
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
date = $.datepicker._newDate( 1, 7 - 1, 4 );
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date, "Default date 07/04/0001" );
inp.datepicker( "option", { dateFormat: "yy-mm-dd", defaultDate: "2007-04-02" } ).
datepicker( "hide" ).val( "" ).datepicker( "show" ).
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
Expand All @@ -362,6 +367,11 @@ QUnit.test( "defaultDate", function( assert ) {
datepicker( "hide" ).val( "" ).datepicker( "show" ).
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date, "Default date 01/26/2007" );
date = $.datepicker._newDate( 1, 1 - 1, 26 );
inp.datepicker( "option", { defaultDate: date } ).
datepicker( "hide" ).val( "" ).datepicker( "show" ).
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date, "Default date 01/26/0001" );
} );

QUnit.test( "miscellaneous", function( assert ) {
Expand Down Expand Up @@ -537,14 +547,17 @@ QUnit.test( "minMax", function( assert ) {
} );

QUnit.test( "setDate", function( assert ) {
assert.expect( 24 );
assert.expect( 26 );
var inl, alt, minDate, maxDate, dateAndTimeToSet, dateAndTimeClone,
inp = testHelper.init( "#inp" ),
date1 = new Date( 2008, 6 - 1, 4 ),
date2 = new Date();
date2 = new Date(),
date3 = $.datepicker._newDate( 1, 4 - 1, 1 );
assert.ok( inp.datepicker( "getDate" ) == null, "Set date - default" );
inp.datepicker( "setDate", date1 );
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date1, "Set date - 2008-06-04" );
inp.datepicker( "setDate", date3 );
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date3, "Set date - 0001-04-01" );
date1 = new Date();
date1.setDate( date1.getDate() + 7 );
inp.datepicker( "setDate", +7 );
Expand All @@ -568,6 +581,10 @@ QUnit.test( "setDate", function( assert ) {
date1.setDate( date1.getDate() - 21 );
inp.datepicker( "setDate", "c -3 w" );
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date1, "Set date - c -3 w" );
date3 = new Date( date1 );
date3.setFullYear( 1 );
inp.datepicker( "setDate", "c " + ( 1 - date1.getFullYear() ) + " y" );
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date3, "Set date - 0001 relatively" );

// Inline
inl = testHelper.init( "#inl" );
Expand Down Expand Up @@ -926,7 +943,7 @@ QUnit.test( "iso8601Week", function( assert ) {
} );

QUnit.test( "parseDate", function( assert ) {
assert.expect( 26 );
assert.expect( 29 );
testHelper.init( "#inp" );
var currentYear, gmtDate, fr, settings, zh;
assert.ok( $.datepicker.parseDate( "d m y", "" ) == null, "Parse date empty" );
Expand All @@ -953,6 +970,8 @@ QUnit.test( "parseDate", function( assert ) {
testHelper.equalsDate( assert, $.datepicker.parseDate( "'day' d 'of' MM (''DD''), yy",
"day 3 of February ('Saturday'), 2001" ), new Date( 2001, 2 - 1, 3 ),
"Parse date 'day' d 'of' MM (''DD''), yy" );
testHelper.equalsDate( assert, $.datepicker.parseDate( "yy-mm-dd", "0001-02-03" ),
$.datepicker._newDate( 1, 2 - 1, 3 ), "Parse ancient date yy-mm-dd" );
currentYear = new Date().getFullYear();
testHelper.equalsDate( assert, $.datepicker.parseDate( "y-m-d", ( currentYear - 2000 ) + "-02-03" ),
new Date( currentYear, 2 - 1, 3 ), "Parse date y-m-d - default cutuff" );
Expand All @@ -972,6 +991,9 @@ QUnit.test( "parseDate", function( assert ) {
gmtDate.setMinutes( gmtDate.getMinutes() - gmtDate.getTimezoneOffset() );
testHelper.equalsDate( assert, $.datepicker.parseDate( "@", "981158400000" ), gmtDate, "Parse date @" );
testHelper.equalsDate( assert, $.datepicker.parseDate( "!", "631167552000000000" ), gmtDate, "Parse date !" );
gmtDate = $.datepicker._newDate( 1, 2 - 1, 3 );
testHelper.equalsDate( assert, $.datepicker.parseDate( "@", "-62132724000000" ), gmtDate, "Parse ancient date @" );
testHelper.equalsDate( assert, $.datepicker.parseDate( "!", "28728000000000" ), gmtDate, "Parse ancient date !" );

fr = $.datepicker.regional.fr;
settings = { dayNamesShort: fr.dayNamesShort, dayNames: fr.dayNames,
Expand Down Expand Up @@ -1066,7 +1088,7 @@ QUnit.test( "Ticket #7244: date parser does not fail when too many numbers are p
} );

QUnit.test( "formatDate", function( assert ) {
assert.expect( 16 );
assert.expect( 17 );
testHelper.init( "#inp" );
var gmtDate, fr, settings;
assert.equal( $.datepicker.formatDate( "d m y", new Date( 2001, 2 - 1, 3 ) ),
Expand All @@ -1090,6 +1112,8 @@ QUnit.test( "formatDate", function( assert ) {
assert.equal( $.datepicker.formatDate( "'day' d 'of' MM (''DD''), yy",
new Date( 2001, 2 - 1, 3 ) ), "day 3 of February ('Saturday'), 2001",
"Format date 'day' d 'of' MM ('DD'), yy" );
assert.equal( $.datepicker.formatDate( "yy-mm-dd", $.datepicker._newDate( 999, 2 - 1, 3 ) ),
"0999-02-03", "Format ancient date yy-mm-dd" );
gmtDate = new Date( 2001, 2 - 1, 3 );
gmtDate.setMinutes( gmtDate.getMinutes() - gmtDate.getTimezoneOffset() );
assert.equal( $.datepicker.formatDate( "@", gmtDate ), "981158400000", "Format date @" );
Expand Down
Loading