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()

This commit is contained in:
kborchers 2012-05-29 20:55:43 -05:00
parent 649a670d1c
commit 40e47c0b08
2 changed files with 29 additions and 0 deletions

View File

@ -153,4 +153,14 @@ test( "outerHeight(true) - setter", function() {
equal( el.height(), 32, "height set properly when hidden" ); 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 ); })( jQuery );

19
ui/jquery.ui.core.js vendored
View File

@ -9,6 +9,9 @@
*/ */
(function( $, undefined ) { (function( $, undefined ) {
var uuid = 0,
runiqueId = /^ui-id-\d+$/;
// prevent duplicate loading // prevent duplicate loading
// this is only a problem because we proxy existing functions // this is only a problem because we proxy existing functions
// and we don't want to double proxy them // and we don't want to double proxy them
@ -107,6 +110,22 @@ $.fn.extend({
return 0; 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() { disableSelection: function() {
return this.bind( ( $.support.selectstart ? "selectstart" : "mousedown" ) + return this.bind( ( $.support.selectstart ? "selectstart" : "mousedown" ) +
".ui-disableSelection", function( event ) { ".ui-disableSelection", function( event ) {