Added a fix for .noConflict(true) not reverting properly. Also added unit tests for noConflict.

This commit is contained in:
John Resig 2007-10-20 22:04:06 +00:00
parent 5736e8d90d
commit 1ac9d6fbee
3 changed files with 26 additions and 4 deletions

View file

@ -10,8 +10,8 @@
*/ */
// Map over jQuery in case of overwrite // Map over jQuery in case of overwrite
if ( typeof jQuery != "undefined" ) if ( window.jQuery )
var _jQuery = jQuery; var _jQuery = window.jQuery;
var jQuery = window.jQuery = function( selector, context ) { var jQuery = window.jQuery = function( selector, context ) {
// If the context is a namespace object, return a new object // 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 // Map over the $ in case of overwrite
if ( typeof $ != "undefined" ) if ( window.$ )
var _$ = $; var _$ = window.$;
// Map the jQuery namespace to the '$' one // Map the jQuery namespace to the '$' one
window.$ = jQuery; window.$ = jQuery;

View file

@ -4,6 +4,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Test Suite</title> <title>jQuery Test Suite</title>
<link rel="Stylesheet" media="screen" href="data/testsuite.css" /> <link rel="Stylesheet" media="screen" href="data/testsuite.css" />
<script>var jQuery = "jQuery", $ = "$"; // For testing .noConflcit()</script>
<script type="text/javascript" src="../dist/jquery.js"></script> <script type="text/javascript" src="../dist/jquery.js"></script>
<script type="text/javascript" src="data/testrunner.js"></script> <script type="text/javascript" src="data/testrunner.js"></script>
<script type="text/javascript" src="unit/core.js"></script> <script type="text/javascript" src="unit/core.js"></script>

View file

@ -38,6 +38,27 @@ test("$()", function() {
equals( div.length, 4, "Correct number of elements generated for div hr code b" ); 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() { test("isFunction", function() {
expect(21); expect(21);