core.js (72784B)
1 module("core"); 2 3 test("Basic requirements", function() { 4 expect(7); 5 ok( Array.prototype.push, "Array.push()" ); 6 ok( Function.prototype.apply, "Function.apply()" ); 7 ok( document.getElementById, "getElementById" ); 8 ok( document.getElementsByTagName, "getElementsByTagName" ); 9 ok( RegExp, "RegExp" ); 10 ok( jQuery, "jQuery" ); 11 ok( $, "$()" ); 12 }); 13 14 test("$()", function() { 15 expect(8); 16 17 var main = $("#main"); 18 isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" ); 19 20 /* 21 // disabled since this test was doing nothing. i tried to fix it but i'm not sure 22 // what the expected behavior should even be. FF returns "\n" for the text node 23 // make sure this is handled 24 var crlfContainer = $('<p>\r\n</p>'); 25 var x = crlfContainer.contents().get(0).nodeValue; 26 equals( x, what???, "Check for \\r and \\n in jQuery()" ); 27 */ 28 29 /* // Disabled until we add this functionality in 30 var pass = true; 31 try { 32 $("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body); 33 } catch(e){ 34 pass = false; 35 } 36 ok( pass, "$('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/ 37 38 var code = $("<code/>"); 39 equals( code.length, 1, "Correct number of elements generated for code" ); 40 var img = $("<img/>"); 41 equals( img.length, 1, "Correct number of elements generated for img" ); 42 var div = $("<div/><hr/><code/><b/>"); 43 equals( div.length, 4, "Correct number of elements generated for div hr code b" ); 44 45 // can actually yield more than one, when iframes are included, the window is an array as well 46 equals( $(window).length, 1, "Correct number of elements generated for window" ); 47 48 equals( $(document).length, 1, "Correct number of elements generated for document" ); 49 50 equals( $([1,2,3]).get(1), 2, "Test passing an array to the factory" ); 51 52 equals( $(document.body).get(0), $('body').get(0), "Test passing an html node to the factory" ); 53 }); 54 55 test("browser", function() { 56 expect(13); 57 var browsers = { 58 //Internet Explorer 59 "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0", 60 "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)": "7.0", 61 /** Failing #1876 62 * "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30)": "7.0", 63 */ 64 //Browsers with Gecko engine 65 //Mozilla 66 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915" : "1.7.12", 67 //Firefox 68 "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3": "1.8.1.3", 69 //Netscape 70 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20070321 Netscape/8.1.3" : "1.7.5", 71 //Flock 72 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.11) Gecko/20070321 Firefox/1.5.0.11 Flock/0.7.12" : "1.8.0.11", 73 //Opera browser 74 "Opera/9.20 (X11; Linux x86_64; U; en)": "9.20", 75 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.20" : "9.20", 76 "Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20", 77 //WebKit engine 78 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "418.9", 79 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" : "418.8", 80 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.8", 81 //Other user agent string 82 "Other browser's user agent 1.0":null 83 }; 84 for (var i in browsers) { 85 var v = i.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ); // RegEx from Core jQuery.browser.version check 86 version = v ? v[1] : null; 87 equals( version, browsers[i], "Checking UA string" ); 88 } 89 }); 90 91 test("noConflict", function() { 92 expect(6); 93 94 var old = jQuery; 95 var newjQuery = jQuery.noConflict(); 96 97 equals( newjQuery, old, "noConflict returned the jQuery object" ); 98 equals( jQuery, old, "Make sure jQuery wasn't touched." ); 99 equals( $, "$", "Make sure $ was reverted." ); 100 101 jQuery = $ = old; 102 103 newjQuery = jQuery.noConflict(true); 104 105 equals( newjQuery, old, "noConflict returned the jQuery object" ); 106 equals( jQuery, "jQuery", "Make sure jQuery was reverted." ); 107 equals( $, "$", "Make sure $ was reverted." ); 108 109 jQuery = $ = old; 110 }); 111 112 test("isFunction", function() { 113 expect(21); 114 115 // Make sure that false values return false 116 ok( !jQuery.isFunction(), "No Value" ); 117 ok( !jQuery.isFunction( null ), "null Value" ); 118 ok( !jQuery.isFunction( undefined ), "undefined Value" ); 119 ok( !jQuery.isFunction( "" ), "Empty String Value" ); 120 ok( !jQuery.isFunction( 0 ), "0 Value" ); 121 122 // Check built-ins 123 // Safari uses "(Internal Function)" 124 ok( jQuery.isFunction(String), "String Function("+String+")" ); 125 ok( jQuery.isFunction(Array), "Array Function("+Array+")" ); 126 ok( jQuery.isFunction(Object), "Object Function("+Object+")" ); 127 ok( jQuery.isFunction(Function), "Function Function("+Function+")" ); 128 129 // When stringified, this could be misinterpreted 130 var mystr = "function"; 131 ok( !jQuery.isFunction(mystr), "Function String" ); 132 133 // When stringified, this could be misinterpreted 134 var myarr = [ "function" ]; 135 ok( !jQuery.isFunction(myarr), "Function Array" ); 136 137 // When stringified, this could be misinterpreted 138 var myfunction = { "function": "test" }; 139 ok( !jQuery.isFunction(myfunction), "Function Object" ); 140 141 // Make sure normal functions still work 142 var fn = function(){}; 143 ok( jQuery.isFunction(fn), "Normal Function" ); 144 145 var obj = document.createElement("object"); 146 147 // Firefox says this is a function 148 ok( !jQuery.isFunction(obj), "Object Element" ); 149 150 // IE says this is an object 151 ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" ); 152 153 var nodes = document.body.childNodes; 154 155 // Safari says this is a function 156 ok( !jQuery.isFunction(nodes), "childNodes Property" ); 157 158 var first = document.body.firstChild; 159 160 // Normal elements are reported ok everywhere 161 ok( !jQuery.isFunction(first), "A normal DOM Element" ); 162 163 var input = document.createElement("input"); 164 input.type = "text"; 165 document.body.appendChild( input ); 166 167 // IE says this is an object 168 ok( jQuery.isFunction(input.focus), "A default function property" ); 169 170 document.body.removeChild( input ); 171 172 var a = document.createElement("a"); 173 a.href = "some-function"; 174 document.body.appendChild( a ); 175 176 // This serializes with the word 'function' in it 177 ok( !jQuery.isFunction(a), "Anchor Element" ); 178 179 document.body.removeChild( a ); 180 181 // Recursive function calls have lengths and array-like properties 182 function callme(callback){ 183 function fn(response){ 184 callback(response); 185 } 186 187 ok( jQuery.isFunction(fn), "Recursive Function Call" ); 188 189 fn({ some: "data" }); 190 }; 191 192 callme(function(){ 193 callme(function(){}); 194 }); 195 }); 196 197 var foo = false; 198 199 test("$('html')", function() { 200 expect(6); 201 202 reset(); 203 foo = false; 204 var s = $("<script>var foo='test';</script>")[0]; 205 ok( s, "Creating a script" ); 206 ok( !foo, "Make sure the script wasn't executed prematurely" ); 207 $("body").append(s); 208 ok( foo, "Executing a scripts contents in the right context" ); 209 210 reset(); 211 ok( $("<link rel='stylesheet'/>")[0], "Creating a link" ); 212 213 reset(); 214 215 var j = $("<span>hi</span> there <!-- mon ami -->"); 216 ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" ); 217 218 ok( !$("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" ); 219 }); 220 221 test("$('html', context)", function() { 222 expect(1); 223 224 var $div = $("<div/>"); 225 var $span = $("<span/>", $div); 226 equals($span.length, 1, "Verify a span created with a div context works, #1763"); 227 }); 228 229 if ( !isLocal ) { 230 test("$(selector, xml).text(str) - Loaded via XML document", function() { 231 expect(2); 232 stop(); 233 $.get('data/dashboard.xml', function(xml) { 234 // tests for #1419 where IE was a problem 235 equals( $("tab:first", xml).text(), "blabla", "Verify initial text correct" ); 236 $("tab:first", xml).text("newtext"); 237 equals( $("tab:first", xml).text(), "newtext", "Verify new text correct" ); 238 start(); 239 }); 240 }); 241 } 242 243 test("length", function() { 244 expect(1); 245 equals( $("p").length, 6, "Get Number of Elements Found" ); 246 }); 247 248 test("size()", function() { 249 expect(1); 250 equals( $("p").size(), 6, "Get Number of Elements Found" ); 251 }); 252 253 test("get()", function() { 254 expect(1); 255 isSet( $("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" ); 256 }); 257 258 test("get(Number)", function() { 259 expect(1); 260 equals( $("p").get(0), document.getElementById("firstp"), "Get A Single Element" ); 261 }); 262 263 test("add(String|Element|Array|undefined)", function() { 264 expect(12); 265 isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" ); 266 isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" ); 267 ok( $([]).add($("#form")[0].elements).length >= 13, "Check elements from array" ); 268 269 // For the time being, we're discontinuing support for $(form.elements) since it's ambiguous in IE 270 // use $([]).add(form.elements) instead. 271 //equals( $([]).add($("#form")[0].elements).length, $($("#form")[0].elements).length, "Array in constructor must equals array in add()" ); 272 273 var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>")); 274 equals( x[0].id, "x1", "Check on-the-fly element1" ); 275 equals( x[1].id, "x2", "Check on-the-fly element2" ); 276 277 var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>"); 278 equals( x[0].id, "x1", "Check on-the-fly element1" ); 279 equals( x[1].id, "x2", "Check on-the-fly element2" ); 280 281 var notDefined; 282 equals( $([]).add(notDefined).length, 0, "Check that undefined adds nothing" ); 283 284 // Added after #2811 285 equals( $([]).add([window,document,document.body,document]).length, 3, "Pass an array" ); 286 equals( $(document).add(document).length, 1, "Check duplicated elements" ); 287 equals( $(window).add(window).length, 1, "Check duplicated elements using the window" ); 288 ok( $([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" ); 289 }); 290 291 test("each(Function)", function() { 292 expect(1); 293 var div = $("div"); 294 div.each(function(){this.foo = 'zoo';}); 295 var pass = true; 296 for ( var i = 0; i < div.size(); i++ ) { 297 if ( div.get(i).foo != "zoo" ) pass = false; 298 } 299 ok( pass, "Execute a function, Relative" ); 300 }); 301 302 test("index(Object)", function() { 303 expect(10); 304 305 var elements = $([window, document]), 306 inputElements = $('#radio1,#radio2,#check1,#check2'); 307 308 equals( elements.index(window), 0, "Check for index of elements" ); 309 equals( elements.index(document), 1, "Check for index of elements" ); 310 equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" ); 311 equals( inputElements.index(document.getElementById('radio2')), 1, "Check for index of elements" ); 312 equals( inputElements.index(document.getElementById('check1')), 2, "Check for index of elements" ); 313 equals( inputElements.index(document.getElementById('check2')), 3, "Check for index of elements" ); 314 equals( inputElements.index(window), -1, "Check for not found index" ); 315 equals( inputElements.index(document), -1, "Check for not found index" ); 316 317 // enabled since [5500] 318 equals( elements.index( elements ), 0, "Pass in a jQuery object" ); 319 equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" ); 320 }); 321 322 test("attr(String)", function() { 323 expect(29); 324 equals( $('#text1').attr('value'), "Test", 'Check for value attribute' ); 325 equals( $('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' ); 326 equals( $('#text1').attr('type'), "text", 'Check for type attribute' ); 327 equals( $('#radio1').attr('type'), "radio", 'Check for type attribute' ); 328 equals( $('#check1').attr('type'), "checkbox", 'Check for type attribute' ); 329 equals( $('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' ); 330 equals( $('#google').attr('title'), "Google!", 'Check for title attribute' ); 331 equals( $('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' ); 332 equals( $('#en').attr('lang'), "en", 'Check for lang attribute' ); 333 equals( $('#simon').attr('class'), "blog link", 'Check for class attribute' ); 334 equals( $('#name').attr('name'), "name", 'Check for name attribute' ); 335 equals( $('#text1').attr('name'), "action", 'Check for name attribute' ); 336 ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' ); 337 equals( $('#text1').attr('minlength'), '20', 'Check for minlength attribute' ); 338 equals( $('#text1').attr('minLength'), '20', 'Check for minLength attribute' ); 339 equals( $('#area1').attr('minLength'), '20', 'Check for minLength attribute' ); 340 equals( $('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' ); 341 equals( $('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' ); 342 equals( $('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' ); 343 equals( $('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' ); 344 equals( $('#foo').attr('nodeName'), 'DIV', 'Check for nodeName attribute' ); 345 equals( $('#foo').attr('tagName'), 'DIV', 'Check for tagName attribute' ); 346 347 $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path 348 equals( $('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' ); 349 350 351 // Related to [5574] and [5683] 352 var body = document.body, $body = $(body); 353 354 ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' ); 355 ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' ); 356 357 body.setAttribute('foo', 'baz'); 358 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' ); 359 360 body.foo = 'bar'; 361 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' ); 362 363 $body.attr('foo','cool'); 364 equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' ); 365 366 body.foo = undefined; 367 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' ); 368 369 body.removeAttribute('foo'); // Cleanup 370 }); 371 372 if ( !isLocal ) { 373 test("attr(String) in XML Files", function() { 374 expect(2); 375 stop(); 376 $.get("data/dashboard.xml", function(xml) { 377 equals( $("locations", xml).attr("class"), "foo", "Check class attribute in XML document" ); 378 equals( $("location", xml).attr("for"), "bar", "Check for attribute in XML document" ); 379 start(); 380 }); 381 }); 382 } 383 384 test("attr(String, Function)", function() { 385 expect(2); 386 equals( $('#text1').attr('value', function() { return this.id })[0].value, "text1", "Set value from id" ); 387 equals( $('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index"); 388 }); 389 390 test("attr(Hash)", function() { 391 expect(1); 392 var pass = true; 393 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){ 394 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false; 395 }); 396 ok( pass, "Set Multiple Attributes" ); 397 }); 398 399 test("attr(String, Object)", function() { 400 expect(19); 401 var div = $("div").attr("foo", "bar"); 402 fail = false; 403 for ( var i = 0; i < div.size(); i++ ) { 404 if ( div.get(i).getAttribute('foo') != "bar" ){ 405 fail = i; 406 break; 407 } 408 } 409 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" ); 410 411 ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" ); 412 413 $("#name").attr('name', 'something'); 414 equals( $("#name").attr('name'), 'something', 'Set name attribute' ); 415 $("#check2").attr('checked', true); 416 equals( document.getElementById('check2').checked, true, 'Set checked attribute' ); 417 $("#check2").attr('checked', false); 418 equals( document.getElementById('check2').checked, false, 'Set checked attribute' ); 419 $("#text1").attr('readonly', true); 420 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' ); 421 $("#text1").attr('readonly', false); 422 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' ); 423 $("#name").attr('maxlength', '5'); 424 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' ); 425 $("#name").attr('maxLength', '10'); 426 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' ); 427 $("#name").attr('minlength', '5'); 428 equals( document.getElementById('name').minLength, '5', 'Set minlength attribute' ); 429 $("#name").attr('minLength', '10'); 430 equals( document.getElementById('name').minLength, '10', 'Set minlength attribute' ); 431 432 // for #1070 433 $("#name").attr('someAttr', '0'); 434 equals( $("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' ); 435 $("#name").attr('someAttr', 0); 436 equals( $("#name").attr('someAttr'), 0, 'Set attribute to the number 0' ); 437 $("#name").attr('someAttr', 1); 438 equals( $("#name").attr('someAttr'), 1, 'Set attribute to the number 1' ); 439 440 // using contents will get comments regular, text, and comment nodes 441 var j = $("#nonnodes").contents(); 442 443 j.attr("name", "attrvalue"); 444 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" ); 445 j.removeAttr("name"); 446 447 reset(); 448 449 var type = $("#check2").attr('type'); 450 var thrown = false; 451 try { 452 $("#check2").attr('type','hidden'); 453 } catch(e) { 454 thrown = true; 455 } 456 ok( thrown, "Exception thrown when trying to change type property" ); 457 equals( type, $("#check2").attr('type'), "Verify that you can't change the type of an input element" ); 458 459 var check = document.createElement("input"); 460 var thrown = true; 461 try { 462 $(check).attr('type','checkbox'); 463 } catch(e) { 464 thrown = false; 465 } 466 ok( thrown, "Exception thrown when trying to change type property" ); 467 equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" ); 468 }); 469 470 if ( !isLocal ) { 471 test("attr(String, Object) - Loaded via XML document", function() { 472 expect(2); 473 stop(); 474 $.get('data/dashboard.xml', function(xml) { 475 var titles = []; 476 $('tab', xml).each(function() { 477 titles.push($(this).attr('title')); 478 }); 479 equals( titles[0], 'Location', 'attr() in XML context: Check first title' ); 480 equals( titles[1], 'Users', 'attr() in XML context: Check second title' ); 481 start(); 482 }); 483 }); 484 } 485 486 test("css(String|Hash)", function() { 487 expect(19); 488 489 equals( $('#main').css("display"), 'none', 'Check for css property "display"'); 490 491 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible'); 492 $('#foo').css({display: 'none'}); 493 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden'); 494 $('#foo').css({display: 'block'}); 495 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible'); 496 497 $('#floatTest').css({styleFloat: 'right'}); 498 equals( $('#floatTest').css('styleFloat'), 'right', 'Modified CSS float using "styleFloat": Assert float is right'); 499 $('#floatTest').css({cssFloat: 'left'}); 500 equals( $('#floatTest').css('cssFloat'), 'left', 'Modified CSS float using "cssFloat": Assert float is left'); 501 $('#floatTest').css({'float': 'right'}); 502 equals( $('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right'); 503 $('#floatTest').css({'font-size': '30px'}); 504 equals( $('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px'); 505 506 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) { 507 $('#foo').css({opacity: n}); 508 equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" ); 509 $('#foo').css({opacity: parseFloat(n)}); 510 equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" ); 511 }); 512 $('#foo').css({opacity: ''}); 513 equals( $('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" ); 514 }); 515 516 test("css(String, Object)", function() { 517 expect(21); 518 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible'); 519 $('#foo').css('display', 'none'); 520 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden'); 521 $('#foo').css('display', 'block'); 522 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible'); 523 524 $('#floatTest').css('styleFloat', 'left'); 525 equals( $('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left'); 526 $('#floatTest').css('cssFloat', 'right'); 527 equals( $('#floatTest').css('cssFloat'), 'right', 'Modified CSS float using "cssFloat": Assert float is right'); 528 $('#floatTest').css('float', 'left'); 529 equals( $('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left'); 530 $('#floatTest').css('font-size', '20px'); 531 equals( $('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px'); 532 533 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) { 534 $('#foo').css('opacity', n); 535 equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" ); 536 $('#foo').css('opacity', parseFloat(n)); 537 equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" ); 538 }); 539 $('#foo').css('opacity', ''); 540 equals( $('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" ); 541 // for #1438, IE throws JS error when filter exists but doesn't have opacity in it 542 if (jQuery.browser.msie) { 543 $('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');"); 544 } 545 equals( $('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" ); 546 547 // using contents will get comments regular, text, and comment nodes 548 var j = $("#nonnodes").contents(); 549 j.css("padding-left", "1px"); 550 equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" ); 551 552 // opera sometimes doesn't update 'display' correctly, see #2037 553 $("#t2037")[0].innerHTML = $("#t2037")[0].innerHTML 554 equals( $("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" ); 555 }); 556 557 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () { 558 expect(4); 559 560 var $checkedtest = $("#checkedtest"); 561 // IE6 was clearing "checked" in jQuery.css(elem, "height"); 562 jQuery.css($checkedtest[0], "height"); 563 ok( !! $(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." ); 564 ok( ! $(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." ); 565 ok( !! $(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." ); 566 ok( ! $(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." ); 567 }); 568 569 test("width()", function() { 570 expect(9); 571 572 var $div = $("#nothiddendiv"); 573 $div.width(30); 574 equals($div.width(), 30, "Test set to 30 correctly"); 575 $div.width(-1); // handle negative numbers by ignoring #1599 576 equals($div.width(), 30, "Test negative width ignored"); 577 $div.css("padding", "20px"); 578 equals($div.width(), 30, "Test padding specified with pixels"); 579 $div.css("border", "2px solid #fff"); 580 equals($div.width(), 30, "Test border specified with pixels"); 581 $div.css("padding", "2em"); 582 equals($div.width(), 30, "Test padding specified with ems"); 583 $div.css("border", "1em solid #fff"); 584 equals($div.width(), 30, "Test border specified with ems"); 585 $div.css("padding", "2%"); 586 equals($div.width(), 30, "Test padding specified with percent"); 587 $div.hide(); 588 equals($div.width(), 30, "Test hidden div"); 589 590 $div.css({ display: "", border: "", padding: "" }); 591 592 $("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" }); 593 equals($("#nothiddendivchild").width(), 20, "Test child width with border and padding"); 594 $("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" }); 595 }); 596 597 test("height()", function() { 598 expect(8); 599 600 var $div = $("#nothiddendiv"); 601 $div.height(30); 602 equals($div.height(), 30, "Test set to 30 correctly"); 603 $div.height(-1); // handle negative numbers by ignoring #1599 604 equals($div.height(), 30, "Test negative height ignored"); 605 $div.css("padding", "20px"); 606 equals($div.height(), 30, "Test padding specified with pixels"); 607 $div.css("border", "2px solid #fff"); 608 equals($div.height(), 30, "Test border specified with pixels"); 609 $div.css("padding", "2em"); 610 equals($div.height(), 30, "Test padding specified with ems"); 611 $div.css("border", "1em solid #fff"); 612 equals($div.height(), 30, "Test border specified with ems"); 613 $div.css("padding", "2%"); 614 equals($div.height(), 30, "Test padding specified with percent"); 615 $div.hide(); 616 equals($div.height(), 30, "Test hidden div"); 617 618 $div.css({ display: "", border: "", padding: "", height: "1px" }); 619 }); 620 621 test("text()", function() { 622 expect(1); 623 var expected = "This link has class=\"blog\": Simon Willison's Weblog"; 624 equals( $('#sap').text(), expected, 'Check for merged text of more then one element.' ); 625 }); 626 627 test("wrap(String|Element)", function() { 628 expect(8); 629 var defaultText = 'Try them out:' 630 var result = $('#first').wrap('<div class="red"><span></span></div>').text(); 631 equals( defaultText, result, 'Check for wrapping of on-the-fly html' ); 632 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' ); 633 634 reset(); 635 var defaultText = 'Try them out:' 636 var result = $('#first').wrap(document.getElementById('empty')).parent(); 637 ok( result.is('ol'), 'Check for element wrapping' ); 638 equals( result.text(), defaultText, 'Check for element wrapping' ); 639 640 reset(); 641 $('#check1').click(function() { 642 var checkbox = this; 643 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); 644 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' ); 645 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); 646 }).click(); 647 648 // using contents will get comments regular, text, and comment nodes 649 var j = $("#nonnodes").contents(); 650 j.wrap("<i></i>"); 651 equals( $("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" ); 652 equals( $("#nonnodes > i").text(), j.text() + j[1].nodeValue, "Check node,textnode,comment wraps doesn't hurt text" ); 653 }); 654 655 test("wrapAll(String|Element)", function() { 656 expect(8); 657 var prev = $("#first")[0].previousSibling; 658 var p = $("#first")[0].parentNode; 659 var result = $('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>'); 660 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' ); 661 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' ); 662 ok( $('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' ); 663 equals( $("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" ); 664 equals( $("#first").parent().parent()[0].parentNode, p, "Correct Parent" ); 665 666 reset(); 667 var prev = $("#first")[0].previousSibling; 668 var p = $("#first")[0].parentNode; 669 var result = $('#first,#firstp').wrapAll(document.getElementById('empty')); 670 equals( $("#first").parent()[0], $("#firstp").parent()[0], "Same Parent" ); 671 equals( $("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" ); 672 equals( $("#first").parent()[0].parentNode, p, "Correct Parent" ); 673 }); 674 675 test("wrapInner(String|Element)", function() { 676 expect(6); 677 var num = $("#first").children().length; 678 var result = $('#first').wrapInner('<div class="red"><div id="tmp"></div></div>'); 679 equals( $("#first").children().length, 1, "Only one child" ); 680 ok( $("#first").children().is(".red"), "Verify Right Element" ); 681 equals( $("#first").children().children().children().length, num, "Verify Elements Intact" ); 682 683 reset(); 684 var num = $("#first").children().length; 685 var result = $('#first').wrapInner(document.getElementById('empty')); 686 equals( $("#first").children().length, 1, "Only one child" ); 687 ok( $("#first").children().is("#empty"), "Verify Right Element" ); 688 equals( $("#first").children().children().length, num, "Verify Elements Intact" ); 689 }); 690 691 test("append(String|Element|Array<Element>|jQuery)", function() { 692 expect(21); 693 var defaultText = 'Try them out:' 694 var result = $('#first').append('<b>buga</b>'); 695 equals( result.text(), defaultText + 'buga', 'Check if text appending works' ); 696 equals( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element'); 697 698 reset(); 699 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:"; 700 $('#sap').append(document.getElementById('first')); 701 equals( expected, $('#sap').text(), "Check for appending of element" ); 702 703 reset(); 704 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; 705 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]); 706 equals( expected, $('#sap').text(), "Check for appending of array of elements" ); 707 708 reset(); 709 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; 710 $('#sap').append($("#first, #yahoo")); 711 equals( expected, $('#sap').text(), "Check for appending of jQuery object" ); 712 713 reset(); 714 $("#sap").append( 5 ); 715 ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" ); 716 717 reset(); 718 $("#sap").append( " text with spaces " ); 719 ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" ); 720 721 reset(); 722 ok( $("#sap").append([]), "Check for appending an empty array." ); 723 ok( $("#sap").append(""), "Check for appending an empty string." ); 724 ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." ); 725 726 reset(); 727 $("#sap").append(document.getElementById('form')); 728 equals( $("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910 729 730 reset(); 731 var pass = true; 732 try { 733 $( $("#iframe")[0].contentWindow.document.body ).append("<div>test</div>"); 734 } catch(e) { 735 pass = false; 736 } 737 738 ok( pass, "Test for appending a DOM node to the contents of an IFrame" ); 739 740 reset(); 741 $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>'); 742 t( 'Append legend', '#legend', ['legend'] ); 743 744 reset(); 745 $('#select1').append('<OPTION>Test</OPTION>'); 746 equals( $('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" ); 747 748 $('#table').append('<colgroup></colgroup>'); 749 ok( $('#table colgroup').length, "Append colgroup" ); 750 751 $('#table colgroup').append('<col/>'); 752 ok( $('#table colgroup col').length, "Append col" ); 753 754 reset(); 755 $('#table').append('<caption></caption>'); 756 ok( $('#table caption').length, "Append caption" ); 757 758 reset(); 759 $('form:last') 760 .append('<select id="appendSelect1"></select>') 761 .append('<select id="appendSelect2"><option>Test</option></select>'); 762 763 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] ); 764 765 // using contents will get comments regular, text, and comment nodes 766 var j = $("#nonnodes").contents(); 767 var d = $("<div/>").appendTo("#nonnodes").append(j); 768 equals( $("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" ); 769 ok( d.contents().length >= 2, "Check node,textnode,comment append works" ); 770 d.contents().appendTo("#nonnodes"); 771 d.remove(); 772 ok( $("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" ); 773 }); 774 775 test("appendTo(String|Element|Array<Element>|jQuery)", function() { 776 expect(6); 777 var defaultText = 'Try them out:' 778 $('<b>buga</b>').appendTo('#first'); 779 equals( $("#first").text(), defaultText + 'buga', 'Check if text appending works' ); 780 equals( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element'); 781 782 reset(); 783 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:"; 784 $(document.getElementById('first')).appendTo('#sap'); 785 equals( expected, $('#sap').text(), "Check for appending of element" ); 786 787 reset(); 788 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; 789 $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap'); 790 equals( expected, $('#sap').text(), "Check for appending of array of elements" ); 791 792 reset(); 793 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; 794 $("#first, #yahoo").appendTo('#sap'); 795 equals( expected, $('#sap').text(), "Check for appending of jQuery object" ); 796 797 reset(); 798 $('#select1').appendTo('#foo'); 799 t( 'Append select', '#foo select', ['select1'] ); 800 }); 801 802 test("prepend(String|Element|Array<Element>|jQuery)", function() { 803 expect(5); 804 var defaultText = 'Try them out:' 805 var result = $('#first').prepend('<b>buga</b>'); 806 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' ); 807 equals( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element'); 808 809 reset(); 810 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog"; 811 $('#sap').prepend(document.getElementById('first')); 812 equals( expected, $('#sap').text(), "Check for prepending of element" ); 813 814 reset(); 815 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; 816 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]); 817 equals( expected, $('#sap').text(), "Check for prepending of array of elements" ); 818 819 reset(); 820 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; 821 $('#sap').prepend($("#first, #yahoo")); 822 equals( expected, $('#sap').text(), "Check for prepending of jQuery object" ); 823 }); 824 825 test("prependTo(String|Element|Array<Element>|jQuery)", function() { 826 expect(6); 827 var defaultText = 'Try them out:' 828 $('<b>buga</b>').prependTo('#first'); 829 equals( $('#first').text(), 'buga' + defaultText, 'Check if text prepending works' ); 830 equals( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element'); 831 832 reset(); 833 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog"; 834 $(document.getElementById('first')).prependTo('#sap'); 835 equals( expected, $('#sap').text(), "Check for prepending of element" ); 836 837 reset(); 838 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; 839 $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap'); 840 equals( expected, $('#sap').text(), "Check for prepending of array of elements" ); 841 842 reset(); 843 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; 844 $("#yahoo, #first").prependTo('#sap'); 845 equals( expected, $('#sap').text(), "Check for prepending of jQuery object" ); 846 847 reset(); 848 $('<select id="prependSelect1"></select>').prependTo('form:last'); 849 $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last'); 850 851 t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] ); 852 }); 853 854 test("before(String|Element|Array<Element>|jQuery)", function() { 855 expect(4); 856 var expected = 'This is a normal link: bugaYahoo'; 857 $('#yahoo').before('<b>buga</b>'); 858 equals( expected, $('#en').text(), 'Insert String before' ); 859 860 reset(); 861 expected = "This is a normal link: Try them out:Yahoo"; 862 $('#yahoo').before(document.getElementById('first')); 863 equals( expected, $('#en').text(), "Insert element before" ); 864 865 reset(); 866 expected = "This is a normal link: Try them out:diveintomarkYahoo"; 867 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]); 868 equals( expected, $('#en').text(), "Insert array of elements before" ); 869 870 reset(); 871 expected = "This is a normal link: Try them out:diveintomarkYahoo"; 872 $('#yahoo').before($("#first, #mark")); 873 equals( expected, $('#en').text(), "Insert jQuery before" ); 874 }); 875 876 test("insertBefore(String|Element|Array<Element>|jQuery)", function() { 877 expect(4); 878 var expected = 'This is a normal link: bugaYahoo'; 879 $('<b>buga</b>').insertBefore('#yahoo'); 880 equals( expected, $('#en').text(), 'Insert String before' ); 881 882 reset(); 883 expected = "This is a normal link: Try them out:Yahoo"; 884 $(document.getElementById('first')).insertBefore('#yahoo'); 885 equals( expected, $('#en').text(), "Insert element before" ); 886 887 reset(); 888 expected = "This is a normal link: Try them out:diveintomarkYahoo"; 889 $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo'); 890 equals( expected, $('#en').text(), "Insert array of elements before" ); 891 892 reset(); 893 expected = "This is a normal link: Try them out:diveintomarkYahoo"; 894 $("#first, #mark").insertBefore('#yahoo'); 895 equals( expected, $('#en').text(), "Insert jQuery before" ); 896 }); 897 898 test("after(String|Element|Array<Element>|jQuery)", function() { 899 expect(4); 900 var expected = 'This is a normal link: Yahoobuga'; 901 $('#yahoo').after('<b>buga</b>'); 902 equals( expected, $('#en').text(), 'Insert String after' ); 903 904 reset(); 905 expected = "This is a normal link: YahooTry them out:"; 906 $('#yahoo').after(document.getElementById('first')); 907 equals( expected, $('#en').text(), "Insert element after" ); 908 909 reset(); 910 expected = "This is a normal link: YahooTry them out:diveintomark"; 911 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]); 912 equals( expected, $('#en').text(), "Insert array of elements after" ); 913 914 reset(); 915 expected = "This is a normal link: YahooTry them out:diveintomark"; 916 $('#yahoo').after($("#first, #mark")); 917 equals( expected, $('#en').text(), "Insert jQuery after" ); 918 }); 919 920 test("insertAfter(String|Element|Array<Element>|jQuery)", function() { 921 expect(4); 922 var expected = 'This is a normal link: Yahoobuga'; 923 $('<b>buga</b>').insertAfter('#yahoo'); 924 equals( expected, $('#en').text(), 'Insert String after' ); 925 926 reset(); 927 expected = "This is a normal link: YahooTry them out:"; 928 $(document.getElementById('first')).insertAfter('#yahoo'); 929 equals( expected, $('#en').text(), "Insert element after" ); 930 931 reset(); 932 expected = "This is a normal link: YahooTry them out:diveintomark"; 933 $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo'); 934 equals( expected, $('#en').text(), "Insert array of elements after" ); 935 936 reset(); 937 expected = "This is a normal link: YahooTry them out:diveintomark"; 938 $("#mark, #first").insertAfter('#yahoo'); 939 equals( expected, $('#en').text(), "Insert jQuery after" ); 940 }); 941 942 test("replaceWith(String|Element|Array<Element>|jQuery)", function() { 943 expect(10); 944 $('#yahoo').replaceWith('<b id="replace">buga</b>'); 945 ok( $("#replace")[0], 'Replace element with string' ); 946 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' ); 947 948 reset(); 949 $('#yahoo').replaceWith(document.getElementById('first')); 950 ok( $("#first")[0], 'Replace element with element' ); 951 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' ); 952 953 reset(); 954 $('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]); 955 ok( $("#first")[0], 'Replace element with array of elements' ); 956 ok( $("#mark")[0], 'Replace element with array of elements' ); 957 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' ); 958 959 reset(); 960 $('#yahoo').replaceWith($("#first, #mark")); 961 ok( $("#first")[0], 'Replace element with set of elements' ); 962 ok( $("#mark")[0], 'Replace element with set of elements' ); 963 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' ); 964 }); 965 966 test("replaceAll(String|Element|Array<Element>|jQuery)", function() { 967 expect(10); 968 $('<b id="replace">buga</b>').replaceAll("#yahoo"); 969 ok( $("#replace")[0], 'Replace element with string' ); 970 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' ); 971 972 reset(); 973 $(document.getElementById('first')).replaceAll("#yahoo"); 974 ok( $("#first")[0], 'Replace element with element' ); 975 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' ); 976 977 reset(); 978 $([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo"); 979 ok( $("#first")[0], 'Replace element with array of elements' ); 980 ok( $("#mark")[0], 'Replace element with array of elements' ); 981 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' ); 982 983 reset(); 984 $("#first, #mark").replaceAll("#yahoo"); 985 ok( $("#first")[0], 'Replace element with set of elements' ); 986 ok( $("#mark")[0], 'Replace element with set of elements' ); 987 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' ); 988 }); 989 990 test("end()", function() { 991 expect(3); 992 equals( 'Yahoo', $('#yahoo').parent().end().text(), 'Check for end' ); 993 ok( $('#yahoo').end(), 'Check for end with nothing to end' ); 994 995 var x = $('#yahoo'); 996 x.parent(); 997 equals( 'Yahoo', $('#yahoo').text(), 'Check for non-destructive behaviour' ); 998 }); 999 1000 test("find(String)", function() { 1001 expect(2); 1002 equals( 'Yahoo', $('#foo').find('.blogTest').text(), 'Check for find' ); 1003 1004 // using contents will get comments regular, text, and comment nodes 1005 var j = $("#nonnodes").contents(); 1006 equals( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" ); 1007 }); 1008 1009 test("clone()", function() { 1010 expect(20); 1011 equals( 'This is a normal link: Yahoo', $('#en').text(), 'Assert text for #en' ); 1012 var clone = $('#yahoo').clone(); 1013 equals( 'Try them out:Yahoo', $('#first').append(clone).text(), 'Check for clone' ); 1014 equals( 'This is a normal link: Yahoo', $('#en').text(), 'Reassert text for #en' ); 1015 1016 var cloneTags = [ 1017 "<table/>", "<tr/>", "<td/>", "<div/>", 1018 "<button/>", "<ul/>", "<ol/>", "<li/>", 1019 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>", 1020 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>" 1021 ]; 1022 for (var i = 0; i < cloneTags.length; i++) { 1023 var j = $(cloneTags[i]); 1024 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1)); 1025 } 1026 1027 // using contents will get comments regular, text, and comment nodes 1028 var cl = $("#nonnodes").contents().clone(); 1029 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" ); 1030 }); 1031 1032 if (!isLocal) { 1033 test("clone() on XML nodes", function() { 1034 expect(2); 1035 stop(); 1036 $.get("data/dashboard.xml", function (xml) { 1037 var root = $(xml.documentElement).clone(); 1038 $("tab:first", xml).text("origval"); 1039 $("tab:first", root).text("cloneval"); 1040 equals($("tab:first", xml).text(), "origval", "Check original XML node was correctly set"); 1041 equals($("tab:first", root).text(), "cloneval", "Check cloned XML node was correctly set"); 1042 start(); 1043 }); 1044 }); 1045 } 1046 1047 test("is(String)", function() { 1048 expect(26); 1049 ok( $('#form').is('form'), 'Check for element: A form must be a form' ); 1050 ok( !$('#form').is('div'), 'Check for element: A form is not a div' ); 1051 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' ); 1052 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' ); 1053 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' ); 1054 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' ); 1055 ok( $('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' ); 1056 ok( !$('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' ); 1057 ok( $('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' ); 1058 ok( !$('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' ); 1059 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' ); 1060 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' ); 1061 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' ); 1062 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' ); 1063 ok( $('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' ); 1064 ok( !$('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' ); 1065 ok( $('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' ); 1066 ok( !$('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' ); 1067 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' ); 1068 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' ); 1069 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' ); 1070 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' ); 1071 1072 // test is() with comma-seperated expressions 1073 ok( $('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' ); 1074 ok( $('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' ); 1075 ok( $('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' ); 1076 ok( $('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' ); 1077 }); 1078 1079 test("$.extend(Object, Object)", function() { 1080 expect(20); 1081 1082 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, 1083 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" }, 1084 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" }, 1085 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" }, 1086 deep1 = { foo: { bar: true } }, 1087 deep1copy = { foo: { bar: true } }, 1088 deep2 = { foo: { baz: true }, foo2: document }, 1089 deep2copy = { foo: { baz: true }, foo2: document }, 1090 deepmerged = { foo: { bar: true, baz: true }, foo2: document }; 1091 1092 jQuery.extend(settings, options); 1093 isObj( settings, merged, "Check if extended: settings must be extended" ); 1094 isObj( options, optionsCopy, "Check if not modified: options must not be modified" ); 1095 1096 jQuery.extend(settings, null, options); 1097 isObj( settings, merged, "Check if extended: settings must be extended" ); 1098 isObj( options, optionsCopy, "Check if not modified: options must not be modified" ); 1099 1100 jQuery.extend(true, deep1, deep2); 1101 isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" ); 1102 isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" ); 1103 equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" ); 1104 1105 var nullUndef; 1106 nullUndef = jQuery.extend({}, options, { xnumber2: null }); 1107 ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied"); 1108 1109 nullUndef = jQuery.extend({}, options, { xnumber2: undefined }); 1110 ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied"); 1111 1112 nullUndef = jQuery.extend({}, options, { xnumber0: null }); 1113 ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted"); 1114 1115 var target = {}; 1116 var recursive = { foo:target, bar:5 }; 1117 jQuery.extend(true, target, recursive); 1118 isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" ); 1119 1120 var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907 1121 equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" ); 1122 1123 var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } ); 1124 ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" ); 1125 1126 var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } ); 1127 ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" ); 1128 1129 var obj = { foo:null }; 1130 jQuery.extend(true, obj, { foo:"notnull" } ); 1131 equals( obj.foo, "notnull", "Make sure a null value can be overwritten" ); 1132 1133 function func() {} 1134 jQuery.extend(func, { key: "value" } ); 1135 equals( func.key, "value", "Verify a function can be extended" ); 1136 1137 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, 1138 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, 1139 options1 = { xnumber2: 1, xstring2: "x" }, 1140 options1Copy = { xnumber2: 1, xstring2: "x" }, 1141 options2 = { xstring2: "xx", xxx: "newstringx" }, 1142 options2Copy = { xstring2: "xx", xxx: "newstringx" }, 1143 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" }; 1144 1145 var settings = jQuery.extend({}, defaults, options1, options2); 1146 isObj( settings, merged2, "Check if extended: settings must be extended" ); 1147 isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" ); 1148 isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" ); 1149 isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" ); 1150 }); 1151 1152 test("val()", function() { 1153 expect(4); 1154 equals( $("#text1").val(), "Test", "Check for value of input element" ); 1155 equals( !$("#text1").val(), "", "Check for value of input element" ); 1156 // ticket #1714 this caused a JS error in IE 1157 equals( $("#first").val(), "", "Check a paragraph element to see if it has a value" ); 1158 ok( $([]).val() === undefined, "Check an empty jQuery object will return undefined from val" ); 1159 }); 1160 1161 test("val(String)", function() { 1162 expect(4); 1163 document.getElementById('text1').value = "bla"; 1164 equals( $("#text1").val(), "bla", "Check for modified value of input element" ); 1165 $("#text1").val('test'); 1166 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" ); 1167 1168 $("#select1").val("3"); 1169 equals( $("#select1").val(), "3", "Check for modified (via val(String)) value of select element" ); 1170 1171 // using contents will get comments regular, text, and comment nodes 1172 var j = $("#nonnodes").contents(); 1173 j.val("asdf"); 1174 equals( j.val(), "asdf", "Check node,textnode,comment with val()" ); 1175 j.removeAttr("value"); 1176 }); 1177 1178 var scriptorder = 0; 1179 1180 test("html(String)", function() { 1181 expect(11); 1182 var div = $("#main > div"); 1183 div.html("<b>test</b>"); 1184 var pass = true; 1185 for ( var i = 0; i < div.size(); i++ ) { 1186 if ( div.get(i).childNodes.length != 1 ) pass = false; 1187 } 1188 ok( pass, "Set HTML" ); 1189 1190 reset(); 1191 // using contents will get comments regular, text, and comment nodes 1192 var j = $("#nonnodes").contents(); 1193 j.html("<b>bold</b>"); 1194 1195 // this is needed, or the expando added by jQuery unique will yield a different html 1196 j.find('b').removeData(); 1197 equals( j.html().toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" ); 1198 1199 $("#main").html("<select/>"); 1200 $("#main select").html("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"); 1201 equals( $("#main select").val(), "O2", "Selected option correct" ); 1202 1203 stop(); 1204 1205 $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>'); 1206 1207 $("#main").html('foo <form><script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>'); 1208 1209 // 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 1210 $("#main").html("<script>equals(scriptorder++, 0, 'Script is executed in order');equals($('#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($('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equals(scriptorder++, 2, 'Script (unnested) is executed in order');equals($('#scriptorder').length, 1,'Execute after html')<\/script>"); 1211 1212 setTimeout( start, 100 ); 1213 }); 1214 1215 test("filter()", function() { 1216 expect(6); 1217 isSet( $("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" ); 1218 isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" ); 1219 isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" ); 1220 isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" ); 1221 1222 // using contents will get comments regular, text, and comment nodes 1223 var j = $("#nonnodes").contents(); 1224 equals( j.filter("span").length, 1, "Check node,textnode,comment to filter the one span" ); 1225 equals( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" ); 1226 }); 1227 1228 test("not()", function() { 1229 expect(8); 1230 equals( $("#main > p#ap > a").not("#google").length, 2, "not('selector')" ); 1231 equals( $("#main > p#ap > a").not(document.getElementById("google")).length, 2, "not(DOMElement)" ); 1232 isSet( $("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" ); 1233 isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" ); 1234 isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" ); 1235 equals( $("p").not(document.getElementsByTagName("p")).length, 0, "not(Array-like DOM collection)" ); 1236 isSet( $("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d" ), "not('complex selector')"); 1237 1238 var selects = $("#form select"); 1239 isSet( selects.not( selects[1] ), q("select1", "select3"), "filter out DOM element"); 1240 }); 1241 1242 test("andSelf()", function() { 1243 expect(4); 1244 isSet( $("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" ); 1245 isSet( $("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" ); 1246 isSet( $("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" ); 1247 isSet( $("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" ); 1248 }); 1249 1250 test("siblings([String])", function() { 1251 expect(5); 1252 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" ); 1253 isSet( $("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" ); 1254 isSet( $("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" ); 1255 isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" ); 1256 isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" ); 1257 }); 1258 1259 test("children([String])", function() { 1260 expect(3); 1261 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" ); 1262 isSet( $("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" ); 1263 isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" ); 1264 }); 1265 1266 test("parent([String])", function() { 1267 expect(5); 1268 equals( $("#groups").parent()[0].id, "ap", "Simple parent check" ); 1269 equals( $("#groups").parent("p")[0].id, "ap", "Filtered parent check" ); 1270 equals( $("#groups").parent("div").length, 0, "Filtered parent check, no match" ); 1271 equals( $("#groups").parent("div, p")[0].id, "ap", "Check for multiple filters" ); 1272 isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" ); 1273 }); 1274 1275 test("parents([String])", function() { 1276 expect(5); 1277 equals( $("#groups").parents()[0].id, "ap", "Simple parents check" ); 1278 equals( $("#groups").parents("p")[0].id, "ap", "Filtered parents check" ); 1279 equals( $("#groups").parents("div")[0].id, "main", "Filtered parents check2" ); 1280 isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" ); 1281 isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" ); 1282 }); 1283 1284 test("next([String])", function() { 1285 expect(4); 1286 equals( $("#ap").next()[0].id, "foo", "Simple next check" ); 1287 equals( $("#ap").next("div")[0].id, "foo", "Filtered next check" ); 1288 equals( $("#ap").next("p").length, 0, "Filtered next check, no match" ); 1289 equals( $("#ap").next("div, p")[0].id, "foo", "Multiple filters" ); 1290 }); 1291 1292 test("prev([String])", function() { 1293 expect(4); 1294 equals( $("#foo").prev()[0].id, "ap", "Simple prev check" ); 1295 equals( $("#foo").prev("p")[0].id, "ap", "Filtered prev check" ); 1296 equals( $("#foo").prev("div").length, 0, "Filtered prev check, no match" ); 1297 equals( $("#foo").prev("p, div")[0].id, "ap", "Multiple filters" ); 1298 }); 1299 1300 test("show()", function() { 1301 expect(15); 1302 var pass = true, div = $("div"); 1303 div.show().each(function(){ 1304 if ( this.style.display == "none" ) pass = false; 1305 }); 1306 ok( pass, "Show" ); 1307 1308 $("#main").append('<div id="show-tests"><div><p><a href="#"></a></p><code></code><pre></pre><span></span></div><table><thead><tr><th></th></tr></thead><tbody><tr><td></td></tr></tbody></table><ul><li></li></ul></div>'); 1309 var test = { 1310 "div" : "block", 1311 "p" : "block", 1312 "a" : "inline", 1313 "code" : "inline", 1314 "pre" : "block", 1315 "span" : "inline", 1316 "table" : $.browser.msie ? "block" : "table", 1317 "thead" : $.browser.msie ? "block" : "table-header-group", 1318 "tbody" : $.browser.msie ? "block" : "table-row-group", 1319 "tr" : $.browser.msie ? "block" : "table-row", 1320 "th" : $.browser.msie ? "block" : "table-cell", 1321 "td" : $.browser.msie ? "block" : "table-cell", 1322 "ul" : "block", 1323 "li" : $.browser.msie ? "block" : "list-item" 1324 }; 1325 1326 $.each(test, function(selector, expected) { 1327 var elem = $(selector, "#show-tests").show(); 1328 equals( elem.css("display"), expected, "Show using correct display type for " + selector ); 1329 }); 1330 }); 1331 1332 test("addClass(String)", function() { 1333 expect(2); 1334 var div = $("div"); 1335 div.addClass("test"); 1336 var pass = true; 1337 for ( var i = 0; i < div.size(); i++ ) { 1338 if ( div.get(i).className.indexOf("test") == -1 ) pass = false; 1339 } 1340 ok( pass, "Add Class" ); 1341 1342 // using contents will get regular, text, and comment nodes 1343 var j = $("#nonnodes").contents(); 1344 j.addClass("asdf"); 1345 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" ); 1346 }); 1347 1348 test("removeClass(String) - simple", function() { 1349 expect(4); 1350 var div = $("div").addClass("test").removeClass("test"), 1351 pass = true; 1352 for ( var i = 0; i < div.size(); i++ ) { 1353 if ( div.get(i).className.indexOf("test") != -1 ) pass = false; 1354 } 1355 ok( pass, "Remove Class" ); 1356 1357 reset(); 1358 var div = $("div").addClass("test").addClass("foo").addClass("bar"); 1359 div.removeClass("test").removeClass("bar").removeClass("foo"); 1360 var pass = true; 1361 for ( var i = 0; i < div.size(); i++ ) { 1362 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false; 1363 } 1364 ok( pass, "Remove multiple classes" ); 1365 1366 reset(); 1367 var div = $("div:eq(0)").addClass("test").removeClass(""); 1368 ok( div.is('.test'), "Empty string passed to removeClass" ); 1369 1370 // using contents will get regular, text, and comment nodes 1371 var j = $("#nonnodes").contents(); 1372 j.removeClass("asdf"); 1373 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" ); 1374 }); 1375 1376 test("toggleClass(String)", function() { 1377 expect(3); 1378 var e = $("#firstp"); 1379 ok( !e.is(".test"), "Assert class not present" ); 1380 e.toggleClass("test"); 1381 ok( e.is(".test"), "Assert class present" ); 1382 e.toggleClass("test"); 1383 ok( !e.is(".test"), "Assert class not present" ); 1384 }); 1385 1386 test("removeAttr(String", function() { 1387 expect(1); 1388 equals( $('#mark').removeAttr("class")[0].className, "", "remove class" ); 1389 }); 1390 1391 test("text(String)", function() { 1392 expect(4); 1393 equals( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML, "<div><b>Hello</b> cruel world!</div>", "Check escaped text" ); 1394 1395 // using contents will get comments regular, text, and comment nodes 1396 var j = $("#nonnodes").contents(); 1397 j.text("hi!"); 1398 equals( $(j[0]).text(), "hi!", "Check node,textnode,comment with text()" ); 1399 equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" ); 1400 equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" ); 1401 }); 1402 1403 test("$.each(Object,Function)", function() { 1404 expect(12); 1405 $.each( [0,1,2], function(i, n){ 1406 equals( i, n, "Check array iteration" ); 1407 }); 1408 1409 $.each( [5,6,7], function(i, n){ 1410 equals( i, n - 5, "Check array iteration" ); 1411 }); 1412 1413 $.each( { name: "name", lang: "lang" }, function(i, n){ 1414 equals( i, n, "Check object iteration" ); 1415 }); 1416 1417 var total = 0; 1418 jQuery.each([1,2,3], function(i,v){ total += v; }); 1419 equals( total, 6, "Looping over an array" ); 1420 total = 0; 1421 jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; }); 1422 equals( total, 3, "Looping over an array, with break" ); 1423 total = 0; 1424 jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; }); 1425 equals( total, 6, "Looping over an object" ); 1426 total = 0; 1427 jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; }); 1428 equals( total, 3, "Looping over an object, with break" ); 1429 }); 1430 1431 test("$.prop", function() { 1432 expect(2); 1433 var handle = function() { return this.id }; 1434 equals( $.prop($("#ap")[0], handle), "ap", "Check with Function argument" ); 1435 equals( $.prop($("#ap")[0], "value"), "value", "Check with value argument" ); 1436 }); 1437 1438 test("$.className", function() { 1439 expect(6); 1440 var x = $("<p>Hi</p>")[0]; 1441 var c = $.className; 1442 c.add(x, "hi"); 1443 equals( x.className, "hi", "Check single added class" ); 1444 c.add(x, "foo bar"); 1445 equals( x.className, "hi foo bar", "Check more added classes" ); 1446 c.remove(x); 1447 equals( x.className, "", "Remove all classes" ); 1448 c.add(x, "hi foo bar"); 1449 c.remove(x, "foo"); 1450 equals( x.className, "hi bar", "Check removal of one class" ); 1451 ok( c.has(x, "hi"), "Check has1" ); 1452 ok( c.has(x, "bar"), "Check has2" ); 1453 }); 1454 1455 test("$.data", function() { 1456 expect(5); 1457 var div = $("#foo")[0]; 1458 equals( jQuery.data(div, "test"), undefined, "Check for no data exists" ); 1459 jQuery.data(div, "test", "success"); 1460 equals( jQuery.data(div, "test"), "success", "Check for added data" ); 1461 jQuery.data(div, "test", "overwritten"); 1462 equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" ); 1463 jQuery.data(div, "test", undefined); 1464 equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed"); 1465 jQuery.data(div, "test", null); 1466 ok( jQuery.data(div, "test") === null, "Check for null data"); 1467 }); 1468 1469 test(".data()", function() { 1470 expect(18); 1471 var div = $("#foo"); 1472 equals( div.data("test"), undefined, "Check for no data exists" ); 1473 div.data("test", "success"); 1474 equals( div.data("test"), "success", "Check for added data" ); 1475 div.data("test", "overwritten"); 1476 equals( div.data("test"), "overwritten", "Check for overwritten data" ); 1477 div.data("test", undefined); 1478 equals( div.data("test"), "overwritten", "Check that data wasn't removed"); 1479 div.data("test", null); 1480 ok( div.data("test") === null, "Check for null data"); 1481 1482 div.data("test", "overwritten"); 1483 var hits = {test:0}, gets = {test:0}; 1484 1485 div 1486 .bind("setData",function(e,key,value){ hits[key] += value; }) 1487 .bind("setData.foo",function(e,key,value){ hits[key] += value; }) 1488 .bind("getData",function(e,key){ gets[key] += 1; }) 1489 .bind("getData.foo",function(e,key){ gets[key] += 3; }); 1490 1491 div.data("test.foo", 2); 1492 equals( div.data("test"), "overwritten", "Check for original data" ); 1493 equals( div.data("test.foo"), 2, "Check for namespaced data" ); 1494 equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" ); 1495 equals( hits.test, 2, "Check triggered setter functions" ); 1496 equals( gets.test, 5, "Check triggered getter functions" ); 1497 1498 hits.test = 0; 1499 gets.test = 0; 1500 1501 div.data("test", 1); 1502 equals( div.data("test"), 1, "Check for original data" ); 1503 equals( div.data("test.foo"), 2, "Check for namespaced data" ); 1504 equals( div.data("test.bar"), 1, "Check for unmatched namespace" ); 1505 equals( hits.test, 1, "Check triggered setter functions" ); 1506 equals( gets.test, 5, "Check triggered getter functions" ); 1507 1508 hits.test = 0; 1509 gets.test = 0; 1510 1511 div 1512 .bind("getData",function(e,key){ return key + "root"; }) 1513 .bind("getData.foo",function(e,key){ return key + "foo"; }); 1514 1515 equals( div.data("test"), "testroot", "Check for original data" ); 1516 equals( div.data("test.foo"), "testfoo", "Check for namespaced data" ); 1517 equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" ); 1518 }); 1519 1520 test("$.removeData", function() { 1521 expect(1); 1522 var div = $("#foo")[0]; 1523 jQuery.data(div, "test", "testing"); 1524 jQuery.removeData(div, "test"); 1525 equals( jQuery.data(div, "test"), undefined, "Check removal of data" ); 1526 }); 1527 1528 test(".removeData()", function() { 1529 expect(6); 1530 var div = $("#foo"); 1531 div.data("test", "testing"); 1532 div.removeData("test"); 1533 equals( div.data("test"), undefined, "Check removal of data" ); 1534 1535 div.data("test", "testing"); 1536 div.data("test.foo", "testing2"); 1537 div.removeData("test.bar"); 1538 equals( div.data("test.foo"), "testing2", "Make sure data is intact" ); 1539 equals( div.data("test"), "testing", "Make sure data is intact" ); 1540 1541 div.removeData("test"); 1542 equals( div.data("test.foo"), "testing2", "Make sure data is intact" ); 1543 equals( div.data("test"), undefined, "Make sure data is intact" ); 1544 1545 div.removeData("test.foo"); 1546 equals( div.data("test.foo"), undefined, "Make sure data is intact" ); 1547 }); 1548 1549 test("remove()", function() { 1550 expect(6); 1551 $("#ap").children().remove(); 1552 ok( $("#ap").text().length > 10, "Check text is not removed" ); 1553 equals( $("#ap").children().length, 0, "Check remove" ); 1554 1555 reset(); 1556 $("#ap").children().remove("a"); 1557 ok( $("#ap").text().length > 10, "Check text is not removed" ); 1558 equals( $("#ap").children().length, 1, "Check filtered remove" ); 1559 1560 // using contents will get comments regular, text, and comment nodes 1561 equals( $("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" ); 1562 $("#nonnodes").contents().remove(); 1563 equals( $("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" ); 1564 }); 1565 1566 test("empty()", function() { 1567 expect(3); 1568 equals( $("#ap").children().empty().text().length, 0, "Check text is removed" ); 1569 equals( $("#ap").children().length, 4, "Check elements are not removed" ); 1570 1571 // using contents will get comments regular, text, and comment nodes 1572 var j = $("#nonnodes").contents(); 1573 j.empty(); 1574 equals( j.html(), "", "Check node,textnode,comment empty works" ); 1575 }); 1576 1577 test("slice()", function() { 1578 expect(5); 1579 isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" ); 1580 isSet( $("#ap a").slice(1), q("groups", "anchor1", "mark"), "slice(1)" ); 1581 isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" ); 1582 isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" ); 1583 1584 isSet( $("#ap a").eq(1), q("groups"), "eq(1)" ); 1585 }); 1586 1587 test("map()", function() { 1588 expect(2);//expect(6); 1589 1590 isSet( 1591 $("#ap").map(function(){ 1592 return $(this).find("a").get(); 1593 }), 1594 q("google", "groups", "anchor1", "mark"), 1595 "Array Map" 1596 ); 1597 1598 isSet( 1599 $("#ap > a").map(function(){ 1600 return this.parentNode; 1601 }), 1602 q("ap","ap","ap"), 1603 "Single Map" 1604 ); 1605 1606 return;//these haven't been accepted yet 1607 1608 //for #2616 1609 var keys = $.map( {a:1,b:2}, function( v, k ){ 1610 return k; 1611 }, [ ] ); 1612 1613 equals( keys.join(""), "ab", "Map the keys from a hash to an array" ); 1614 1615 var values = $.map( {a:1,b:2}, function( v, k ){ 1616 return v; 1617 }, [ ] ); 1618 1619 equals( values.join(""), "12", "Map the values from a hash to an array" ); 1620 1621 var scripts = document.getElementsByTagName("script"); 1622 var mapped = $.map( scripts, function( v, k ){ 1623 return v; 1624 }, {length:0} ); 1625 1626 equals( mapped.length, scripts.length, "Map an array(-like) to a hash" ); 1627 1628 var flat = $.map( Array(4), function( v, k ){ 1629 return k % 2 ? k : [k,k,k];//try mixing array and regular returns 1630 }); 1631 1632 equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" ); 1633 }); 1634 1635 test("contents()", function() { 1636 expect(12); 1637 equals( $("#ap").contents().length, 9, "Check element contents" ); 1638 ok( $("#iframe").contents()[0], "Check existance of IFrame document" ); 1639 var ibody = $("#loadediframe").contents()[0].body; 1640 ok( ibody, "Check existance of IFrame body" ); 1641 1642 equals( $("span", ibody).text(), "span text", "Find span in IFrame and check its text" ); 1643 1644 $(ibody).append("<div>init text</div>"); 1645 equals( $("div", ibody).length, 2, "Check the original div and the new div are in IFrame" ); 1646 1647 equals( $("div:last", ibody).text(), "init text", "Add text to div in IFrame" ); 1648 1649 $("div:last", ibody).text("div text"); 1650 equals( $("div:last", ibody).text(), "div text", "Add text to div in IFrame" ); 1651 1652 $("div:last", ibody).remove(); 1653 equals( $("div", ibody).length, 1, "Delete the div and check only one div left in IFrame" ); 1654 1655 equals( $("div", ibody).text(), "span text", "Make sure the correct div is still left after deletion in IFrame" ); 1656 1657 $("<table/>", ibody).append("<tr><td>cell</td></tr>").appendTo(ibody); 1658 $("table", ibody).remove(); 1659 equals( $("div", ibody).length, 1, "Check for JS error on add and delete of a table in IFrame" ); 1660 1661 // using contents will get comments regular, text, and comment nodes 1662 var c = $("#nonnodes").contents().contents(); 1663 equals( c.length, 1, "Check node,textnode,comment contents is just one" ); 1664 equals( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" ); 1665 }); 1666 1667 test("$.makeArray", function(){ 1668 expect(15); 1669 1670 equals( $.makeArray($('html>*'))[0].nodeName, "HEAD", "Pass makeArray a jQuery object" ); 1671 1672 equals( $.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" ); 1673 1674 equals( (function(){ return $.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" ); 1675 1676 equals( $.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" ); 1677 1678 equals( $.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" ); 1679 1680 equals( $.makeArray( 0 )[0], 0 , "Pass makeArray a number" ); 1681 1682 equals( $.makeArray( "foo" )[0], "foo", "Pass makeArray a string" ); 1683 1684 equals( $.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" ); 1685 1686 equals( $.makeArray( document.createElement("div") )[0].nodeName, "DIV", "Pass makeArray a single node" ); 1687 1688 equals( $.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" ); 1689 1690 equals( $.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "HEAD", "Pass makeArray a childNodes array" ); 1691 1692 //function, is tricky as it has length 1693 equals( $.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" ); 1694 //window, also has length 1695 equals( $.makeArray(window)[0], window, "Pass makeArray the window" ); 1696 1697 equals( $.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" ); 1698 1699 ok( $.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" ); 1700 });