Reworked the .clone() function in IE. Fixes jQuery bugs #3500 (jQuery expandos were...
[jquery.git] / test / unit / core.js
index e1c3733..6da62be 100644 (file)
@@ -53,7 +53,7 @@ test("jQuery()", function() {
 });
 
 test("selector state", function() {
-       expect(26);
+       expect(30);
 
        var test;
        
@@ -72,6 +72,10 @@ test("selector state", function() {
        test = jQuery("#main");
        equals( test.selector, "#main", "#main Selector" );
        equals( test.context, document, "#main Context" );
+
+       test = jQuery("#notfoundnono");
+       equals( test.selector, "#notfoundnono", "#notfoundnono Selector" );
+       equals( test.context, document, "#notfoundnono Context" );
        
        test = jQuery("#main", document);
        equals( test.selector, "#main", "#main Selector" );
@@ -80,6 +84,11 @@ test("selector state", function() {
        test = jQuery("#main", document.body);
        equals( test.selector, "#main", "#main Selector" );
        equals( test.context, document.body, "#main Context" );
+
+       // Test cloning
+       test = jQuery(test);
+       equals( test.selector, "#main", "#main Selector" );
+       equals( test.context, document.body, "#main Context" );
        
        test = jQuery(document.body).find("#main");
        equals( test.selector, "#main", "#main find Selector" );
@@ -141,7 +150,7 @@ test("browser", function() {
        };
        for (var i in browsers) {
                var v = i.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ); // RegEx from Core jQuery.browser.version check
-               version = v ? v[1] : null;
+               var version = v ? v[1] : null;
                equals( version, browsers[i], "Checking UA string" );
        }
 });
@@ -251,18 +260,16 @@ test("isFunction", function() {
        });
 });
 
-var foo = false;
-
 test("jQuery('html')", function() {
        expect(8);
 
        reset();
-       foo = false;
-       var s = jQuery("<script>foo='test';</script>")[0];
+       jQuery.foo = false;
+       var s = jQuery("<script>jQuery.foo='test';</script>")[0];
        ok( s, "Creating a script" );
-       ok( !foo, "Make sure the script wasn't executed prematurely" );
-       jQuery("body").append("<script>foo='test';</script>");
-       ok( foo, "Executing a scripts contents in the right context" );
+       ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
+       jQuery("body").append("<script>jQuery.foo='test';</script>");
+       ok( jQuery.foo, "Executing a scripts contents in the right context" );
 
        reset();
        ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
@@ -380,7 +387,7 @@ test("index(Object)", function() {
 });
 
 test("attr(String)", function() {
-       expect(26);
+       expect(27);
        equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
        equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );
        equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );
@@ -398,12 +405,14 @@ test("attr(String)", function() {
        equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
        equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
        equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
-       equals( jQuery('#foo').attr('nodeName'), 'DIV', 'Check for nodeName attribute' );
-       equals( jQuery('#foo').attr('tagName'), 'DIV', 'Check for tagName attribute' );
+       equals( jQuery('#foo').attr('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' );
+       equals( jQuery('#foo').attr('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' );
 
        jQuery('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
        equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
 
+       equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
+
 
        // Related to [5574] and [5683]
        var body = document.body, $body = jQuery(body);
@@ -455,7 +464,7 @@ test("attr(Hash)", function() {
 
 test("attr(String, Object)", function() {
        expect(19);
-       var div = jQuery("div").attr("foo", "bar");
+       var div = jQuery("div").attr("foo", "bar"),
                fail = false;
        for ( var i = 0; i < div.size(); i++ ) {
                if ( div.get(i).getAttribute('foo') != "bar" ){
@@ -547,22 +556,21 @@ if ( !isLocal ) {
 }
 
 test("attr('tabindex')", function() {
-       expect(5);
-
-       // tabindex 0
-       equals(jQuery('#listWithTabIndex').attr('tabindex'), 0, 'tabindex of 0');
-
-       // positive tabindex
-       equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'tabindex of 2');
-
-       // negative tabindex
-       equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'negative tabindex');
+       expect(8);
 
-       // regular element without a tabindex
-       equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'no tabindex, not tabbable by default');
+       // elements not natively tabbable
+       equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
+       equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
+       
+       // anchor with href
+       equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
+       equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
+       equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
 
-    // link without a tabindex
-       equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), undefined, 'no tabindex, tabbable by default');
+       // anchor without href
+       equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
+       equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
+       equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
 });
 
 test("attr('tabindex', value)", function() {
@@ -746,7 +754,7 @@ test("text()", function() {
 });
 
 test("wrap(String|Element)", function() {
-       expect(8);
+       expect(10);
        var defaultText = 'Try them out:'
        var result = jQuery('#first').wrap('<div class="red"><span></span></div>').text();
        equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
@@ -771,6 +779,11 @@ test("wrap(String|Element)", function() {
        j.wrap("<i></i>");
        equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
        equals( jQuery("#nonnodes > i").text(), j.text() + j[1].nodeValue, "Check node,textnode,comment wraps doesn't hurt text" );
+
+       // Try wrapping a disconnected node
+       j = jQuery("<label/>").wrap("<li/>");
+       equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
+       equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
 });
 
 test("wrapAll(String|Element)", function() {
@@ -896,7 +909,7 @@ test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
 });
 
 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
-       expect(6);
+       expect(7);
        var defaultText = 'Try them out:'
        jQuery('<b>buga</b>').appendTo('#first');
        equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
@@ -913,6 +926,9 @@ test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
        equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
 
        reset();
+       ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
+
+       reset();
        expected = document.querySelectorAll ?
                "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:" :
                "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
@@ -1140,7 +1156,7 @@ test("find(String)", function() {
 });
 
 test("clone()", function() {
-       expect(20);
+       expect(28);
        equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
        var clone = jQuery('#yahoo').clone();
        equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
@@ -1160,6 +1176,31 @@ test("clone()", function() {
        // using contents will get comments regular, text, and comment nodes
        var cl = jQuery("#nonnodes").contents().clone();
        ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
+
+       var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
+               ok( true, "Bound event still exists." );
+       });
+
+       div = div.clone(true).clone(true);
+       equals( div.length, 1, "One element cloned" );
+       equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
+       div.trigger("click");
+
+       div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
+       div.find("table").click(function(){
+               ok( true, "Bound event still exists." );
+       });
+
+       div = div.clone(true);
+       equals( div.length, 1, "One element cloned" );
+       equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
+       div.find("table:last").trigger("click");
+
+       div = jQuery("<div/>").html('<object height="355" width="425">  <param name="movie" value="http://www.youtube.com/v/JikaHBDoV3k&amp;hl=en">  <param name="wmode" value="transparent"> </object>');
+
+       div = div.clone(true);
+       equals( div.length, 1, "One element cloned" );
+       equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
 });
 
 if (!isLocal) {
@@ -1345,10 +1386,11 @@ test("val(String/Number)", function() {
        j.removeAttr("value");
 });
 
-var scriptorder = 0;
-
 test("html(String)", function() {
-       expect(13);
+       expect(17);
+       
+       jQuery.scriptorder = 0;
+       
        var div = jQuery("#main > div");
        div.html("<b>test</b>");
        var pass = true;
@@ -1364,7 +1406,7 @@ test("html(String)", function() {
 
        // this is needed, or the expando added by jQuery unique will yield a different html
        j.find('b').removeData();
-       equals( j.html().toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
+       equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
 
        jQuery("#main").html("<select/>");
        jQuery("#main select").html("<option>O1</option><option selected='selected'>O2</option><option>O3</option>");
@@ -1374,6 +1416,10 @@ test("html(String)", function() {
        equals( $div.html( 5 ).html(), '5', 'Setting a number as html' );
        equals( $div.html( 0 ).html(), '0', 'Setting a zero as html' );
 
+       reset();
+
+       jQuery("#main").html('<script type="something/else">ok( false, "Non-script evaluated." );</script><script type="text/javascript">ok( true, "text/javascript is evaluated." );</script><script>ok( true, "No type is evaluated." );</script><div><script type="text/javascript">ok( true, "Inner text/javascript is evaluated." );</script><script>ok( true, "Inner No type is evaluated." );</script><script type="something/else">ok( false, "Non-script evaluated." );</script></div>');
+
        stop();
 
        jQuery("#main").html('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>');
@@ -1381,7 +1427,7 @@ test("html(String)", function() {
        jQuery("#main").html('foo <form><script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
 
        // it was decided that waiting to execute ALL scripts makes sense since nested ones have to wait anyway so this test case is changed, see #1959
-       jQuery("#main").html("<script>equals(scriptorder++, 0, 'Script is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html (even though appears before)')<\/script><span id='scriptorder'><script>equals(scriptorder++, 1, 'Script (nested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equals(scriptorder++, 2, 'Script (unnested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script>");
+       jQuery("#main").html("<script>equals(jQuery.scriptorder++, 0, 'Script is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html (even though appears before)')<\/script><span id='scriptorder'><script>equals(jQuery.scriptorder++, 1, 'Script (nested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equals(jQuery.scriptorder++, 2, 'Script (unnested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script>");
 
        setTimeout( start, 100 );
 });
@@ -1400,11 +1446,14 @@ test("filter()", function() {
 });
 
 test("closest()", function() {
-       expect(4);
+       expect(6);
        isSet( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
        isSet( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
        isSet( jQuery("body").closest("div").get(), [], "closest(div)" );
        isSet( jQuery("#main").closest("span,#html").get(), q("html"), "closest(span,#html)" );
+
+       isSet( jQuery("div:eq(1)").closest("div:first").get(), [], "closest(div:first)" );
+       isSet( jQuery("div").closest("body:first div:last").get(), q("divWithNoTabIndex"), "closest(body:first div:last)" );
 });
 
 test("not()", function() {
@@ -1533,7 +1582,7 @@ test("addClass(String)", function() {
 });
 
 test("removeClass(String) - simple", function() {
-       expect(4);
+       expect(5);
        
        var $divs = jQuery('div');
        
@@ -1542,13 +1591,17 @@ test("removeClass(String) - simple", function() {
        ok( !$divs.is('.test'), "Remove Class" );
 
        reset();
-       
+
        $divs.addClass("test").addClass("foo").addClass("bar");
        $divs.removeClass("test").removeClass("bar").removeClass("foo");
        
        ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
 
        reset();
+
+       // Make sure that a null value doesn't cause problems
+       $divs.eq(0).addClass("test").removeClass(null);
+       ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
        
        $divs.eq(0).addClass("test").removeClass("");
        ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
@@ -1560,7 +1613,7 @@ test("removeClass(String) - simple", function() {
 });
 
 test("toggleClass(String)", function() {
-       expect(6);\r
+       expect(6);
        var e = jQuery("#firstp");
        ok( !e.is(".test"), "Assert class not present" );
        e.toggleClass("test");
@@ -1568,12 +1621,12 @@ test("toggleClass(String)", function() {
        e.toggleClass("test");
        ok( !e.is(".test"), "Assert class not present" );
 
-       e.toggleClass("test", false);\r
-       ok( !e.is(".test"), "Assert class not present" );\r
-       e.toggleClass("test", true);\r
-       ok( e.is(".test"), "Assert class present" );\r
-       e.toggleClass("test", false);\r
-       ok( !e.is(".test"), "Assert class not present" );\r
+       e.toggleClass("test", false);
+       ok( !e.is(".test"), "Assert class not present" );
+       e.toggleClass("test", true);
+       ok( e.is(".test"), "Assert class present" );
+       e.toggleClass("test", false);
+       ok( !e.is(".test"), "Assert class not present" );
 });
 
 test("removeAttr(String", function() {
@@ -1771,7 +1824,7 @@ test("contents()", function() {
 test("jQuery.makeArray", function(){
        expect(15);
 
-       equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName, "HEAD", "Pass makeArray a jQuery object" );
+       equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
 
        equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
 
@@ -1787,7 +1840,7 @@ test("jQuery.makeArray", function(){
 
        equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
 
-       equals( jQuery.makeArray( document.createElement("div") )[0].nodeName, "DIV", "Pass makeArray a single node" );
+       equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
 
        equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );