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('type') == "text", 'Check for type attribute' );
229 ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
230 ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
231 ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
232 ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
233 ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
234 ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
235 ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
236 ok( $('#name').attr('name') == "name", 'Check for name attribute' );
237 ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
238 ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
240 $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
241 ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
245 test("attr(String) in XML Files", function() {
248 $.get("data/dashboard.xml", function(xml) {
249 ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
250 ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
256 test("attr(String, Function)", function() {
258 ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
259 ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
262 test("attr(Hash)", function() {
265 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
266 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
268 ok( pass, "Set Multiple Attributes" );
271 test("attr(String, Object)", function() {
274 div.attr("foo", "bar");
276 for ( var i = 0; i < div.size(); i++ ) {
277 if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
279 ok( pass, "Set Attribute" );
281 ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
283 $("#name").attr('name', 'something');
284 ok( $("#name").attr('name') == 'something', 'Set name attribute' );
285 $("#check2").attr('checked', true);
286 ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
287 $("#check2").attr('checked', false);
288 ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
289 $("#text1").attr('readonly', true);
290 ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
291 $("#text1").attr('readonly', false);
292 ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
293 $("#name").attr('maxlength', '5');
294 ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' );
298 var type = $("#check2").attr('type');
301 $("#check2").attr('type','hidden');
305 ok( thrown, "Exception thrown when trying to change type property" );
306 equals( type, $("#check2").attr('type'), "Verify that you can't change the type of an input element" );
308 var check = document.createElement("input");
311 $(check).attr('type','checkbox');
315 ok( thrown, "Exception thrown when trying to change type property" );
316 equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
320 test("attr(String, Object) - Loaded via XML document", function() {
323 $.get('data/dashboard.xml', function(xml) {
325 $('tab', xml).each(function() {
326 titles.push($(this).attr('title'));
328 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
329 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
335 test("css(String|Hash)", function() {
338 ok( $('#main').css("display") == 'none', 'Check for css property "display"');
340 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
341 $('#foo').css({display: 'none'});
342 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
343 $('#foo').css({display: 'block'});
344 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
346 $('#floatTest').css({styleFloat: 'right'});
347 ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
348 $('#floatTest').css({cssFloat: 'left'});
349 ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
350 $('#floatTest').css({'float': 'right'});
351 ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
352 $('#floatTest').css({'font-size': '30px'});
353 ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
355 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
356 $('#foo').css({opacity: n});
357 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
358 $('#foo').css({opacity: parseFloat(n)});
359 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
361 $('#foo').css({opacity: ''});
362 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
365 test("css(String, Object)", function() {
367 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
368 $('#foo').css('display', 'none');
369 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
370 $('#foo').css('display', 'block');
371 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
373 $('#floatTest').css('styleFloat', 'left');
374 ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
375 $('#floatTest').css('cssFloat', 'right');
376 ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
377 $('#floatTest').css('float', 'left');
378 ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
379 $('#floatTest').css('font-size', '20px');
380 ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
382 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
383 $('#foo').css('opacity', n);
384 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
385 $('#foo').css('opacity', parseFloat(n));
386 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
388 $('#foo').css('opacity', '');
389 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
392 test("text()", function() {
394 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
395 ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
398 test("wrap(String|Element)", function() {
400 var defaultText = 'Try them out:'
401 var result = $('#first').wrap('<div class="red"><span></span></div>').text();
402 ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
403 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
406 var defaultText = 'Try them out:'
407 var result = $('#first').wrap(document.getElementById('empty')).parent();
408 ok( result.is('ol'), 'Check for element wrapping' );
409 ok( result.text() == defaultText, 'Check for element wrapping' );
412 $('#check1').click(function() {
414 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
415 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
416 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
420 test("wrapAll(String|Element)", function() {
422 var prev = $("#first")[0].previousSibling;
423 var p = $("#first")[0].parentNode;
424 var result = $('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>');
425 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
426 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
427 ok( $('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
428 equals( $("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
429 equals( $("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
432 var prev = $("#first")[0].previousSibling;
433 var p = $("#first")[0].parentNode;
434 var result = $('#first,#firstp').wrapAll(document.getElementById('empty'));
435 equals( $("#first").parent()[0], $("#firstp").parent()[0], "Same Parent" );
436 equals( $("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
437 equals( $("#first").parent()[0].parentNode, p, "Correct Parent" );
440 test("wrapInner(String|Element)", function() {
442 var num = $("#first").children().length;
443 var result = $('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
444 equals( $("#first").children().length, 1, "Only one child" );
445 ok( $("#first").children().is(".red"), "Verify Right Element" );
446 equals( $("#first").children().children().children().length, num, "Verify Elements Intact" );
449 var num = $("#first").children().length;
450 var result = $('#first').wrapInner(document.getElementById('empty'));
451 equals( $("#first").children().length, 1, "Only one child" );
452 ok( $("#first").children().is("#empty"), "Verify Right Element" );
453 equals( $("#first").children().children().length, num, "Verify Elements Intact" );
456 test("append(String|Element|Array<Element>|jQuery)", function() {
458 var defaultText = 'Try them out:'
459 var result = $('#first').append('<b>buga</b>');
460 ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
461 ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
464 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
465 $('#sap').append(document.getElementById('first'));
466 ok( expected == $('#sap').text(), "Check for appending of element" );
469 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
470 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
471 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
474 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
475 $('#sap').append($("#first, #yahoo"));
476 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
479 $("#sap").append( 5 );
480 ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
483 $("#sap").append( " text with spaces " );
484 ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
487 ok( $("#sap").append([]), "Check for appending an empty array." );
488 ok( $("#sap").append(""), "Check for appending an empty string." );
489 ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
492 $("#sap").append(document.getElementById('form'));
493 ok( $("#sap>form").size() == 1, "Check for appending a form" ); // Bug #910
498 $( $("iframe")[0].contentWindow.document.body ).append("<div>test</div>");
503 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
506 $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
507 t( 'Append legend', '#legend', ['legend'] );
510 $('#select1').append('<OPTION>Test</OPTION>');
511 ok( $('#select1 option:last').text() == "Test", "Appending <OPTION> (all caps)" );
513 $('#table').append('<colgroup></colgroup>');
514 ok( $('#table colgroup').length, "Append colgroup" );
516 $('#table colgroup').append('<col/>');
517 ok( $('#table colgroup col').length, "Append col" );
520 $('#table').append('<caption></caption>');
521 ok( $('#table caption').length, "Append caption" );
525 .append('<select id="appendSelect1"></select>')
526 .append('<select id="appendSelect2"><option>Test</option></select>');
528 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
531 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
533 var defaultText = 'Try them out:'
534 $('<b>buga</b>').appendTo('#first');
535 ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
536 ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
539 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
540 $(document.getElementById('first')).appendTo('#sap');
541 ok( expected == $('#sap').text(), "Check for appending of element" );
544 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
545 $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
546 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
549 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
550 $("#first, #yahoo").appendTo('#sap');
551 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
554 $('#select1').appendTo('#foo');
555 t( 'Append select', '#foo select', ['select1'] );
558 test("prepend(String|Element|Array<Element>|jQuery)", function() {
560 var defaultText = 'Try them out:'
561 var result = $('#first').prepend('<b>buga</b>');
562 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
563 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
566 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
567 $('#sap').prepend(document.getElementById('first'));
568 ok( expected == $('#sap').text(), "Check for prepending of element" );
571 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
572 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
573 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
576 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
577 $('#sap').prepend($("#first, #yahoo"));
578 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
581 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
583 var defaultText = 'Try them out:'
584 $('<b>buga</b>').prependTo('#first');
585 ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
586 ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
589 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
590 $(document.getElementById('first')).prependTo('#sap');
591 ok( expected == $('#sap').text(), "Check for prepending of element" );
594 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
595 $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
596 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
599 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
600 $("#yahoo, #first").prependTo('#sap');
601 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
604 $('<select id="prependSelect1"></select>').prependTo('form:last');
605 $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
607 t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
610 test("before(String|Element|Array<Element>|jQuery)", function() {
612 var expected = 'This is a normal link: bugaYahoo';
613 $('#yahoo').before('<b>buga</b>');
614 ok( expected == $('#en').text(), 'Insert String before' );
617 expected = "This is a normal link: Try them out:Yahoo";
618 $('#yahoo').before(document.getElementById('first'));
619 ok( expected == $('#en').text(), "Insert element before" );
622 expected = "This is a normal link: Try them out:diveintomarkYahoo";
623 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
624 ok( expected == $('#en').text(), "Insert array of elements before" );
627 expected = "This is a normal link: Try them out:diveintomarkYahoo";
628 $('#yahoo').before($("#first, #mark"));
629 ok( expected == $('#en').text(), "Insert jQuery before" );
632 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
634 var expected = 'This is a normal link: bugaYahoo';
635 $('<b>buga</b>').insertBefore('#yahoo');
636 ok( expected == $('#en').text(), 'Insert String before' );
639 expected = "This is a normal link: Try them out:Yahoo";
640 $(document.getElementById('first')).insertBefore('#yahoo');
641 ok( expected == $('#en').text(), "Insert element before" );
644 expected = "This is a normal link: Try them out:diveintomarkYahoo";
645 $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
646 ok( expected == $('#en').text(), "Insert array of elements before" );
649 expected = "This is a normal link: Try them out:diveintomarkYahoo";
650 $("#first, #mark").insertBefore('#yahoo');
651 ok( expected == $('#en').text(), "Insert jQuery before" );
654 test("after(String|Element|Array<Element>|jQuery)", function() {
656 var expected = 'This is a normal link: Yahoobuga';
657 $('#yahoo').after('<b>buga</b>');
658 ok( expected == $('#en').text(), 'Insert String after' );
661 expected = "This is a normal link: YahooTry them out:";
662 $('#yahoo').after(document.getElementById('first'));
663 ok( expected == $('#en').text(), "Insert element after" );
666 expected = "This is a normal link: YahooTry them out:diveintomark";
667 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
668 ok( expected == $('#en').text(), "Insert array of elements after" );
671 expected = "This is a normal link: YahooTry them out:diveintomark";
672 $('#yahoo').after($("#first, #mark"));
673 ok( expected == $('#en').text(), "Insert jQuery after" );
676 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
678 var expected = 'This is a normal link: Yahoobuga';
679 $('<b>buga</b>').insertAfter('#yahoo');
680 ok( expected == $('#en').text(), 'Insert String after' );
683 expected = "This is a normal link: YahooTry them out:";
684 $(document.getElementById('first')).insertAfter('#yahoo');
685 ok( expected == $('#en').text(), "Insert element after" );
688 expected = "This is a normal link: YahooTry them out:diveintomark";
689 $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
690 ok( expected == $('#en').text(), "Insert array of elements after" );
693 expected = "This is a normal link: YahooTry them out:diveintomark";
694 $("#mark, #first").insertAfter('#yahoo');
695 ok( expected == $('#en').text(), "Insert jQuery after" );
698 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
700 $('#yahoo').replaceWith('<b id="replace">buga</b>');
701 ok( $("#replace")[0], 'Replace element with string' );
702 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
705 $('#yahoo').replaceWith(document.getElementById('first'));
706 ok( $("#first")[0], 'Replace element with element' );
707 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
710 $('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
711 ok( $("#first")[0], 'Replace element with array of elements' );
712 ok( $("#mark")[0], 'Replace element with array of elements' );
713 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
716 $('#yahoo').replaceWith($("#first, #mark"));
717 ok( $("#first")[0], 'Replace element with set of elements' );
718 ok( $("#mark")[0], 'Replace element with set of elements' );
719 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
722 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
724 $('<b id="replace">buga</b>').replaceAll("#yahoo");
725 ok( $("#replace")[0], 'Replace element with string' );
726 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
729 $(document.getElementById('first')).replaceAll("#yahoo");
730 ok( $("#first")[0], 'Replace element with element' );
731 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
734 $([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
735 ok( $("#first")[0], 'Replace element with array of elements' );
736 ok( $("#mark")[0], 'Replace element with array of elements' );
737 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
740 $("#first, #mark").replaceAll("#yahoo");
741 ok( $("#first")[0], 'Replace element with set of elements' );
742 ok( $("#mark")[0], 'Replace element with set of elements' );
743 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
746 test("end()", function() {
748 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
749 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
753 ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
756 test("find(String)", function() {
758 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
761 test("clone()", function() {
763 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
764 var clone = $('#yahoo').clone();
765 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
766 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
769 test("is(String)", function() {
771 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
772 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
773 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
774 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
775 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
776 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
777 ok( $('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
778 ok( !$('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
779 ok( $('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
780 ok( !$('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
781 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
782 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
783 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
784 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
785 ok( $('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );
786 ok( !$('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );
787 ok( $('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );
788 ok( !$('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
789 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
790 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
791 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
792 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
794 // test is() with comma-seperated expressions
795 ok( $('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
796 ok( $('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
797 ok( $('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
798 ok( $('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
801 test("$.extend(Object, Object)", function() {
804 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
805 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
806 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
807 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
808 deep1 = { foo: { bar: true } },
809 deep1copy = { foo: { bar: true } },
810 deep2 = { foo: { baz: true }, foo2: document },
811 deep2copy = { foo: { baz: true }, foo2: document },
812 deepmerged = { foo: { bar: true, baz: true }, foo2: document };
814 jQuery.extend(settings, options);
815 isObj( settings, merged, "Check if extended: settings must be extended" );
816 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
818 jQuery.extend(settings, null, options);
819 isObj( settings, merged, "Check if extended: settings must be extended" );
820 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
822 jQuery.extend(true, deep1, deep2);
823 isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
824 isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
825 equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
827 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
828 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
829 options1 = { xnumber2: 1, xstring2: "x" },
830 options1Copy = { xnumber2: 1, xstring2: "x" },
831 options2 = { xstring2: "xx", xxx: "newstringx" },
832 options2Copy = { xstring2: "xx", xxx: "newstringx" },
833 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
835 var settings = jQuery.extend({}, defaults, options1, options2);
836 isObj( settings, merged2, "Check if extended: settings must be extended" );
837 isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
838 isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
839 isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
842 test("val()", function() {
844 ok( $("#text1").val() == "Test", "Check for value of input element" );
845 ok( !$("#text1").val() == "", "Check for value of input element" );
848 test("val(String)", function() {
850 document.getElementById('text1').value = "bla";
851 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
852 $("#text1").val('test');
853 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
855 $("#select1").val("3");
856 ok( $("#select1").val() == "3", "Check for modified (via val(String)) value of select element" );
861 test("html(String)", function() {
864 div.html("<b>test</b>");
866 for ( var i = 0; i < div.size(); i++ ) {
867 if ( div.get(i).childNodes.length == 0 ) pass = false;
869 ok( pass, "Set HTML" );
873 $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>');
875 $("#main").html('foo <form><script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
877 $("#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>");
879 setTimeout( start, 100 );
882 test("filter()", function() {
884 isSet( $("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
885 isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
886 isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
887 isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
890 test("not()", function() {
892 ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
893 isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
894 isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
897 test("andSelf()", function() {
899 isSet( $("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
900 isSet( $("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
901 isSet( $("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" );
902 isSet( $("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" );
905 test("siblings([String])", function() {
907 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
908 isSet( $("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
909 isSet( $("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
910 isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" );
911 isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );
914 test("children([String])", function() {
916 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
917 isSet( $("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
918 isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
921 test("parent([String])", function() {
923 ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
924 ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
925 ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
926 ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
927 isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
930 test("parents([String])", function() {
932 ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
933 ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
934 ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
935 isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
936 isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );
939 test("next([String])", function() {
941 ok( $("#ap").next()[0].id == "foo", "Simple next check" );
942 ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
943 ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
944 ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
947 test("prev([String])", function() {
949 ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
950 ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
951 ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
952 ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
955 test("show()", function() {
957 var pass = true, div = $("div");
958 div.show().each(function(){
959 if ( this.style.display == "none" ) pass = false;
964 test("addClass(String)", function() {
967 div.addClass("test");
969 for ( var i = 0; i < div.size(); i++ ) {
970 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
972 ok( pass, "Add Class" );
975 test("removeClass(String) - simple", function() {
977 var div = $("div").addClass("test").removeClass("test"),
979 for ( var i = 0; i < div.size(); i++ ) {
980 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
982 ok( pass, "Remove Class" );
985 var div = $("div").addClass("test").addClass("foo").addClass("bar");
986 div.removeClass("test").removeClass("bar").removeClass("foo");
988 for ( var i = 0; i < div.size(); i++ ) {
989 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
991 ok( pass, "Remove multiple classes" );
994 var div = $("div:eq(0)").addClass("test").removeClass("");
995 ok( div.is('.test'), "Empty string passed to removeClass" );
999 test("toggleClass(String)", function() {
1001 var e = $("#firstp");
1002 ok( !e.is(".test"), "Assert class not present" );
1003 e.toggleClass("test");
1004 ok( e.is(".test"), "Assert class present" );
1005 e.toggleClass("test");
1006 ok( !e.is(".test"), "Assert class not present" );
1009 test("removeAttr(String", function() {
1011 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
1014 test("text(String)", function() {
1016 ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
1019 test("$.each(Object,Function)", function() {
1021 $.each( [0,1,2], function(i, n){
1022 ok( i == n, "Check array iteration" );
1025 $.each( [5,6,7], function(i, n){
1026 ok( i == n - 5, "Check array iteration" );
1029 $.each( { name: "name", lang: "lang" }, function(i, n){
1030 ok( i == n, "Check object iteration" );
1034 test("$.prop", function() {
1036 var handle = function() { return this.id };
1037 ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
1038 ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
1041 test("$.className", function() {
1043 var x = $("<p>Hi</p>")[0];
1044 var c = $.className;
1046 ok( x.className == "hi", "Check single added class" );
1047 c.add(x, "foo bar");
1048 ok( x.className == "hi foo bar", "Check more added classes" );
1050 ok( x.className == "", "Remove all classes" );
1051 c.add(x, "hi foo bar");
1053 ok( x.className == "hi bar", "Check removal of one class" );
1054 ok( c.has(x, "hi"), "Check has1" );
1055 ok( c.has(x, "bar"), "Check has2" );
1058 test("remove()", function() {
1060 $("#ap").children().remove();
1061 ok( $("#ap").text().length > 10, "Check text is not removed" );
1062 ok( $("#ap").children().length == 0, "Check remove" );
1065 $("#ap").children().remove("a");
1066 ok( $("#ap").text().length > 10, "Check text is not removed" );
1067 ok( $("#ap").children().length == 1, "Check filtered remove" );
1070 test("empty()", function() {
1072 ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
1073 ok( $("#ap").children().length == 4, "Check elements are not removed" );
1076 test("slice()", function() {
1078 isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" );
1079 isSet( $("#ap a").slice(1), q("groups", "anchor1", "mark"), "slice(1)" );
1080 isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
1081 isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" );
1083 isSet( $("#ap a").eq(1), q("groups"), "eq(1)" );
1086 test("map()", function() {
1090 $("#ap").map(function(){
1091 return $(this).find("a").get();
1093 q("google", "groups", "anchor1", "mark"),
1098 $("#ap > a").map(function(){
1099 return this.parentNode;
1106 test("contents()", function() {
1108 equals( $("#ap").contents().length, 9, "Check element contents" );
1109 ok( $("#iframe").contents()[0], "Check existance of IFrame document" );
1110 // Disabled, randomly fails
1111 //ok( $("#iframe").contents()[0].body, "Check existance of IFrame body" );