Fix for #2002
[jquery.git] / test / unit / core.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 /*
21         // disabled since this test was doing nothing. i tried to fix it but i'm not sure
22         // what the expected behavior should even be. FF returns "\n" for the text node
23         // make sure this is handled
24         var crlfContainer = $('<p>\r\n</p>');
25         var x = crlfContainer.contents().get(0).nodeValue;
26         equals( x, what???, "Check for \\r and \\n in jQuery()" );
27 */
28         
29         /* // Disabled until we add this functionality in
30         var pass = true;
31         try {
32                 $("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
33         } catch(e){
34                 pass = false;
35         }
36         ok( pass, "$('&lt;tag&gt;') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
37
38         var code = $("<code/>");
39         equals( code.length, 1, "Correct number of elements generated for code" );
40         var img = $("<img/>");
41         equals( img.length, 1, "Correct number of elements generated for img" );
42         var div = $("<div/><hr/><code/><b/>");
43         equals( div.length, 4, "Correct number of elements generated for div hr code b" );
44 });
45
46 test("noConflict", function() {
47         expect(6);
48         
49         var old = jQuery;
50         var newjQuery = jQuery.noConflict();
51
52         ok( newjQuery == old, "noConflict returned the jQuery object" );
53         ok( jQuery == old, "Make sure jQuery wasn't touched." );
54         ok( $ == "$", "Make sure $ was reverted." );
55
56         jQuery = $ = old;
57
58         newjQuery = jQuery.noConflict(true);
59
60         ok( newjQuery == old, "noConflict returned the jQuery object" );
61         ok( jQuery == "jQuery", "Make sure jQuery was reverted." );
62         ok( $ == "$", "Make sure $ was reverted." );
63
64         jQuery = $ = old;
65 });
66
67 test("isFunction", function() {
68         expect(21);
69
70         // Make sure that false values return false
71         ok( !jQuery.isFunction(), "No Value" );
72         ok( !jQuery.isFunction( null ), "null Value" );
73         ok( !jQuery.isFunction( undefined ), "undefined Value" );
74         ok( !jQuery.isFunction( "" ), "Empty String Value" );
75         ok( !jQuery.isFunction( 0 ), "0 Value" );
76
77         // Check built-ins
78         // Safari uses "(Internal Function)"
79         ok( jQuery.isFunction(String), "String Function" );
80         ok( jQuery.isFunction(Array), "Array Function" );
81         ok( jQuery.isFunction(Object), "Object Function" );
82         ok( jQuery.isFunction(Function), "Function Function" );
83
84         // When stringified, this could be misinterpreted
85         var mystr = "function";
86         ok( !jQuery.isFunction(mystr), "Function String" );
87
88         // When stringified, this could be misinterpreted
89         var myarr = [ "function" ];
90         ok( !jQuery.isFunction(myarr), "Function Array" );
91
92         // When stringified, this could be misinterpreted
93         var myfunction = { "function": "test" };
94         ok( !jQuery.isFunction(myfunction), "Function Object" );
95
96         // Make sure normal functions still work
97         var fn = function(){};
98         ok( jQuery.isFunction(fn), "Normal Function" );
99
100         var obj = document.createElement("object");
101
102         // Firefox says this is a function
103         ok( !jQuery.isFunction(obj), "Object Element" );
104
105         // IE says this is an object
106         ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
107
108         var nodes = document.body.childNodes;
109
110         // Safari says this is a function
111         ok( !jQuery.isFunction(nodes), "childNodes Property" );
112
113         var first = document.body.firstChild;
114         
115         // Normal elements are reported ok everywhere
116         ok( !jQuery.isFunction(first), "A normal DOM Element" );
117
118         var input = document.createElement("input");
119         input.type = "text";
120         document.body.appendChild( input );
121
122         // IE says this is an object
123         ok( jQuery.isFunction(input.focus), "A default function property" );
124
125         document.body.removeChild( input );
126
127         var a = document.createElement("a");
128         a.href = "some-function";
129         document.body.appendChild( a );
130
131         // This serializes with the word 'function' in it
132         ok( !jQuery.isFunction(a), "Anchor Element" );
133
134         document.body.removeChild( a );
135
136         // Recursive function calls have lengths and array-like properties
137         function callme(callback){
138                 function fn(response){
139                         callback(response);
140                 }
141
142                 ok( jQuery.isFunction(fn), "Recursive Function Call" );
143
144         fn({ some: "data" });
145         };
146
147         callme(function(){
148         callme(function(){});
149         });
150 });
151
152 var foo = false;
153
154 test("$('html')", function() {
155         expect(4);
156
157         reset();
158         foo = false;
159         var s = $("<script>var foo='test';</script>")[0];
160         ok( s, "Creating a script" );
161         ok( !foo, "Make sure the script wasn't executed prematurely" );
162         $("body").append(s);
163         ok( foo, "Executing a scripts contents in the right context" );
164         
165         reset();
166         ok( $("<link rel='stylesheet'/>")[0], "Creating a link" );
167         
168         reset();
169 });
170
171 test("$('html', context)", function() {
172         expect(1);
173
174         var $div = $("<div/>");
175         var $span = $("<span/>", $div);
176         equals($span.length, 1, "Verify a span created with a div context works, #1763");
177 });
178
179 test("$(selector, xml).text(str) - Loaded via XML document", function() {
180         expect(2);
181         stop();
182         $.get('data/dashboard.xml', function(xml) { 
183                 // tests for #1419 where IE was a problem
184                 equals( $("tab:first", xml).text(), "blabla", "Verify initial text correct" );
185                 $("tab:first", xml).text("newtext");
186                 equals( $("tab:first", xml).text(), "newtext", "Verify new text correct" );
187                 start();
188         });
189 });
190
191 test("length", function() {
192         expect(1);
193         ok( $("p").length == 6, "Get Number of Elements Found" );
194 });
195
196 test("size()", function() {
197         expect(1);
198         ok( $("p").size() == 6, "Get Number of Elements Found" );
199 });
200
201 test("get()", function() {
202         expect(1);
203         isSet( $("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
204 });
205
206 test("get(Number)", function() {
207         expect(1);
208         ok( $("p").get(0) == document.getElementById("firstp"), "Get A Single Element" );
209 });
210
211 test("add(String|Element|Array|undefined)", function() {
212         expect(8);
213         isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
214         isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
215         ok( $([]).add($("#form")[0].elements).length >= 13, "Check elements from array" );
216         
217         var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
218         ok( x[0].id == "x1", "Check on-the-fly element1" );
219         ok( x[1].id == "x2", "Check on-the-fly element2" );
220         
221         var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
222         ok( x[0].id == "x1", "Check on-the-fly element1" );
223         ok( x[1].id == "x2", "Check on-the-fly element2" );
224         
225         var notDefined;
226         equals( $([]).add(notDefined).length, 0, "Check that undefined adds nothing." );
227 });
228
229 test("each(Function)", function() {
230         expect(1);
231         var div = $("div");
232         div.each(function(){this.foo = 'zoo';});
233         var pass = true;
234         for ( var i = 0; i < div.size(); i++ ) {
235           if ( div.get(i).foo != "zoo" ) pass = false;
236         }
237         ok( pass, "Execute a function, Relative" );
238 });
239
240 test("index(Object)", function() {
241         expect(8);
242         ok( $([window, document]).index(window) == 0, "Check for index of elements" );
243         ok( $([window, document]).index(document) == 1, "Check for index of elements" );
244         var inputElements = $('#radio1,#radio2,#check1,#check2');
245         ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
246         ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
247         ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
248         ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
249         ok( inputElements.index(window) == -1, "Check for not found index" );
250         ok( inputElements.index(document) == -1, "Check for not found index" );
251 });
252
253 test("attr(String)", function() {
254         expect(20);
255         ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
256         ok( $('#text1').attr('value', "Test2").attr('defaultValue') == "Test", 'Check for defaultValue attribute' );
257         ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
258         ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
259         ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
260         ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
261         ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
262         ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
263         ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
264         ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
265         ok( $('#name').attr('name') == "name", 'Check for name attribute' );
266         ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
267         ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
268         ok( $('#text1').attr('maxlength') == '30', 'Check for maxlength attribute' );
269         ok( $('#text1').attr('maxLength') == '30', 'Check for maxLength attribute' );
270         ok( $('#area1').attr('maxLength') == '30', 'Check for maxLength attribute' );
271         ok( $('#select2').attr('selectedIndex') == 3, 'Check for selectedIndex attribute' );
272         ok( $('#foo').attr('nodeName') == 'DIV', 'Check for nodeName attribute' );
273         ok( $('#foo').attr('tagName') == 'DIV', 'Check for tagName attribute' );
274         
275         $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
276         ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
277 });
278
279 if ( !isLocal ) {
280     test("attr(String) in XML Files", function() {
281         expect(2);
282         stop();
283         $.get("data/dashboard.xml", function(xml) {
284             ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
285             ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
286             start();
287         });
288     });
289 }
290
291 test("attr(String, Function)", function() {
292         expect(2);
293         ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
294         ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
295 });
296
297 test("attr(Hash)", function() {
298         expect(1);
299         var pass = true;
300         $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
301           if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
302         });
303         ok( pass, "Set Multiple Attributes" );
304 });
305
306 test("attr(String, Object)", function() {
307         expect(16);
308         var div = $("div");
309         div.attr("foo", "bar");
310         var pass = true;
311         for ( var i = 0; i < div.size(); i++ ) {
312           if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
313         }
314         ok( pass, "Set Attribute" );
315
316         ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );    
317         
318         $("#name").attr('name', 'something');
319         ok( $("#name").attr('name') == 'something', 'Set name attribute' );
320         $("#check2").attr('checked', true);
321         ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
322         $("#check2").attr('checked', false);
323         ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
324         $("#text1").attr('readonly', true);
325         ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
326         $("#text1").attr('readonly', false);
327         ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
328         $("#name").attr('maxlength', '5');
329         ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' );
330         $("#name").attr('maxLength', '10');
331         ok( document.getElementById('name').maxLength == '10', 'Set maxlength attribute' );
332
333         // for #1070
334         $("#name").attr('someAttr', '0');
335         equals( $("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
336         $("#name").attr('someAttr', 0);
337         equals( $("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
338         $("#name").attr('someAttr', 1);
339         equals( $("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
340
341         reset();
342
343         var type = $("#check2").attr('type');
344         var thrown = false;
345         try {
346                 $("#check2").attr('type','hidden');
347         } catch(e) {
348                 thrown = true;
349         }
350         ok( thrown, "Exception thrown when trying to change type property" );
351         equals( type, $("#check2").attr('type'), "Verify that you can't change the type of an input element" );
352
353         var check = document.createElement("input");
354         var thrown = true;
355         try {
356                 $(check).attr('type','checkbox');
357         } catch(e) {
358                 thrown = false;
359         }
360         ok( thrown, "Exception thrown when trying to change type property" );
361         equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
362 });
363
364 if ( !isLocal ) {
365     test("attr(String, Object) - Loaded via XML document", function() {
366         expect(2);
367         stop();
368         $.get('data/dashboard.xml', function(xml) { 
369               var titles = [];
370               $('tab', xml).each(function() {
371                     titles.push($(this).attr('title'));
372               });
373               equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
374               equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
375               start();
376         });
377     });
378 }
379
380 test("css(String|Hash)", function() {
381         expect(19);
382         
383         ok( $('#main').css("display") == 'none', 'Check for css property "display"');
384         
385         ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
386         $('#foo').css({display: 'none'});
387         ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
388         $('#foo').css({display: 'block'});
389         ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
390         
391         $('#floatTest').css({styleFloat: 'right'});
392         ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
393         $('#floatTest').css({cssFloat: 'left'});
394         ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
395         $('#floatTest').css({'float': 'right'});
396         ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
397         $('#floatTest').css({'font-size': '30px'});
398         ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
399         
400         $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
401                 $('#foo').css({opacity: n});
402                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
403                 $('#foo').css({opacity: parseFloat(n)});
404                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
405         });     
406         $('#foo').css({opacity: ''});
407         ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
408 });
409
410 test("css(String, Object)", function() {
411         expect(19);
412         ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
413         $('#foo').css('display', 'none');
414         ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
415         $('#foo').css('display', 'block');
416         ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
417         
418         $('#floatTest').css('styleFloat', 'left');
419         ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
420         $('#floatTest').css('cssFloat', 'right');
421         ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
422         $('#floatTest').css('float', 'left');
423         ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
424         $('#floatTest').css('font-size', '20px');
425         ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
426         
427         $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
428                 $('#foo').css('opacity', n);
429                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
430                 $('#foo').css('opacity', parseFloat(n));
431                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
432         });
433         $('#foo').css('opacity', '');
434         ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
435         // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
436         if (jQuery.browser.msie) {
437                 $('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
438         }
439         equals( $('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
440 });
441
442 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
443         expect(4);
444
445         var $checkedtest = $("#checkedtest");
446         // IE6 was clearing "checked" in jQuery.css(elem, "height");
447         jQuery.css($checkedtest[0], "height");
448         ok( !! $(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
449         ok( ! $(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
450         ok( !! $(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
451         ok( ! $(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
452 });
453
454 test("width()", function() {
455         expect(2);
456
457         $("#nothiddendiv").width(30);
458         equals($("#nothiddendiv").width(), 30, "Test set to 30 correctly");
459         $("#nothiddendiv").width(-1); // handle negative numbers by ignoring #1599
460         equals($("#nothiddendiv").width(), 30, "Test negative width ignored");
461 });
462
463 test("text()", function() {
464         expect(1);
465         var expected = "This link has class=\"blog\": Simon Willison's Weblog";
466         ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
467 });
468
469 test("wrap(String|Element)", function() {
470         expect(6);
471         var defaultText = 'Try them out:'
472         var result = $('#first').wrap('<div class="red"><span></span></div>').text();
473         ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
474         ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
475
476         reset();
477         var defaultText = 'Try them out:'
478         var result = $('#first').wrap(document.getElementById('empty')).parent();
479         ok( result.is('ol'), 'Check for element wrapping' );
480         ok( result.text() == defaultText, 'Check for element wrapping' );
481         
482         reset();
483         $('#check1').click(function() {         
484                 var checkbox = this;            
485                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
486                 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
487                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
488         }).click();
489 });
490
491 test("wrapAll(String|Element)", function() {
492         expect(8);
493         var prev = $("#first")[0].previousSibling;
494         var p = $("#first")[0].parentNode;
495         var result = $('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>');
496         equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
497         ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
498         ok( $('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
499         equals( $("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
500         equals( $("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
501
502         reset();
503         var prev = $("#first")[0].previousSibling;
504         var p = $("#first")[0].parentNode;
505         var result = $('#first,#firstp').wrapAll(document.getElementById('empty'));
506         equals( $("#first").parent()[0], $("#firstp").parent()[0], "Same Parent" );
507         equals( $("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
508         equals( $("#first").parent()[0].parentNode, p, "Correct Parent" );
509 });
510
511 test("wrapInner(String|Element)", function() {
512         expect(6);
513         var num = $("#first").children().length;
514         var result = $('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
515         equals( $("#first").children().length, 1, "Only one child" );
516         ok( $("#first").children().is(".red"), "Verify Right Element" );
517         equals( $("#first").children().children().children().length, num, "Verify Elements Intact" );
518
519         reset();
520         var num = $("#first").children().length;
521         var result = $('#first').wrapInner(document.getElementById('empty'));
522         equals( $("#first").children().length, 1, "Only one child" );
523         ok( $("#first").children().is("#empty"), "Verify Right Element" );
524         equals( $("#first").children().children().length, num, "Verify Elements Intact" );
525 });
526
527 test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
528         expect(18);
529         var defaultText = 'Try them out:'
530         var result = $('#first').append('<b>buga</b>');
531         ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
532         ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
533         
534         reset();
535         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
536         $('#sap').append(document.getElementById('first'));
537         ok( expected == $('#sap').text(), "Check for appending of element" );
538         
539         reset();
540         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
541         $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
542         ok( expected == $('#sap').text(), "Check for appending of array of elements" );
543         
544         reset();
545         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
546         $('#sap').append($("#first, #yahoo"));
547         ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
548
549         reset();
550         $("#sap").append( 5 );
551         ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
552
553         reset();
554         $("#sap").append( " text with spaces " );
555         ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
556
557         reset();
558         ok( $("#sap").append([]), "Check for appending an empty array." );
559         ok( $("#sap").append(""), "Check for appending an empty string." );
560         ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
561         
562         reset();
563         $("#sap").append(document.getElementById('form'));
564         ok( $("#sap>form").size() == 1, "Check for appending a form" );  // Bug #910
565
566         reset();
567         var pass = true;
568         try {
569                 $( $("#iframe")[0].contentWindow.document.body ).append("<div>test</div>");
570         } catch(e) {
571                 pass = false;
572         }
573
574         ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
575         
576         reset();
577         $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
578         t( 'Append legend', '#legend', ['legend'] );
579         
580         reset();
581         $('#select1').append('<OPTION>Test</OPTION>');
582         ok( $('#select1 option:last').text() == "Test", "Appending &lt;OPTION&gt; (all caps)" );
583         
584         $('#table').append('<colgroup></colgroup>');
585         ok( $('#table colgroup').length, "Append colgroup" );
586         
587         $('#table colgroup').append('<col/>');
588         ok( $('#table colgroup col').length, "Append col" );
589         
590         reset();
591         $('#table').append('<caption></caption>');
592         ok( $('#table caption').length, "Append caption" );
593
594         reset();
595         $('form:last')
596                 .append('<select id="appendSelect1"></select>')
597                 .append('<select id="appendSelect2"><option>Test</option></select>');
598         
599         t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
600 });
601
602 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
603         expect(6);
604         var defaultText = 'Try them out:'
605         $('<b>buga</b>').appendTo('#first');
606         ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
607         ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
608         
609         reset();
610         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
611         $(document.getElementById('first')).appendTo('#sap');
612         ok( expected == $('#sap').text(), "Check for appending of element" );
613         
614         reset();
615         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
616         $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
617         ok( expected == $('#sap').text(), "Check for appending of array of elements" );
618         
619         reset();
620         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
621         $("#first, #yahoo").appendTo('#sap');
622         ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
623         
624         reset();
625         $('#select1').appendTo('#foo');
626         t( 'Append select', '#foo select', ['select1'] );
627 });
628
629 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
630         expect(5);
631         var defaultText = 'Try them out:'
632         var result = $('#first').prepend('<b>buga</b>');
633         ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
634         ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
635         
636         reset();
637         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
638         $('#sap').prepend(document.getElementById('first'));
639         ok( expected == $('#sap').text(), "Check for prepending of element" );
640
641         reset();
642         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
643         $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
644         ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
645         
646         reset();
647         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
648         $('#sap').prepend($("#first, #yahoo"));
649         ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
650 });
651
652 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
653         expect(6);
654         var defaultText = 'Try them out:'
655         $('<b>buga</b>').prependTo('#first');
656         ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
657         ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
658         
659         reset();
660         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
661         $(document.getElementById('first')).prependTo('#sap');
662         ok( expected == $('#sap').text(), "Check for prepending of element" );
663
664         reset();
665         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
666         $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
667         ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
668         
669         reset();
670         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
671         $("#yahoo, #first").prependTo('#sap');
672         ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
673         
674         reset();
675         $('<select id="prependSelect1"></select>').prependTo('form:last');
676         $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
677         
678         t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
679 });
680
681 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
682         expect(4);
683         var expected = 'This is a normal link: bugaYahoo';
684         $('#yahoo').before('<b>buga</b>');
685         ok( expected == $('#en').text(), 'Insert String before' );
686         
687         reset();
688         expected = "This is a normal link: Try them out:Yahoo";
689         $('#yahoo').before(document.getElementById('first'));
690         ok( expected == $('#en').text(), "Insert element before" );
691         
692         reset();
693         expected = "This is a normal link: Try them out:diveintomarkYahoo";
694         $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
695         ok( expected == $('#en').text(), "Insert array of elements before" );
696         
697         reset();
698         expected = "This is a normal link: Try them out:diveintomarkYahoo";
699         $('#yahoo').before($("#first, #mark"));
700         ok( expected == $('#en').text(), "Insert jQuery before" );
701 });
702
703 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
704         expect(4);
705         var expected = 'This is a normal link: bugaYahoo';
706         $('<b>buga</b>').insertBefore('#yahoo');
707         ok( expected == $('#en').text(), 'Insert String before' );
708         
709         reset();
710         expected = "This is a normal link: Try them out:Yahoo";
711         $(document.getElementById('first')).insertBefore('#yahoo');
712         ok( expected == $('#en').text(), "Insert element before" );
713         
714         reset();
715         expected = "This is a normal link: Try them out:diveintomarkYahoo";
716         $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
717         ok( expected == $('#en').text(), "Insert array of elements before" );
718         
719         reset();
720         expected = "This is a normal link: Try them out:diveintomarkYahoo";
721         $("#first, #mark").insertBefore('#yahoo');
722         ok( expected == $('#en').text(), "Insert jQuery before" );
723 });
724
725 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
726         expect(4);
727         var expected = 'This is a normal link: Yahoobuga';
728         $('#yahoo').after('<b>buga</b>');
729         ok( expected == $('#en').text(), 'Insert String after' );
730         
731         reset();
732         expected = "This is a normal link: YahooTry them out:";
733         $('#yahoo').after(document.getElementById('first'));
734         ok( expected == $('#en').text(), "Insert element after" );
735
736         reset();
737         expected = "This is a normal link: YahooTry them out:diveintomark";
738         $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
739         ok( expected == $('#en').text(), "Insert array of elements after" );
740         
741         reset();
742         expected = "This is a normal link: YahooTry them out:diveintomark";
743         $('#yahoo').after($("#first, #mark"));
744         ok( expected == $('#en').text(), "Insert jQuery after" );
745 });
746
747 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
748         expect(4);
749         var expected = 'This is a normal link: Yahoobuga';
750         $('<b>buga</b>').insertAfter('#yahoo');
751         ok( expected == $('#en').text(), 'Insert String after' );
752         
753         reset();
754         expected = "This is a normal link: YahooTry them out:";
755         $(document.getElementById('first')).insertAfter('#yahoo');
756         ok( expected == $('#en').text(), "Insert element after" );
757
758         reset();
759         expected = "This is a normal link: YahooTry them out:diveintomark";
760         $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
761         ok( expected == $('#en').text(), "Insert array of elements after" );
762         
763         reset();
764         expected = "This is a normal link: YahooTry them out:diveintomark";
765         $("#mark, #first").insertAfter('#yahoo');
766         ok( expected == $('#en').text(), "Insert jQuery after" );
767 });
768
769 test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
770         expect(10);
771         $('#yahoo').replaceWith('<b id="replace">buga</b>');
772         ok( $("#replace")[0], 'Replace element with string' );
773         ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
774         
775         reset();
776         $('#yahoo').replaceWith(document.getElementById('first'));
777         ok( $("#first")[0], 'Replace element with element' );
778         ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
779
780         reset();
781         $('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
782         ok( $("#first")[0], 'Replace element with array of elements' );
783         ok( $("#mark")[0], 'Replace element with array of elements' );
784         ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
785         
786         reset();
787         $('#yahoo').replaceWith($("#first, #mark"));
788         ok( $("#first")[0], 'Replace element with set of elements' );
789         ok( $("#mark")[0], 'Replace element with set of elements' );
790         ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
791 });
792
793 test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
794         expect(10);
795         $('<b id="replace">buga</b>').replaceAll("#yahoo");
796         ok( $("#replace")[0], 'Replace element with string' );
797         ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
798         
799         reset();
800         $(document.getElementById('first')).replaceAll("#yahoo");
801         ok( $("#first")[0], 'Replace element with element' );
802         ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
803
804         reset();
805         $([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
806         ok( $("#first")[0], 'Replace element with array of elements' );
807         ok( $("#mark")[0], 'Replace element with array of elements' );
808         ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
809         
810         reset();
811         $("#first, #mark").replaceAll("#yahoo");
812         ok( $("#first")[0], 'Replace element with set of elements' );
813         ok( $("#mark")[0], 'Replace element with set of elements' );
814         ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
815 });
816
817 test("end()", function() {
818         expect(3);
819         ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
820         ok( $('#yahoo').end(), 'Check for end with nothing to end' );
821         
822         var x = $('#yahoo');
823         x.parent();
824         ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
825 });
826
827 test("find(String)", function() {
828         expect(1);
829         ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
830 });
831
832 test("clone()", function() {
833         expect(3);
834         ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
835         var clone = $('#yahoo').clone();
836         ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
837         ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
838 });
839
840 test("is(String)", function() {
841         expect(26);
842         ok( $('#form').is('form'), 'Check for element: A form must be a form' );
843         ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
844         ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
845         ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
846         ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
847         ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
848         ok( $('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
849         ok( !$('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
850         ok( $('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
851         ok( !$('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
852         ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
853         ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
854         ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
855         ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
856         ok( $('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );
857         ok( !$('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );
858         ok( $('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );
859         ok( !$('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
860         ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
861         ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
862         ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
863         ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
864         
865         // test is() with comma-seperated expressions
866         ok( $('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
867         ok( $('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
868         ok( $('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
869         ok( $('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
870 });
871
872 test("$.extend(Object, Object)", function() {
873         expect(17);
874
875         var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
876                 options =     { xnumber2: 1, xstring2: "x", xxx: "newstring" },
877                 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
878                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
879                 deep1 = { foo: { bar: true } },
880                 deep1copy = { foo: { bar: true } },
881                 deep2 = { foo: { baz: true }, foo2: document },
882                 deep2copy = { foo: { baz: true }, foo2: document },
883                 deepmerged = { foo: { bar: true, baz: true }, foo2: document };
884
885         jQuery.extend(settings, options);
886         isObj( settings, merged, "Check if extended: settings must be extended" );
887         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
888
889         jQuery.extend(settings, null, options);
890         isObj( settings, merged, "Check if extended: settings must be extended" );
891         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
892
893         jQuery.extend(true, deep1, deep2);
894         isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
895         isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
896         equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
897
898         var target = {};
899         var recursive = { foo:target, bar:5 };
900         jQuery.extend(true, target, recursive);
901         isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
902
903         var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
904         ok( ret.foo.length == 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
905
906         var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
907         ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
908
909         var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
910         ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
911
912         var obj = { foo:null };
913         jQuery.extend(true, obj, { foo:"notnull" } );
914         equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
915
916         function func() {}
917         jQuery.extend(func, { key: "value" } );
918         equals( func.key, "value", "Verify a function can be extended" );
919
920         var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
921                 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
922                 options1 =     { xnumber2: 1, xstring2: "x" },
923                 options1Copy = { xnumber2: 1, xstring2: "x" },
924                 options2 =     { xstring2: "xx", xxx: "newstringx" },
925                 options2Copy = { xstring2: "xx", xxx: "newstringx" },
926                 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
927
928         var settings = jQuery.extend({}, defaults, options1, options2);
929         isObj( settings, merged2, "Check if extended: settings must be extended" );
930         isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
931         isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
932         isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
933 });
934
935 test("val()", function() {
936         expect(3);
937         ok( $("#text1").val() == "Test", "Check for value of input element" );
938         ok( !$("#text1").val() == "", "Check for value of input element" );
939         // ticket #1714 this caused a JS error in IE
940         ok( $("#first").val() == "", "Check a paragraph element to see if it has a value" );
941 });
942
943 test("val(String)", function() {
944         expect(3);
945         document.getElementById('text1').value = "bla";
946         ok( $("#text1").val() == "bla", "Check for modified value of input element" );
947         $("#text1").val('test');
948         ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
949         
950         $("#select1").val("3");
951         ok( $("#select1").val() == "3", "Check for modified (via val(String)) value of select element" );
952 });
953
954 var scriptorder = 0;
955
956 test("html(String)", function() {
957         expect(10);
958         var div = $("#main > div");
959         div.html("<b>test</b>");
960         var pass = true;
961         for ( var i = 0; i < div.size(); i++ ) {
962                 if ( div.get(i).childNodes.length != 1 ) pass = false;
963         }
964         ok( pass, "Set HTML" );
965
966         $("#main").html("<select/>");
967         $("#main select").html("<option>O1</option><option selected='selected'>O2</option><option>O3</option>");
968         equals( $("#main select").val(), "O2", "Selected option correct" );
969
970         stop();
971
972         $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>');
973
974         $("#main").html('foo <form><script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
975
976         $("#main").html("<script>ok(scriptorder++ == 0, 'Script is executed in order');ok($('#scriptorder').length == 0,'Execute before html')<\/script><span id='scriptorder'><script>ok(scriptorder++ == 1, 'Script is executed in order');ok($('#scriptorder').length == 1,'Execute after html')<\/script></span><script>ok(scriptorder++ == 2, 'Script is executed in order');ok($('#scriptorder').length == 1,'Execute after html')<\/script>");
977
978         setTimeout( start, 100 );
979 });
980
981 test("filter()", function() {
982         expect(4);
983         isSet( $("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
984         isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
985         isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
986         isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
987 });
988
989 test("not()", function() {
990         expect(3);
991         ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
992         isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
993         isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
994 });
995
996 test("andSelf()", function() {
997         expect(4);
998         isSet( $("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
999         isSet( $("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
1000         isSet( $("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" );
1001         isSet( $("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" );
1002 });
1003
1004 test("siblings([String])", function() {
1005         expect(5);
1006         isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
1007         isSet( $("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" ); 
1008         isSet( $("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
1009         isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" );
1010         isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );
1011 });
1012
1013 test("children([String])", function() {
1014         expect(3);
1015         isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
1016         isSet( $("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
1017         isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
1018 });
1019
1020 test("parent([String])", function() {
1021         expect(5);
1022         ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
1023         ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
1024         ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
1025         ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
1026         isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
1027 });
1028         
1029 test("parents([String])", function() {
1030         expect(5);
1031         ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
1032         ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
1033         ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
1034         isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
1035         isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );
1036 });
1037
1038 test("next([String])", function() {
1039         expect(4);
1040         ok( $("#ap").next()[0].id == "foo", "Simple next check" );
1041         ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
1042         ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
1043         ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
1044 });
1045         
1046 test("prev([String])", function() {
1047         expect(4);
1048         ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
1049         ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
1050         ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
1051         ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
1052 });
1053
1054 test("show()", function() {
1055         expect(1);
1056         var pass = true, div = $("div");
1057         div.show().each(function(){
1058           if ( this.style.display == "none" ) pass = false;
1059         });
1060         ok( pass, "Show" );
1061 });
1062
1063 test("addClass(String)", function() {
1064         expect(1);
1065         var div = $("div");
1066         div.addClass("test");
1067         var pass = true;
1068         for ( var i = 0; i < div.size(); i++ ) {
1069          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
1070         }
1071         ok( pass, "Add Class" );
1072 });
1073
1074 test("removeClass(String) - simple", function() {
1075         expect(3);
1076         var div = $("div").addClass("test").removeClass("test"),
1077                 pass = true;
1078         for ( var i = 0; i < div.size(); i++ ) {
1079                 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
1080         }
1081         ok( pass, "Remove Class" );
1082         
1083         reset();
1084         var div = $("div").addClass("test").addClass("foo").addClass("bar");
1085         div.removeClass("test").removeClass("bar").removeClass("foo");
1086         var pass = true;
1087         for ( var i = 0; i < div.size(); i++ ) {
1088          if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
1089         }
1090         ok( pass, "Remove multiple classes" );
1091         
1092         reset();
1093         var div = $("div:eq(0)").addClass("test").removeClass("");
1094         ok( div.is('.test'), "Empty string passed to removeClass" );
1095         
1096 });
1097
1098 test("toggleClass(String)", function() {
1099         expect(3);
1100         var e = $("#firstp");
1101         ok( !e.is(".test"), "Assert class not present" );
1102         e.toggleClass("test");
1103         ok( e.is(".test"), "Assert class present" ); 
1104         e.toggleClass("test");
1105         ok( !e.is(".test"), "Assert class not present" );
1106 });
1107
1108 test("removeAttr(String", function() {
1109         expect(1);
1110         ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
1111 });
1112
1113 test("text(String)", function() {
1114         expect(1);
1115         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" );
1116 });
1117
1118 test("$.each(Object,Function)", function() {
1119         expect(8);
1120         $.each( [0,1,2], function(i, n){
1121                 ok( i == n, "Check array iteration" );
1122         });
1123         
1124         $.each( [5,6,7], function(i, n){
1125                 ok( i == n - 5, "Check array iteration" );
1126         });
1127          
1128         $.each( { name: "name", lang: "lang" }, function(i, n){
1129                 ok( i == n, "Check object iteration" );
1130         });
1131 });
1132
1133 test("$.prop", function() {
1134         expect(2);
1135         var handle = function() { return this.id };
1136         ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
1137         ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
1138 });
1139
1140 test("$.className", function() {
1141         expect(6);
1142         var x = $("<p>Hi</p>")[0];
1143         var c = $.className;
1144         c.add(x, "hi");
1145         ok( x.className == "hi", "Check single added class" );
1146         c.add(x, "foo bar");
1147         ok( x.className == "hi foo bar", "Check more added classes" );
1148         c.remove(x);
1149         ok( x.className == "", "Remove all classes" );
1150         c.add(x, "hi foo bar");
1151         c.remove(x, "foo");
1152         ok( x.className == "hi bar", "Check removal of one class" );
1153         ok( c.has(x, "hi"), "Check has1" );
1154         ok( c.has(x, "bar"), "Check has2" );
1155 });
1156
1157 test("remove()", function() {
1158         expect(4);
1159         $("#ap").children().remove();
1160         ok( $("#ap").text().length > 10, "Check text is not removed" );
1161         ok( $("#ap").children().length == 0, "Check remove" );
1162         
1163         reset();
1164         $("#ap").children().remove("a");
1165         ok( $("#ap").text().length > 10, "Check text is not removed" );
1166         ok( $("#ap").children().length == 1, "Check filtered remove" );
1167 });
1168
1169 test("empty()", function() {
1170         expect(2);
1171         ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
1172         ok( $("#ap").children().length == 4, "Check elements are not removed" );
1173 });
1174
1175 test("slice()", function() {
1176         expect(5);
1177         isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" );
1178         isSet( $("#ap a").slice(1), q("groups", "anchor1", "mark"), "slice(1)" );
1179         isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
1180         isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" );
1181
1182         isSet( $("#ap a").eq(1), q("groups"), "eq(1)" );
1183 });
1184
1185 test("map()", function() {
1186         expect(2);
1187
1188         isSet(
1189                 $("#ap").map(function(){
1190                         return $(this).find("a").get();
1191                 }),
1192                 q("google", "groups", "anchor1", "mark"),
1193                 "Array Map"
1194         );
1195
1196         isSet(
1197                 $("#ap > a").map(function(){
1198                         return this.parentNode;
1199                 }),
1200                 q("ap","ap","ap"),
1201                 "Single Map"
1202         );
1203 });
1204
1205 test("contents()", function() {
1206         expect(10);
1207         equals( $("#ap").contents().length, 9, "Check element contents" );
1208         ok( $("#iframe").contents()[0], "Check existance of IFrame document" );
1209         var ibody = $("#loadediframe").contents()[0].body;
1210         ok( ibody, "Check existance of IFrame body" );
1211
1212         equals( $("span", ibody).text(), "span text", "Find span in IFrame and check its text" );
1213
1214         $(ibody).append("<div>init text</div>");
1215         equals( $("div", ibody).length, 2, "Check the original div and the new div are in IFrame" );
1216
1217         equals( $("div:last", ibody).text(), "init text", "Add text to div in IFrame" );
1218
1219         $("div:last", ibody).text("div text");
1220         equals( $("div:last", ibody).text(), "div text", "Add text to div in IFrame" );
1221
1222         $("div:last", ibody).remove();
1223         equals( $("div", ibody).length, 1, "Delete the div and check only one div left in IFrame" );
1224
1225         equals( $("div", ibody).text(), "span text", "Make sure the correct div is still left after deletion in IFrame" );
1226
1227         $("<table/>", ibody).append("<tr><td>cell</td></tr>").appendTo(ibody);
1228         $("table", ibody).remove();
1229         equals( $("div", ibody).length, 1, "Check for JS error on add and delete of a table in IFrame" );
1230 });