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" );
20 // make sure this is handled
22 ok( true, "Check for \\r and \\n in jQuery()" );
24 /* // Disabled until we add this functionality in
27 $("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
31 ok( pass, "$('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
33 var code = $("<code/>");
34 equals( code.length, 1, "Correct number of elements generated for code" );
35 var img = $("<img/>");
36 equals( img.length, 1, "Correct number of elements generated for img" );
37 var div = $("<div/><hr/><code/><b/>");
38 equals( div.length, 4, "Correct number of elements generated for div hr code b" );
41 test("noConflict", function() {
45 var newjQuery = jQuery.noConflict();
47 ok( newjQuery == old, "noConflict returned the jQuery object" );
48 ok( jQuery == old, "Make sure jQuery wasn't touched." );
49 ok( $ == "$", "Make sure $ was reverted." );
53 newjQuery = jQuery.noConflict(true);
55 ok( newjQuery == old, "noConflict returned the jQuery object" );
56 ok( jQuery == "jQuery", "Make sure jQuery was reverted." );
57 ok( $ == "$", "Make sure $ was reverted." );
62 test("isFunction", function() {
65 // Make sure that false values return false
66 ok( !jQuery.isFunction(), "No Value" );
67 ok( !jQuery.isFunction( null ), "null Value" );
68 ok( !jQuery.isFunction( undefined ), "undefined Value" );
69 ok( !jQuery.isFunction( "" ), "Empty String Value" );
70 ok( !jQuery.isFunction( 0 ), "0 Value" );
73 // Safari uses "(Internal Function)"
74 ok( jQuery.isFunction(String), "String Function" );
75 ok( jQuery.isFunction(Array), "Array Function" );
76 ok( jQuery.isFunction(Object), "Object Function" );
77 ok( jQuery.isFunction(Function), "Function Function" );
79 // When stringified, this could be misinterpreted
80 var mystr = "function";
81 ok( !jQuery.isFunction(mystr), "Function String" );
83 // When stringified, this could be misinterpreted
84 var myarr = [ "function" ];
85 ok( !jQuery.isFunction(myarr), "Function Array" );
87 // When stringified, this could be misinterpreted
88 var myfunction = { "function": "test" };
89 ok( !jQuery.isFunction(myfunction), "Function Object" );
91 // Make sure normal functions still work
92 var fn = function(){};
93 ok( jQuery.isFunction(fn), "Normal Function" );
95 var obj = document.createElement("object");
97 // Firefox says this is a function
98 ok( !jQuery.isFunction(obj), "Object Element" );
100 // IE says this is an object
101 ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
103 var nodes = document.body.childNodes;
105 // Safari says this is a function
106 ok( !jQuery.isFunction(nodes), "childNodes Property" );
108 var first = document.body.firstChild;
110 // Normal elements are reported ok everywhere
111 ok( !jQuery.isFunction(first), "A normal DOM Element" );
113 var input = document.createElement("input");
115 document.body.appendChild( input );
117 // IE says this is an object
118 ok( jQuery.isFunction(input.focus), "A default function property" );
120 document.body.removeChild( input );
122 var a = document.createElement("a");
123 a.href = "some-function";
124 document.body.appendChild( a );
126 // This serializes with the word 'function' in it
127 ok( !jQuery.isFunction(a), "Anchor Element" );
129 document.body.removeChild( a );
131 // Recursive function calls have lengths and array-like properties
132 function callme(callback){
133 function fn(response){
137 ok( jQuery.isFunction(fn), "Recursive Function Call" );
139 fn({ some: "data" });
143 callme(function(){});
149 test("$('html')", function() {
154 var s = $("<script>var foo='test';</script>")[0];
155 ok( s, "Creating a script" );
156 ok( !foo, "Make sure the script wasn't executed prematurely" );
158 ok( foo, "Executing a scripts contents in the right context" );
161 ok( $("<link rel='stylesheet'/>")[0], "Creating a link" );
166 test("length", function() {
168 ok( $("p").length == 6, "Get Number of Elements Found" );
171 test("size()", function() {
173 ok( $("p").size() == 6, "Get Number of Elements Found" );
176 test("get()", function() {
178 isSet( $("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
181 test("get(Number)", function() {
183 ok( $("p").get(0) == document.getElementById("firstp"), "Get A Single Element" );
186 test("add(String|Element|Array)", function() {
188 isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
189 isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
190 ok( $([]).add($("#form")[0].elements).length >= 13, "Check elements from array" );
192 var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
193 ok( x[0].id == "x1", "Check on-the-fly element1" );
194 ok( x[1].id == "x2", "Check on-the-fly element2" );
196 var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
197 ok( x[0].id == "x1", "Check on-the-fly element1" );
198 ok( x[1].id == "x2", "Check on-the-fly element2" );
201 test("each(Function)", function() {
204 div.each(function(){this.foo = 'zoo';});
206 for ( var i = 0; i < div.size(); i++ ) {
207 if ( div.get(i).foo != "zoo" ) pass = false;
209 ok( pass, "Execute a function, Relative" );
212 test("index(Object)", function() {
214 ok( $([window, document]).index(window) == 0, "Check for index of elements" );
215 ok( $([window, document]).index(document) == 1, "Check for index of elements" );
216 var inputElements = $('#radio1,#radio2,#check1,#check2');
217 ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
218 ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
219 ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
220 ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
221 ok( inputElements.index(window) == -1, "Check for not found index" );
222 ok( inputElements.index(document) == -1, "Check for not found index" );
225 test("attr(String)", function() {
227 ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
228 ok( $('#text1').attr('value', "Test2").attr('defaultValue') == "Test", 'Check for defaultValue attribute' );
229 ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
230 ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
231 ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
232 ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
233 ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
234 ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
235 ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
236 ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
237 ok( $('#name').attr('name') == "name", 'Check for name attribute' );
238 ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
239 ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
240 ok( $('#text1').attr('maxlength') == '30', 'Check for maxlength attribute' );
241 ok( $('#text1').attr('maxLength') == '30', 'Check for maxLength attribute' );
242 ok( $('#area1').attr('maxLength') == '30', 'Check for maxLength attribute' );
243 ok( $('#select2').attr('selectedIndex') == 3, 'Check for selectedIndex attribute' );
244 ok( $('#foo').attr('nodeName') == 'DIV', 'Check for nodeName attribute' );
245 ok( $('#foo').attr('tagName') == 'DIV', 'Check for tagName attribute' );
247 $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
248 ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
252 test("attr(String) in XML Files", function() {
255 $.get("data/dashboard.xml", function(xml) {
256 ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
257 ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
263 test("attr(String, Function)", function() {
265 ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
266 ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
269 test("attr(Hash)", function() {
272 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
273 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
275 ok( pass, "Set Multiple Attributes" );
278 test("attr(String, Object)", function() {
281 div.attr("foo", "bar");
283 for ( var i = 0; i < div.size(); i++ ) {
284 if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
286 ok( pass, "Set Attribute" );
288 ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
290 $("#name").attr('name', 'something');
291 ok( $("#name").attr('name') == 'something', 'Set name attribute' );
292 $("#check2").attr('checked', true);
293 ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
294 $("#check2").attr('checked', false);
295 ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
296 $("#text1").attr('readonly', true);
297 ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
298 $("#text1").attr('readonly', false);
299 ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
300 $("#name").attr('maxlength', '5');
301 ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' );
302 $("#name").attr('maxLength', '10');
303 ok( document.getElementById('name').maxLength == '10', 'Set maxlength attribute' );
307 var type = $("#check2").attr('type');
310 $("#check2").attr('type','hidden');
314 ok( thrown, "Exception thrown when trying to change type property" );
315 equals( type, $("#check2").attr('type'), "Verify that you can't change the type of an input element" );
317 var check = document.createElement("input");
320 $(check).attr('type','checkbox');
324 ok( thrown, "Exception thrown when trying to change type property" );
325 equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
329 test("attr(String, Object) - Loaded via XML document", function() {
332 $.get('data/dashboard.xml', function(xml) {
334 $('tab', xml).each(function() {
335 titles.push($(this).attr('title'));
337 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
338 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
344 test("css(String|Hash)", function() {
347 ok( $('#main').css("display") == 'none', 'Check for css property "display"');
349 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
350 $('#foo').css({display: 'none'});
351 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
352 $('#foo').css({display: 'block'});
353 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
355 $('#floatTest').css({styleFloat: 'right'});
356 ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
357 $('#floatTest').css({cssFloat: 'left'});
358 ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
359 $('#floatTest').css({'float': 'right'});
360 ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
361 $('#floatTest').css({'font-size': '30px'});
362 ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
364 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
365 $('#foo').css({opacity: n});
366 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
367 $('#foo').css({opacity: parseFloat(n)});
368 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
370 $('#foo').css({opacity: ''});
371 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
374 test("css(String, Object)", function() {
376 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
377 $('#foo').css('display', 'none');
378 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
379 $('#foo').css('display', 'block');
380 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
382 $('#floatTest').css('styleFloat', 'left');
383 ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
384 $('#floatTest').css('cssFloat', 'right');
385 ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
386 $('#floatTest').css('float', 'left');
387 ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
388 $('#floatTest').css('font-size', '20px');
389 ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
391 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
392 $('#foo').css('opacity', n);
393 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
394 $('#foo').css('opacity', parseFloat(n));
395 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
397 $('#foo').css('opacity', '');
398 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
401 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
404 var $checkedtest = $("#checkedtest");
405 // IE6 was clearing "checked" in jQuery.css(elem, "height");
406 jQuery.css($checkedtest[0], "height");
407 ok( !! $(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
408 ok( ! $(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
409 ok( !! $(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
410 ok( ! $(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
413 test("text()", function() {
415 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
416 ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
419 test("wrap(String|Element)", function() {
421 var defaultText = 'Try them out:'
422 var result = $('#first').wrap('<div class="red"><span></span></div>').text();
423 ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
424 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
427 var defaultText = 'Try them out:'
428 var result = $('#first').wrap(document.getElementById('empty')).parent();
429 ok( result.is('ol'), 'Check for element wrapping' );
430 ok( result.text() == defaultText, 'Check for element wrapping' );
433 $('#check1').click(function() {
435 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
436 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
437 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
441 test("wrapAll(String|Element)", function() {
443 var prev = $("#first")[0].previousSibling;
444 var p = $("#first")[0].parentNode;
445 var result = $('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>');
446 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
447 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
448 ok( $('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
449 equals( $("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
450 equals( $("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
453 var prev = $("#first")[0].previousSibling;
454 var p = $("#first")[0].parentNode;
455 var result = $('#first,#firstp').wrapAll(document.getElementById('empty'));
456 equals( $("#first").parent()[0], $("#firstp").parent()[0], "Same Parent" );
457 equals( $("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
458 equals( $("#first").parent()[0].parentNode, p, "Correct Parent" );
461 test("wrapInner(String|Element)", function() {
463 var num = $("#first").children().length;
464 var result = $('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
465 equals( $("#first").children().length, 1, "Only one child" );
466 ok( $("#first").children().is(".red"), "Verify Right Element" );
467 equals( $("#first").children().children().children().length, num, "Verify Elements Intact" );
470 var num = $("#first").children().length;
471 var result = $('#first').wrapInner(document.getElementById('empty'));
472 equals( $("#first").children().length, 1, "Only one child" );
473 ok( $("#first").children().is("#empty"), "Verify Right Element" );
474 equals( $("#first").children().children().length, num, "Verify Elements Intact" );
477 test("append(String|Element|Array<Element>|jQuery)", function() {
479 var defaultText = 'Try them out:'
480 var result = $('#first').append('<b>buga</b>');
481 ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
482 ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
485 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
486 $('#sap').append(document.getElementById('first'));
487 ok( expected == $('#sap').text(), "Check for appending of element" );
490 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
491 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
492 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
495 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
496 $('#sap').append($("#first, #yahoo"));
497 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
500 $("#sap").append( 5 );
501 ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
504 $("#sap").append( " text with spaces " );
505 ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
508 ok( $("#sap").append([]), "Check for appending an empty array." );
509 ok( $("#sap").append(""), "Check for appending an empty string." );
510 ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
513 $("#sap").append(document.getElementById('form'));
514 ok( $("#sap>form").size() == 1, "Check for appending a form" ); // Bug #910
519 $( $("iframe")[0].contentWindow.document.body ).append("<div>test</div>");
524 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
527 $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
528 t( 'Append legend', '#legend', ['legend'] );
531 $('#select1').append('<OPTION>Test</OPTION>');
532 ok( $('#select1 option:last').text() == "Test", "Appending <OPTION> (all caps)" );
534 $('#table').append('<colgroup></colgroup>');
535 ok( $('#table colgroup').length, "Append colgroup" );
537 $('#table colgroup').append('<col/>');
538 ok( $('#table colgroup col').length, "Append col" );
541 $('#table').append('<caption></caption>');
542 ok( $('#table caption').length, "Append caption" );
546 .append('<select id="appendSelect1"></select>')
547 .append('<select id="appendSelect2"><option>Test</option></select>');
549 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
552 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
554 var defaultText = 'Try them out:'
555 $('<b>buga</b>').appendTo('#first');
556 ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
557 ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
560 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
561 $(document.getElementById('first')).appendTo('#sap');
562 ok( expected == $('#sap').text(), "Check for appending of element" );
565 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
566 $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
567 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
570 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
571 $("#first, #yahoo").appendTo('#sap');
572 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
575 $('#select1').appendTo('#foo');
576 t( 'Append select', '#foo select', ['select1'] );
579 test("prepend(String|Element|Array<Element>|jQuery)", function() {
581 var defaultText = 'Try them out:'
582 var result = $('#first').prepend('<b>buga</b>');
583 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
584 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
587 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
588 $('#sap').prepend(document.getElementById('first'));
589 ok( expected == $('#sap').text(), "Check for prepending of element" );
592 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
593 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
594 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
597 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
598 $('#sap').prepend($("#first, #yahoo"));
599 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
602 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
604 var defaultText = 'Try them out:'
605 $('<b>buga</b>').prependTo('#first');
606 ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
607 ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
610 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
611 $(document.getElementById('first')).prependTo('#sap');
612 ok( expected == $('#sap').text(), "Check for prepending of element" );
615 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
616 $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
617 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
620 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
621 $("#yahoo, #first").prependTo('#sap');
622 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
625 $('<select id="prependSelect1"></select>').prependTo('form:last');
626 $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
628 t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
631 test("before(String|Element|Array<Element>|jQuery)", function() {
633 var expected = 'This is a normal link: bugaYahoo';
634 $('#yahoo').before('<b>buga</b>');
635 ok( expected == $('#en').text(), 'Insert String before' );
638 expected = "This is a normal link: Try them out:Yahoo";
639 $('#yahoo').before(document.getElementById('first'));
640 ok( expected == $('#en').text(), "Insert element before" );
643 expected = "This is a normal link: Try them out:diveintomarkYahoo";
644 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
645 ok( expected == $('#en').text(), "Insert array of elements before" );
648 expected = "This is a normal link: Try them out:diveintomarkYahoo";
649 $('#yahoo').before($("#first, #mark"));
650 ok( expected == $('#en').text(), "Insert jQuery before" );
653 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
655 var expected = 'This is a normal link: bugaYahoo';
656 $('<b>buga</b>').insertBefore('#yahoo');
657 ok( expected == $('#en').text(), 'Insert String before' );
660 expected = "This is a normal link: Try them out:Yahoo";
661 $(document.getElementById('first')).insertBefore('#yahoo');
662 ok( expected == $('#en').text(), "Insert element before" );
665 expected = "This is a normal link: Try them out:diveintomarkYahoo";
666 $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
667 ok( expected == $('#en').text(), "Insert array of elements before" );
670 expected = "This is a normal link: Try them out:diveintomarkYahoo";
671 $("#first, #mark").insertBefore('#yahoo');
672 ok( expected == $('#en').text(), "Insert jQuery before" );
675 test("after(String|Element|Array<Element>|jQuery)", function() {
677 var expected = 'This is a normal link: Yahoobuga';
678 $('#yahoo').after('<b>buga</b>');
679 ok( expected == $('#en').text(), 'Insert String after' );
682 expected = "This is a normal link: YahooTry them out:";
683 $('#yahoo').after(document.getElementById('first'));
684 ok( expected == $('#en').text(), "Insert element after" );
687 expected = "This is a normal link: YahooTry them out:diveintomark";
688 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
689 ok( expected == $('#en').text(), "Insert array of elements after" );
692 expected = "This is a normal link: YahooTry them out:diveintomark";
693 $('#yahoo').after($("#first, #mark"));
694 ok( expected == $('#en').text(), "Insert jQuery after" );
697 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
699 var expected = 'This is a normal link: Yahoobuga';
700 $('<b>buga</b>').insertAfter('#yahoo');
701 ok( expected == $('#en').text(), 'Insert String after' );
704 expected = "This is a normal link: YahooTry them out:";
705 $(document.getElementById('first')).insertAfter('#yahoo');
706 ok( expected == $('#en').text(), "Insert element after" );
709 expected = "This is a normal link: YahooTry them out:diveintomark";
710 $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
711 ok( expected == $('#en').text(), "Insert array of elements after" );
714 expected = "This is a normal link: YahooTry them out:diveintomark";
715 $("#mark, #first").insertAfter('#yahoo');
716 ok( expected == $('#en').text(), "Insert jQuery after" );
719 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
721 $('#yahoo').replaceWith('<b id="replace">buga</b>');
722 ok( $("#replace")[0], 'Replace element with string' );
723 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
726 $('#yahoo').replaceWith(document.getElementById('first'));
727 ok( $("#first")[0], 'Replace element with element' );
728 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
731 $('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
732 ok( $("#first")[0], 'Replace element with array of elements' );
733 ok( $("#mark")[0], 'Replace element with array of elements' );
734 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
737 $('#yahoo').replaceWith($("#first, #mark"));
738 ok( $("#first")[0], 'Replace element with set of elements' );
739 ok( $("#mark")[0], 'Replace element with set of elements' );
740 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
743 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
745 $('<b id="replace">buga</b>').replaceAll("#yahoo");
746 ok( $("#replace")[0], 'Replace element with string' );
747 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
750 $(document.getElementById('first')).replaceAll("#yahoo");
751 ok( $("#first")[0], 'Replace element with element' );
752 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
755 $([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
756 ok( $("#first")[0], 'Replace element with array of elements' );
757 ok( $("#mark")[0], 'Replace element with array of elements' );
758 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
761 $("#first, #mark").replaceAll("#yahoo");
762 ok( $("#first")[0], 'Replace element with set of elements' );
763 ok( $("#mark")[0], 'Replace element with set of elements' );
764 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
767 test("end()", function() {
769 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
770 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
774 ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
777 test("find(String)", function() {
779 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
782 test("clone()", function() {
784 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
785 var clone = $('#yahoo').clone();
786 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
787 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
790 test("is(String)", function() {
792 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
793 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
794 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
795 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
796 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
797 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
798 ok( $('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
799 ok( !$('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
800 ok( $('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
801 ok( !$('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
802 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
803 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
804 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
805 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
806 ok( $('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );
807 ok( !$('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );
808 ok( $('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );
809 ok( !$('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
810 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
811 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
812 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
813 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
815 // test is() with comma-seperated expressions
816 ok( $('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
817 ok( $('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
818 ok( $('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
819 ok( $('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
822 test("$.extend(Object, Object)", function() {
825 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
826 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
827 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
828 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
829 deep1 = { foo: { bar: true } },
830 deep1copy = { foo: { bar: true } },
831 deep2 = { foo: { baz: true }, foo2: document },
832 deep2copy = { foo: { baz: true }, foo2: document },
833 deepmerged = { foo: { bar: true, baz: true }, foo2: document };
835 jQuery.extend(settings, options);
836 isObj( settings, merged, "Check if extended: settings must be extended" );
837 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
839 jQuery.extend(settings, null, options);
840 isObj( settings, merged, "Check if extended: settings must be extended" );
841 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
843 jQuery.extend(true, deep1, deep2);
844 isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
845 isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
846 equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
849 var recursive = { foo:target, bar:5 };
850 jQuery.extend(true, target, recursive);
851 isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
853 var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
854 ok( ret.foo.length == 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
856 var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
857 ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
859 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
860 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
861 options1 = { xnumber2: 1, xstring2: "x" },
862 options1Copy = { xnumber2: 1, xstring2: "x" },
863 options2 = { xstring2: "xx", xxx: "newstringx" },
864 options2Copy = { xstring2: "xx", xxx: "newstringx" },
865 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
867 var settings = jQuery.extend({}, defaults, options1, options2);
868 isObj( settings, merged2, "Check if extended: settings must be extended" );
869 isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
870 isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
871 isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
874 test("val()", function() {
876 ok( $("#text1").val() == "Test", "Check for value of input element" );
877 ok( !$("#text1").val() == "", "Check for value of input element" );
880 test("val(String)", function() {
882 document.getElementById('text1').value = "bla";
883 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
884 $("#text1").val('test');
885 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
887 $("#select1").val("3");
888 ok( $("#select1").val() == "3", "Check for modified (via val(String)) value of select element" );
893 test("html(String)", function() {
895 var div = $("#main > div");
896 div.html("<b>test</b>");
898 for ( var i = 0; i < div.size(); i++ ) {
899 if ( div.get(i).childNodes.length != 1 ) pass = false;
901 ok( pass, "Set HTML" );
903 $("#main").html("<select/>");
904 $("#main select").html("<option>O1</option><option selected='selected'>O2</option><option>O3</option>");
905 equals( $("#main select").val(), "O2", "Selected option correct" );
909 $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>');
911 $("#main").html('foo <form><script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
913 $("#main").html("<script>ok(scriptorder++ == 0, 'Script is executed in order');ok($('#scriptorder').length == 0,'Execute before html')<\/script><span id='scriptorder'><script>ok(scriptorder++ == 1, 'Script is executed in order');ok($('#scriptorder').length == 1,'Execute after html')<\/script></span><script>ok(scriptorder++ == 2, 'Script is executed in order');ok($('#scriptorder').length == 1,'Execute after html')<\/script>");
915 setTimeout( start, 100 );
918 test("filter()", function() {
920 isSet( $("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
921 isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
922 isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
923 isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
926 test("not()", function() {
928 ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
929 isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
930 isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
933 test("andSelf()", function() {
935 isSet( $("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
936 isSet( $("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
937 isSet( $("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" );
938 isSet( $("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" );
941 test("siblings([String])", function() {
943 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
944 isSet( $("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
945 isSet( $("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
946 isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" );
947 isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );
950 test("children([String])", function() {
952 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
953 isSet( $("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
954 isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
957 test("parent([String])", function() {
959 ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
960 ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
961 ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
962 ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
963 isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
966 test("parents([String])", function() {
968 ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
969 ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
970 ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
971 isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
972 isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );
975 test("next([String])", function() {
977 ok( $("#ap").next()[0].id == "foo", "Simple next check" );
978 ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
979 ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
980 ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
983 test("prev([String])", function() {
985 ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
986 ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
987 ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
988 ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
991 test("show()", function() {
993 var pass = true, div = $("div");
994 div.show().each(function(){
995 if ( this.style.display == "none" ) pass = false;
1000 test("addClass(String)", function() {
1003 div.addClass("test");
1005 for ( var i = 0; i < div.size(); i++ ) {
1006 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
1008 ok( pass, "Add Class" );
1011 test("removeClass(String) - simple", function() {
1013 var div = $("div").addClass("test").removeClass("test"),
1015 for ( var i = 0; i < div.size(); i++ ) {
1016 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
1018 ok( pass, "Remove Class" );
1021 var div = $("div").addClass("test").addClass("foo").addClass("bar");
1022 div.removeClass("test").removeClass("bar").removeClass("foo");
1024 for ( var i = 0; i < div.size(); i++ ) {
1025 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
1027 ok( pass, "Remove multiple classes" );
1030 var div = $("div:eq(0)").addClass("test").removeClass("");
1031 ok( div.is('.test'), "Empty string passed to removeClass" );
1035 test("toggleClass(String)", function() {
1037 var e = $("#firstp");
1038 ok( !e.is(".test"), "Assert class not present" );
1039 e.toggleClass("test");
1040 ok( e.is(".test"), "Assert class present" );
1041 e.toggleClass("test");
1042 ok( !e.is(".test"), "Assert class not present" );
1045 test("removeAttr(String", function() {
1047 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
1050 test("text(String)", function() {
1052 ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
1055 test("$.each(Object,Function)", function() {
1057 $.each( [0,1,2], function(i, n){
1058 ok( i == n, "Check array iteration" );
1061 $.each( [5,6,7], function(i, n){
1062 ok( i == n - 5, "Check array iteration" );
1065 $.each( { name: "name", lang: "lang" }, function(i, n){
1066 ok( i == n, "Check object iteration" );
1070 test("$.prop", function() {
1072 var handle = function() { return this.id };
1073 ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
1074 ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
1077 test("$.className", function() {
1079 var x = $("<p>Hi</p>")[0];
1080 var c = $.className;
1082 ok( x.className == "hi", "Check single added class" );
1083 c.add(x, "foo bar");
1084 ok( x.className == "hi foo bar", "Check more added classes" );
1086 ok( x.className == "", "Remove all classes" );
1087 c.add(x, "hi foo bar");
1089 ok( x.className == "hi bar", "Check removal of one class" );
1090 ok( c.has(x, "hi"), "Check has1" );
1091 ok( c.has(x, "bar"), "Check has2" );
1094 test("remove()", function() {
1096 $("#ap").children().remove();
1097 ok( $("#ap").text().length > 10, "Check text is not removed" );
1098 ok( $("#ap").children().length == 0, "Check remove" );
1101 $("#ap").children().remove("a");
1102 ok( $("#ap").text().length > 10, "Check text is not removed" );
1103 ok( $("#ap").children().length == 1, "Check filtered remove" );
1106 test("empty()", function() {
1108 ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
1109 ok( $("#ap").children().length == 4, "Check elements are not removed" );
1112 test("slice()", function() {
1114 isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" );
1115 isSet( $("#ap a").slice(1), q("groups", "anchor1", "mark"), "slice(1)" );
1116 isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
1117 isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" );
1119 isSet( $("#ap a").eq(1), q("groups"), "eq(1)" );
1122 test("map()", function() {
1126 $("#ap").map(function(){
1127 return $(this).find("a").get();
1129 q("google", "groups", "anchor1", "mark"),
1134 $("#ap > a").map(function(){
1135 return this.parentNode;
1142 test("contents()", function() {
1144 equals( $("#ap").contents().length, 9, "Check element contents" );
1145 ok( $("#iframe").contents()[0], "Check existance of IFrame document" );
1146 // Disabled, randomly fails
1147 //ok( $("#iframe").contents()[0].body, "Check existance of IFrame body" );