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" );*/
34 test("isFunction", function() {
37 // Make sure that false values return false
38 ok( !jQuery.isFunction(), "No Value" );
39 ok( !jQuery.isFunction( null ), "null Value" );
40 ok( !jQuery.isFunction( undefined ), "undefined Value" );
41 ok( !jQuery.isFunction( "" ), "Empty String Value" );
42 ok( !jQuery.isFunction( 0 ), "0 Value" );
45 // Safari uses "(Internal Function)"
46 ok( jQuery.isFunction(String), "String Function" );
47 ok( jQuery.isFunction(Array), "Array Function" );
48 ok( jQuery.isFunction(Object), "Object Function" );
49 ok( jQuery.isFunction(Function), "Function Function" );
51 // When stringified, this could be misinterpreted
52 var mystr = "function";
53 ok( !jQuery.isFunction(mystr), "Function String" );
55 // When stringified, this could be misinterpreted
56 var myarr = [ "function" ];
57 ok( !jQuery.isFunction(myarr), "Function Array" );
59 // When stringified, this could be misinterpreted
60 var myfunction = { "function": "test" };
61 ok( !jQuery.isFunction(myfunction), "Function Object" );
63 // Make sure normal functions still work
64 var fn = function(){};
65 ok( jQuery.isFunction(fn), "Normal Function" );
67 var obj = document.createElement("object");
69 // Firefox says this is a function
70 ok( !jQuery.isFunction(obj), "Object Element" );
72 // IE says this is an object
73 ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
75 var nodes = document.body.childNodes;
77 // Safari says this is a function
78 ok( !jQuery.isFunction(nodes), "childNodes Property" );
80 var first = document.body.firstChild;
82 // Normal elements are reported ok everywhere
83 ok( !jQuery.isFunction(first), "A normal DOM Element" );
85 var input = document.createElement("input");
87 document.body.appendChild( input );
89 // IE says this is an object
90 ok( jQuery.isFunction(input.focus), "A default function property" );
92 document.body.removeChild( input );
94 var a = document.createElement("a");
95 a.href = "some-function";
96 document.body.appendChild( a );
98 // This serializes with the word 'function' in it
99 ok( !jQuery.isFunction(a), "Anchor Element" );
101 document.body.removeChild( a );
103 // Recursive function calls have lengths and array-like properties
104 function callme(callback){
105 function fn(response){
109 ok( jQuery.isFunction(fn), "Recursive Function Call" );
111 fn({ some: "data" });
115 callme(function(){});
119 test("$('html')", function() {
123 ok( $("<script>var foo='test';</script>")[0], "Creating a script" );
126 ok( $("<link rel='stylesheet'/>")[0], "Creating a link" );
131 test("length", function() {
133 ok( $("p").length == 6, "Get Number of Elements Found" );
136 test("size()", function() {
138 ok( $("p").size() == 6, "Get Number of Elements Found" );
141 test("get()", function() {
143 isSet( $("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
146 test("get(Number)", function() {
148 ok( $("p").get(0) == document.getElementById("firstp"), "Get A Single Element" );
151 test("add(String|Element|Array)", function() {
153 isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
154 isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
155 ok( $([]).add($("#form")[0].elements).length >= 13, "Check elements from array" );
157 var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
158 ok( x[0].id == "x1", "Check on-the-fly element1" );
159 ok( x[1].id == "x2", "Check on-the-fly element2" );
161 var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
162 ok( x[0].id == "x1", "Check on-the-fly element1" );
163 ok( x[1].id == "x2", "Check on-the-fly element2" );
166 test("each(Function)", function() {
169 div.each(function(){this.foo = 'zoo';});
171 for ( var i = 0; i < div.size(); i++ ) {
172 if ( div.get(i).foo != "zoo" ) pass = false;
174 ok( pass, "Execute a function, Relative" );
177 test("index(Object)", function() {
179 ok( $([window, document]).index(window) == 0, "Check for index of elements" );
180 ok( $([window, document]).index(document) == 1, "Check for index of elements" );
181 var inputElements = $('#radio1,#radio2,#check1,#check2');
182 ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
183 ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
184 ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
185 ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
186 ok( inputElements.index(window) == -1, "Check for not found index" );
187 ok( inputElements.index(document) == -1, "Check for not found index" );
190 test("attr(String)", function() {
192 ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
193 ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
194 ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
195 ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
196 ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
197 ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
198 ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
199 ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
200 ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
201 ok( $('#name').attr('name') == "name", 'Check for name attribute' );
202 ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
203 ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
205 $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
206 ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
209 test("attr(String) in XML Files", function() {
212 $.get("data/dashboard.xml", function(xml) {
213 ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
214 ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
219 test("attr(String, Function)", function() {
221 ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
222 ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
225 test("attr(Hash)", function() {
228 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
229 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
231 ok( pass, "Set Multiple Attributes" );
234 test("attr(String, Object)", function() {
237 div.attr("foo", "bar");
239 for ( var i = 0; i < div.size(); i++ ) {
240 if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
242 ok( pass, "Set Attribute" );
244 ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
246 $("#name").attr('name', 'something');
247 ok( $("#name").attr('name') == 'something', 'Set name attribute' );
248 $("#check2").attr('checked', true);
249 ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
250 $("#check2").attr('checked', false);
251 ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
252 $("#text1").attr('readonly', true);
253 ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
254 $("#text1").attr('readonly', false);
255 ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
256 $("#name").attr('maxlength', '5');
257 ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' );
260 test("attr(String, Object) - Loaded via XML document", function() {
263 $.get('data/dashboard.xml', function(xml) {
265 $('tab', xml).each(function() {
266 titles.push($(this).attr('title'));
268 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
269 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
274 test("css(String|Hash)", function() {
277 ok( $('#main').css("display") == 'none', 'Check for css property "display"');
279 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
280 $('#foo').css({display: 'none'});
281 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
282 $('#foo').css({display: 'block'});
283 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
285 $('#floatTest').css({styleFloat: 'right'});
286 ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
287 $('#floatTest').css({cssFloat: 'left'});
288 ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
289 $('#floatTest').css({'float': 'right'});
290 ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
291 $('#floatTest').css({'font-size': '30px'});
292 ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
294 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
295 $('#foo').css({opacity: n});
296 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
297 $('#foo').css({opacity: parseFloat(n)});
298 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
300 $('#foo').css({opacity: ''});
301 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
304 test("css(String, Object)", function() {
306 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
307 $('#foo').css('display', 'none');
308 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
309 $('#foo').css('display', 'block');
310 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
312 $('#floatTest').css('styleFloat', 'left');
313 ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
314 $('#floatTest').css('cssFloat', 'right');
315 ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
316 $('#floatTest').css('float', 'left');
317 ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
318 $('#floatTest').css('font-size', '20px');
319 ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
321 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
322 $('#foo').css('opacity', n);
323 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
324 $('#foo').css('opacity', parseFloat(n));
325 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
327 $('#foo').css('opacity', '');
328 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
331 test("text()", function() {
333 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
334 ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
337 test("wrap(String|Element)", function() {
339 var defaultText = 'Try them out:'
340 var result = $('#first').wrap('<div class="red"><span></span></div>').text();
341 ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
342 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
345 var defaultText = 'Try them out:'
346 var result = $('#first').wrap(document.getElementById('empty')).parent();
347 ok( result.is('ol'), 'Check for element wrapping' );
348 ok( result.text() == defaultText, 'Check for element wrapping' );
351 $('#check1').click(function() {
353 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
354 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
355 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
359 test("wrapAll(String|Element)", function() {
361 var prev = $("#first")[0].previousSibling;
362 var p = $("#first")[0].parentNode;
363 var result = $('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>');
364 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
365 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
366 ok( $('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
367 equals( $("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
368 equals( $("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
371 var prev = $("#first")[0].previousSibling;
372 var p = $("#first")[0].parentNode;
373 var result = $('#first,#firstp').wrapAll(document.getElementById('empty'));
374 equals( $("#first").parent()[0], $("#firstp").parent()[0], "Same Parent" );
375 equals( $("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
376 equals( $("#first").parent()[0].parentNode, p, "Correct Parent" );
379 test("wrapInner(String|Element)", function() {
381 var num = $("#first").children().length;
382 var result = $('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
383 equals( $("#first").children().length, 1, "Only one child" );
384 ok( $("#first").children().is(".red"), "Verify Right Element" );
385 equals( $("#first").children().children().children().length, num, "Verify Elements Intact" );
388 var num = $("#first").children().length;
389 var result = $('#first').wrapInner(document.getElementById('empty'));
390 equals( $("#first").children().length, 1, "Only one child" );
391 ok( $("#first").children().is("#empty"), "Verify Right Element" );
392 equals( $("#first").children().children().length, num, "Verify Elements Intact" );
395 test("append(String|Element|Array<Element>|jQuery)", function() {
397 var defaultText = 'Try them out:'
398 var result = $('#first').append('<b>buga</b>');
399 ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
400 ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
403 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
404 $('#sap').append(document.getElementById('first'));
405 ok( expected == $('#sap').text(), "Check for appending of element" );
408 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
409 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
410 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
413 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
414 $('#sap').append($("#first, #yahoo"));
415 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
418 $("#sap").append( 5 );
419 ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
422 $("#sap").append( " text with spaces " );
423 ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
426 ok( $("#sap").append([]), "Check for appending an empty array." );
427 ok( $("#sap").append(""), "Check for appending an empty string." );
428 ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
431 $("#sap").append(document.getElementById('form'));
432 ok( $("#sap>form").size() == 1, "Check for appending a form" ); // Bug #910
437 $( $("iframe")[0].contentWindow.document.body ).append("<div>test</div>");
442 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
445 $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
446 t( 'Append legend', '#legend', ['legend'] );
449 $('#select1').append('<OPTION>Test</OPTION>');
450 ok( $('#select1 option:last').text() == "Test", "Appending <OPTION> (all caps)" );
452 $('#table').append('<colgroup></colgroup>');
453 ok( $('#table colgroup').length, "Append colgroup" );
455 $('#table colgroup').append('<col/>');
456 ok( $('#table colgroup col').length, "Append col" );
459 $('#table').append('<caption></caption>');
460 ok( $('#table caption').length, "Append caption" );
464 .append('<select id="appendSelect1"></select>')
465 .append('<select id="appendSelect2"><option>Test</option></select>');
467 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
470 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
472 var defaultText = 'Try them out:'
473 $('<b>buga</b>').appendTo('#first');
474 ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
475 ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
478 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
479 $(document.getElementById('first')).appendTo('#sap');
480 ok( expected == $('#sap').text(), "Check for appending of element" );
483 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
484 $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
485 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
488 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
489 $("#first, #yahoo").appendTo('#sap');
490 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
493 $('#select1').appendTo('#foo');
494 t( 'Append select', '#foo select', ['select1'] );
497 test("prepend(String|Element|Array<Element>|jQuery)", function() {
499 var defaultText = 'Try them out:'
500 var result = $('#first').prepend('<b>buga</b>');
501 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
502 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
505 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
506 $('#sap').prepend(document.getElementById('first'));
507 ok( expected == $('#sap').text(), "Check for prepending of element" );
510 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
511 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
512 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
515 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
516 $('#sap').prepend($("#first, #yahoo"));
517 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
520 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
522 var defaultText = 'Try them out:'
523 $('<b>buga</b>').prependTo('#first');
524 ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
525 ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
528 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
529 $(document.getElementById('first')).prependTo('#sap');
530 ok( expected == $('#sap').text(), "Check for prepending of element" );
533 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
534 $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
535 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
538 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
539 $("#yahoo, #first").prependTo('#sap');
540 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
543 $('<select id="prependSelect1"></select>').prependTo('form:last');
544 $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
546 t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
549 test("before(String|Element|Array<Element>|jQuery)", function() {
551 var expected = 'This is a normal link: bugaYahoo';
552 $('#yahoo').before('<b>buga</b>');
553 ok( expected == $('#en').text(), 'Insert String before' );
556 expected = "This is a normal link: Try them out:Yahoo";
557 $('#yahoo').before(document.getElementById('first'));
558 ok( expected == $('#en').text(), "Insert element before" );
561 expected = "This is a normal link: Try them out:diveintomarkYahoo";
562 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
563 ok( expected == $('#en').text(), "Insert array of elements before" );
566 expected = "This is a normal link: Try them out:diveintomarkYahoo";
567 $('#yahoo').before($("#first, #mark"));
568 ok( expected == $('#en').text(), "Insert jQuery before" );
571 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
573 var expected = 'This is a normal link: bugaYahoo';
574 $('<b>buga</b>').insertBefore('#yahoo');
575 ok( expected == $('#en').text(), 'Insert String before' );
578 expected = "This is a normal link: Try them out:Yahoo";
579 $(document.getElementById('first')).insertBefore('#yahoo');
580 ok( expected == $('#en').text(), "Insert element before" );
583 expected = "This is a normal link: Try them out:diveintomarkYahoo";
584 $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
585 ok( expected == $('#en').text(), "Insert array of elements before" );
588 expected = "This is a normal link: Try them out:diveintomarkYahoo";
589 $("#first, #mark").insertBefore('#yahoo');
590 ok( expected == $('#en').text(), "Insert jQuery before" );
593 test("after(String|Element|Array<Element>|jQuery)", function() {
595 var expected = 'This is a normal link: Yahoobuga';
596 $('#yahoo').after('<b>buga</b>');
597 ok( expected == $('#en').text(), 'Insert String after' );
600 expected = "This is a normal link: YahooTry them out:";
601 $('#yahoo').after(document.getElementById('first'));
602 ok( expected == $('#en').text(), "Insert element after" );
605 expected = "This is a normal link: YahooTry them out:diveintomark";
606 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
607 ok( expected == $('#en').text(), "Insert array of elements after" );
610 expected = "This is a normal link: YahooTry them out:diveintomark";
611 $('#yahoo').after($("#first, #mark"));
612 ok( expected == $('#en').text(), "Insert jQuery after" );
615 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
617 var expected = 'This is a normal link: Yahoobuga';
618 $('<b>buga</b>').insertAfter('#yahoo');
619 ok( expected == $('#en').text(), 'Insert String after' );
622 expected = "This is a normal link: YahooTry them out:";
623 $(document.getElementById('first')).insertAfter('#yahoo');
624 ok( expected == $('#en').text(), "Insert element after" );
627 expected = "This is a normal link: YahooTry them out:diveintomark";
628 $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
629 ok( expected == $('#en').text(), "Insert array of elements after" );
632 expected = "This is a normal link: YahooTry them out:diveintomark";
633 $("#mark, #first").insertAfter('#yahoo');
634 ok( expected == $('#en').text(), "Insert jQuery after" );
637 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
639 $('#yahoo').replaceWith('<b id="replace">buga</b>');
640 ok( $("#replace")[0], 'Replace element with string' );
641 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
644 $('#yahoo').replaceWith(document.getElementById('first'));
645 ok( $("#first")[0], 'Replace element with element' );
646 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
649 $('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
650 ok( $("#first")[0], 'Replace element with array of elements' );
651 ok( $("#mark")[0], 'Replace element with array of elements' );
652 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
655 $('#yahoo').replaceWith($("#first, #mark"));
656 ok( $("#first")[0], 'Replace element with set of elements' );
657 ok( $("#mark")[0], 'Replace element with set of elements' );
658 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
661 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
663 $('<b id="replace">buga</b>').replaceAll("#yahoo");
664 ok( $("#replace")[0], 'Replace element with string' );
665 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
668 $(document.getElementById('first')).replaceAll("#yahoo");
669 ok( $("#first")[0], 'Replace element with element' );
670 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
673 $([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
674 ok( $("#first")[0], 'Replace element with array of elements' );
675 ok( $("#mark")[0], 'Replace element with array of elements' );
676 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
679 $("#first, #mark").replaceAll("#yahoo");
680 ok( $("#first")[0], 'Replace element with set of elements' );
681 ok( $("#mark")[0], 'Replace element with set of elements' );
682 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
685 test("end()", function() {
687 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
688 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
692 ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
695 test("find(String)", function() {
697 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
700 test("clone()", function() {
702 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
703 var clone = $('#yahoo').clone();
704 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
705 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
708 test("is(String)", function() {
710 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
711 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
712 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
713 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
714 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
715 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
716 ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
717 ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
718 ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
719 ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
720 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
721 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
722 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
723 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
724 ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
725 ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
726 ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
727 ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
728 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
729 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
730 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
731 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
733 // test is() with comma-seperated expressions
734 ok( $('#en').is('[@lang="en"],[@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
735 ok( $('#en').is('[@lang="de"],[@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
736 ok( $('#en').is('[@lang="en"] , [@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
737 ok( $('#en').is('[@lang="de"] , [@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
740 test("$.extend(Object, Object)", function() {
743 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
744 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
745 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
746 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
747 deep1 = { foo: { bar: true } },
748 deep1copy = { foo: { bar: true } },
749 deep2 = { foo: { baz: true } },
750 deep2copy = { foo: { baz: true } },
751 deepmerged = { foo: { bar: true, baz: true } };
753 jQuery.extend(settings, options);
754 isObj( settings, merged, "Check if extended: settings must be extended" );
755 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
757 jQuery.extend(settings, null, options);
758 isObj( settings, merged, "Check if extended: settings must be extended" );
759 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
761 jQuery.extend(true, deep1, deep2);
762 isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
763 isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
765 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
766 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
767 options1 = { xnumber2: 1, xstring2: "x" },
768 options1Copy = { xnumber2: 1, xstring2: "x" },
769 options2 = { xstring2: "xx", xxx: "newstringx" },
770 options2Copy = { xstring2: "xx", xxx: "newstringx" },
771 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
773 var settings = jQuery.extend({}, defaults, options1, options2);
774 isObj( settings, merged2, "Check if extended: settings must be extended" );
775 isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
776 isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
777 isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
780 test("val()", function() {
782 ok( $("#text1").val() == "Test", "Check for value of input element" );
783 ok( !$("#text1").val() == "", "Check for value of input element" );
786 test("val(String)", function() {
788 document.getElementById('text1').value = "bla";
789 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
790 $("#text1").val('test');
791 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
794 test("html(String)", function() {
797 div.html("<b>test</b>");
799 for ( var i = 0; i < div.size(); i++ ) {
800 if ( div.get(i).childNodes.length == 0 ) pass = false;
802 ok( pass, "Set HTML" );
804 // Ccommented out until we can resolve it
805 // $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>').evalScripts();
808 test("filter()", function() {
810 isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
811 isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
812 isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
813 isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
816 test("not()", function() {
818 ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
819 isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
820 isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
823 test("siblings([String])", function() {
825 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
826 isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" );
827 isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
828 isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "floatTest"), "Check for multiple filters" );
829 isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );
832 test("children([String])", function() {
834 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
835 isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );
836 isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
839 test("parent([String])", function() {
841 ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
842 ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
843 ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
844 ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
845 isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
848 test("parents([String])", function() {
850 ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
851 ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
852 ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
853 isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
854 isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );
857 test("next([String])", function() {
859 ok( $("#ap").next()[0].id == "foo", "Simple next check" );
860 ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
861 ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
862 ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
865 test("prev([String])", function() {
867 ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
868 ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
869 ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
870 ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
873 test("show()", function() {
875 var pass = true, div = $("div");
876 div.show().each(function(){
877 if ( this.style.display == "none" ) pass = false;
882 test("addClass(String)", function() {
885 div.addClass("test");
887 for ( var i = 0; i < div.size(); i++ ) {
888 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
890 ok( pass, "Add Class" );
893 test("removeClass(String) - simple", function() {
895 var div = $("div").addClass("test").removeClass("test"),
897 for ( var i = 0; i < div.size(); i++ ) {
898 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
900 ok( pass, "Remove Class" );
903 var div = $("div").addClass("test").addClass("foo").addClass("bar");
904 div.removeClass("test").removeClass("bar").removeClass("foo");
906 for ( var i = 0; i < div.size(); i++ ) {
907 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
909 ok( pass, "Remove multiple classes" );
912 var div = $("div:eq(0)").addClass("test").removeClass("");
913 ok( div.is('.test'), "Empty string passed to removeClass" );
917 test("toggleClass(String)", function() {
919 var e = $("#firstp");
920 ok( !e.is(".test"), "Assert class not present" );
921 e.toggleClass("test");
922 ok( e.is(".test"), "Assert class present" );
923 e.toggleClass("test");
924 ok( !e.is(".test"), "Assert class not present" );
927 test("removeAttr(String", function() {
929 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
932 test("text(String)", function() {
934 ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
937 test("$.each(Object,Function)", function() {
939 $.each( [0,1,2], function(i, n){
940 ok( i == n, "Check array iteration" );
943 $.each( [5,6,7], function(i, n){
944 ok( i == n - 5, "Check array iteration" );
947 $.each( { name: "name", lang: "lang" }, function(i, n){
948 ok( i == n, "Check object iteration" );
952 test("$.prop", function() {
954 var handle = function() { return this.id };
955 ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
956 ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
959 test("$.className", function() {
961 var x = $("<p>Hi</p>")[0];
964 ok( x.className == "hi", "Check single added class" );
966 ok( x.className == "hi foo bar", "Check more added classes" );
968 ok( x.className == "", "Remove all classes" );
969 c.add(x, "hi foo bar");
971 ok( x.className == "hi bar", "Check removal of one class" );
972 ok( c.has(x, "hi"), "Check has1" );
973 ok( c.has(x, "bar"), "Check has2" );
976 test("remove()", function() {
978 $("#ap").children().remove();
979 ok( $("#ap").text().length > 10, "Check text is not removed" );
980 ok( $("#ap").children().length == 0, "Check remove" );
983 $("#ap").children().remove("a");
984 ok( $("#ap").text().length > 10, "Check text is not removed" );
985 ok( $("#ap").children().length == 1, "Check filtered remove" );
988 test("empty()", function() {
990 ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
991 ok( $("#ap").children().length == 4, "Check elements are not removed" );
994 test("eq(), gt(), lt(), contains()", function() {
996 ok( $("#ap a").eq(1)[0].id == "groups", "eq()" );
997 isSet( $("#ap a").gt(0).get(), q("groups", "anchor1", "mark"), "gt()" );
998 isSet( $("#ap a").lt(3).get(), q("google", "groups", "anchor1"), "lt()" );
999 isSet( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" );
1002 test("slice()", function() {
1004 isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" );
1005 isSet( $("#ap a").slice(1), q("groups", "anchor1", "mark"), "slice(1)" );
1006 isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
1007 isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" );
1010 test("map()", function() {
1014 $("#ap").map(function(){
1015 return $(this).find("a").get();
1017 q("google", "groups", "anchor1", "mark"),
1022 $("#ap > a").map(function(){
1023 return this.parentNode;
1030 test("contents()", function() {
1032 equals( $("#ap").contents().length, 9, "Check element contents" );
1033 ok( $("#iframe").contents()[0], "Check existance of IFrame document" );
1034 ok( $("#iframe").contents()[0].body, "Check existance of IFrame body" );