Support for .foo(Function) and testing. TODO: More tests
[jquery.git] / test / unit / manipulation.js
1 module("manipulation");
2
3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
5
6 test("text()", function() {
7         expect(1);
8         var expected = "This link has class=\"blog\": Simon Willison's Weblog";
9         equals( jQuery('#sap').text(), expected, 'Check for merged text of more then one element.' );
10 });
11
12 test("wrap(String|Element)", function() {
13         expect(10);
14         var defaultText = 'Try them out:'
15         var result = jQuery('#first').wrap('<div class="red"><span></span></div>').text();
16         equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
17         ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
18
19         reset();
20         var defaultText = 'Try them out:'
21         var result = jQuery('#first').wrap(document.getElementById('empty')).parent();
22         ok( result.is('ol'), 'Check for element wrapping' );
23         equals( result.text(), defaultText, 'Check for element wrapping' );
24
25         reset();
26         jQuery('#check1').click(function() {
27                 var checkbox = this;
28                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
29                 jQuery(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
30                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
31         }).click();
32
33         // using contents will get comments regular, text, and comment nodes
34         var j = jQuery("#nonnodes").contents();
35         j.wrap("<i></i>");
36         equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
37         equals( jQuery("#nonnodes > i").text(), j.text() + j[1].nodeValue, "Check node,textnode,comment wraps doesn't hurt text" );
38
39         // Try wrapping a disconnected node
40         j = jQuery("<label/>").wrap("<li/>");
41         equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
42         equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
43 });
44
45 test("wrapAll(String|Element)", function() {
46         expect(8);
47         var prev = jQuery("#firstp")[0].previousSibling;
48         var p = jQuery("#firstp,#first")[0].parentNode;
49         var result = jQuery('#firstp,#first').wrapAll('<div class="red"><div id="tmp"></div></div>');
50         equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
51         ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
52         ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
53         equals( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
54         equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
55
56         reset();
57         var prev = jQuery("#firstp")[0].previousSibling;
58         var p = jQuery("#first")[0].parentNode;
59         var result = jQuery('#firstp,#first').wrapAll(document.getElementById('empty'));
60         equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
61         equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
62         equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
63 });
64
65 var testWrapInner = function(val) {
66         expect(6);
67         var num = jQuery("#first").children().length;
68         var result = jQuery('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
69         equals( jQuery("#first").children().length, 1, "Only one child" );
70         ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
71         equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
72
73         reset();
74         var num = jQuery("#first").children().length;
75         var result = jQuery('#first').wrapInner(document.getElementById('empty'));
76         equals( jQuery("#first").children().length, 1, "Only one child" );
77         ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
78         equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" ); 
79 }
80
81 test("wrapInner(String|Element)", function() {
82         testWrapInner(bareObj);
83 });
84
85 /* test("wrapInner(Function)", function() {
86         testWrapInner(functionReturningObj)
87 }) */
88
89 var testAppend = function(valueObj) {
90         expect(21);
91         var defaultText = 'Try them out:'
92         var result = jQuery('#first').append(valueObj('<b>buga</b>'));
93         equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
94         equals( jQuery('#select3').append(valueObj('<option value="appendTest">Append Test</option>')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
95
96         reset();
97         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
98         jQuery('#sap').append(valueObj(document.getElementById('first')));
99         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
100
101         reset();
102         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
103         jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
104         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
105
106         reset();
107         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
108         jQuery('#sap').append(valueObj(jQuery("#first, #yahoo")));
109         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
110
111         reset();
112         jQuery("#sap").append(valueObj( 5 ));
113         ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
114
115         reset();
116         jQuery("#sap").append(valueObj( " text with spaces " ));
117         ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
118
119         reset();
120         ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
121         ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
122         ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
123
124         reset();
125         jQuery("#sap").append(valueObj( document.getElementById('form') ));
126         equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
127
128         reset();
129         var pass = true;
130         try {
131                 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "<div>test</div>" ));
132         } catch(e) {
133                 pass = false;
134         }
135
136         ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
137
138         reset();
139         jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
140         t( 'Append legend', '#legend', ['legend'] );
141
142         reset();
143         jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
144         equals( jQuery('#select1 option:last').text(), "Test", "Appending &lt;OPTION&gt; (all caps)" );
145
146         jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
147         ok( jQuery('#table colgroup').length, "Append colgroup" );
148
149         jQuery('#table colgroup').append(valueObj( '<col/>' ));
150         ok( jQuery('#table colgroup col').length, "Append col" );
151
152         reset();
153         jQuery('#table').append(valueObj( '<caption></caption>' ));
154         ok( jQuery('#table caption').length, "Append caption" );
155
156         reset();
157         jQuery('form:last')
158                 .append(valueObj( '<select id="appendSelect1"></select>' ))
159                 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
160
161         t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
162
163         // using contents will get comments regular, text, and comment nodes
164         var j = jQuery("#nonnodes").contents();
165         var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
166         equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
167         ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
168         d.contents().appendTo("#nonnodes");
169         d.remove();
170         ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
171 }
172
173 test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
174   testAppend(bareObj);
175 });
176
177 test("append(Function)", function() {
178         testAppend(functionReturningObj);
179 })
180
181 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
182         expect(12);
183         var defaultText = 'Try them out:'
184         jQuery('<b>buga</b>').appendTo('#first');
185         equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
186         equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
187
188         reset();
189         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
190         jQuery(document.getElementById('first')).appendTo('#sap');
191         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
192
193         reset();
194         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
195         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
196         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
197
198         reset();
199         ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
200
201         reset();
202         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
203         jQuery("#first, #yahoo").appendTo('#sap');
204         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
205
206         reset();
207         jQuery('#select1').appendTo('#foo');
208         t( 'Append select', '#foo select', ['select1'] );
209
210         reset();
211         var div = jQuery("<div/>").click(function(){
212                 ok(true, "Running a cloned click.");
213         });
214         div.appendTo("#main, #moretests");
215
216         jQuery("#main div:last").click();
217         jQuery("#moretests div:last").click();
218
219         reset();
220         var div = jQuery("<div/>").appendTo("#main, #moretests");
221
222         equals( div.length, 2, "appendTo returns the inserted elements" );
223         
224         div.addClass("test");
225
226         ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
227         ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
228
229         reset();
230 });
231
232 var testPrepend = function(val) {
233         expect(5);
234         var defaultText = 'Try them out:'
235         var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
236         equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
237         equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
238
239         reset();
240         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
241         jQuery('#sap').prepend(val( document.getElementById('first') ));
242         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
243
244         reset();
245         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
246         jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
247         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
248
249         reset();
250         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
251         jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
252         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
253 }
254
255 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
256         testPrepend(bareObj);
257 });
258
259 test("prepend(Function)", function() {
260         testPrepend(functionReturningObj);
261 })
262
263 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
264         expect(6);
265         var defaultText = 'Try them out:'
266         jQuery('<b>buga</b>').prependTo('#first');
267         equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
268         equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
269
270         reset();
271         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
272         jQuery(document.getElementById('first')).prependTo('#sap');
273         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
274
275         reset();
276         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
277         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
278         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
279
280         reset();
281         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
282         jQuery("#first, #yahoo").prependTo('#sap');
283         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
284
285         reset();
286         jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
287         jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
288
289         t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
290 });
291
292 var testBefore = function(val) {
293         expect(4);
294         var expected = 'This is a normal link: bugaYahoo';
295         jQuery('#yahoo').before(val( '<b>buga</b>' ));
296         equals( expected, jQuery('#en').text(), 'Insert String before' );
297
298         reset();
299         expected = "This is a normal link: Try them out:Yahoo";
300         jQuery('#yahoo').before(val( document.getElementById('first') ));
301         equals( expected, jQuery('#en').text(), "Insert element before" );
302
303         reset();
304         expected = "This is a normal link: Try them out:diveintomarkYahoo";
305         jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
306         equals( expected, jQuery('#en').text(), "Insert array of elements before" );
307
308         reset();
309         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
310         jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
311         equals( expected, jQuery('#en').text(), "Insert jQuery before" );
312 }
313
314 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
315         testBefore(bareObj);
316 });
317
318 test("before(Function)", function() {
319         testBefore(functionReturningObj);
320 })
321
322 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
323         expect(4);
324         var expected = 'This is a normal link: bugaYahoo';
325         jQuery('<b>buga</b>').insertBefore('#yahoo');
326         equals( expected, jQuery('#en').text(), 'Insert String before' );
327
328         reset();
329         expected = "This is a normal link: Try them out:Yahoo";
330         jQuery(document.getElementById('first')).insertBefore('#yahoo');
331         equals( expected, jQuery('#en').text(), "Insert element before" );
332
333         reset();
334         expected = "This is a normal link: Try them out:diveintomarkYahoo";
335         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
336         equals( expected, jQuery('#en').text(), "Insert array of elements before" );
337
338         reset();
339         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
340         jQuery("#first, #mark").insertBefore('#yahoo');
341         equals( expected, jQuery('#en').text(), "Insert jQuery before" );
342 });
343
344 var testAfter = function(val) {
345         expect(4);
346         var expected = 'This is a normal link: Yahoobuga';
347         jQuery('#yahoo').after(val( '<b>buga</b>' ));
348         equals( expected, jQuery('#en').text(), 'Insert String after' );
349
350         reset();
351         expected = "This is a normal link: YahooTry them out:";
352         jQuery('#yahoo').after(val( document.getElementById('first') ));
353         equals( expected, jQuery('#en').text(), "Insert element after" );
354
355         reset();
356         expected = "This is a normal link: YahooTry them out:diveintomark";
357         jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
358         equals( expected, jQuery('#en').text(), "Insert array of elements after" );
359
360         reset();
361         expected = "This is a normal link: YahoodiveintomarkTry them out:";
362         jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
363         equals( expected, jQuery('#en').text(), "Insert jQuery after" );        
364 };
365
366 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
367         testAfter(bareObj);
368 });
369
370 test("after(Function)", function() {
371         testAfter(functionReturningObj);
372 })
373
374 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
375         expect(4);
376         var expected = 'This is a normal link: Yahoobuga';
377         jQuery('<b>buga</b>').insertAfter('#yahoo');
378         equals( expected, jQuery('#en').text(), 'Insert String after' );
379
380         reset();
381         expected = "This is a normal link: YahooTry them out:";
382         jQuery(document.getElementById('first')).insertAfter('#yahoo');
383         equals( expected, jQuery('#en').text(), "Insert element after" );
384
385         reset();
386         expected = "This is a normal link: YahooTry them out:diveintomark";
387         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
388         equals( expected, jQuery('#en').text(), "Insert array of elements after" );
389
390         reset();
391         expected = "This is a normal link: YahoodiveintomarkTry them out:";
392         jQuery("#first, #mark").insertAfter('#yahoo');
393         equals( expected, jQuery('#en').text(), "Insert jQuery after" );
394 });
395
396 test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
397         expect(10);
398         jQuery('#yahoo').replaceWith('<b id="replace">buga</b>');
399         ok( jQuery("#replace")[0], 'Replace element with string' );
400         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
401
402         reset();
403         jQuery('#yahoo').replaceWith(document.getElementById('first'));
404         ok( jQuery("#first")[0], 'Replace element with element' );
405         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
406
407         reset();
408         jQuery('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
409         ok( jQuery("#first")[0], 'Replace element with array of elements' );
410         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
411         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
412
413         reset();
414         jQuery('#yahoo').replaceWith(jQuery("#first, #mark"));
415         ok( jQuery("#first")[0], 'Replace element with set of elements' );
416         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
417         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
418 });
419
420 test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
421         expect(10);
422         jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
423         ok( jQuery("#replace")[0], 'Replace element with string' );
424         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
425         
426         reset();
427         jQuery(document.getElementById('first')).replaceAll("#yahoo");
428         ok( jQuery("#first")[0], 'Replace element with element' );
429         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
430
431         reset();
432         jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
433         ok( jQuery("#first")[0], 'Replace element with array of elements' );
434         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
435         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
436
437         reset();
438         jQuery("#first, #mark").replaceAll("#yahoo");
439         ok( jQuery("#first")[0], 'Replace element with set of elements' );
440         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
441         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
442 });
443
444 test("clone()", function() {
445         expect(28);
446         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
447         var clone = jQuery('#yahoo').clone();
448         equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
449         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
450
451         var cloneTags = [
452                 "<table/>", "<tr/>", "<td/>", "<div/>",
453                 "<button/>", "<ul/>", "<ol/>", "<li/>",
454                 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
455                 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
456         ];
457         for (var i = 0; i < cloneTags.length; i++) {
458                 var j = jQuery(cloneTags[i]);
459                 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a &lt;' + cloneTags[i].substring(1));
460         }
461
462         // using contents will get comments regular, text, and comment nodes
463         var cl = jQuery("#nonnodes").contents().clone();
464         ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
465
466         var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
467                 ok( true, "Bound event still exists." );
468         });
469
470         div = div.clone(true).clone(true);
471         equals( div.length, 1, "One element cloned" );
472         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
473         div.trigger("click");
474
475         div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
476         div.find("table").click(function(){
477                 ok( true, "Bound event still exists." );
478         });
479
480         div = div.clone(true);
481         equals( div.length, 1, "One element cloned" );
482         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
483         div.find("table:last").trigger("click");
484
485         div = jQuery("<div/>").html('<object height="355" width="425">  <param name="movie" value="http://www.youtube.com/v/JikaHBDoV3k&amp;hl=en">  <param name="wmode" value="transparent"> </object>');
486
487         div = div.clone(true);
488         equals( div.length, 1, "One element cloned" );
489         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
490 });
491
492 if (!isLocal) {
493 test("clone() on XML nodes", function() {
494         expect(2);
495         stop();
496         jQuery.get("data/dashboard.xml", function (xml) {
497                 var root = jQuery(xml.documentElement).clone();
498                 var origTab = jQuery("tab", xml).eq(0);
499                 var cloneTab = jQuery("tab", root).eq(0);
500                 origTab.text("origval");
501                 cloneTab.text("cloneval");
502                 equals(origTab.text(), "origval", "Check original XML node was correctly set");
503                 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
504                 start();
505         });
506 });
507 }
508
509 test("val()", function() {
510         expect(9);
511
512         document.getElementById('text1').value = "bla";
513         equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
514         
515         reset();
516
517         equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
518         // ticket #1714 this caused a JS error in IE
519         equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
520         ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
521         
522         equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
523
524         isSet( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
525
526         equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
527         
528         equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
529         
530         equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
531         
532 });
533
534 var testVal = function(valueObj) {
535         expect(5);
536
537         jQuery("#text1").val(valueObj( 'test' ));
538         equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
539         
540         jQuery("#text1").val(valueObj( 67 ));
541         equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
542
543         jQuery("#select1").val(valueObj( "3" ));
544         equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
545
546         jQuery("#select1").val(valueObj( 2 ));
547         equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
548
549         // using contents will get comments regular, text, and comment nodes
550         var j = jQuery("#nonnodes").contents();
551         j.val(valueObj( "asdf" ));
552         equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
553         j.removeAttr("value");
554 }
555
556 test("val(String/Number)", function() {
557         testVal(bareObj);
558 });
559
560 test("val(Function)", function() {
561         testVal(functionReturningObj);
562 })
563
564 var testHtml = function(valueObj) {
565         expect(17);
566         
567         window.debug = true;
568         
569         jQuery.scriptorder = 0;
570         
571         var div = jQuery("#main > div");
572         div.html(valueObj("<b>test</b>"));
573         var pass = true;
574         for ( var i = 0; i < div.size(); i++ ) {
575                 if ( div.get(i).childNodes.length != 1 ) pass = false;
576         }
577         ok( pass, "Set HTML" );
578         
579         window.debug = false;
580
581         reset();
582         // using contents will get comments regular, text, and comment nodes
583         var j = jQuery("#nonnodes").contents();
584         j.html(valueObj("<b>bold</b>"));
585         
586         // this is needed, or the expando added by jQuery unique will yield a different html
587         j.find('b').removeData();
588         equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
589         
590         jQuery("#main").html(valueObj("<select/>"));
591         jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
592         equals( jQuery("#main select").val(), "O2", "Selected option correct" );
593         
594         var $div = jQuery('<div />');
595         equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
596         equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
597         
598         reset();
599         
600         jQuery("#main").html(valueObj('<script type="something/else">ok( false, "Non-script evaluated." );</script><script type="text/javascript">ok( true, "text/javascript is evaluated." );</script><script>ok( true, "No type is evaluated." );</script><div><script type="text/javascript">ok( true, "Inner text/javascript is evaluated." );</script><script>ok( true, "Inner No type is evaluated." );</script><script type="something/else">ok( false, "Non-script evaluated." );</script></div>'));
601         
602         stop();
603         
604         jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>'));
605         
606         jQuery("#main").html(valueObj('foo <form><script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>'));
607         
608         // 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
609         jQuery("#main").html(valueObj("<script>equals(jQuery.scriptorder++, 0, 'Script is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html (even though appears before)')<\/script><span id='scriptorder'><script>equals(jQuery.scriptorder++, 1, 'Script (nested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equals(jQuery.scriptorder++, 2, 'Script (unnested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script>"));
610         
611         setTimeout( start, 100 );
612 }
613
614 test("html(String)", function() {
615         testHtml(bareObj);
616 });
617
618 test("html(Function)", function() {
619         testHtml(functionReturningObj);
620 })
621
622 var testText = function(valueObj) {
623         expect(4);
624         equals( jQuery("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML.replace(/>/g, "&gt;"), "&lt;div&gt;&lt;b&gt;Hello&lt;/b&gt; cruel world!&lt;/div&gt;", "Check escaped text" );
625
626         // using contents will get comments regular, text, and comment nodes
627         var j = jQuery("#nonnodes").contents();
628         j.text("hi!");
629         equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
630         equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
631         equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );  
632 }
633
634 test("text(String)", function() {
635         testText(bareObj)
636 });
637
638 test("text(Function)", function() {
639         testText(functionReturningObj);
640 })
641
642 test("remove()", function() {
643         expect(7);
644         jQuery("#ap").children().remove();
645         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
646         equals( jQuery("#ap").children().length, 0, "Check remove" );
647
648         reset();
649         jQuery("#ap").children().remove("a");
650         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
651         equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
652
653         jQuery("#ap").children().remove("a, code");
654         equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
655
656         // using contents will get comments regular, text, and comment nodes
657         equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
658         jQuery("#nonnodes").contents().remove();
659         equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
660 });
661
662 test("empty()", function() {
663         expect(3);
664         equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
665         equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
666
667         // using contents will get comments regular, text, and comment nodes
668         var j = jQuery("#nonnodes").contents();
669         j.empty();
670         equals( j.html(), "", "Check node,textnode,comment empty works" );
671 });
672