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("noConflict", function() {
50 var newjQuery = jQuery.noConflict();
52 ok( newjQuery == old, "noConflict returned the jQuery object" );
53 ok( jQuery == old, "Make sure jQuery wasn't touched." );
54 ok( $ == "$", "Make sure $ was reverted." );
58 newjQuery = jQuery.noConflict(true);
60 ok( newjQuery == old, "noConflict returned the jQuery object" );
61 ok( jQuery == "jQuery", "Make sure jQuery was reverted." );
62 ok( $ == "$", "Make sure $ was reverted." );
67 test("isFunction", function() {
70 // Make sure that false values return false
71 ok( !jQuery.isFunction(), "No Value" );
72 ok( !jQuery.isFunction( null ), "null Value" );
73 ok( !jQuery.isFunction( undefined ), "undefined Value" );
74 ok( !jQuery.isFunction( "" ), "Empty String Value" );
75 ok( !jQuery.isFunction( 0 ), "0 Value" );
78 // Safari uses "(Internal Function)"
79 ok( jQuery.isFunction(String), "String Function" );
80 ok( jQuery.isFunction(Array), "Array Function" );
81 ok( jQuery.isFunction(Object), "Object Function" );
82 ok( jQuery.isFunction(Function), "Function Function" );
84 // When stringified, this could be misinterpreted
85 var mystr = "function";
86 ok( !jQuery.isFunction(mystr), "Function String" );
88 // When stringified, this could be misinterpreted
89 var myarr = [ "function" ];
90 ok( !jQuery.isFunction(myarr), "Function Array" );
92 // When stringified, this could be misinterpreted
93 var myfunction = { "function": "test" };
94 ok( !jQuery.isFunction(myfunction), "Function Object" );
96 // Make sure normal functions still work
97 var fn = function(){};
98 ok( jQuery.isFunction(fn), "Normal Function" );
100 var obj = document.createElement("object");
102 // Firefox says this is a function
103 ok( !jQuery.isFunction(obj), "Object Element" );
105 // IE says this is an object
106 ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
108 var nodes = document.body.childNodes;
110 // Safari says this is a function
111 ok( !jQuery.isFunction(nodes), "childNodes Property" );
113 var first = document.body.firstChild;
115 // Normal elements are reported ok everywhere
116 ok( !jQuery.isFunction(first), "A normal DOM Element" );
118 var input = document.createElement("input");
120 document.body.appendChild( input );
122 // IE says this is an object
123 ok( jQuery.isFunction(input.focus), "A default function property" );
125 document.body.removeChild( input );
127 var a = document.createElement("a");
128 a.href = "some-function";
129 document.body.appendChild( a );
131 // This serializes with the word 'function' in it
132 ok( !jQuery.isFunction(a), "Anchor Element" );
134 document.body.removeChild( a );
136 // Recursive function calls have lengths and array-like properties
137 function callme(callback){
138 function fn(response){
142 ok( jQuery.isFunction(fn), "Recursive Function Call" );
144 fn({ some: "data" });
148 callme(function(){});
154 test("$('html')", function() {
159 var s = $("<script>var foo='test';</script>")[0];
160 ok( s, "Creating a script" );
161 ok( !foo, "Make sure the script wasn't executed prematurely" );
163 ok( foo, "Executing a scripts contents in the right context" );
166 ok( $("<link rel='stylesheet'/>")[0], "Creating a link" );
170 var j = $("<span>hi</span> there <!-- mon ami -->");
171 ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
173 ok( !$("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
176 test("$('html', context)", function() {
179 var $div = $("<div/>");
180 var $span = $("<span/>", $div);
181 equals($span.length, 1, "Verify a span created with a div context works, #1763");
184 test("$(selector, xml).text(str) - Loaded via XML document", function() {
187 $.get('data/dashboard.xml', function(xml) {
188 // tests for #1419 where IE was a problem
189 equals( $("tab:first", xml).text(), "blabla", "Verify initial text correct" );
190 $("tab:first", xml).text("newtext");
191 equals( $("tab:first", xml).text(), "newtext", "Verify new text correct" );
196 test("length", function() {
198 ok( $("p").length == 6, "Get Number of Elements Found" );
201 test("size()", function() {
203 ok( $("p").size() == 6, "Get Number of Elements Found" );
206 test("get()", function() {
208 isSet( $("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
211 test("get(Number)", function() {
213 ok( $("p").get(0) == document.getElementById("firstp"), "Get A Single Element" );
216 test("add(String|Element|Array|undefined)", function() {
218 isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
219 isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
220 ok( $([]).add($("#form")[0].elements).length >= 13, "Check elements from array" );
221 equals( $([]).add($("#form")[0].elements).length, $($("#form")[0].elements).length, "Array in constructor must equals array in add()" );
223 var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
224 ok( x[0].id == "x1", "Check on-the-fly element1" );
225 ok( x[1].id == "x2", "Check on-the-fly element2" );
227 var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
228 ok( x[0].id == "x1", "Check on-the-fly element1" );
229 ok( x[1].id == "x2", "Check on-the-fly element2" );
232 equals( $([]).add(notDefined).length, 0, "Check that undefined adds nothing." );
235 test("each(Function)", function() {
238 div.each(function(){this.foo = 'zoo';});
240 for ( var i = 0; i < div.size(); i++ ) {
241 if ( div.get(i).foo != "zoo" ) pass = false;
243 ok( pass, "Execute a function, Relative" );
246 test("index(Object)", function() {
248 ok( $([window, document]).index(window) == 0, "Check for index of elements" );
249 ok( $([window, document]).index(document) == 1, "Check for index of elements" );
250 var inputElements = $('#radio1,#radio2,#check1,#check2');
251 ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
252 ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
253 ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
254 ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
255 ok( inputElements.index(window) == -1, "Check for not found index" );
256 ok( inputElements.index(document) == -1, "Check for not found index" );
259 test("attr(String)", function() {
261 ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
262 ok( $('#text1').attr('value', "Test2").attr('defaultValue') == "Test", 'Check for defaultValue attribute' );
263 ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
264 ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
265 ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
266 ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
267 ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
268 ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
269 ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
270 ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
271 ok( $('#name').attr('name') == "name", 'Check for name attribute' );
272 ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
273 ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
274 ok( $('#text1').attr('maxlength') == '30', 'Check for maxlength attribute' );
275 ok( $('#text1').attr('maxLength') == '30', 'Check for maxLength attribute' );
276 ok( $('#area1').attr('maxLength') == '30', 'Check for maxLength attribute' );
277 ok( $('#select2').attr('selectedIndex') == 3, 'Check for selectedIndex attribute' );
278 ok( $('#foo').attr('nodeName') == 'DIV', 'Check for nodeName attribute' );
279 ok( $('#foo').attr('tagName') == 'DIV', 'Check for tagName attribute' );
281 $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
282 ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
286 test("attr(String) in XML Files", function() {
289 $.get("data/dashboard.xml", function(xml) {
290 ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
291 ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
297 test("attr(String, Function)", function() {
299 ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
300 ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
303 test("attr(Hash)", function() {
306 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
307 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
309 ok( pass, "Set Multiple Attributes" );
312 test("attr(String, Object)", function() {
315 div.attr("foo", "bar");
317 for ( var i = 0; i < div.size(); i++ ) {
318 if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
320 ok( pass, "Set Attribute" );
322 ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
324 $("#name").attr('name', 'something');
325 ok( $("#name").attr('name') == 'something', 'Set name attribute' );
326 $("#check2").attr('checked', true);
327 ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
328 $("#check2").attr('checked', false);
329 ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
330 $("#text1").attr('readonly', true);
331 ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
332 $("#text1").attr('readonly', false);
333 ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
334 $("#name").attr('maxlength', '5');
335 ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' );
336 $("#name").attr('maxLength', '10');
337 ok( document.getElementById('name').maxLength == '10', 'Set maxlength attribute' );
340 $("#name").attr('someAttr', '0');
341 equals( $("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
342 $("#name").attr('someAttr', 0);
343 equals( $("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
344 $("#name").attr('someAttr', 1);
345 equals( $("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
347 // using contents will get comments regular, text, and comment nodes
348 var j = $("#nonnodes").contents();
350 j.attr("name", "attrvalue");
351 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
356 var type = $("#check2").attr('type');
359 $("#check2").attr('type','hidden');
363 ok( thrown, "Exception thrown when trying to change type property" );
364 equals( type, $("#check2").attr('type'), "Verify that you can't change the type of an input element" );
366 var check = document.createElement("input");
369 $(check).attr('type','checkbox');
373 ok( thrown, "Exception thrown when trying to change type property" );
374 equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
378 test("attr(String, Object) - Loaded via XML document", function() {
381 $.get('data/dashboard.xml', function(xml) {
383 $('tab', xml).each(function() {
384 titles.push($(this).attr('title'));
386 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
387 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
393 test("css(String|Hash)", function() {
396 ok( $('#main').css("display") == 'none', 'Check for css property "display"');
398 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
399 $('#foo').css({display: 'none'});
400 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
401 $('#foo').css({display: 'block'});
402 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
404 $('#floatTest').css({styleFloat: 'right'});
405 ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
406 $('#floatTest').css({cssFloat: 'left'});
407 ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
408 $('#floatTest').css({'float': 'right'});
409 ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
410 $('#floatTest').css({'font-size': '30px'});
411 ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
413 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
414 $('#foo').css({opacity: n});
415 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
416 $('#foo').css({opacity: parseFloat(n)});
417 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
419 $('#foo').css({opacity: ''});
420 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
423 test("css(String, Object)", function() {
425 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
426 $('#foo').css('display', 'none');
427 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
428 $('#foo').css('display', 'block');
429 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
431 $('#floatTest').css('styleFloat', 'left');
432 ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
433 $('#floatTest').css('cssFloat', 'right');
434 ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
435 $('#floatTest').css('float', 'left');
436 ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
437 $('#floatTest').css('font-size', '20px');
438 ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
440 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
441 $('#foo').css('opacity', n);
442 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
443 $('#foo').css('opacity', parseFloat(n));
444 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
446 $('#foo').css('opacity', '');
447 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
448 // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
449 if (jQuery.browser.msie) {
450 $('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
452 equals( $('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
454 // using contents will get comments regular, text, and comment nodes
455 var j = $("#nonnodes").contents();
456 j.css("padding-left", "1px");
457 equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
459 // opera sometimes doesn't update 'display' correctly, see #2037
460 $("#t2037")[0].innerHTML = $("#t2037")[0].innerHTML
461 equals( $("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
464 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
467 var $checkedtest = $("#checkedtest");
468 // IE6 was clearing "checked" in jQuery.css(elem, "height");
469 jQuery.css($checkedtest[0], "height");
470 ok( !! $(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
471 ok( ! $(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
472 ok( !! $(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
473 ok( ! $(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
476 test("width()", function() {
479 var $div = $("#nothiddendiv");
481 equals($div.width(), 30, "Test set to 30 correctly");
482 $div.width(-1); // handle negative numbers by ignoring #1599
483 equals($div.width(), 30, "Test negative width ignored");
484 $div.css("padding", "20px");
485 equals($div.width(), 30, "Test padding specified with pixels");
486 $div.css("border", "2px solid #fff");
487 equals($div.width(), 30, "Test border specified with pixels");
488 $div.css("padding", "2em");
489 equals($div.width(), 30, "Test padding specified with ems");
490 $div.css("border", "1em solid #fff");
491 equals($div.width(), 30, "Test border specified with ems");
492 $div.css("padding", "2%");
493 equals($div.width(), 30, "Test padding specified with percent");
495 equals($div.width(), 30, "Test hidden div");
497 $div.css({ display: "", border: "", padding: "" });
499 $("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" });
500 equals($("#nothiddendivchild").width(), 20, "Test child width with border and padding");
501 $("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" });
504 test("height()", function() {
507 var $div = $("#nothiddendiv");
509 equals($div.height(), 30, "Test set to 30 correctly");
510 $div.height(-1); // handle negative numbers by ignoring #1599
511 equals($div.height(), 30, "Test negative height ignored");
512 $div.css("padding", "20px");
513 equals($div.height(), 30, "Test padding specified with pixels");
514 $div.css("border", "2px solid #fff");
515 equals($div.height(), 30, "Test border specified with pixels");
516 $div.css("padding", "2em");
517 equals($div.height(), 30, "Test padding specified with ems");
518 $div.css("border", "1em solid #fff");
519 equals($div.height(), 30, "Test border specified with ems");
520 $div.css("padding", "2%");
521 equals($div.height(), 30, "Test padding specified with percent");
523 equals($div.height(), 30, "Test hidden div");
525 $div.css({ display: "", border: "", padding: "", height: "1px" });
528 test("text()", function() {
530 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
531 ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
534 test("wrap(String|Element)", function() {
536 var defaultText = 'Try them out:'
537 var result = $('#first').wrap('<div class="red"><span></span></div>').text();
538 ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
539 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
542 var defaultText = 'Try them out:'
543 var result = $('#first').wrap(document.getElementById('empty')).parent();
544 ok( result.is('ol'), 'Check for element wrapping' );
545 ok( result.text() == defaultText, 'Check for element wrapping' );
548 $('#check1').click(function() {
550 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
551 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
552 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
555 // using contents will get comments regular, text, and comment nodes
556 var j = $("#nonnodes").contents();
558 equals( $("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
559 equals( $("#nonnodes > i").text(), j.text() + j[1].nodeValue, "Check node,textnode,comment wraps doesn't hurt text" );
562 test("wrapAll(String|Element)", function() {
564 var prev = $("#first")[0].previousSibling;
565 var p = $("#first")[0].parentNode;
566 var result = $('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>');
567 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
568 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
569 ok( $('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
570 equals( $("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
571 equals( $("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
574 var prev = $("#first")[0].previousSibling;
575 var p = $("#first")[0].parentNode;
576 var result = $('#first,#firstp').wrapAll(document.getElementById('empty'));
577 equals( $("#first").parent()[0], $("#firstp").parent()[0], "Same Parent" );
578 equals( $("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
579 equals( $("#first").parent()[0].parentNode, p, "Correct Parent" );
582 test("wrapInner(String|Element)", function() {
584 var num = $("#first").children().length;
585 var result = $('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
586 equals( $("#first").children().length, 1, "Only one child" );
587 ok( $("#first").children().is(".red"), "Verify Right Element" );
588 equals( $("#first").children().children().children().length, num, "Verify Elements Intact" );
591 var num = $("#first").children().length;
592 var result = $('#first').wrapInner(document.getElementById('empty'));
593 equals( $("#first").children().length, 1, "Only one child" );
594 ok( $("#first").children().is("#empty"), "Verify Right Element" );
595 equals( $("#first").children().children().length, num, "Verify Elements Intact" );
598 test("append(String|Element|Array<Element>|jQuery)", function() {
600 var defaultText = 'Try them out:'
601 var result = $('#first').append('<b>buga</b>');
602 ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
603 ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
606 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
607 $('#sap').append(document.getElementById('first'));
608 ok( expected == $('#sap').text(), "Check for appending of element" );
611 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
612 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
613 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
616 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
617 $('#sap').append($("#first, #yahoo"));
618 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
621 $("#sap").append( 5 );
622 ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
625 $("#sap").append( " text with spaces " );
626 ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
629 ok( $("#sap").append([]), "Check for appending an empty array." );
630 ok( $("#sap").append(""), "Check for appending an empty string." );
631 ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
634 $("#sap").append(document.getElementById('form'));
635 ok( $("#sap>form").size() == 1, "Check for appending a form" ); // Bug #910
640 $( $("#iframe")[0].contentWindow.document.body ).append("<div>test</div>");
645 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
648 $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
649 t( 'Append legend', '#legend', ['legend'] );
652 $('#select1').append('<OPTION>Test</OPTION>');
653 ok( $('#select1 option:last').text() == "Test", "Appending <OPTION> (all caps)" );
655 $('#table').append('<colgroup></colgroup>');
656 ok( $('#table colgroup').length, "Append colgroup" );
658 $('#table colgroup').append('<col/>');
659 ok( $('#table colgroup col').length, "Append col" );
662 $('#table').append('<caption></caption>');
663 ok( $('#table caption').length, "Append caption" );
667 .append('<select id="appendSelect1"></select>')
668 .append('<select id="appendSelect2"><option>Test</option></select>');
670 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
672 // using contents will get comments regular, text, and comment nodes
673 var j = $("#nonnodes").contents();
674 var d = $("<div/>").appendTo("#nonnodes").append(j);
675 equals( $("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
676 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
677 d.contents().appendTo("#nonnodes");
679 ok( $("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
682 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
684 var defaultText = 'Try them out:'
685 $('<b>buga</b>').appendTo('#first');
686 ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
687 ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
690 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
691 $(document.getElementById('first')).appendTo('#sap');
692 ok( expected == $('#sap').text(), "Check for appending of element" );
695 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
696 $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
697 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
700 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
701 $("#first, #yahoo").appendTo('#sap');
702 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
705 $('#select1').appendTo('#foo');
706 t( 'Append select', '#foo select', ['select1'] );
709 test("prepend(String|Element|Array<Element>|jQuery)", function() {
711 var defaultText = 'Try them out:'
712 var result = $('#first').prepend('<b>buga</b>');
713 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
714 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
717 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
718 $('#sap').prepend(document.getElementById('first'));
719 ok( expected == $('#sap').text(), "Check for prepending of element" );
722 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
723 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
724 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
727 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
728 $('#sap').prepend($("#first, #yahoo"));
729 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
732 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
734 var defaultText = 'Try them out:'
735 $('<b>buga</b>').prependTo('#first');
736 ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
737 ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
740 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
741 $(document.getElementById('first')).prependTo('#sap');
742 ok( expected == $('#sap').text(), "Check for prepending of element" );
745 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
746 $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
747 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
750 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
751 $("#yahoo, #first").prependTo('#sap');
752 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
755 $('<select id="prependSelect1"></select>').prependTo('form:last');
756 $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
758 t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
761 test("before(String|Element|Array<Element>|jQuery)", function() {
763 var expected = 'This is a normal link: bugaYahoo';
764 $('#yahoo').before('<b>buga</b>');
765 ok( expected == $('#en').text(), 'Insert String before' );
768 expected = "This is a normal link: Try them out:Yahoo";
769 $('#yahoo').before(document.getElementById('first'));
770 ok( expected == $('#en').text(), "Insert element before" );
773 expected = "This is a normal link: Try them out:diveintomarkYahoo";
774 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
775 ok( expected == $('#en').text(), "Insert array of elements before" );
778 expected = "This is a normal link: Try them out:diveintomarkYahoo";
779 $('#yahoo').before($("#first, #mark"));
780 ok( expected == $('#en').text(), "Insert jQuery before" );
783 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
785 var expected = 'This is a normal link: bugaYahoo';
786 $('<b>buga</b>').insertBefore('#yahoo');
787 ok( expected == $('#en').text(), 'Insert String before' );
790 expected = "This is a normal link: Try them out:Yahoo";
791 $(document.getElementById('first')).insertBefore('#yahoo');
792 ok( expected == $('#en').text(), "Insert element before" );
795 expected = "This is a normal link: Try them out:diveintomarkYahoo";
796 $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
797 ok( expected == $('#en').text(), "Insert array of elements before" );
800 expected = "This is a normal link: Try them out:diveintomarkYahoo";
801 $("#first, #mark").insertBefore('#yahoo');
802 ok( expected == $('#en').text(), "Insert jQuery before" );
805 test("after(String|Element|Array<Element>|jQuery)", function() {
807 var expected = 'This is a normal link: Yahoobuga';
808 $('#yahoo').after('<b>buga</b>');
809 ok( expected == $('#en').text(), 'Insert String after' );
812 expected = "This is a normal link: YahooTry them out:";
813 $('#yahoo').after(document.getElementById('first'));
814 ok( expected == $('#en').text(), "Insert element after" );
817 expected = "This is a normal link: YahooTry them out:diveintomark";
818 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
819 ok( expected == $('#en').text(), "Insert array of elements after" );
822 expected = "This is a normal link: YahooTry them out:diveintomark";
823 $('#yahoo').after($("#first, #mark"));
824 ok( expected == $('#en').text(), "Insert jQuery after" );
827 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
829 var expected = 'This is a normal link: Yahoobuga';
830 $('<b>buga</b>').insertAfter('#yahoo');
831 ok( expected == $('#en').text(), 'Insert String after' );
834 expected = "This is a normal link: YahooTry them out:";
835 $(document.getElementById('first')).insertAfter('#yahoo');
836 ok( expected == $('#en').text(), "Insert element after" );
839 expected = "This is a normal link: YahooTry them out:diveintomark";
840 $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
841 ok( expected == $('#en').text(), "Insert array of elements after" );
844 expected = "This is a normal link: YahooTry them out:diveintomark";
845 $("#mark, #first").insertAfter('#yahoo');
846 ok( expected == $('#en').text(), "Insert jQuery after" );
849 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
851 $('#yahoo').replaceWith('<b id="replace">buga</b>');
852 ok( $("#replace")[0], 'Replace element with string' );
853 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
856 $('#yahoo').replaceWith(document.getElementById('first'));
857 ok( $("#first")[0], 'Replace element with element' );
858 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
861 $('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
862 ok( $("#first")[0], 'Replace element with array of elements' );
863 ok( $("#mark")[0], 'Replace element with array of elements' );
864 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
867 $('#yahoo').replaceWith($("#first, #mark"));
868 ok( $("#first")[0], 'Replace element with set of elements' );
869 ok( $("#mark")[0], 'Replace element with set of elements' );
870 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
873 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
875 $('<b id="replace">buga</b>').replaceAll("#yahoo");
876 ok( $("#replace")[0], 'Replace element with string' );
877 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
880 $(document.getElementById('first')).replaceAll("#yahoo");
881 ok( $("#first")[0], 'Replace element with element' );
882 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
885 $([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
886 ok( $("#first")[0], 'Replace element with array of elements' );
887 ok( $("#mark")[0], 'Replace element with array of elements' );
888 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
891 $("#first, #mark").replaceAll("#yahoo");
892 ok( $("#first")[0], 'Replace element with set of elements' );
893 ok( $("#mark")[0], 'Replace element with set of elements' );
894 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
897 test("end()", function() {
899 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
900 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
904 ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
907 test("find(String)", function() {
909 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
911 // using contents will get comments regular, text, and comment nodes
912 var j = $("#nonnodes").contents();
913 equals( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" );
916 test("clone()", function() {
918 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
919 var clone = $('#yahoo').clone();
920 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
921 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
922 // using contents will get comments regular, text, and comment nodes
923 var cl = $("#nonnodes").contents().clone();
924 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
927 $.get("data/dashboard.xml", function (xml) {
928 var root = $(xml.documentElement).clone();
929 $("tab:first", xml).text("origval");
930 $("tab:first", root).text("cloneval");
931 equals($("tab:first", xml).text(), "origval", "Check original XML node was correctly set");
932 equals($("tab:first", root).text(), "cloneval", "Check cloned XML node was correctly set");
937 test("is(String)", function() {
939 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
940 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
941 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
942 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
943 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
944 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
945 ok( $('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
946 ok( !$('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
947 ok( $('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
948 ok( !$('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
949 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
950 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
951 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
952 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
953 ok( $('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );
954 ok( !$('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );
955 ok( $('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );
956 ok( !$('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
957 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
958 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
959 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
960 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
962 // test is() with comma-seperated expressions
963 ok( $('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
964 ok( $('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
965 ok( $('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
966 ok( $('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
969 test("$.extend(Object, Object)", function() {
972 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
973 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
974 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
975 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
976 deep1 = { foo: { bar: true } },
977 deep1copy = { foo: { bar: true } },
978 deep2 = { foo: { baz: true }, foo2: document },
979 deep2copy = { foo: { baz: true }, foo2: document },
980 deepmerged = { foo: { bar: true, baz: true }, foo2: document };
982 jQuery.extend(settings, options);
983 isObj( settings, merged, "Check if extended: settings must be extended" );
984 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
986 jQuery.extend(settings, null, options);
987 isObj( settings, merged, "Check if extended: settings must be extended" );
988 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
990 jQuery.extend(true, deep1, deep2);
991 isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
992 isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
993 equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
996 var recursive = { foo:target, bar:5 };
997 jQuery.extend(true, target, recursive);
998 isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
1000 var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
1001 ok( ret.foo.length == 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
1003 var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
1004 ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
1006 var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
1007 ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
1009 var obj = { foo:null };
1010 jQuery.extend(true, obj, { foo:"notnull" } );
1011 equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
1014 jQuery.extend(func, { key: "value" } );
1015 equals( func.key, "value", "Verify a function can be extended" );
1017 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
1018 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
1019 options1 = { xnumber2: 1, xstring2: "x" },
1020 options1Copy = { xnumber2: 1, xstring2: "x" },
1021 options2 = { xstring2: "xx", xxx: "newstringx" },
1022 options2Copy = { xstring2: "xx", xxx: "newstringx" },
1023 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
1025 var settings = jQuery.extend({}, defaults, options1, options2);
1026 isObj( settings, merged2, "Check if extended: settings must be extended" );
1027 isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
1028 isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
1029 isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
1032 test("val()", function() {
1034 ok( $("#text1").val() == "Test", "Check for value of input element" );
1035 ok( !$("#text1").val() == "", "Check for value of input element" );
1036 // ticket #1714 this caused a JS error in IE
1037 ok( $("#first").val() == "", "Check a paragraph element to see if it has a value" );
1038 ok( $([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
1041 test("val(String)", function() {
1043 document.getElementById('text1').value = "bla";
1044 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
1045 $("#text1").val('test');
1046 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
1048 $("#select1").val("3");
1049 ok( $("#select1").val() == "3", "Check for modified (via val(String)) value of select element" );
1051 // using contents will get comments regular, text, and comment nodes
1052 var j = $("#nonnodes").contents();
1054 equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
1055 j.removeAttr("value");
1058 var scriptorder = 0;
1060 test("html(String)", function() {
1062 var div = $("#main > div");
1063 div.html("<b>test</b>");
1065 for ( var i = 0; i < div.size(); i++ ) {
1066 if ( div.get(i).childNodes.length != 1 ) pass = false;
1068 ok( pass, "Set HTML" );
1071 // using contents will get comments regular, text, and comment nodes
1072 var j = $("#nonnodes").contents();
1073 j.html("<b>bold</b>");
1074 equals( j.html().toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
1076 $("#main").html("<select/>");
1077 $("#main select").html("<option>O1</option><option selected='selected'>O2</option><option>O3</option>");
1078 equals( $("#main select").val(), "O2", "Selected option correct" );
1082 $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>');
1084 $("#main").html('foo <form><script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
1086 // 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
1087 $("#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>");
1089 setTimeout( start, 100 );
1092 test("filter()", function() {
1094 isSet( $("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
1095 isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
1096 isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
1097 isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
1099 // using contents will get comments regular, text, and comment nodes
1100 var j = $("#nonnodes").contents();
1101 equals( j.filter("span").length, 1, "Check node,textnode,comment to filter the one span" );
1102 equals( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" );
1105 test("not()", function() {
1107 ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
1108 ok( $("#main > p#ap > a").not(document.getElementById("google")).length == 2, "not(DOMElement)" );
1109 isSet( $("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );
1110 isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
1111 isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
1112 ok( $("p").not(document.getElementsByTagName("p")).length == 0, "not(Array-like DOM collection)" );
1113 isSet( $("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d" ), "not('complex selector')");
1115 var selects = $("#form select");
1116 isSet( selects.not( selects[1] ), q("select1", "select3"), "filter out DOM element");
1119 test("andSelf()", function() {
1121 isSet( $("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
1122 isSet( $("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
1123 isSet( $("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" );
1124 isSet( $("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" );
1127 test("siblings([String])", function() {
1129 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
1130 isSet( $("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
1131 isSet( $("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
1132 isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" );
1133 isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );
1136 test("children([String])", function() {
1138 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
1139 isSet( $("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
1140 isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
1143 test("parent([String])", function() {
1145 ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
1146 ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
1147 ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
1148 ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
1149 isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
1152 test("parents([String])", function() {
1154 ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
1155 ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
1156 ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
1157 isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
1158 isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );
1161 test("next([String])", function() {
1163 ok( $("#ap").next()[0].id == "foo", "Simple next check" );
1164 ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
1165 ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
1166 ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
1169 test("prev([String])", function() {
1171 ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
1172 ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
1173 ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
1174 ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
1177 test("show()", function() {
1179 var pass = true, div = $("div");
1180 div.show().each(function(){
1181 if ( this.style.display == "none" ) pass = false;
1185 $("#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>');
1193 "table" : $.browser.msie ? "block" : "table",
1194 "thead" : $.browser.msie ? "block" : "table-header-group",
1195 "tbody" : $.browser.msie ? "block" : "table-row-group",
1196 "tr" : $.browser.msie ? "block" : "table-row",
1197 "th" : $.browser.msie ? "block" : "table-cell",
1198 "td" : $.browser.msie ? "block" : "table-cell",
1200 "li" : $.browser.msie ? "block" : "list-item"
1203 $.each(test, function(selector, expected) {
1204 var elem = $(selector, "#show-tests").show();
1205 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
1209 test("addClass(String)", function() {
1212 div.addClass("test");
1214 for ( var i = 0; i < div.size(); i++ ) {
1215 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
1217 ok( pass, "Add Class" );
1219 // using contents will get regular, text, and comment nodes
1220 var j = $("#nonnodes").contents();
1222 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
1225 test("removeClass(String) - simple", function() {
1227 var div = $("div").addClass("test").removeClass("test"),
1229 for ( var i = 0; i < div.size(); i++ ) {
1230 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
1232 ok( pass, "Remove Class" );
1235 var div = $("div").addClass("test").addClass("foo").addClass("bar");
1236 div.removeClass("test").removeClass("bar").removeClass("foo");
1238 for ( var i = 0; i < div.size(); i++ ) {
1239 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
1241 ok( pass, "Remove multiple classes" );
1244 var div = $("div:eq(0)").addClass("test").removeClass("");
1245 ok( div.is('.test'), "Empty string passed to removeClass" );
1247 // using contents will get regular, text, and comment nodes
1248 var j = $("#nonnodes").contents();
1249 j.removeClass("asdf");
1250 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
1253 test("toggleClass(String)", function() {
1255 var e = $("#firstp");
1256 ok( !e.is(".test"), "Assert class not present" );
1257 e.toggleClass("test");
1258 ok( e.is(".test"), "Assert class present" );
1259 e.toggleClass("test");
1260 ok( !e.is(".test"), "Assert class not present" );
1263 test("removeAttr(String", function() {
1265 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
1268 test("text(String)", function() {
1270 ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
1272 // using contents will get comments regular, text, and comment nodes
1273 var j = $("#nonnodes").contents();
1275 equals( $(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
1276 equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
1277 equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
1280 test("$.each(Object,Function)", function() {
1282 $.each( [0,1,2], function(i, n){
1283 ok( i == n, "Check array iteration" );
1286 $.each( [5,6,7], function(i, n){
1287 ok( i == n - 5, "Check array iteration" );
1290 $.each( { name: "name", lang: "lang" }, function(i, n){
1291 ok( i == n, "Check object iteration" );
1295 test("$.prop", function() {
1297 var handle = function() { return this.id };
1298 ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
1299 ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
1302 test("$.className", function() {
1304 var x = $("<p>Hi</p>")[0];
1305 var c = $.className;
1307 ok( x.className == "hi", "Check single added class" );
1308 c.add(x, "foo bar");
1309 ok( x.className == "hi foo bar", "Check more added classes" );
1311 ok( x.className == "", "Remove all classes" );
1312 c.add(x, "hi foo bar");
1314 ok( x.className == "hi bar", "Check removal of one class" );
1315 ok( c.has(x, "hi"), "Check has1" );
1316 ok( c.has(x, "bar"), "Check has2" );
1319 test("$.data", function() {
1321 var div = $("#foo")[0];
1322 ok( jQuery.data(div, "test") == undefined, "Check for no data exists" );
1323 jQuery.data(div, "test", "success");
1324 ok( jQuery.data(div, "test") == "success", "Check for added data" );
1325 jQuery.data(div, "test", "overwritten");
1326 ok( jQuery.data(div, "test") == "overwritten", "Check for overwritten data" );
1329 test("$.removeData", function() {
1331 var div = $("#foo")[0];
1332 jQuery.data(div, "test", "testing");
1333 jQuery.removeData(div, "test");
1334 ok( jQuery.data(div, "test") == undefined, "Check removal of data" );
1337 test("remove()", function() {
1339 $("#ap").children().remove();
1340 ok( $("#ap").text().length > 10, "Check text is not removed" );
1341 ok( $("#ap").children().length == 0, "Check remove" );
1344 $("#ap").children().remove("a");
1345 ok( $("#ap").text().length > 10, "Check text is not removed" );
1346 ok( $("#ap").children().length == 1, "Check filtered remove" );
1348 // using contents will get comments regular, text, and comment nodes
1349 equals( $("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
1350 $("#nonnodes").contents().remove();
1351 equals( $("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1354 test("empty()", function() {
1356 ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
1357 ok( $("#ap").children().length == 4, "Check elements are not removed" );
1359 // using contents will get comments regular, text, and comment nodes
1360 var j = $("#nonnodes").contents();
1362 equals( j.html(), "", "Check node,textnode,comment empty works" );
1365 test("slice()", function() {
1367 isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" );
1368 isSet( $("#ap a").slice(1), q("groups", "anchor1", "mark"), "slice(1)" );
1369 isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
1370 isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" );
1372 isSet( $("#ap a").eq(1), q("groups"), "eq(1)" );
1375 test("map()", function() {
1379 $("#ap").map(function(){
1380 return $(this).find("a").get();
1382 q("google", "groups", "anchor1", "mark"),
1387 $("#ap > a").map(function(){
1388 return this.parentNode;
1395 test("contents()", function() {
1397 equals( $("#ap").contents().length, 9, "Check element contents" );
1398 ok( $("#iframe").contents()[0], "Check existance of IFrame document" );
1399 var ibody = $("#loadediframe").contents()[0].body;
1400 ok( ibody, "Check existance of IFrame body" );
1402 equals( $("span", ibody).text(), "span text", "Find span in IFrame and check its text" );
1404 $(ibody).append("<div>init text</div>");
1405 equals( $("div", ibody).length, 2, "Check the original div and the new div are in IFrame" );
1407 equals( $("div:last", ibody).text(), "init text", "Add text to div in IFrame" );
1409 $("div:last", ibody).text("div text");
1410 equals( $("div:last", ibody).text(), "div text", "Add text to div in IFrame" );
1412 $("div:last", ibody).remove();
1413 equals( $("div", ibody).length, 1, "Delete the div and check only one div left in IFrame" );
1415 equals( $("div", ibody).text(), "span text", "Make sure the correct div is still left after deletion in IFrame" );
1417 $("<table/>", ibody).append("<tr><td>cell</td></tr>").appendTo(ibody);
1418 $("table", ibody).remove();
1419 equals( $("div", ibody).length, 1, "Check for JS error on add and delete of a table in IFrame" );
1421 // using contents will get comments regular, text, and comment nodes
1422 var c = $("#nonnodes").contents().contents();
1423 equals( c.length, 1, "Check node,textnode,comment contents is just one" );
1424 equals( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );