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