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