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