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