3 test("Basic requirements", function() {
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" );
14 test("$()", function() {
17 var main = $("#main");
18 isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
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()" );
29 /* // Disabled until we add this functionality in
32 $("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
36 ok( pass, "$('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
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" );
46 test("browser", function() {
50 "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0",
51 "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",
53 * "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",
55 //Browsers with Gecko engine
57 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915" : "1.7.12",
59 "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",
61 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20070321 Netscape/8.1.3" : "1.7.5",
63 "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",
65 "Opera/9.20 (X11; Linux x86_64; U; en)": "9.20",
66 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.20" : "9.20",
67 "Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20",
69 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "418.9",
70 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" : "418.8",
71 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.8",
72 //Other user agent string
73 "Other browser's user agent 1.0":null
75 for (var i in browsers) {
76 var v = i.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ); // RegEx from Core jQuery.browser.version check
77 version = v ? v[1] : null;
78 equals( version, browsers[i], "Checking UA string" );
82 test("noConflict", function() {
86 var newjQuery = jQuery.noConflict();
88 ok( newjQuery == old, "noConflict returned the jQuery object" );
89 ok( jQuery == old, "Make sure jQuery wasn't touched." );
90 ok( $ == "$", "Make sure $ was reverted." );
94 newjQuery = jQuery.noConflict(true);
96 ok( newjQuery == old, "noConflict returned the jQuery object" );
97 ok( jQuery == "jQuery", "Make sure jQuery was reverted." );
98 ok( $ == "$", "Make sure $ was reverted." );
103 test("isFunction", function() {
106 // Make sure that false values return false
107 ok( !jQuery.isFunction(), "No Value" );
108 ok( !jQuery.isFunction( null ), "null Value" );
109 ok( !jQuery.isFunction( undefined ), "undefined Value" );
110 ok( !jQuery.isFunction( "" ), "Empty String Value" );
111 ok( !jQuery.isFunction( 0 ), "0 Value" );
114 // Safari uses "(Internal Function)"
115 ok( jQuery.isFunction(String), "String Function" );
116 ok( jQuery.isFunction(Array), "Array Function" );
117 ok( jQuery.isFunction(Object), "Object Function" );
118 ok( jQuery.isFunction(Function), "Function Function" );
120 // When stringified, this could be misinterpreted
121 var mystr = "function";
122 ok( !jQuery.isFunction(mystr), "Function String" );
124 // When stringified, this could be misinterpreted
125 var myarr = [ "function" ];
126 ok( !jQuery.isFunction(myarr), "Function Array" );
128 // When stringified, this could be misinterpreted
129 var myfunction = { "function": "test" };
130 ok( !jQuery.isFunction(myfunction), "Function Object" );
132 // Make sure normal functions still work
133 var fn = function(){};
134 ok( jQuery.isFunction(fn), "Normal Function" );
136 var obj = document.createElement("object");
138 // Firefox says this is a function
139 ok( !jQuery.isFunction(obj), "Object Element" );
141 // IE says this is an object
142 ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
144 var nodes = document.body.childNodes;
146 // Safari says this is a function
147 ok( !jQuery.isFunction(nodes), "childNodes Property" );
149 var first = document.body.firstChild;
151 // Normal elements are reported ok everywhere
152 ok( !jQuery.isFunction(first), "A normal DOM Element" );
154 var input = document.createElement("input");
156 document.body.appendChild( input );
158 // IE says this is an object
159 ok( jQuery.isFunction(input.focus), "A default function property" );
161 document.body.removeChild( input );
163 var a = document.createElement("a");
164 a.href = "some-function";
165 document.body.appendChild( a );
167 // This serializes with the word 'function' in it
168 ok( !jQuery.isFunction(a), "Anchor Element" );
170 document.body.removeChild( a );
172 // Recursive function calls have lengths and array-like properties
173 function callme(callback){
174 function fn(response){
178 ok( jQuery.isFunction(fn), "Recursive Function Call" );
180 fn({ some: "data" });
184 callme(function(){});
190 test("$('html')", function() {
195 var s = $("<script>var foo='test';</script>")[0];
196 ok( s, "Creating a script" );
197 ok( !foo, "Make sure the script wasn't executed prematurely" );
199 ok( foo, "Executing a scripts contents in the right context" );
202 ok( $("<link rel='stylesheet'/>")[0], "Creating a link" );
206 var j = $("<span>hi</span> there <!-- mon ami -->");
207 ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
209 ok( !$("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
212 test("$('html', context)", function() {
215 var $div = $("<div/>");
216 var $span = $("<span/>", $div);
217 equals($span.length, 1, "Verify a span created with a div context works, #1763");
221 test("$(selector, xml).text(str) - Loaded via XML document", function() {
224 $.get('data/dashboard.xml', function(xml) {
225 // tests for #1419 where IE was a problem
226 equals( $("tab:first", xml).text(), "blabla", "Verify initial text correct" );
227 $("tab:first", xml).text("newtext");
228 equals( $("tab:first", xml).text(), "newtext", "Verify new text correct" );
234 test("length", function() {
236 ok( $("p").length == 6, "Get Number of Elements Found" );
239 test("size()", function() {
241 ok( $("p").size() == 6, "Get Number of Elements Found" );
244 test("get()", function() {
246 isSet( $("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
249 test("get(Number)", function() {
251 ok( $("p").get(0) == document.getElementById("firstp"), "Get A Single Element" );
254 test("add(String|Element|Array|undefined)", function() {
256 isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
257 isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
258 ok( $([]).add($("#form")[0].elements).length >= 13, "Check elements from array" );
260 // For the time being, we're discontinuing support for $(form.elements) since it's ambiguous in IE
261 // use $([]).add(form.elements) instead.
262 //equals( $([]).add($("#form")[0].elements).length, $($("#form")[0].elements).length, "Array in constructor must equals array in add()" );
264 var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
265 ok( x[0].id == "x1", "Check on-the-fly element1" );
266 ok( x[1].id == "x2", "Check on-the-fly element2" );
268 var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
269 ok( x[0].id == "x1", "Check on-the-fly element1" );
270 ok( x[1].id == "x2", "Check on-the-fly element2" );
273 equals( $([]).add(notDefined).length, 0, "Check that undefined adds nothing." );
276 test("each(Function)", function() {
279 div.each(function(){this.foo = 'zoo';});
281 for ( var i = 0; i < div.size(); i++ ) {
282 if ( div.get(i).foo != "zoo" ) pass = false;
284 ok( pass, "Execute a function, Relative" );
287 test("index(Object)", function() {
289 ok( $([window, document]).index(window) == 0, "Check for index of elements" );
290 ok( $([window, document]).index(document) == 1, "Check for index of elements" );
291 var inputElements = $('#radio1,#radio2,#check1,#check2');
292 ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
293 ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
294 ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
295 ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
296 ok( inputElements.index(window) == -1, "Check for not found index" );
297 ok( inputElements.index(document) == -1, "Check for not found index" );
300 test("attr(String)", function() {
302 ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
303 ok( $('#text1').attr('value', "Test2").attr('defaultValue') == "Test", 'Check for defaultValue attribute' );
304 ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
305 ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
306 ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
307 ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
308 ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
309 ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
310 ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
311 ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
312 ok( $('#name').attr('name') == "name", 'Check for name attribute' );
313 ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
314 ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
315 ok( $('#text1').attr('maxlength') == '30', 'Check for maxlength attribute' );
316 ok( $('#text1').attr('maxLength') == '30', 'Check for maxLength attribute' );
317 ok( $('#area1').attr('maxLength') == '30', 'Check for maxLength attribute' );
318 ok( $('#select2').attr('selectedIndex') == 3, 'Check for selectedIndex attribute' );
319 ok( $('#foo').attr('nodeName') == 'DIV', 'Check for nodeName attribute' );
320 ok( $('#foo').attr('tagName') == 'DIV', 'Check for tagName attribute' );
322 $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
323 ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
327 test("attr(String) in XML Files", function() {
330 $.get("data/dashboard.xml", function(xml) {
331 ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
332 ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
338 test("attr(String, Function)", function() {
340 ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
341 ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
344 test("attr(Hash)", function() {
347 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
348 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
350 ok( pass, "Set Multiple Attributes" );
353 test("attr(String, Object)", function() {
356 div.attr("foo", "bar");
358 for ( var i = 0; i < div.size(); i++ ) {
359 if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
361 ok( pass, "Set Attribute" );
363 ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
365 $("#name").attr('name', 'something');
366 ok( $("#name").attr('name') == 'something', 'Set name attribute' );
367 $("#check2").attr('checked', true);
368 ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
369 $("#check2").attr('checked', false);
370 ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
371 $("#text1").attr('readonly', true);
372 ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
373 $("#text1").attr('readonly', false);
374 ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
375 $("#name").attr('maxlength', '5');
376 ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' );
377 $("#name").attr('maxLength', '10');
378 ok( document.getElementById('name').maxLength == '10', 'Set maxlength attribute' );
381 $("#name").attr('someAttr', '0');
382 equals( $("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
383 $("#name").attr('someAttr', 0);
384 equals( $("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
385 $("#name").attr('someAttr', 1);
386 equals( $("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
388 // using contents will get comments regular, text, and comment nodes
389 var j = $("#nonnodes").contents();
391 j.attr("name", "attrvalue");
392 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
397 var type = $("#check2").attr('type');
400 $("#check2").attr('type','hidden');
404 ok( thrown, "Exception thrown when trying to change type property" );
405 equals( type, $("#check2").attr('type'), "Verify that you can't change the type of an input element" );
407 var check = document.createElement("input");
410 $(check).attr('type','checkbox');
414 ok( thrown, "Exception thrown when trying to change type property" );
415 equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
419 test("attr(String, Object) - Loaded via XML document", function() {
422 $.get('data/dashboard.xml', function(xml) {
424 $('tab', xml).each(function() {
425 titles.push($(this).attr('title'));
427 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
428 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
434 test("css(String|Hash)", function() {
437 ok( $('#main').css("display") == 'none', 'Check for css property "display"');
439 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
440 $('#foo').css({display: 'none'});
441 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
442 $('#foo').css({display: 'block'});
443 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
445 $('#floatTest').css({styleFloat: 'right'});
446 ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
447 $('#floatTest').css({cssFloat: 'left'});
448 ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
449 $('#floatTest').css({'float': 'right'});
450 ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
451 $('#floatTest').css({'font-size': '30px'});
452 ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
454 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
455 $('#foo').css({opacity: n});
456 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
457 $('#foo').css({opacity: parseFloat(n)});
458 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
460 $('#foo').css({opacity: ''});
461 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
464 test("css(String, Object)", function() {
466 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
467 $('#foo').css('display', 'none');
468 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
469 $('#foo').css('display', 'block');
470 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
472 $('#floatTest').css('styleFloat', 'left');
473 ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
474 $('#floatTest').css('cssFloat', 'right');
475 ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
476 $('#floatTest').css('float', 'left');
477 ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
478 $('#floatTest').css('font-size', '20px');
479 ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
481 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
482 $('#foo').css('opacity', n);
483 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
484 $('#foo').css('opacity', parseFloat(n));
485 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
487 $('#foo').css('opacity', '');
488 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
489 // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
490 if (jQuery.browser.msie) {
491 $('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
493 equals( $('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
495 // using contents will get comments regular, text, and comment nodes
496 var j = $("#nonnodes").contents();
497 j.css("padding-left", "1px");
498 equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
500 // opera sometimes doesn't update 'display' correctly, see #2037
501 $("#t2037")[0].innerHTML = $("#t2037")[0].innerHTML
502 equals( $("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
505 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
508 var $checkedtest = $("#checkedtest");
509 // IE6 was clearing "checked" in jQuery.css(elem, "height");
510 jQuery.css($checkedtest[0], "height");
511 ok( !! $(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
512 ok( ! $(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
513 ok( !! $(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
514 ok( ! $(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
517 test("width()", function() {
520 var $div = $("#nothiddendiv");
522 equals($div.width(), 30, "Test set to 30 correctly");
523 $div.width(-1); // handle negative numbers by ignoring #1599
524 equals($div.width(), 30, "Test negative width ignored");
525 $div.css("padding", "20px");
526 equals($div.width(), 30, "Test padding specified with pixels");
527 $div.css("border", "2px solid #fff");
528 equals($div.width(), 30, "Test border specified with pixels");
529 $div.css("padding", "2em");
530 equals($div.width(), 30, "Test padding specified with ems");
531 $div.css("border", "1em solid #fff");
532 equals($div.width(), 30, "Test border specified with ems");
533 $div.css("padding", "2%");
534 equals($div.width(), 30, "Test padding specified with percent");
536 equals($div.width(), 30, "Test hidden div");
538 $div.css({ display: "", border: "", padding: "" });
540 $("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" });
541 equals($("#nothiddendivchild").width(), 20, "Test child width with border and padding");
542 $("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" });
545 test("height()", function() {
548 var $div = $("#nothiddendiv");
550 equals($div.height(), 30, "Test set to 30 correctly");
551 $div.height(-1); // handle negative numbers by ignoring #1599
552 equals($div.height(), 30, "Test negative height ignored");
553 $div.css("padding", "20px");
554 equals($div.height(), 30, "Test padding specified with pixels");
555 $div.css("border", "2px solid #fff");
556 equals($div.height(), 30, "Test border specified with pixels");
557 $div.css("padding", "2em");
558 equals($div.height(), 30, "Test padding specified with ems");
559 $div.css("border", "1em solid #fff");
560 equals($div.height(), 30, "Test border specified with ems");
561 $div.css("padding", "2%");
562 equals($div.height(), 30, "Test padding specified with percent");
564 equals($div.height(), 30, "Test hidden div");
566 $div.css({ display: "", border: "", padding: "", height: "1px" });
569 test("text()", function() {
571 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
572 ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
575 test("wrap(String|Element)", function() {
577 var defaultText = 'Try them out:'
578 var result = $('#first').wrap('<div class="red"><span></span></div>').text();
579 ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
580 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
583 var defaultText = 'Try them out:'
584 var result = $('#first').wrap(document.getElementById('empty')).parent();
585 ok( result.is('ol'), 'Check for element wrapping' );
586 ok( result.text() == defaultText, 'Check for element wrapping' );
589 $('#check1').click(function() {
591 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
592 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
593 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
596 // using contents will get comments regular, text, and comment nodes
597 var j = $("#nonnodes").contents();
599 equals( $("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
600 equals( $("#nonnodes > i").text(), j.text() + j[1].nodeValue, "Check node,textnode,comment wraps doesn't hurt text" );
603 test("wrapAll(String|Element)", function() {
605 var prev = $("#first")[0].previousSibling;
606 var p = $("#first")[0].parentNode;
607 var result = $('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>');
608 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
609 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
610 ok( $('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
611 equals( $("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
612 equals( $("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
615 var prev = $("#first")[0].previousSibling;
616 var p = $("#first")[0].parentNode;
617 var result = $('#first,#firstp').wrapAll(document.getElementById('empty'));
618 equals( $("#first").parent()[0], $("#firstp").parent()[0], "Same Parent" );
619 equals( $("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
620 equals( $("#first").parent()[0].parentNode, p, "Correct Parent" );
623 test("wrapInner(String|Element)", function() {
625 var num = $("#first").children().length;
626 var result = $('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
627 equals( $("#first").children().length, 1, "Only one child" );
628 ok( $("#first").children().is(".red"), "Verify Right Element" );
629 equals( $("#first").children().children().children().length, num, "Verify Elements Intact" );
632 var num = $("#first").children().length;
633 var result = $('#first').wrapInner(document.getElementById('empty'));
634 equals( $("#first").children().length, 1, "Only one child" );
635 ok( $("#first").children().is("#empty"), "Verify Right Element" );
636 equals( $("#first").children().children().length, num, "Verify Elements Intact" );
639 test("append(String|Element|Array<Element>|jQuery)", function() {
641 var defaultText = 'Try them out:'
642 var result = $('#first').append('<b>buga</b>');
643 ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
644 ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
647 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
648 $('#sap').append(document.getElementById('first'));
649 ok( expected == $('#sap').text(), "Check for appending of element" );
652 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
653 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
654 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
657 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
658 $('#sap').append($("#first, #yahoo"));
659 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
662 $("#sap").append( 5 );
663 ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
666 $("#sap").append( " text with spaces " );
667 ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
670 ok( $("#sap").append([]), "Check for appending an empty array." );
671 ok( $("#sap").append(""), "Check for appending an empty string." );
672 ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
675 $("#sap").append(document.getElementById('form'));
676 ok( $("#sap>form").size() == 1, "Check for appending a form" ); // Bug #910
681 $( $("#iframe")[0].contentWindow.document.body ).append("<div>test</div>");
686 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
689 $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
690 t( 'Append legend', '#legend', ['legend'] );
693 $('#select1').append('<OPTION>Test</OPTION>');
694 ok( $('#select1 option:last').text() == "Test", "Appending <OPTION> (all caps)" );
696 $('#table').append('<colgroup></colgroup>');
697 ok( $('#table colgroup').length, "Append colgroup" );
699 $('#table colgroup').append('<col/>');
700 ok( $('#table colgroup col').length, "Append col" );
703 $('#table').append('<caption></caption>');
704 ok( $('#table caption').length, "Append caption" );
708 .append('<select id="appendSelect1"></select>')
709 .append('<select id="appendSelect2"><option>Test</option></select>');
711 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
713 // using contents will get comments regular, text, and comment nodes
714 var j = $("#nonnodes").contents();
715 var d = $("<div/>").appendTo("#nonnodes").append(j);
716 equals( $("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
717 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
718 d.contents().appendTo("#nonnodes");
720 ok( $("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
723 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
725 var defaultText = 'Try them out:'
726 $('<b>buga</b>').appendTo('#first');
727 ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
728 ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
731 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
732 $(document.getElementById('first')).appendTo('#sap');
733 ok( expected == $('#sap').text(), "Check for appending of element" );
736 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
737 $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
738 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
741 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
742 $("#first, #yahoo").appendTo('#sap');
743 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
746 $('#select1').appendTo('#foo');
747 t( 'Append select', '#foo select', ['select1'] );
750 test("prepend(String|Element|Array<Element>|jQuery)", function() {
752 var defaultText = 'Try them out:'
753 var result = $('#first').prepend('<b>buga</b>');
754 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
755 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
758 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
759 $('#sap').prepend(document.getElementById('first'));
760 ok( expected == $('#sap').text(), "Check for prepending of element" );
763 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
764 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
765 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
768 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
769 $('#sap').prepend($("#first, #yahoo"));
770 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
773 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
775 var defaultText = 'Try them out:'
776 $('<b>buga</b>').prependTo('#first');
777 ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
778 ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
781 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
782 $(document.getElementById('first')).prependTo('#sap');
783 ok( expected == $('#sap').text(), "Check for prepending of element" );
786 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
787 $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
788 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
791 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
792 $("#yahoo, #first").prependTo('#sap');
793 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
796 $('<select id="prependSelect1"></select>').prependTo('form:last');
797 $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
799 t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
802 test("before(String|Element|Array<Element>|jQuery)", function() {
804 var expected = 'This is a normal link: bugaYahoo';
805 $('#yahoo').before('<b>buga</b>');
806 ok( expected == $('#en').text(), 'Insert String before' );
809 expected = "This is a normal link: Try them out:Yahoo";
810 $('#yahoo').before(document.getElementById('first'));
811 ok( expected == $('#en').text(), "Insert element before" );
814 expected = "This is a normal link: Try them out:diveintomarkYahoo";
815 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
816 ok( expected == $('#en').text(), "Insert array of elements before" );
819 expected = "This is a normal link: Try them out:diveintomarkYahoo";
820 $('#yahoo').before($("#first, #mark"));
821 ok( expected == $('#en').text(), "Insert jQuery before" );
824 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
826 var expected = 'This is a normal link: bugaYahoo';
827 $('<b>buga</b>').insertBefore('#yahoo');
828 ok( expected == $('#en').text(), 'Insert String before' );
831 expected = "This is a normal link: Try them out:Yahoo";
832 $(document.getElementById('first')).insertBefore('#yahoo');
833 ok( expected == $('#en').text(), "Insert element before" );
836 expected = "This is a normal link: Try them out:diveintomarkYahoo";
837 $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
838 ok( expected == $('#en').text(), "Insert array of elements before" );
841 expected = "This is a normal link: Try them out:diveintomarkYahoo";
842 $("#first, #mark").insertBefore('#yahoo');
843 ok( expected == $('#en').text(), "Insert jQuery before" );
846 test("after(String|Element|Array<Element>|jQuery)", function() {
848 var expected = 'This is a normal link: Yahoobuga';
849 $('#yahoo').after('<b>buga</b>');
850 ok( expected == $('#en').text(), 'Insert String after' );
853 expected = "This is a normal link: YahooTry them out:";
854 $('#yahoo').after(document.getElementById('first'));
855 ok( expected == $('#en').text(), "Insert element after" );
858 expected = "This is a normal link: YahooTry them out:diveintomark";
859 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
860 ok( expected == $('#en').text(), "Insert array of elements after" );
863 expected = "This is a normal link: YahooTry them out:diveintomark";
864 $('#yahoo').after($("#first, #mark"));
865 ok( expected == $('#en').text(), "Insert jQuery after" );
868 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
870 var expected = 'This is a normal link: Yahoobuga';
871 $('<b>buga</b>').insertAfter('#yahoo');
872 ok( expected == $('#en').text(), 'Insert String after' );
875 expected = "This is a normal link: YahooTry them out:";
876 $(document.getElementById('first')).insertAfter('#yahoo');
877 ok( expected == $('#en').text(), "Insert element after" );
880 expected = "This is a normal link: YahooTry them out:diveintomark";
881 $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
882 ok( expected == $('#en').text(), "Insert array of elements after" );
885 expected = "This is a normal link: YahooTry them out:diveintomark";
886 $("#mark, #first").insertAfter('#yahoo');
887 ok( expected == $('#en').text(), "Insert jQuery after" );
890 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
892 $('#yahoo').replaceWith('<b id="replace">buga</b>');
893 ok( $("#replace")[0], 'Replace element with string' );
894 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
897 $('#yahoo').replaceWith(document.getElementById('first'));
898 ok( $("#first")[0], 'Replace element with element' );
899 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
902 $('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
903 ok( $("#first")[0], 'Replace element with array of elements' );
904 ok( $("#mark")[0], 'Replace element with array of elements' );
905 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
908 $('#yahoo').replaceWith($("#first, #mark"));
909 ok( $("#first")[0], 'Replace element with set of elements' );
910 ok( $("#mark")[0], 'Replace element with set of elements' );
911 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
914 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
916 $('<b id="replace">buga</b>').replaceAll("#yahoo");
917 ok( $("#replace")[0], 'Replace element with string' );
918 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
921 $(document.getElementById('first')).replaceAll("#yahoo");
922 ok( $("#first")[0], 'Replace element with element' );
923 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
926 $([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
927 ok( $("#first")[0], 'Replace element with array of elements' );
928 ok( $("#mark")[0], 'Replace element with array of elements' );
929 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
932 $("#first, #mark").replaceAll("#yahoo");
933 ok( $("#first")[0], 'Replace element with set of elements' );
934 ok( $("#mark")[0], 'Replace element with set of elements' );
935 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
938 test("end()", function() {
940 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
941 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
945 ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
948 test("find(String)", function() {
950 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
952 // using contents will get comments regular, text, and comment nodes
953 var j = $("#nonnodes").contents();
954 equals( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" );
957 test("clone()", function() {
959 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
960 var clone = $('#yahoo').clone();
961 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
962 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
963 // using contents will get comments regular, text, and comment nodes
964 var cl = $("#nonnodes").contents().clone();
965 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
969 test("clone() on XML nodes", function() {
972 $.get("data/dashboard.xml", function (xml) {
973 var root = $(xml.documentElement).clone();
974 $("tab:first", xml).text("origval");
975 $("tab:first", root).text("cloneval");
976 equals($("tab:first", xml).text(), "origval", "Check original XML node was correctly set");
977 equals($("tab:first", root).text(), "cloneval", "Check cloned XML node was correctly set");
983 test("is(String)", function() {
985 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
986 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
987 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
988 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
989 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
990 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
991 ok( $('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
992 ok( !$('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
993 ok( $('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
994 ok( !$('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
995 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
996 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
997 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
998 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
999 ok( $('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );
1000 ok( !$('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );
1001 ok( $('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );
1002 ok( !$('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
1003 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
1004 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
1005 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
1006 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
1008 // test is() with comma-seperated expressions
1009 ok( $('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
1010 ok( $('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
1011 ok( $('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
1012 ok( $('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
1015 test("$.extend(Object, Object)", function() {
1018 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
1019 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
1020 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
1021 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
1022 deep1 = { foo: { bar: true } },
1023 deep1copy = { foo: { bar: true } },
1024 deep2 = { foo: { baz: true }, foo2: document },
1025 deep2copy = { foo: { baz: true }, foo2: document },
1026 deepmerged = { foo: { bar: true, baz: true }, foo2: document };
1028 jQuery.extend(settings, options);
1029 isObj( settings, merged, "Check if extended: settings must be extended" );
1030 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
1032 jQuery.extend(settings, null, options);
1033 isObj( settings, merged, "Check if extended: settings must be extended" );
1034 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
1036 jQuery.extend(true, deep1, deep2);
1037 isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
1038 isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
1039 equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
1042 var recursive = { foo:target, bar:5 };
1043 jQuery.extend(true, target, recursive);
1044 isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
1046 var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
1047 ok( ret.foo.length == 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
1049 var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
1050 ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
1052 var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
1053 ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
1055 var obj = { foo:null };
1056 jQuery.extend(true, obj, { foo:"notnull" } );
1057 equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
1060 jQuery.extend(func, { key: "value" } );
1061 equals( func.key, "value", "Verify a function can be extended" );
1063 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
1064 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
1065 options1 = { xnumber2: 1, xstring2: "x" },
1066 options1Copy = { xnumber2: 1, xstring2: "x" },
1067 options2 = { xstring2: "xx", xxx: "newstringx" },
1068 options2Copy = { xstring2: "xx", xxx: "newstringx" },
1069 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
1071 var settings = jQuery.extend({}, defaults, options1, options2);
1072 isObj( settings, merged2, "Check if extended: settings must be extended" );
1073 isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
1074 isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
1075 isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
1078 test("val()", function() {
1080 ok( $("#text1").val() == "Test", "Check for value of input element" );
1081 ok( !$("#text1").val() == "", "Check for value of input element" );
1082 // ticket #1714 this caused a JS error in IE
1083 ok( $("#first").val() == "", "Check a paragraph element to see if it has a value" );
1084 ok( $([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
1087 test("val(String)", function() {
1089 document.getElementById('text1').value = "bla";
1090 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
1091 $("#text1").val('test');
1092 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
1094 $("#select1").val("3");
1095 ok( $("#select1").val() == "3", "Check for modified (via val(String)) value of select element" );
1097 // using contents will get comments regular, text, and comment nodes
1098 var j = $("#nonnodes").contents();
1100 equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
1101 j.removeAttr("value");
1104 var scriptorder = 0;
1106 test("html(String)", function() {
1108 var div = $("#main > div");
1109 div.html("<b>test</b>");
1111 for ( var i = 0; i < div.size(); i++ ) {
1112 if ( div.get(i).childNodes.length != 1 ) pass = false;
1114 ok( pass, "Set HTML" );
1117 // using contents will get comments regular, text, and comment nodes
1118 var j = $("#nonnodes").contents();
1119 j.html("<b>bold</b>");
1120 equals( j.html().toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
1122 $("#main").html("<select/>");
1123 $("#main select").html("<option>O1</option><option selected='selected'>O2</option><option>O3</option>");
1124 equals( $("#main select").val(), "O2", "Selected option correct" );
1128 $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>');
1130 $("#main").html('foo <form><script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
1132 // 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
1133 $("#main").html("<script>ok(scriptorder++ == 0, 'Script is executed in order');ok($('#scriptorder').length == 1,'Execute after html (even though appears before)')<\/script><span id='scriptorder'><script>ok(scriptorder++ == 1, 'Script (nested) is executed in order');ok($('#scriptorder').length == 1,'Execute after html')<\/script></span><script>ok(scriptorder++ == 2, 'Script (unnested) is executed in order');ok($('#scriptorder').length == 1,'Execute after html')<\/script>");
1135 setTimeout( start, 100 );
1138 test("filter()", function() {
1140 isSet( $("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
1141 isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
1142 isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
1143 isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
1145 // using contents will get comments regular, text, and comment nodes
1146 var j = $("#nonnodes").contents();
1147 equals( j.filter("span").length, 1, "Check node,textnode,comment to filter the one span" );
1148 equals( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" );
1151 test("not()", function() {
1153 ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
1154 ok( $("#main > p#ap > a").not(document.getElementById("google")).length == 2, "not(DOMElement)" );
1155 isSet( $("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );
1156 isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
1157 isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
1158 ok( $("p").not(document.getElementsByTagName("p")).length == 0, "not(Array-like DOM collection)" );
1159 isSet( $("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d" ), "not('complex selector')");
1161 var selects = $("#form select");
1162 isSet( selects.not( selects[1] ), q("select1", "select3"), "filter out DOM element");
1165 test("andSelf()", function() {
1167 isSet( $("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
1168 isSet( $("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
1169 isSet( $("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" );
1170 isSet( $("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" );
1173 test("siblings([String])", function() {
1175 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
1176 isSet( $("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
1177 isSet( $("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
1178 isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" );
1179 isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );
1182 test("children([String])", function() {
1184 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
1185 isSet( $("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
1186 isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
1189 test("parent([String])", function() {
1191 ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
1192 ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
1193 ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
1194 ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
1195 isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
1198 test("parents([String])", function() {
1200 ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
1201 ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
1202 ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
1203 isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
1204 isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );
1207 test("next([String])", function() {
1209 ok( $("#ap").next()[0].id == "foo", "Simple next check" );
1210 ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
1211 ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
1212 ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
1215 test("prev([String])", function() {
1217 ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
1218 ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
1219 ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
1220 ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
1223 test("show()", function() {
1225 var pass = true, div = $("div");
1226 div.show().each(function(){
1227 if ( this.style.display == "none" ) pass = false;
1231 $("#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>');
1239 "table" : $.browser.msie ? "block" : "table",
1240 "thead" : $.browser.msie ? "block" : "table-header-group",
1241 "tbody" : $.browser.msie ? "block" : "table-row-group",
1242 "tr" : $.browser.msie ? "block" : "table-row",
1243 "th" : $.browser.msie ? "block" : "table-cell",
1244 "td" : $.browser.msie ? "block" : "table-cell",
1246 "li" : $.browser.msie ? "block" : "list-item"
1249 $.each(test, function(selector, expected) {
1250 var elem = $(selector, "#show-tests").show();
1251 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
1255 test("addClass(String)", function() {
1258 div.addClass("test");
1260 for ( var i = 0; i < div.size(); i++ ) {
1261 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
1263 ok( pass, "Add Class" );
1265 // using contents will get regular, text, and comment nodes
1266 var j = $("#nonnodes").contents();
1268 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
1271 test("removeClass(String) - simple", function() {
1273 var div = $("div").addClass("test").removeClass("test"),
1275 for ( var i = 0; i < div.size(); i++ ) {
1276 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
1278 ok( pass, "Remove Class" );
1281 var div = $("div").addClass("test").addClass("foo").addClass("bar");
1282 div.removeClass("test").removeClass("bar").removeClass("foo");
1284 for ( var i = 0; i < div.size(); i++ ) {
1285 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
1287 ok( pass, "Remove multiple classes" );
1290 var div = $("div:eq(0)").addClass("test").removeClass("");
1291 ok( div.is('.test'), "Empty string passed to removeClass" );
1293 // using contents will get regular, text, and comment nodes
1294 var j = $("#nonnodes").contents();
1295 j.removeClass("asdf");
1296 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
1299 test("toggleClass(String)", function() {
1301 var e = $("#firstp");
1302 ok( !e.is(".test"), "Assert class not present" );
1303 e.toggleClass("test");
1304 ok( e.is(".test"), "Assert class present" );
1305 e.toggleClass("test");
1306 ok( !e.is(".test"), "Assert class not present" );
1309 test("removeAttr(String", function() {
1311 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
1314 test("text(String)", function() {
1316 ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
1318 // using contents will get comments regular, text, and comment nodes
1319 var j = $("#nonnodes").contents();
1321 equals( $(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
1322 equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
1323 equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
1326 test("$.each(Object,Function)", function() {
1328 $.each( [0,1,2], function(i, n){
1329 ok( i == n, "Check array iteration" );
1332 $.each( [5,6,7], function(i, n){
1333 ok( i == n - 5, "Check array iteration" );
1336 $.each( { name: "name", lang: "lang" }, function(i, n){
1337 ok( i == n, "Check object iteration" );
1341 jQuery.each([1,2,3], function(i,v){ total += v; });
1342 ok( total == 6, "Looping over an array" );
1344 jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
1345 ok( total == 3, "Looping over an array, with break" );
1347 jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
1348 ok( total == 6, "Looping over an object" );
1350 jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
1351 ok( total == 3, "Looping over an object, with break" );
1354 test("$.prop", function() {
1356 var handle = function() { return this.id };
1357 ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
1358 ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
1361 test("$.className", function() {
1363 var x = $("<p>Hi</p>")[0];
1364 var c = $.className;
1366 ok( x.className == "hi", "Check single added class" );
1367 c.add(x, "foo bar");
1368 ok( x.className == "hi foo bar", "Check more added classes" );
1370 ok( x.className == "", "Remove all classes" );
1371 c.add(x, "hi foo bar");
1373 ok( x.className == "hi bar", "Check removal of one class" );
1374 ok( c.has(x, "hi"), "Check has1" );
1375 ok( c.has(x, "bar"), "Check has2" );
1378 test("$.data", function() {
1380 var div = $("#foo")[0];
1381 ok( jQuery.data(div, "test") == undefined, "Check for no data exists" );
1382 jQuery.data(div, "test", "success");
1383 ok( jQuery.data(div, "test") == "success", "Check for added data" );
1384 jQuery.data(div, "test", "overwritten");
1385 ok( jQuery.data(div, "test") == "overwritten", "Check for overwritten data" );
1388 test("$.removeData", function() {
1390 var div = $("#foo")[0];
1391 jQuery.data(div, "test", "testing");
1392 jQuery.removeData(div, "test");
1393 ok( jQuery.data(div, "test") == undefined, "Check removal of data" );
1396 test("remove()", function() {
1398 $("#ap").children().remove();
1399 ok( $("#ap").text().length > 10, "Check text is not removed" );
1400 ok( $("#ap").children().length == 0, "Check remove" );
1403 $("#ap").children().remove("a");
1404 ok( $("#ap").text().length > 10, "Check text is not removed" );
1405 ok( $("#ap").children().length == 1, "Check filtered remove" );
1407 // using contents will get comments regular, text, and comment nodes
1408 equals( $("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
1409 $("#nonnodes").contents().remove();
1410 equals( $("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1413 test("empty()", function() {
1415 ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
1416 ok( $("#ap").children().length == 4, "Check elements are not removed" );
1418 // using contents will get comments regular, text, and comment nodes
1419 var j = $("#nonnodes").contents();
1421 equals( j.html(), "", "Check node,textnode,comment empty works" );
1424 test("slice()", function() {
1426 isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" );
1427 isSet( $("#ap a").slice(1), q("groups", "anchor1", "mark"), "slice(1)" );
1428 isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
1429 isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" );
1431 isSet( $("#ap a").eq(1), q("groups"), "eq(1)" );
1434 test("map()", function() {
1438 $("#ap").map(function(){
1439 return $(this).find("a").get();
1441 q("google", "groups", "anchor1", "mark"),
1446 $("#ap > a").map(function(){
1447 return this.parentNode;
1454 test("contents()", function() {
1456 equals( $("#ap").contents().length, 9, "Check element contents" );
1457 ok( $("#iframe").contents()[0], "Check existance of IFrame document" );
1458 var ibody = $("#loadediframe").contents()[0].body;
1459 ok( ibody, "Check existance of IFrame body" );
1461 equals( $("span", ibody).text(), "span text", "Find span in IFrame and check its text" );
1463 $(ibody).append("<div>init text</div>");
1464 equals( $("div", ibody).length, 2, "Check the original div and the new div are in IFrame" );
1466 equals( $("div:last", ibody).text(), "init text", "Add text to div in IFrame" );
1468 $("div:last", ibody).text("div text");
1469 equals( $("div:last", ibody).text(), "div text", "Add text to div in IFrame" );
1471 $("div:last", ibody).remove();
1472 equals( $("div", ibody).length, 1, "Delete the div and check only one div left in IFrame" );
1474 equals( $("div", ibody).text(), "span text", "Make sure the correct div is still left after deletion in IFrame" );
1476 $("<table/>", ibody).append("<tr><td>cell</td></tr>").appendTo(ibody);
1477 $("table", ibody).remove();
1478 equals( $("div", ibody).length, 1, "Check for JS error on add and delete of a table in IFrame" );
1480 // using contents will get comments regular, text, and comment nodes
1481 var c = $("#nonnodes").contents().contents();
1482 equals( c.length, 1, "Check node,textnode,comment contents is just one" );
1483 equals( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );