From 1ac9d6fbeed214d59ea5c31a87ff2c19bb08dbc8 Mon Sep 17 00:00:00 2001 From: John Resig Date: Sat, 20 Oct 2007 22:04:06 +0000 Subject: [PATCH] Added a fix for .noConflict(true) not reverting properly. Also added unit tests for noConflict. --- src/core.js | 8 ++++---- test/index.html | 1 + test/unit/core.js | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index 5221b7b5e..7b97646ae 100644 --- a/src/core.js +++ b/src/core.js @@ -10,8 +10,8 @@ */ // Map over jQuery in case of overwrite -if ( typeof jQuery != "undefined" ) - var _jQuery = jQuery; +if ( window.jQuery ) + var _jQuery = window.jQuery; var jQuery = window.jQuery = function( selector, context ) { // If the context is a namespace object, return a new object @@ -21,8 +21,8 @@ var jQuery = window.jQuery = function( selector, context ) { }; // Map over the $ in case of overwrite -if ( typeof $ != "undefined" ) - var _$ = $; +if ( window.$ ) + var _$ = window.$; // Map the jQuery namespace to the '$' one window.$ = jQuery; diff --git a/test/index.html b/test/index.html index 7d60e098b..00ca904f9 100644 --- a/test/index.html +++ b/test/index.html @@ -4,6 +4,7 @@ jQuery Test Suite + diff --git a/test/unit/core.js b/test/unit/core.js index f5fc895a4..5101f8181 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -38,6 +38,27 @@ test("$()", function() { equals( div.length, 4, "Correct number of elements generated for div hr code b" ); }); +test("noConflict", function() { + expect(6); + + var old = jQuery; + var newjQuery = jQuery.noConflict(); + + ok( newjQuery == old, "noConflict returned the jQuery object" ); + ok( jQuery == old, "Make sure jQuery wasn't touched." ); + ok( $ == "$", "Make sure $ was reverted." ); + + jQuery = $ = old; + + newjQuery = jQuery.noConflict(true); + + ok( newjQuery == old, "noConflict returned the jQuery object" ); + ok( jQuery == "jQuery", "Make sure jQuery was reverted." ); + ok( $ == "$", "Make sure $ was reverted." ); + + jQuery = $ = old; +}); + test("isFunction", function() { expect(21); -- 2.39.5