From: kborchers Date: Wed, 30 May 2012 01:55:43 +0000 (-0500) Subject: Core: Add the uniqueId() and removeUniqueId() methods written by @scottgonzalez to... X-Git-Tag: 1.9.0-beta.1~90 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=40e47c0b08c31e5003833b9c46d36e7468a63de8;p=jquery-ui.git Core: Add the uniqueId() and removeUniqueId() methods written by @scottgonzalez to provide a generalized way of generating and removing generated element id's. Also, added a unit test. Fixed #8361 - Add uniqueId() and removeUniqueId() --- diff --git a/tests/unit/core/core.js b/tests/unit/core/core.js index 7a78ae34d..1026c5b0b 100644 --- a/tests/unit/core/core.js +++ b/tests/unit/core/core.js @@ -153,4 +153,14 @@ test( "outerHeight(true) - setter", function() { equal( el.height(), 32, "height set properly when hidden" ); }); +test( "uniqueId / removeUniqueId", function() { + var el = $( "img" ).eq( 0 ); + + equal( el.attr( "id" ), undefined, "element has no initial id" ); + el.uniqueId(); + ok( /ui-id-\d+$/.test( el.attr( "id" ) ), "element has generated id" ); + el.removeUniqueId(); + equal( el.attr( "id" ), undefined, "unique id has been removed from element" ); +}); + })( jQuery ); diff --git a/ui/jquery.ui.core.js b/ui/jquery.ui.core.js index d8fff91e1..a511de24b 100644 --- a/ui/jquery.ui.core.js +++ b/ui/jquery.ui.core.js @@ -9,6 +9,9 @@ */ (function( $, undefined ) { +var uuid = 0, + runiqueId = /^ui-id-\d+$/; + // prevent duplicate loading // this is only a problem because we proxy existing functions // and we don't want to double proxy them @@ -107,6 +110,22 @@ $.fn.extend({ return 0; }, + uniqueId: function() { + return this.each(function() { + if ( !this.id ) { + this.id = "ui-id-" + (++uuid); + } + }); + }, + + removeUniqueId: function() { + return this.each(function() { + if ( runiqueId.test( this.id ) ) { + $( this ).removeAttr( "id" ); + } + }); + }, + disableSelection: function() { return this.bind( ( $.support.selectstart ? "selectstart" : "mousedown" ) + ".ui-disableSelection", function( event ) {