Ticket #3235 (closed bug: fixed)

Opened 6 years ago

Last modified 19 months ago

datepicker id generation not unique; with patch

Reported by: mattwallace Owned by: grabanski
Priority: minor Milestone:
Component: ui.datepicker Version: 1.6b
Keywords: Cc: matt@…
Blocking: Blocked by:

Description

Datepicker automatically assigns ids when one is not present. However, it uses new Date().getTime(), which only is granular to the millisecond level. However, calling the datepicker instantiation method on a class, ie, $("div.calpick").datepicker({ selectRange: false });, or such, can easily result in the datepicker trying to assign the same id because it handles more than 1 per millisecond.

This patch creates a .uuid variable in the singleton and assigns its incremented value as an addition to the Date().getTime() call; although I imagine you could probably get rid of the getTime call entirely, but I left it in. I tested this and it worked fine resolving my issue.

Although I am using a personalized bundle, I added a couple lines of context to this diff to make it clear where I changed lines:

*** jquery-ui.js        Tue Aug 19 17:06:08 2008
--- /tmp/jquery-ui-personalized-1.6b.js Fri Aug 15 15:51:54 2008
***************
*** 2366,2370 ****
                var inline = (nodeName == 'div' || nodeName == 'span');
                if (!target.id)
!                       target.id = 'dp' + new Date().getTime() + ++$.datepicker.uuid;
                var inst = this._newInst($(target), inline);
                inst.settings = $.extend({}, settings || {}, inlineSettings || {});
--- 2366,2370 ----
                var inline = (nodeName == 'div' || nodeName == 'span');
                if (!target.id)
!                       target.id = 'dp' + new Date().getTime();
                var inst = this._newInst($(target), inline);
                inst.settings = $.extend({}, settings || {}, inlineSettings || {});
***************
*** 2460,2464 ****
                var inst = this._dialogInst; // internal instance
                if (!inst) {
!                       var id = 'dp' + new Date().getTime() + ++$.datepicker.uuid;
                        this._dialogInput = $('<input type="text" id="' + id +
                                '" size="1" style="position: absolute; top: -100px;"/>');
--- 2460,2464 ----
                var inst = this._dialogInst; // internal instance
                if (!inst) {
!                       var id = 'dp' + new Date().getTime();
                        this._dialogInput = $('<input type="text" id="' + id +
                                '" size="1" style="position: absolute; top: -100px;"/>');
***************
*** 3868,3872 ****
  $.datepicker = new Datepicker(); // singleton instance
  $.datepicker.initialized = false;
- $.datepicker.uuid = 0; // for creating unique identifiers

  })(jQuery);
--- 3868,3871 ----

Change History

comment:1 Changed 6 years ago by mattwallace

My bad! I submitted the diff backwards.

Forwards diff:

*** /tmp/jquery-ui-personalized-1.6b.js Fri Aug 15 15:51:54 2008
--- jquery-ui.js        Tue Aug 19 17:06:08 2008
***************
*** 2366,2370 ****
                var inline = (nodeName == 'div' || nodeName == 'span');
                if (!target.id)
!                       target.id = 'dp' + new Date().getTime();
                var inst = this._newInst($(target), inline);
                inst.settings = $.extend({}, settings || {}, inlineSettings || {}); 
--- 2366,2370 ----
                var inline = (nodeName == 'div' || nodeName == 'span');
                if (!target.id)
!                       target.id = 'dp' + new Date().getTime() + ++$.datepicker.uuid;
                var inst = this._newInst($(target), inline);
                inst.settings = $.extend({}, settings || {}, inlineSettings || {}); 
***************
*** 2460,2464 ****
                var inst = this._dialogInst; // internal instance
                if (!inst) {
!                       var id = 'dp' + new Date().getTime();
                        this._dialogInput = $('<input type="text" id="' + id +
                                '" size="1" style="position: absolute; top: -100px;"/>');
--- 2460,2464 ----
                var inst = this._dialogInst; // internal instance
                if (!inst) {
!                       var id = 'dp' + new Date().getTime() + ++$.datepicker.uuid;
                        this._dialogInput = $('<input type="text" id="' + id +
                                '" size="1" style="position: absolute; top: -100px;"/>');
***************
*** 3868,3871 ****
--- 3868,3872 ----
  $.datepicker = new Datepicker(); // singleton instance
  $.datepicker.initialized = false;
+ $.datepicker.uuid = 0; // for creating unique identifiers
  
  })(jQuery);

comment:2 Changed 6 years ago by kbwood

  • Status changed from new to closed
  • Resolution set to fixed

Fixed. Start with current time in milliseconds and then increment for each ID.

comment:3 Changed 19 months ago by scott.gonzalez

  • Milestone TBD deleted

Milestone TBD deleted

Note: See TracTickets for help on using tickets.