Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to avoid expando collisions #29

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/core.js
Expand Up @@ -372,12 +372,20 @@ jQuery.extend = jQuery.fn.extend = function() {
};

jQuery.extend({
noConflict: function( deep ) {
noConflict: function( deep, expandoPrefix ) {
window.$ = _$;

if ( deep ) {
window.jQuery = _jQuery;
}

// If the expando needs a prefix for explicit uniqueness
// and the cache has not been used yet (otherwise it's worth the risk).
// See #6842
if ( expandoPrefix && jQuery.isEmptyObject( jQuery.cache ) ) {
// Set the expando to the old one plus the prefix
jQuery.expando = expandoPrefix + jQuery.expando;
}

return jQuery;
},
Expand Down
5 changes: 3 additions & 2 deletions src/data.js
Expand Up @@ -9,8 +9,9 @@ jQuery.extend({
// Please use with caution
uuid: 0,

// Unique for each copy of jQuery on the page
expando: "jQuery" + jQuery.now(),
// Usually unique for each copy of jQuery on the page and for each version
// Use a jQuery.noConflict(true, 'unqiuePrefix') to manually avoid time collisions
expando: "jQuery" + jQuery.fn.jquery + jQuery.now(),

// The following elements throw uncatchable exceptions if you
// attempt to add expando properties to them.
Expand Down
30 changes: 27 additions & 3 deletions test/unit/core.js
Expand Up @@ -183,7 +183,7 @@ test("browser", function() {
}

test("noConflict", function() {
expect(7);
expect(9);

var $$ = jQuery;

Expand All @@ -197,8 +197,32 @@ test("noConflict", function() {
equals( jQuery, originaljQuery, "Make sure jQuery was reverted." );
equals( $, original$, "Make sure $ was reverted." );
ok( $$("#main").html("test"), "Make sure that jQuery still works." );

jQuery = $$;

jQuery = $ = $$;

var oldExpando = jQuery.expando;
var oldCache = jQuery.cache;
var testPrefix = 'testPrefix';

// simulate no data in the cache
jQuery.cache = {};

equals( jQuery.noConflict(true, testPrefix).expando, testPrefix + oldExpando, "noConflict prefix was added" );

jQuery = $ = $$;

// Set the expando back to the original
jQuery.expando = oldExpando;

// Populate jQuery.cache
jQuery.cache = {'key': 'val'};

equals( jQuery.noConflict(true, testPrefix).expando, oldExpando, "noConflict does not allow a prefix if jQuery.cache has been used" )

jQuery = $ = $$;

// Replace the data that was there prior to the tests for good measure
jQuery.cache = oldCache;
});

test("trim", function() {
Expand Down