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("isFunction", function() {
44 // Make sure that false values return false
45 ok( !jQuery.isFunction(), "No Value" );
46 ok( !jQuery.isFunction( null ), "null Value" );
47 ok( !jQuery.isFunction( undefined ), "undefined Value" );
48 ok( !jQuery.isFunction( "" ), "Empty String Value" );
49 ok( !jQuery.isFunction( 0 ), "0 Value" );
52 // Safari uses "(Internal Function)"
53 ok( jQuery.isFunction(String), "String Function" );
54 ok( jQuery.isFunction(Array), "Array Function" );
55 ok( jQuery.isFunction(Object), "Object Function" );
56 ok( jQuery.isFunction(Function), "Function Function" );
58 // When stringified, this could be misinterpreted
59 var mystr = "function";
60 ok( !jQuery.isFunction(mystr), "Function String" );
62 // When stringified, this could be misinterpreted
63 var myarr = [ "function" ];
64 ok( !jQuery.isFunction(myarr), "Function Array" );
66 // When stringified, this could be misinterpreted
67 var myfunction = { "function": "test" };
68 ok( !jQuery.isFunction(myfunction), "Function Object" );
70 // Make sure normal functions still work
71 var fn = function(){};
72 ok( jQuery.isFunction(fn), "Normal Function" );
74 var obj = document.createElement("object");
76 // Firefox says this is a function
77 ok( !jQuery.isFunction(obj), "Object Element" );
79 // IE says this is an object
80 ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
82 var nodes = document.body.childNodes;
84 // Safari says this is a function
85 ok( !jQuery.isFunction(nodes), "childNodes Property" );
87 var first = document.body.firstChild;
89 // Normal elements are reported ok everywhere
90 ok( !jQuery.isFunction(first), "A normal DOM Element" );
92 var input = document.createElement("input");
94 document.body.appendChild( input );
96 // IE says this is an object
97 ok( jQuery.isFunction(input.focus), "A default function property" );
99 document.body.removeChild( input );
101 var a = document.createElement("a");
102 a.href = "some-function";
103 document.body.appendChild( a );
105 // This serializes with the word 'function' in it
106 ok( !jQuery.isFunction(a), "Anchor Element" );
108 document.body.removeChild( a );
110 // Recursive function calls have lengths and array-like properties
111 function callme(callback){
112 function fn(response){
116 ok( jQuery.isFunction(fn), "Recursive Function Call" );
118 fn({ some: "data" });
122 callme(function(){});
128 test("$('html')", function() {
133 var s = $("<script>var foo='test';</script>")[0];
134 ok( s, "Creating a script" );
135 ok( !foo, "Make sure the script wasn't executed prematurely" );
137 ok( foo, "Executing a scripts contents in the right context" );
140 ok( $("<link rel='stylesheet'/>")[0], "Creating a link" );
145 test("length", function() {
147 ok( $("p").length == 6, "Get Number of Elements Found" );
150 test("size()", function() {
152 ok( $("p").size() == 6, "Get Number of Elements Found" );
155 test("get()", function() {
157 isSet( $("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
160 test("get(Number)", function() {
162 ok( $("p").get(0) == document.getElementById("firstp"), "Get A Single Element" );
165 test("add(String|Element|Array)", function() {
167 isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
168 isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
169 ok( $([]).add($("#form")[0].elements).length >= 13, "Check elements from array" );
171 var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
172 ok( x[0].id == "x1", "Check on-the-fly element1" );
173 ok( x[1].id == "x2", "Check on-the-fly element2" );
175 var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
176 ok( x[0].id == "x1", "Check on-the-fly element1" );
177 ok( x[1].id == "x2", "Check on-the-fly element2" );
180 test("each(Function)", function() {
183 div.each(function(){this.foo = 'zoo';});
185 for ( var i = 0; i < div.size(); i++ ) {
186 if ( div.get(i).foo != "zoo" ) pass = false;
188 ok( pass, "Execute a function, Relative" );
191 test("index(Object)", function() {
193 ok( $([window, document]).index(window) == 0, "Check for index of elements" );
194 ok( $([window, document]).index(document) == 1, "Check for index of elements" );
195 var inputElements = $('#radio1,#radio2,#check1,#check2');
196 ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
197 ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
198 ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
199 ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
200 ok( inputElements.index(window) == -1, "Check for not found index" );
201 ok( inputElements.index(document) == -1, "Check for not found index" );
204 test("attr(String)", function() {
206 ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
207 ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
208 ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
209 ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
210 ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
211 ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
212 ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
213 ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
214 ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
215 ok( $('#name').attr('name') == "name", 'Check for name attribute' );
216 ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
217 ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
219 $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
220 ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
224 test("attr(String) in XML Files", function() {
227 $.get("data/dashboard.xml", function(xml) {
228 ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
229 ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
235 test("attr(String, Function)", function() {
237 ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
238 ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
241 test("attr(Hash)", function() {
244 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
245 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
247 ok( pass, "Set Multiple Attributes" );
250 test("attr(String, Object)", function() {
253 div.attr("foo", "bar");
255 for ( var i = 0; i < div.size(); i++ ) {
256 if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
258 ok( pass, "Set Attribute" );
260 ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
262 $("#name").attr('name', 'something');
263 ok( $("#name").attr('name') == 'something', 'Set name attribute' );
264 $("#check2").attr('checked', true);
265 ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
266 $("#check2").attr('checked', false);
267 ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
268 $("#text1").attr('readonly', true);
269 ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
270 $("#text1").attr('readonly', false);
271 ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
272 $("#name").attr('maxlength', '5');
273 ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' );
277 var type = $("#check2").attr('type');
280 $("#check2").attr('type','hidden');
284 ok( thrown, "Exception thrown when trying to change type property" );
285 equals( type, $("#check2").attr('type'), "Verify that you can't change the type of an input element" );
287 var check = document.createElement("input");
290 $(check).attr('type','checkbox');
294 ok( thrown, "Exception thrown when trying to change type property" );
295 equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
299 test("attr(String, Object) - Loaded via XML document", function() {
302 $.get('data/dashboard.xml', function(xml) {
304 $('tab', xml).each(function() {
305 titles.push($(this).attr('title'));
307 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
308 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
314 test("css(String|Hash)", function() {
317 ok( $('#main').css("display") == 'none', 'Check for css property "display"');
319 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
320 $('#foo').css({display: 'none'});
321 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
322 $('#foo').css({display: 'block'});
323 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
325 $('#floatTest').css({styleFloat: 'right'});
326 ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
327 $('#floatTest').css({cssFloat: 'left'});
328 ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
329 $('#floatTest').css({'float': 'right'});
330 ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
331 $('#floatTest').css({'font-size': '30px'});
332 ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
334 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
335 $('#foo').css({opacity: n});
336 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
337 $('#foo').css({opacity: parseFloat(n)});
338 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
340 $('#foo').css({opacity: ''});
341 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
344 test("css(String, Object)", function() {
346 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
347 $('#foo').css('display', 'none');
348 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
349 $('#foo').css('display', 'block');
350 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
352 $('#floatTest').css('styleFloat', 'left');
353 ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
354 $('#floatTest').css('cssFloat', 'right');
355 ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
356 $('#floatTest').css('float', 'left');
357 ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
358 $('#floatTest').css('font-size', '20px');
359 ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
361 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
362 $('#foo').css('opacity', n);
363 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
364 $('#foo').css('opacity', parseFloat(n));
365 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
367 $('#foo').css('opacity', '');
368 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
371 test("text()", function() {
373 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
374 ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
377 test("wrap(String|Element)", function() {
379 var defaultText = 'Try them out:'
380 var result = $('#first').wrap('<div class="red"><span></span></div>').text();
381 ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
382 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
385 var defaultText = 'Try them out:'
386 var result = $('#first').wrap(document.getElementById('empty')).parent();
387 ok( result.is('ol'), 'Check for element wrapping' );
388 ok( result.text() == defaultText, 'Check for element wrapping' );
391 $('#check1').click(function() {
393 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
394 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
395 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
399 test("wrapAll(String|Element)", function() {
401 var prev = $("#first")[0].previousSibling;
402 var p = $("#first")[0].parentNode;
403 var result = $('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>');
404 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
405 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
406 ok( $('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
407 equals( $("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
408 equals( $("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
411 var prev = $("#first")[0].previousSibling;
412 var p = $("#first")[0].parentNode;
413 var result = $('#first,#firstp').wrapAll(document.getElementById('empty'));
414 equals( $("#first").parent()[0], $("#firstp").parent()[0], "Same Parent" );
415 equals( $("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
416 equals( $("#first").parent()[0].parentNode, p, "Correct Parent" );
419 test("wrapInner(String|Element)", function() {
421 var num = $("#first").children().length;
422 var result = $('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
423 equals( $("#first").children().length, 1, "Only one child" );
424 ok( $("#first").children().is(".red"), "Verify Right Element" );
425 equals( $("#first").children().children().children().length, num, "Verify Elements Intact" );
428 var num = $("#first").children().length;
429 var result = $('#first').wrapInner(document.getElementById('empty'));
430 equals( $("#first").children().length, 1, "Only one child" );
431 ok( $("#first").children().is("#empty"), "Verify Right Element" );
432 equals( $("#first").children().children().length, num, "Verify Elements Intact" );
435 test("append(String|Element|Array<Element>|jQuery)", function() {
437 var defaultText = 'Try them out:'
438 var result = $('#first').append('<b>buga</b>');
439 ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
440 ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
443 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
444 $('#sap').append(document.getElementById('first'));
445 ok( expected == $('#sap').text(), "Check for appending of element" );
448 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
449 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
450 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
453 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
454 $('#sap').append($("#first, #yahoo"));
455 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
458 $("#sap").append( 5 );
459 ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
462 $("#sap").append( " text with spaces " );
463 ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
466 ok( $("#sap").append([]), "Check for appending an empty array." );
467 ok( $("#sap").append(""), "Check for appending an empty string." );
468 ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
471 $("#sap").append(document.getElementById('form'));
472 ok( $("#sap>form").size() == 1, "Check for appending a form" ); // Bug #910
477 $( $("iframe")[0].contentWindow.document.body ).append("<div>test</div>");
482 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
485 $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
486 t( 'Append legend', '#legend', ['legend'] );
489 $('#select1').append('<OPTION>Test</OPTION>');
490 ok( $('#select1 option:last').text() == "Test", "Appending <OPTION> (all caps)" );
492 $('#table').append('<colgroup></colgroup>');
493 ok( $('#table colgroup').length, "Append colgroup" );
495 $('#table colgroup').append('<col/>');
496 ok( $('#table colgroup col').length, "Append col" );
499 $('#table').append('<caption></caption>');
500 ok( $('#table caption').length, "Append caption" );
504 .append('<select id="appendSelect1"></select>')
505 .append('<select id="appendSelect2"><option>Test</option></select>');
507 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
510 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
512 var defaultText = 'Try them out:'
513 $('<b>buga</b>').appendTo('#first');
514 ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
515 ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
518 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
519 $(document.getElementById('first')).appendTo('#sap');
520 ok( expected == $('#sap').text(), "Check for appending of element" );
523 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
524 $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
525 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
528 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
529 $("#first, #yahoo").appendTo('#sap');
530 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
533 $('#select1').appendTo('#foo');
534 t( 'Append select', '#foo select', ['select1'] );
537 test("prepend(String|Element|Array<Element>|jQuery)", function() {
539 var defaultText = 'Try them out:'
540 var result = $('#first').prepend('<b>buga</b>');
541 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
542 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
545 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
546 $('#sap').prepend(document.getElementById('first'));
547 ok( expected == $('#sap').text(), "Check for prepending of element" );
550 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
551 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
552 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
555 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
556 $('#sap').prepend($("#first, #yahoo"));
557 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
560 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
562 var defaultText = 'Try them out:'
563 $('<b>buga</b>').prependTo('#first');
564 ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
565 ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
568 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
569 $(document.getElementById('first')).prependTo('#sap');
570 ok( expected == $('#sap').text(), "Check for prepending of element" );
573 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
574 $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
575 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
578 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
579 $("#yahoo, #first").prependTo('#sap');
580 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
583 $('<select id="prependSelect1"></select>').prependTo('form:last');
584 $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
586 t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
589 test("before(String|Element|Array<Element>|jQuery)", function() {
591 var expected = 'This is a normal link: bugaYahoo';
592 $('#yahoo').before('<b>buga</b>');
593 ok( expected == $('#en').text(), 'Insert String before' );
596 expected = "This is a normal link: Try them out:Yahoo";
597 $('#yahoo').before(document.getElementById('first'));
598 ok( expected == $('#en').text(), "Insert element before" );
601 expected = "This is a normal link: Try them out:diveintomarkYahoo";
602 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
603 ok( expected == $('#en').text(), "Insert array of elements before" );
606 expected = "This is a normal link: Try them out:diveintomarkYahoo";
607 $('#yahoo').before($("#first, #mark"));
608 ok( expected == $('#en').text(), "Insert jQuery before" );
611 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
613 var expected = 'This is a normal link: bugaYahoo';
614 $('<b>buga</b>').insertBefore('#yahoo');
615 ok( expected == $('#en').text(), 'Insert String before' );
618 expected = "This is a normal link: Try them out:Yahoo";
619 $(document.getElementById('first')).insertBefore('#yahoo');
620 ok( expected == $('#en').text(), "Insert element before" );
623 expected = "This is a normal link: Try them out:diveintomarkYahoo";
624 $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
625 ok( expected == $('#en').text(), "Insert array of elements before" );
628 expected = "This is a normal link: Try them out:diveintomarkYahoo";
629 $("#first, #mark").insertBefore('#yahoo');
630 ok( expected == $('#en').text(), "Insert jQuery before" );
633 test("after(String|Element|Array<Element>|jQuery)", function() {
635 var expected = 'This is a normal link: Yahoobuga';
636 $('#yahoo').after('<b>buga</b>');
637 ok( expected == $('#en').text(), 'Insert String after' );
640 expected = "This is a normal link: YahooTry them out:";
641 $('#yahoo').after(document.getElementById('first'));
642 ok( expected == $('#en').text(), "Insert element after" );
645 expected = "This is a normal link: YahooTry them out:diveintomark";
646 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
647 ok( expected == $('#en').text(), "Insert array of elements after" );
650 expected = "This is a normal link: YahooTry them out:diveintomark";
651 $('#yahoo').after($("#first, #mark"));
652 ok( expected == $('#en').text(), "Insert jQuery after" );
655 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
657 var expected = 'This is a normal link: Yahoobuga';
658 $('<b>buga</b>').insertAfter('#yahoo');
659 ok( expected == $('#en').text(), 'Insert String after' );
662 expected = "This is a normal link: YahooTry them out:";
663 $(document.getElementById('first')).insertAfter('#yahoo');
664 ok( expected == $('#en').text(), "Insert element after" );
667 expected = "This is a normal link: YahooTry them out:diveintomark";
668 $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
669 ok( expected == $('#en').text(), "Insert array of elements after" );
672 expected = "This is a normal link: YahooTry them out:diveintomark";
673 $("#mark, #first").insertAfter('#yahoo');
674 ok( expected == $('#en').text(), "Insert jQuery after" );
677 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
679 $('#yahoo').replaceWith('<b id="replace">buga</b>');
680 ok( $("#replace")[0], 'Replace element with string' );
681 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
684 $('#yahoo').replaceWith(document.getElementById('first'));
685 ok( $("#first")[0], 'Replace element with element' );
686 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
689 $('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
690 ok( $("#first")[0], 'Replace element with array of elements' );
691 ok( $("#mark")[0], 'Replace element with array of elements' );
692 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
695 $('#yahoo').replaceWith($("#first, #mark"));
696 ok( $("#first")[0], 'Replace element with set of elements' );
697 ok( $("#mark")[0], 'Replace element with set of elements' );
698 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
701 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
703 $('<b id="replace">buga</b>').replaceAll("#yahoo");
704 ok( $("#replace")[0], 'Replace element with string' );
705 ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
708 $(document.getElementById('first')).replaceAll("#yahoo");
709 ok( $("#first")[0], 'Replace element with element' );
710 ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
713 $([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
714 ok( $("#first")[0], 'Replace element with array of elements' );
715 ok( $("#mark")[0], 'Replace element with array of elements' );
716 ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
719 $("#first, #mark").replaceAll("#yahoo");
720 ok( $("#first")[0], 'Replace element with set of elements' );
721 ok( $("#mark")[0], 'Replace element with set of elements' );
722 ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
725 test("end()", function() {
727 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
728 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
732 ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
735 test("find(String)", function() {
737 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
740 test("clone()", function() {
742 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
743 var clone = $('#yahoo').clone();
744 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
745 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
748 test("is(String)", function() {
750 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
751 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
752 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
753 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
754 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
755 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
756 ok( $('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
757 ok( !$('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
758 ok( $('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
759 ok( !$('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
760 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
761 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
762 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
763 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
764 ok( $('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );
765 ok( !$('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );
766 ok( $('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );
767 ok( !$('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
768 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
769 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
770 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
771 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
773 // test is() with comma-seperated expressions
774 ok( $('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
775 ok( $('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
776 ok( $('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
777 ok( $('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
780 test("$.extend(Object, Object)", function() {
783 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
784 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
785 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
786 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
787 deep1 = { foo: { bar: true } },
788 deep1copy = { foo: { bar: true } },
789 deep2 = { foo: { baz: true } },
790 deep2copy = { foo: { baz: true } },
791 deepmerged = { foo: { bar: true, baz: true } };
793 jQuery.extend(settings, options);
794 isObj( settings, merged, "Check if extended: settings must be extended" );
795 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
797 jQuery.extend(settings, null, options);
798 isObj( settings, merged, "Check if extended: settings must be extended" );
799 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
801 jQuery.extend(true, deep1, deep2);
802 isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
803 isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
805 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
806 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
807 options1 = { xnumber2: 1, xstring2: "x" },
808 options1Copy = { xnumber2: 1, xstring2: "x" },
809 options2 = { xstring2: "xx", xxx: "newstringx" },
810 options2Copy = { xstring2: "xx", xxx: "newstringx" },
811 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
813 var settings = jQuery.extend({}, defaults, options1, options2);
814 isObj( settings, merged2, "Check if extended: settings must be extended" );
815 isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
816 isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
817 isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
820 test("val()", function() {
822 ok( $("#text1").val() == "Test", "Check for value of input element" );
823 ok( !$("#text1").val() == "", "Check for value of input element" );
826 test("val(String)", function() {
828 document.getElementById('text1').value = "bla";
829 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
830 $("#text1").val('test');
831 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
834 test("html(String)", function() {
837 div.html("<b>test</b>");
839 for ( var i = 0; i < div.size(); i++ ) {
840 if ( div.get(i).childNodes.length == 0 ) pass = false;
842 ok( pass, "Set HTML" );
846 $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>');
848 $("#main").html('foo <form><script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
850 setTimeout( start, 100 );
853 test("filter()", function() {
855 isSet( $("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
856 isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
857 isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
858 isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
861 test("not()", function() {
863 ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
864 isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
865 isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
868 test("andSelf()", function() {
870 isSet( $("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
871 isSet( $("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
872 isSet( $("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" );
873 isSet( $("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" );
876 test("siblings([String])", function() {
878 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
879 isSet( $("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
880 isSet( $("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
881 isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" );
882 isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );
885 test("children([String])", function() {
887 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
888 isSet( $("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
889 isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
892 test("parent([String])", function() {
894 ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
895 ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
896 ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
897 ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
898 isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
901 test("parents([String])", function() {
903 ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
904 ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
905 ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
906 isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
907 isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );
910 test("next([String])", function() {
912 ok( $("#ap").next()[0].id == "foo", "Simple next check" );
913 ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
914 ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
915 ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
918 test("prev([String])", function() {
920 ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
921 ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
922 ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
923 ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
926 test("show()", function() {
928 var pass = true, div = $("div");
929 div.show().each(function(){
930 if ( this.style.display == "none" ) pass = false;
935 test("addClass(String)", function() {
938 div.addClass("test");
940 for ( var i = 0; i < div.size(); i++ ) {
941 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
943 ok( pass, "Add Class" );
946 test("removeClass(String) - simple", function() {
948 var div = $("div").addClass("test").removeClass("test"),
950 for ( var i = 0; i < div.size(); i++ ) {
951 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
953 ok( pass, "Remove Class" );
956 var div = $("div").addClass("test").addClass("foo").addClass("bar");
957 div.removeClass("test").removeClass("bar").removeClass("foo");
959 for ( var i = 0; i < div.size(); i++ ) {
960 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
962 ok( pass, "Remove multiple classes" );
965 var div = $("div:eq(0)").addClass("test").removeClass("");
966 ok( div.is('.test'), "Empty string passed to removeClass" );
970 test("toggleClass(String)", function() {
972 var e = $("#firstp");
973 ok( !e.is(".test"), "Assert class not present" );
974 e.toggleClass("test");
975 ok( e.is(".test"), "Assert class present" );
976 e.toggleClass("test");
977 ok( !e.is(".test"), "Assert class not present" );
980 test("removeAttr(String", function() {
982 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
985 test("text(String)", function() {
987 ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
990 test("$.each(Object,Function)", function() {
992 $.each( [0,1,2], function(i, n){
993 ok( i == n, "Check array iteration" );
996 $.each( [5,6,7], function(i, n){
997 ok( i == n - 5, "Check array iteration" );
1000 $.each( { name: "name", lang: "lang" }, function(i, n){
1001 ok( i == n, "Check object iteration" );
1005 test("$.prop", function() {
1007 var handle = function() { return this.id };
1008 ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
1009 ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
1012 test("$.className", function() {
1014 var x = $("<p>Hi</p>")[0];
1015 var c = $.className;
1017 ok( x.className == "hi", "Check single added class" );
1018 c.add(x, "foo bar");
1019 ok( x.className == "hi foo bar", "Check more added classes" );
1021 ok( x.className == "", "Remove all classes" );
1022 c.add(x, "hi foo bar");
1024 ok( x.className == "hi bar", "Check removal of one class" );
1025 ok( c.has(x, "hi"), "Check has1" );
1026 ok( c.has(x, "bar"), "Check has2" );
1029 test("remove()", function() {
1031 $("#ap").children().remove();
1032 ok( $("#ap").text().length > 10, "Check text is not removed" );
1033 ok( $("#ap").children().length == 0, "Check remove" );
1036 $("#ap").children().remove("a");
1037 ok( $("#ap").text().length > 10, "Check text is not removed" );
1038 ok( $("#ap").children().length == 1, "Check filtered remove" );
1041 test("empty()", function() {
1043 ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
1044 ok( $("#ap").children().length == 4, "Check elements are not removed" );
1047 test("slice()", function() {
1049 isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" );
1050 isSet( $("#ap a").slice(1), q("groups", "anchor1", "mark"), "slice(1)" );
1051 isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
1052 isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" );
1054 isSet( $("#ap a").eq(1), q("groups"), "eq(1)" );
1057 test("map()", function() {
1061 $("#ap").map(function(){
1062 return $(this).find("a").get();
1064 q("google", "groups", "anchor1", "mark"),
1069 $("#ap > a").map(function(){
1070 return this.parentNode;
1077 test("contents()", function() {
1079 equals( $("#ap").contents().length, 9, "Check element contents" );
1080 ok( $("#iframe").contents()[0], "Check existance of IFrame document" );
1081 // Disabled, randomly fails
1082 //ok( $("#iframe").contents()[0].body, "Check existance of IFrame body" );