r95045 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95044‎ | r95045 | r95046 >
Date:22:12, 19 August 2011
Author:salvatoreingala
Status:deferred
Tags:
Comment:
Avoiding date constructor with string argument, which is not reliable cross-browser.
Modified paths:
  • /branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js
@@ -914,7 +914,7 @@
915915 var value = options.values && options.values[this.desc.name],
916916 date;
917917 if ( typeof value != 'undefined' && value !== null ) {
918 - date = new Date( value );
 918+ date = this._parseDate( value );
919919
920920 if ( !isFinite( date ) ) {
921921 $.error( "value is invalid" );
@@ -933,6 +933,39 @@
934934 DateField.prototype = Object.create( SimpleField.prototype );
935935 DateField.prototype.constructor = DateField;
936936
 937+ //Parses a date in the [YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]Z format, returns a date object
 938+ //Used to avoid the "new Date( dateString )" constructor, which is implementation-specific.
 939+ DateField.prototype._parseDate = function( str ) {
 940+ var date,
 941+ parts = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/.exec( str );
 942+
 943+ if ( parts === null ) {
 944+ return new Date( NaN );
 945+ }
 946+
 947+ var year = parseInt( parts[1], 10 ),
 948+ month = parseInt( parts[2], 10 ) - 1,
 949+ day = parseInt( parts[3], 10 ),
 950+ h = parseInt( parts[4], 10 ),
 951+ m = parseInt( parts[5], 10 ),
 952+ s = parseInt( parts[6], 10 );
 953+
 954+ date = new Date();
 955+ date.setUTCFullYear( year, month, day );
 956+ date.setUTCHours( h );
 957+ date.setUTCMinutes( m );
 958+ date.setUTCSeconds( s );
 959+
 960+ //Check if the date was actually correct, since the date handling functions may wrap around invalid dates
 961+ if ( date.getUTCFullYear() !== year || date.getUTCMonth() !== month || date.getUTCDate() !== day ||
 962+ date.getUTCHours() !== h || date.getUTCMinutes() !== m || date.getUTCSeconds() !== s )
 963+ {
 964+ return new Date( NaN );
 965+ }
 966+
 967+ return date;
 968+ };
 969+
937970 DateField.prototype.getValues = function() {
938971 var d = this.$text.datepicker( 'getDate' ),
939972 res = {};

Status & tagging log