Making the test suite a little more XML-compliant.
[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(8);
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         $("#name").attr('maxlength', '5');
250         ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' );
251 });
252
253 if ( location.protocol != "file:" ) {
254         test("attr(String, Object)x", function() {
255                 expect(2);
256                 stop();
257                 $.get('data/dashboard.xml', function(xml) { 
258                 var titles = [];
259                 $('tab', xml).each(function() {
260                 titles.push($(this).attr('title'));
261                 });
262                 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
263                 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
264                 start();
265                 });
266         });
267 }
268
269 test("css(String|Hash)", function() {
270         expect(19);
271         
272         ok( $('#main').css("display") == 'none', 'Check for css property "display"');
273         
274         ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
275         $('#foo').css({display: 'none'});
276         ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
277         $('#foo').css({display: 'block'});
278         ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
279         
280         $('#floatTest').css({styleFloat: 'right'});
281         ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
282         $('#floatTest').css({cssFloat: 'left'});
283         ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
284         $('#floatTest').css({'float': 'right'});
285         ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
286         $('#floatTest').css({'font-size': '30px'});
287         ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
288         
289         $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
290                 $('#foo').css({opacity: n});
291                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
292                 $('#foo').css({opacity: parseFloat(n)});
293                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
294         });     
295         $('#foo').css({opacity: ''});
296         ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
297 });
298
299 test("css(String, Object)", function() {
300         expect(18);
301         ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
302         $('#foo').css('display', 'none');
303         ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
304         $('#foo').css('display', 'block');
305         ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
306         
307         $('#floatTest').css('styleFloat', 'left');
308         ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
309         $('#floatTest').css('cssFloat', 'right');
310         ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
311         $('#floatTest').css('float', 'left');
312         ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
313         $('#floatTest').css('font-size', '20px');
314         ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
315         
316         $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
317                 $('#foo').css('opacity', n);
318                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
319                 $('#foo').css('opacity', parseFloat(n));
320                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
321         });
322         $('#foo').css('opacity', '');
323         ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
324 });
325
326 test("text()", function() {
327         expect(1);
328         var expected = "This link has class=\"blog\": Simon Willison's Weblog";
329         ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
330 });
331
332 test("wrap(String|Element)", function() {
333         expect(7);
334         var defaultText = 'Try them out:'
335         var result = $('#first').wrap('<div class="red"><span></span></div>').text();
336         ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
337         ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
338
339         reset();
340         var defaultText = 'Try them out:'
341         var result = $('#first').wrap(document.getElementById('empty')).parent();
342         ok( result.is('ol'), 'Check for element wrapping' );
343         ok( result.text() == defaultText, 'Check for element wrapping' );
344         
345         reset();
346         stop();
347         $('#check1').click(function() {         
348                 var checkbox = this;            
349                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
350                 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
351                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
352                 // use a fade in to check state after this event handler has finished
353                 setTimeout(function() {
354                         ok( !checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
355                         start();
356                 }, 100);
357         }).click();
358 });
359
360 test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
361         expect(17);
362         var defaultText = 'Try them out:'
363         var result = $('#first').append('<b>buga</b>');
364         ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
365         ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
366         
367         reset();
368         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
369         $('#sap').append(document.getElementById('first'));
370         ok( expected == $('#sap').text(), "Check for appending of element" );
371         
372         reset();
373         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
374         $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
375         ok( expected == $('#sap').text(), "Check for appending of array of elements" );
376         
377         reset();
378         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
379         $('#sap').append($("#first, #yahoo"));
380         ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
381
382         reset();
383         $("#sap").append( 5 );
384         ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
385
386         reset();
387         $("#sap").append( " text with spaces " );
388         ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
389
390         reset();
391         ok( $("#sap").append([]), "Check for appending an empty array." );
392         ok( $("#sap").append(""), "Check for appending an empty string." );
393         ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
394         
395         reset();
396         $("#sap").append(document.getElementById('form'));
397         ok( $("#sap>form").size() == 1, "Check for appending a form" );  // Bug #910
398
399         reset();
400         var pass = true;
401         try {
402                 $( $("iframe")[0].contentWindow.document.body ).append("<div>test</div>");
403         } catch(e) {
404                 pass = false;
405         }
406
407         ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
408         
409         reset();
410         $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
411         t( 'Append legend', '#legend', ['legend'] );
412         
413         reset();
414         $('#select1').append('<OPTION>Test</OPTION>');
415         ok( $('#select1 option:last').text() == "Test", "Appending &lt;OPTION&gt; (all caps)" );
416         
417         $('#table').append('<colgroup></colgroup>');
418         ok( $('#table colgroup').length, "Append colgroup" );
419         
420         $('#table colgroup').append('<col/>');
421         ok( $('#table colgroup col').length, "Append col" );
422         
423         reset();
424         $('form:last')
425                 .append('<select id="appendSelect1"></select>')
426                 .append('<select id="appendSelect2"><option>Test</option></select>');
427         
428         t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
429 });
430
431 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
432         expect(6);
433         var defaultText = 'Try them out:'
434         $('<b>buga</b>').appendTo('#first');
435         ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
436         ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
437         
438         reset();
439         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
440         $(document.getElementById('first')).appendTo('#sap');
441         ok( expected == $('#sap').text(), "Check for appending of element" );
442         
443         reset();
444         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
445         $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
446         ok( expected == $('#sap').text(), "Check for appending of array of elements" );
447         
448         reset();
449         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
450         $("#first, #yahoo").appendTo('#sap');
451         ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
452         
453         reset();
454         $('#select1').appendTo('#foo');
455         t( 'Append select', '#foo select', ['select1'] );
456 });
457
458 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
459         expect(5);
460         var defaultText = 'Try them out:'
461         var result = $('#first').prepend('<b>buga</b>');
462         ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
463         ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
464         
465         reset();
466         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
467         $('#sap').prepend(document.getElementById('first'));
468         ok( expected == $('#sap').text(), "Check for prepending of element" );
469
470         reset();
471         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
472         $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
473         ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
474         
475         reset();
476         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
477         $('#sap').prepend($("#first, #yahoo"));
478         ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
479 });
480
481 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
482         expect(6);
483         var defaultText = 'Try them out:'
484         $('<b>buga</b>').prependTo('#first');
485         ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
486         ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
487         
488         reset();
489         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
490         $(document.getElementById('first')).prependTo('#sap');
491         ok( expected == $('#sap').text(), "Check for prepending of element" );
492
493         reset();
494         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
495         $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
496         ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
497         
498         reset();
499         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
500         $("#yahoo, #first").prependTo('#sap');
501         ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
502         
503         reset();
504         $('<select id="prependSelect1"></select>').prependTo('form:last');
505         $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
506         
507         t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
508 });
509
510 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
511         expect(4);
512         var expected = 'This is a normal link: bugaYahoo';
513         $('#yahoo').before('<b>buga</b>');
514         ok( expected == $('#en').text(), 'Insert String before' );
515         
516         reset();
517         expected = "This is a normal link: Try them out:Yahoo";
518         $('#yahoo').before(document.getElementById('first'));
519         ok( expected == $('#en').text(), "Insert element before" );
520         
521         reset();
522         expected = "This is a normal link: Try them out:diveintomarkYahoo";
523         $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
524         ok( expected == $('#en').text(), "Insert array of elements before" );
525         
526         reset();
527         expected = "This is a normal link: Try them out:diveintomarkYahoo";
528         $('#yahoo').before($("#first, #mark"));
529         ok( expected == $('#en').text(), "Insert jQuery before" );
530 });
531
532 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
533         expect(4);
534         var expected = 'This is a normal link: bugaYahoo';
535         $('<b>buga</b>').insertBefore('#yahoo');
536         ok( expected == $('#en').text(), 'Insert String before' );
537         
538         reset();
539         expected = "This is a normal link: Try them out:Yahoo";
540         $(document.getElementById('first')).insertBefore('#yahoo');
541         ok( expected == $('#en').text(), "Insert element before" );
542         
543         reset();
544         expected = "This is a normal link: Try them out:diveintomarkYahoo";
545         $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
546         ok( expected == $('#en').text(), "Insert array of elements before" );
547         
548         reset();
549         expected = "This is a normal link: Try them out:diveintomarkYahoo";
550         $("#first, #mark").insertBefore('#yahoo');
551         ok( expected == $('#en').text(), "Insert jQuery before" );
552 });
553
554 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
555         expect(4);
556         var expected = 'This is a normal link: Yahoobuga';
557         $('#yahoo').after('<b>buga</b>');
558         ok( expected == $('#en').text(), 'Insert String after' );
559         
560         reset();
561         expected = "This is a normal link: YahooTry them out:";
562         $('#yahoo').after(document.getElementById('first'));
563         ok( expected == $('#en').text(), "Insert element after" );
564
565         reset();
566         expected = "This is a normal link: YahooTry them out:diveintomark";
567         $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
568         ok( expected == $('#en').text(), "Insert array of elements after" );
569         
570         reset();
571         expected = "This is a normal link: YahooTry them out:diveintomark";
572         $('#yahoo').after($("#first, #mark"));
573         ok( expected == $('#en').text(), "Insert jQuery after" );
574 });
575
576 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
577         expect(4);
578         var expected = 'This is a normal link: Yahoobuga';
579         $('<b>buga</b>').insertAfter('#yahoo');
580         ok( expected == $('#en').text(), 'Insert String after' );
581         
582         reset();
583         expected = "This is a normal link: YahooTry them out:";
584         $(document.getElementById('first')).insertAfter('#yahoo');
585         ok( expected == $('#en').text(), "Insert element after" );
586
587         reset();
588         expected = "This is a normal link: YahooTry them out:diveintomark";
589         $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
590         ok( expected == $('#en').text(), "Insert array of elements after" );
591         
592         reset();
593         expected = "This is a normal link: YahooTry them out:diveintomark";
594         $("#mark, #first").insertAfter('#yahoo');
595         ok( expected == $('#en').text(), "Insert jQuery after" );
596 });
597
598 test("end()", function() {
599         expect(3);
600         ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
601         ok( $('#yahoo').end(), 'Check for end with nothing to end' );
602         
603         var x = $('#yahoo');
604         x.parent();
605         ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
606 });
607
608 test("find(String)", function() {
609         expect(1);
610         ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
611 });
612
613 test("clone()", function() {
614         expect(3);
615         ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
616         var clone = $('#yahoo').clone();
617         ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
618         ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
619 });
620
621 test("is(String)", function() {
622         expect(26);
623         ok( $('#form').is('form'), 'Check for element: A form must be a form' );
624         ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
625         ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
626         ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
627         ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
628         ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
629         ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
630         ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
631         ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
632         ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
633         ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
634         ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
635         ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
636         ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
637         ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
638         ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
639         ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
640         ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
641         ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
642         ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
643         ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
644         ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
645         
646         // test is() with comma-seperated expressions
647         ok( $('#en').is('[@lang="en"],[@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
648         ok( $('#en').is('[@lang="de"],[@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
649         ok( $('#en').is('[@lang="en"] , [@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
650         ok( $('#en').is('[@lang="de"] , [@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
651 });
652
653 test("$.extend(Object, Object)", function() {
654         expect(2);
655         var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
656                 options =     { xnumber2: 1, xstring2: "x", xxx: "newstring" },
657                 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
658                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" };
659         jQuery.extend(settings, options);
660         isObj( settings, merged, "Check if extended: settings must be extended" );
661         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
662 });
663
664 test("$.extend(Object, Object, Object, Object)", function() {
665         expect(4);
666         var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
667                 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
668                 options1 =     { xnumber2: 1, xstring2: "x" },
669                 options1Copy = { xnumber2: 1, xstring2: "x" },
670                 options2 =     { xstring2: "xx", xxx: "newstringx" },
671                 options2Copy = { xstring2: "xx", xxx: "newstringx" },
672                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
673         var settings = jQuery.extend({}, defaults, options1, options2);
674         isObj( settings, merged, "Check if extended: settings must be extended" );
675         isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
676         isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
677         isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
678 });
679
680 test("val()", function() {
681         expect(2);
682         ok( $("#text1").val() == "Test", "Check for value of input element" );
683         ok( !$("#text1").val() == "", "Check for value of input element" );
684 });
685
686 test("val(String)", function() {
687         expect(2);
688         document.getElementById('text1').value = "bla";
689         ok( $("#text1").val() == "bla", "Check for modified value of input element" );
690         $("#text1").val('test');
691         ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
692 });
693
694 test("html(String)", function() {
695         expect(1);
696         var div = $("div");
697         div.html("<b>test</b>");
698         var pass = true;
699         for ( var i = 0; i < div.size(); i++ ) {
700           if ( div.get(i).childNodes.length == 0 ) pass = false;
701         }
702         ok( pass, "Set HTML" );
703
704         // Ccommented out until we can resolve it       
705         // $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>').evalScripts();
706 });
707
708 test("filter()", function() {
709         expect(4);
710         isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
711         isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
712         isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
713         isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
714 });
715
716 test("not()", function() {
717         expect(3);
718         ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
719         isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
720         isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
721 });
722
723 test("siblings([String])", function() {
724         expect(4);
725         isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
726         isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" ); 
727         isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
728         isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "floatTest"), "Check for multiple filters" );
729 });
730
731 test("children([String])", function() {
732         expect(3);
733         isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
734         isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );
735         isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
736 });
737
738 test("parent[s]([String])", function() {
739         expect(8);
740         ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
741         ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
742         ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
743         ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
744         
745         ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
746         ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
747         ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
748         isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
749 });
750
751 test("next/prev([String])", function() {
752         expect(8);
753         ok( $("#ap").next()[0].id == "foo", "Simple next check" );
754         ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
755         ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
756         ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
757         
758         ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
759         ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
760         ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
761         ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
762 });
763
764 test("show()", function() {
765         expect(1);
766         var pass = true, div = $("div");
767         div.show().each(function(){
768           if ( this.style.display == "none" ) pass = false;
769         });
770         ok( pass, "Show" );
771 });
772
773 test("addClass(String)", function() {
774         expect(1);
775         var div = $("div");
776         div.addClass("test");
777         var pass = true;
778         for ( var i = 0; i < div.size(); i++ ) {
779          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
780         }
781         ok( pass, "Add Class" );
782 });
783
784 test("removeClass(String) - simple", function() {
785         expect(3);
786         var div = $("div").addClass("test").removeClass("test"),
787                 pass = true;
788         for ( var i = 0; i < div.size(); i++ ) {
789                 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
790         }
791         ok( pass, "Remove Class" );
792         
793         reset();
794         var div = $("div").addClass("test").addClass("foo").addClass("bar");
795         div.removeClass("test").removeClass("bar").removeClass("foo");
796         var pass = true;
797         for ( var i = 0; i < div.size(); i++ ) {
798          if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
799         }
800         ok( pass, "Remove multiple classes" );
801         
802         reset();
803         var div = $("div:eq(0)").addClass("test").removeClass("");
804         ok( div.is('.test'), "Empty string passed to removeClass" );
805         
806 });
807
808 test("toggleClass(String)", function() {
809         expect(3);
810         var e = $("#firstp");
811         ok( !e.is(".test"), "Assert class not present" );
812         e.toggleClass("test");
813         ok( e.is(".test"), "Assert class present" ); 
814         e.toggleClass("test");
815         ok( !e.is(".test"), "Assert class not present" );
816 });
817
818 test("removeAttr(String", function() {
819         expect(1);
820         ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
821 });
822
823 test("text(String)", function() {
824         expect(1);
825         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" );
826 });
827
828 test("$.each(Object,Function)", function() {
829         expect(8);
830         $.each( [0,1,2], function(i, n){
831                 ok( i == n, "Check array iteration" );
832         });
833         
834         $.each( [5,6,7], function(i, n){
835                 ok( i == n - 5, "Check array iteration" );
836         });
837          
838         $.each( { name: "name", lang: "lang" }, function(i, n){
839                 ok( i == n, "Check object iteration" );
840         });
841 });
842
843 test("$.prop", function() {
844         expect(2);
845         var handle = function() { return this.id };
846         ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
847         ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
848 });
849
850 test("$.className", function() {
851         expect(6);
852         var x = $("<p>Hi</p>")[0];
853         var c = $.className;
854         c.add(x, "hi");
855         ok( x.className == "hi", "Check single added class" );
856         c.add(x, "foo bar");
857         ok( x.className == "hi foo bar", "Check more added classes" );
858         c.remove(x);
859         ok( x.className == "", "Remove all classes" );
860         c.add(x, "hi foo bar");
861         c.remove(x, "foo");
862         ok( x.className == "hi bar", "Check removal of one class" );
863         ok( c.has(x, "hi"), "Check has1" );
864         ok( c.has(x, "bar"), "Check has2" );
865 });
866
867 test("remove()", function() {
868         expect(4);
869         $("#ap").children().remove();
870         ok( $("#ap").text().length > 10, "Check text is not removed" );
871         ok( $("#ap").children().length == 0, "Check remove" );
872         
873         reset();
874         $("#ap").children().remove("a");
875         ok( $("#ap").text().length > 10, "Check text is not removed" );
876         ok( $("#ap").children().length == 1, "Check filtered remove" );
877 });
878
879 test("empty()", function() {
880         expect(2);
881         ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
882         ok( $("#ap").children().length == 4, "Check elements are not removed" );
883 });
884
885 test("eq(), gt(), lt(), contains()", function() {
886         expect(4);
887         ok( $("#ap a").eq(1)[0].id == "groups", "eq()" );
888         isSet( $("#ap a").gt(0).get(), q("groups", "anchor1", "mark"), "gt()" );
889         isSet( $("#ap a").lt(3).get(), q("google", "groups", "anchor1"), "lt()" );
890         isSet( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" );
891 });