Fix for #1163
[jquery.git] / src / jquery / coreTest.js
1 module("core");
2
3 test("Basic requirements", function() {
4         expect(7);
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" );
11         ok( $, "$()" );
12 });
13
14 test("$()", function() {
15         expect(4);
16         
17         var main = $("#main");
18         isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
19         
20         // make sure this is handled
21         $('<p>\r\n</p>');
22         ok( true, "Check for \\r and \\n in jQuery()" );
23         
24         var pass = true;
25         try {
26                 var f = document.getElementById("iframe").contentDocument;
27                 f.open();
28                 f.write("<html><body></body></html>");
29                 f.close();
30                 $("<div>Testing</div>").appendTo(f.body);
31         } catch(e){
32                 pass = false;
33         }
34         ok( pass, "$('&lt;tag&gt;') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );
35         
36         var form = $("form")[0];
37         equals( 15, $(form.elements).size(), "$(form.elements)" );
38 });
39
40 test("isFunction", function() {
41         expect(21);
42
43         // Make sure that false values return false
44         ok( !jQuery.isFunction(), "No Value" );
45         ok( !jQuery.isFunction( null ), "null Value" );
46         ok( !jQuery.isFunction( undefined ), "undefined Value" );
47         ok( !jQuery.isFunction( "" ), "Empty String Value" );
48         ok( !jQuery.isFunction( 0 ), "0 Value" );
49
50         // Check built-ins
51         // Safari uses "(Internal Function)"
52         ok( jQuery.isFunction(String), "String Function" );
53         ok( jQuery.isFunction(Array), "Array Function" );
54         ok( jQuery.isFunction(Object), "Object Function" );
55         ok( jQuery.isFunction(Function), "Function Function" );
56
57         // When stringified, this could be misinterpreted
58         var mystr = "function";
59         ok( !jQuery.isFunction(mystr), "Function String" );
60
61         // When stringified, this could be misinterpreted
62         var myarr = [ "function" ];
63         ok( !jQuery.isFunction(myarr), "Function Array" );
64
65         // When stringified, this could be misinterpreted
66         var myfunction = { "function": "test" };
67         ok( !jQuery.isFunction(myfunction), "Function Object" );
68
69         // Make sure normal functions still work
70         var fn = function(){};
71         ok( jQuery.isFunction(fn), "Normal Function" );
72
73         var obj = document.createElement("object");
74
75         // Firefox says this is a function
76         ok( !jQuery.isFunction(obj), "Object Element" );
77
78         // IE says this is an object
79         ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
80
81         var nodes = document.body.childNodes;
82
83         // Safari says this is a function
84         ok( !jQuery.isFunction(nodes), "childNodes Property" );
85
86         var first = document.body.firstChild;
87         
88         // Normal elements are reported ok everywhere
89         ok( !jQuery.isFunction(first), "A normal DOM Element" );
90
91         var input = document.createElement("input");
92         input.type = "text";
93         document.body.appendChild( input );
94
95         // IE says this is an object
96         ok( jQuery.isFunction(input.focus), "A default function property" );
97
98         document.body.removeChild( input );
99
100         var a = document.createElement("a");
101         a.href = "some-function";
102         document.body.appendChild( a );
103
104         // This serializes with the word 'function' in it
105         ok( !jQuery.isFunction(a), "Anchor Element" );
106
107         document.body.removeChild( a );
108
109         // Recursive function calls have lengths and array-like properties
110         function callme(callback){
111                 function fn(response){
112                         callback(response);
113                 }
114
115                 ok( jQuery.isFunction(fn), "Recursive Function Call" );
116
117         fn({ some: "data" });
118         };
119
120         callme(function(){
121         callme(function(){});
122         });
123 });
124
125 test("length", function() {
126         expect(1);
127         ok( $("div").length == 2, "Get Number of Elements Found" );
128 });
129
130 test("size()", function() {
131         expect(1);
132         ok( $("div").size() == 2, "Get Number of Elements Found" );
133 });
134
135 test("get()", function() {
136         expect(1);
137         isSet( $("div").get(), q("main","foo"), "Get All Elements" );
138 });
139
140 test("get(Number)", function() {
141         expect(1);
142         ok( $("div").get(0) == document.getElementById("main"), "Get A Single Element" );
143 });
144
145 test("add(String|Element|Array)", function() {
146         expect(7);
147         isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
148         isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
149         ok( $([]).add($("#form")[0].elements).length > 13, "Check elements from array" );
150         
151         var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
152         ok( x[0].id == "x1", "Check on-the-fly element1" );
153         ok( x[1].id == "x2", "Check on-the-fly element2" );
154         
155         var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
156         ok( x[0].id == "x1", "Check on-the-fly element1" );
157         ok( x[1].id == "x2", "Check on-the-fly element2" );
158 });
159
160 test("each(Function)", function() {
161         expect(1);
162         var div = $("div");
163         div.each(function(){this.foo = 'zoo';});
164         var pass = true;
165         for ( var i = 0; i < div.size(); i++ ) {
166           if ( div.get(i).foo != "zoo" ) pass = false;
167         }
168         ok( pass, "Execute a function, Relative" );
169 });
170
171 test("index(Object)", function() {
172         expect(8);
173         ok( $([window, document]).index(window) == 0, "Check for index of elements" );
174         ok( $([window, document]).index(document) == 1, "Check for index of elements" );
175         var inputElements = $('#radio1,#radio2,#check1,#check2');
176         ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
177         ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
178         ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
179         ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
180         ok( inputElements.index(window) == -1, "Check for not found index" );
181         ok( inputElements.index(document) == -1, "Check for not found index" );
182 });
183
184 test("attr(String)", function() {
185         expect(13);
186         ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
187         ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
188         ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
189         ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
190         ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
191         ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
192         ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
193         ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
194         ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
195         ok( $('#name').attr('name') == "name", 'Check for name attribute' );
196         ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
197         ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
198         
199         $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
200         ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
201 });
202
203 if ( location.protocol != "file:" ) {
204         test("attr(String) in XML Files", function() {
205                 expect(2);
206                 stop();
207                 $.get("data/dashboard.xml", function(xml) {
208                         ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
209                         ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
210                         start();
211                 });
212         });
213 }
214
215 test("attr(String, Function)", function() {
216         expect(2);
217         ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
218         ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
219 });
220
221 test("attr(Hash)", function() {
222         expect(1);
223         var pass = true;
224         $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
225           if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
226         });
227         ok( pass, "Set Multiple Attributes" );
228 });
229
230 test("attr(String, Object)", function() {
231         expect(7);
232         var div = $("div");
233         div.attr("foo", "bar");
234         var pass = true;
235         for ( var i = 0; i < div.size(); i++ ) {
236           if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
237         }
238         ok( pass, "Set Attribute" );
239
240         ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );    
241         
242         $("#name").attr('name', 'something');
243         ok( $("#name").attr('name') == 'something', 'Set name attribute' );
244         $("#check2").attr('checked', true);
245         ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
246         $("#check2").attr('checked', false);
247         ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
248         $("#text1").attr('readonly', true);
249         ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
250         $("#text1").attr('readonly', false);
251         ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
252 });
253
254 if ( location.protocol != "file:" ) {
255         test("attr(String, Object)x", function() {
256                 expect(2);
257                 stop();
258                 $.get('data/dashboard.xml', function(xml) { 
259                 var titles = [];
260                 $('tab', xml).each(function() {
261                 titles.push($(this).attr('title'));
262                 });
263                 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
264                 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
265                 start();
266                 });
267         });
268 }
269
270 test("css(String|Hash)", function() {
271         expect(19);
272         
273         ok( $('#main').css("display") == 'none', 'Check for css property "display"');
274         
275         ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
276         $('#foo').css({display: 'none'});
277         ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
278         $('#foo').css({display: 'block'});
279         ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
280         
281         $('#floatTest').css({styleFloat: 'right'});
282         ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
283         $('#floatTest').css({cssFloat: 'left'});
284         ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
285         $('#floatTest').css({'float': 'right'});
286         ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
287         $('#floatTest').css({'font-size': '30px'});
288         ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
289         
290         $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
291                 $('#foo').css({opacity: n});
292                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
293                 $('#foo').css({opacity: parseFloat(n)});
294                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
295         });     
296         $('#foo').css({opacity: ''});
297         ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
298 });
299
300 test("css(String, Object)", function() {
301         expect(18);
302         ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
303         $('#foo').css('display', 'none');
304         ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
305         $('#foo').css('display', 'block');
306         ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
307         
308         $('#floatTest').css('styleFloat', 'left');
309         ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
310         $('#floatTest').css('cssFloat', 'right');
311         ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
312         $('#floatTest').css('float', 'left');
313         ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
314         $('#floatTest').css('font-size', '20px');
315         ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
316         
317         $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
318                 $('#foo').css('opacity', n);
319                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
320                 $('#foo').css('opacity', parseFloat(n));
321                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
322         });
323         $('#foo').css('opacity', '');
324         ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
325 });
326
327 test("text()", function() {
328         expect(1);
329         var expected = "This link has class=\"blog\": Simon Willison's Weblog";
330         ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
331 });
332
333 test("wrap(String|Element)", function() {
334         expect(7);
335         var defaultText = 'Try them out:'
336         var result = $('#first').wrap('<div class="red"><span></span></div>').text();
337         ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
338         ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
339
340         reset();
341         var defaultText = 'Try them out:'
342         var result = $('#first').wrap(document.getElementById('empty')).parent();
343         ok( result.is('ol'), 'Check for element wrapping' );
344         ok( result.text() == defaultText, 'Check for element wrapping' );
345         
346         reset();
347         stop();
348         $('#check1').click(function() {         
349                 var checkbox = this;            
350                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
351                 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
352                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
353                 // use a fade in to check state after this event handler has finished
354                 setTimeout(function() {
355                         ok( !checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
356                         start();
357                 }, 100);
358         }).click();
359 });
360
361 test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
362         expect(16);
363         var defaultText = 'Try them out:'
364         var result = $('#first').append('<b>buga</b>');
365         ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
366         ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
367         
368         reset();
369         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
370         $('#sap').append(document.getElementById('first'));
371         ok( expected == $('#sap').text(), "Check for appending of element" );
372         
373         reset();
374         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
375         $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
376         ok( expected == $('#sap').text(), "Check for appending of array of elements" );
377         
378         reset();
379         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
380         $('#sap').append($("#first, #yahoo"));
381         ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
382
383         reset();
384         $("#sap").append( 5 );
385         ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
386
387         reset();
388         $("#sap").append( " text with spaces " );
389         ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
390
391         reset();
392         ok( $("#sap").append([]), "Check for appending an empty array." );
393         ok( $("#sap").append(""), "Check for appending an empty string." );
394         ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
395         
396         reset();
397         $("#sap").append(document.getElementById('form'));
398         ok( $("#sap>form").size() == 1, "Check for appending a form" );  // Bug #910
399
400         reset();
401         var pass = true;
402         try {
403                 $( $("iframe")[0].contentWindow.document.body ).append("<div>test</div>");
404         } catch(e) {
405                 pass = false;
406         }
407
408         ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
409         
410         reset();
411         $('<fieldset>').appendTo('#form').append('<legend id="legend">test</legend>');
412         t( 'Append legend', '#legend', ['legend'] );
413         
414         reset();
415         $('#select1').append('<OPTION>Test</OPTION>');
416         ok( $('#select1 option:last').text() == "Test", "Appending &lt;OPTION&gt; (all caps)" );
417         
418         $('#table').append('<colgroup></colgroup>');
419         ok( $('#table colgroup').length, "Append colgroup" );
420         
421         $('#table colgroup').append('<col>');
422         ok( $('#table colgroup col').length, "Append col" );
423 });
424
425 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
426         expect(6);
427         var defaultText = 'Try them out:'
428         $('<b>buga</b>').appendTo('#first');
429         ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
430         ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
431         
432         reset();
433         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
434         $(document.getElementById('first')).appendTo('#sap');
435         ok( expected == $('#sap').text(), "Check for appending of element" );
436         
437         reset();
438         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
439         $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
440         ok( expected == $('#sap').text(), "Check for appending of array of elements" );
441         
442         reset();
443         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
444         $("#first, #yahoo").appendTo('#sap');
445         ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
446         
447         reset();
448         $('#select1').appendTo('#foo');
449         t( 'Append select', '#foo select', ['select1'] );
450 });
451
452 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
453         expect(5);
454         var defaultText = 'Try them out:'
455         var result = $('#first').prepend('<b>buga</b>');
456         ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
457         ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
458         
459         reset();
460         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
461         $('#sap').prepend(document.getElementById('first'));
462         ok( expected == $('#sap').text(), "Check for prepending of element" );
463
464         reset();
465         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
466         $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
467         ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
468         
469         reset();
470         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
471         $('#sap').prepend($("#first, #yahoo"));
472         ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
473 });
474
475 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
476         expect(5);
477         var defaultText = 'Try them out:'
478         $('<b>buga</b>').prependTo('#first');
479         ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
480         ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
481         
482         reset();
483         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
484         $(document.getElementById('first')).prependTo('#sap');
485         ok( expected == $('#sap').text(), "Check for prepending of element" );
486
487         reset();
488         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
489         $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
490         ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
491         
492         reset();
493         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
494         $("#yahoo, #first").prependTo('#sap');
495         ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
496 });
497
498 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
499         expect(4);
500         var expected = 'This is a normal link: bugaYahoo';
501         $('#yahoo').before('<b>buga</b>');
502         ok( expected == $('#en').text(), 'Insert String before' );
503         
504         reset();
505         expected = "This is a normal link: Try them out:Yahoo";
506         $('#yahoo').before(document.getElementById('first'));
507         ok( expected == $('#en').text(), "Insert element before" );
508         
509         reset();
510         expected = "This is a normal link: Try them out:diveintomarkYahoo";
511         $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
512         ok( expected == $('#en').text(), "Insert array of elements before" );
513         
514         reset();
515         expected = "This is a normal link: Try them out:diveintomarkYahoo";
516         $('#yahoo').before($("#first, #mark"));
517         ok( expected == $('#en').text(), "Insert jQuery before" );
518 });
519
520 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
521         expect(4);
522         var expected = 'This is a normal link: bugaYahoo';
523         $('<b>buga</b>').insertBefore('#yahoo');
524         ok( expected == $('#en').text(), 'Insert String before' );
525         
526         reset();
527         expected = "This is a normal link: Try them out:Yahoo";
528         $(document.getElementById('first')).insertBefore('#yahoo');
529         ok( expected == $('#en').text(), "Insert element before" );
530         
531         reset();
532         expected = "This is a normal link: Try them out:diveintomarkYahoo";
533         $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
534         ok( expected == $('#en').text(), "Insert array of elements before" );
535         
536         reset();
537         expected = "This is a normal link: Try them out:diveintomarkYahoo";
538         $("#first, #mark").insertBefore('#yahoo');
539         ok( expected == $('#en').text(), "Insert jQuery before" );
540 });
541
542 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
543         expect(4);
544         var expected = 'This is a normal link: Yahoobuga';
545         $('#yahoo').after('<b>buga</b>');
546         ok( expected == $('#en').text(), 'Insert String after' );
547         
548         reset();
549         expected = "This is a normal link: YahooTry them out:";
550         $('#yahoo').after(document.getElementById('first'));
551         ok( expected == $('#en').text(), "Insert element after" );
552
553         reset();
554         expected = "This is a normal link: YahooTry them out:diveintomark";
555         $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
556         ok( expected == $('#en').text(), "Insert array of elements after" );
557         
558         reset();
559         expected = "This is a normal link: YahooTry them out:diveintomark";
560         $('#yahoo').after($("#first, #mark"));
561         ok( expected == $('#en').text(), "Insert jQuery after" );
562 });
563
564 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
565         expect(4);
566         var expected = 'This is a normal link: Yahoobuga';
567         $('<b>buga</b>').insertAfter('#yahoo');
568         ok( expected == $('#en').text(), 'Insert String after' );
569         
570         reset();
571         expected = "This is a normal link: YahooTry them out:";
572         $(document.getElementById('first')).insertAfter('#yahoo');
573         ok( expected == $('#en').text(), "Insert element after" );
574
575         reset();
576         expected = "This is a normal link: YahooTry them out:diveintomark";
577         $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
578         ok( expected == $('#en').text(), "Insert array of elements after" );
579         
580         reset();
581         expected = "This is a normal link: YahooTry them out:diveintomark";
582         $("#mark, #first").insertAfter('#yahoo');
583         ok( expected == $('#en').text(), "Insert jQuery after" );
584 });
585
586 test("end()", function() {
587         expect(3);
588         ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
589         ok( $('#yahoo').end(), 'Check for end with nothing to end' );
590         
591         var x = $('#yahoo');
592         x.parent();
593         ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
594 });
595
596 test("find(String)", function() {
597         expect(1);
598         ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
599 });
600
601 test("clone()", function() {
602         expect(3);
603         ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
604         var clone = $('#yahoo').clone();
605         ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
606         ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
607 });
608
609 test("is(String)", function() {
610         expect(26);
611         ok( $('#form').is('form'), 'Check for element: A form must be a form' );
612         ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
613         ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
614         ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
615         ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
616         ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
617         ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
618         ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
619         ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
620         ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
621         ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
622         ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
623         ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
624         ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
625         ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
626         ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
627         ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
628         ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
629         ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
630         ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
631         ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
632         ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
633         
634         // test is() with comma-seperated expressions
635         ok( $('#en').is('[@lang="en"],[@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
636         ok( $('#en').is('[@lang="de"],[@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
637         ok( $('#en').is('[@lang="en"] , [@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
638         ok( $('#en').is('[@lang="de"] , [@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
639 });
640
641 test("$.extend(Object, Object)", function() {
642         expect(2);
643         var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
644                 options =     { xnumber2: 1, xstring2: "x", xxx: "newstring" },
645                 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
646                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" };
647         jQuery.extend(settings, options);
648         isObj( settings, merged, "Check if extended: settings must be extended" );
649         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
650 });
651
652 test("$.extend(Object, Object, Object, Object)", function() {
653         expect(4);
654         var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
655                 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
656                 options1 =     { xnumber2: 1, xstring2: "x" },
657                 options1Copy = { xnumber2: 1, xstring2: "x" },
658                 options2 =     { xstring2: "xx", xxx: "newstringx" },
659                 options2Copy = { xstring2: "xx", xxx: "newstringx" },
660                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
661         var settings = jQuery.extend({}, defaults, options1, options2);
662         isObj( settings, merged, "Check if extended: settings must be extended" );
663         isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
664         isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
665         isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
666 });
667
668 test("val()", function() {
669         expect(2);
670         ok( $("#text1").val() == "Test", "Check for value of input element" );
671         ok( !$("#text1").val() == "", "Check for value of input element" );
672 });
673
674 test("val(String)", function() {
675         expect(2);
676         document.getElementById('text1').value = "bla";
677         ok( $("#text1").val() == "bla", "Check for modified value of input element" );
678         $("#text1").val('test');
679         ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
680 });
681
682 test("html(String)", function() {
683         expect(2);
684         var div = $("div");
685         div.html("<b>test</b>");
686         var pass = true;
687         for ( var i = 0; i < div.size(); i++ ) {
688           if ( div.get(i).childNodes.length == 0 ) pass = false;
689         }
690         ok( pass, "Set HTML" );
691         
692         $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>').evalScripts();
693 });
694
695 test("filter()", function() {
696         expect(4);
697         isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
698         isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
699         isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
700         isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
701 });
702
703 test("not()", function() {
704         expect(3);
705         ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
706         isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
707         isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
708 });
709
710 test("siblings([String])", function() {
711         expect(4);
712         isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
713         isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" ); 
714         isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
715         isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "floatTest"), "Check for multiple filters" );
716 });
717
718 test("children([String])", function() {
719         expect(3);
720         isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
721         isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );
722         isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
723 });
724
725 test("parent[s]([String])", function() {
726         expect(8);
727         ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
728         ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
729         ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
730         ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
731         
732         ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
733         ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
734         ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
735         isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
736 });
737
738 test("next/prev([String])", function() {
739         expect(8);
740         ok( $("#ap").next()[0].id == "foo", "Simple next check" );
741         ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
742         ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
743         ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
744         
745         ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
746         ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
747         ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
748         ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
749 });
750
751 test("show()", function() {
752         expect(1);
753         var pass = true, div = $("div");
754         div.show().each(function(){
755           if ( this.style.display == "none" ) pass = false;
756         });
757         ok( pass, "Show" );
758 });
759
760 test("addClass(String)", function() {
761         expect(1);
762         var div = $("div");
763         div.addClass("test");
764         var pass = true;
765         for ( var i = 0; i < div.size(); i++ ) {
766          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
767         }
768         ok( pass, "Add Class" );
769 });
770
771 test("removeClass(String) - simple", function() {
772         expect(3);
773         var div = $("div").addClass("test").removeClass("test"),
774                 pass = true;
775         for ( var i = 0; i < div.size(); i++ ) {
776                 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
777         }
778         ok( pass, "Remove Class" );
779         
780         reset();
781         var div = $("div").addClass("test").addClass("foo").addClass("bar");
782         div.removeClass("test").removeClass("bar").removeClass("foo");
783         var pass = true;
784         for ( var i = 0; i < div.size(); i++ ) {
785          if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
786         }
787         ok( pass, "Remove multiple classes" );
788         
789         reset();
790         var div = $("div:eq(0)").addClass("test").removeClass("");
791         ok( div.is('.test'), "Empty string passed to removeClass" );
792         
793 });
794
795 test("toggleClass(String)", function() {
796         expect(3);
797         var e = $("#firstp");
798         ok( !e.is(".test"), "Assert class not present" );
799         e.toggleClass("test");
800         ok( e.is(".test"), "Assert class present" ); 
801         e.toggleClass("test");
802         ok( !e.is(".test"), "Assert class not present" );
803 });
804
805 test("removeAttr(String", function() {
806         expect(1);
807         ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
808 });
809
810 test("text(String)", function() {
811         expect(1);
812         ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "&lt;div&gt;&lt;b&gt;Hello&lt;/b&gt; cruel world!&lt;/div&gt;", "Check escaped text" );
813 });
814
815 test("$.each(Object,Function)", function() {
816         expect(8);
817         $.each( [0,1,2], function(i, n){
818                 ok( i == n, "Check array iteration" );
819         });
820         
821         $.each( [5,6,7], function(i, n){
822                 ok( i == n - 5, "Check array iteration" );
823         });
824          
825         $.each( { name: "name", lang: "lang" }, function(i, n){
826                 ok( i == n, "Check object iteration" );
827         });
828 });
829
830 test("$.prop", function() {
831         expect(2);
832         var handle = function() { return this.id };
833         ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
834         ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
835 });
836
837 test("$.className", function() {
838         expect(6);
839         var x = $("<p>Hi</p>")[0];
840         var c = $.className;
841         c.add(x, "hi");
842         ok( x.className == "hi", "Check single added class" );
843         c.add(x, "foo bar");
844         ok( x.className == "hi foo bar", "Check more added classes" );
845         c.remove(x);
846         ok( x.className == "", "Remove all classes" );
847         c.add(x, "hi foo bar");
848         c.remove(x, "foo");
849         ok( x.className == "hi bar", "Check removal of one class" );
850         ok( c.has(x, "hi"), "Check has1" );
851         ok( c.has(x, "bar"), "Check has2" );
852 });
853
854 test("remove()", function() {
855         expect(4);
856         $("#ap").children().remove();
857         ok( $("#ap").text().length > 10, "Check text is not removed" );
858         ok( $("#ap").children().length == 0, "Check remove" );
859         
860         reset();
861         $("#ap").children().remove("a");
862         ok( $("#ap").text().length > 10, "Check text is not removed" );
863         ok( $("#ap").children().length == 1, "Check filtered remove" );
864 });
865
866 test("empty()", function() {
867         expect(2);
868         ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
869         ok( $("#ap").children().length == 4, "Check elements are not removed" );
870 });
871
872 test("eq(), gt(), lt(), contains()", function() {
873         expect(4);
874         ok( $("#ap a").eq(1)[0].id == "groups", "eq()" );
875         isSet( $("#ap a").gt(0).get(), q("groups", "anchor1", "mark"), "gt()" );
876         isSet( $("#ap a").lt(3).get(), q("google", "groups", "anchor1"), "lt()" );
877         isSet( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" );
878 });