http://www.mediawiki.org/wiki/Special:Code/MediaWiki/95045

Revision: 95045
Author:   salvatoreingala
Date:     2011-08-19 22:12:48 +0000 (Fri, 19 Aug 2011)
Log Message:
-----------
Avoiding date constructor with string argument, which is not reliable 
cross-browser.

Modified Paths:
--------------
    branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js

Modified: branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js
===================================================================
--- branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js 
2011-08-19 21:44:14 UTC (rev 95044)
+++ branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js 
2011-08-19 22:12:48 UTC (rev 95045)
@@ -914,7 +914,7 @@
                var     value = options.values && 
options.values[this.desc.name],
                        date;
                if ( typeof value != 'undefined' && value !== null ) {
-                       date = new Date( value );
+                       date = this._parseDate( value );
                        
                        if ( !isFinite( date ) ) {
                                $.error( "value is invalid" );
@@ -933,6 +933,39 @@
        DateField.prototype = Object.create( SimpleField.prototype );
        DateField.prototype.constructor = DateField;
        
+       //Parses a date in the [YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]Z format, returns 
a date object
+       //Used to avoid the "new Date( dateString )" constructor, which is 
implementation-specific.
+       DateField.prototype._parseDate = function( str ) {
+               var     date,
+                       parts = 
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/.exec( str );
+               
+               if ( parts === null ) {
+                       return new Date( NaN );
+               }
+               
+               var     year = parseInt( parts[1], 10 ),
+                       month = parseInt( parts[2], 10 ) - 1,
+                       day = parseInt( parts[3], 10 ),
+                       h = parseInt( parts[4], 10 ),
+                       m = parseInt( parts[5], 10 ),
+                       s = parseInt( parts[6], 10 );
+               
+               date = new Date();
+               date.setUTCFullYear( year, month, day );
+               date.setUTCHours( h );
+               date.setUTCMinutes( m );
+               date.setUTCSeconds( s );
+               
+               //Check if the date was actually correct, since the date 
handling functions may wrap around invalid dates
+               if ( date.getUTCFullYear() !== year || date.getUTCMonth() !== 
month || date.getUTCDate() !== day ||
+                       date.getUTCHours() !== h || date.getUTCMinutes() !== m 
|| date.getUTCSeconds() !== s )
+               {
+                       return new Date( NaN );
+               }
+               
+               return date;
+       };
+       
        DateField.prototype.getValues = function() {
                var     d = this.$text.datepicker( 'getDate' ),
                        res = {};


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to