Switched to using DOM Fragments in domManip.
[jquery.git] / test / unit / core.js
1 module("core");\r
2 \r
3 test("Basic requirements", function() {\r
4         expect(7);\r
5         ok( Array.prototype.push, "Array.push()" );\r
6         ok( Function.prototype.apply, "Function.apply()" );\r
7         ok( document.getElementById, "getElementById" );\r
8         ok( document.getElementsByTagName, "getElementsByTagName" );\r
9         ok( RegExp, "RegExp" );\r
10         ok( jQuery, "jQuery" );\r
11         ok( $, "$" );\r
12 });\r
13 \r
14 test("jQuery()", function() {\r
15         expect(8);\r
16 \r
17         var main = jQuery("#main");\r
18         isSet( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );\r
19 \r
20 /*\r
21         // disabled since this test was doing nothing. i tried to fix it but i'm not sure\r
22         // what the expected behavior should even be. FF returns "\n" for the text node\r
23         // make sure this is handled\r
24         var crlfContainer = jQuery('<p>\r\n</p>');\r
25         var x = crlfContainer.contents().get(0).nodeValue;\r
26         equals( x, what???, "Check for \\r and \\n in jQuery()" );\r
27 */\r
28 \r
29         /* // Disabled until we add this functionality in\r
30         var pass = true;\r
31         try {\r
32                 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);\r
33         } catch(e){\r
34                 pass = false;\r
35         }\r
36         ok( pass, "jQuery('&lt;tag&gt;') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/\r
37 \r
38         var code = jQuery("<code/>");\r
39         equals( code.length, 1, "Correct number of elements generated for code" );\r
40         var img = jQuery("<img/>");\r
41         equals( img.length, 1, "Correct number of elements generated for img" );\r
42         var div = jQuery("<div/><hr/><code/><b/>");\r
43         equals( div.length, 4, "Correct number of elements generated for div hr code b" );\r
44 \r
45         // can actually yield more than one, when iframes are included, the window is an array as well\r
46         equals( jQuery(window).length, 1, "Correct number of elements generated for window" );\r
47 \r
48         equals( jQuery(document).length, 1, "Correct number of elements generated for document" );\r
49 \r
50         equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );\r
51 \r
52         equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" );\r
53 });
54
55 test("selector state", function() {
56         expect(26);
57
58         var test;
59         
60         test = jQuery();
61         equals( test.selector, "", "Empty jQuery Selector" );
62         equals( test.context, document, "Empty jQuery Context" );
63         
64         test = jQuery(document);
65         equals( test.selector, "", "Document Selector" );
66         equals( test.context, document, "Document Context" );
67         
68         test = jQuery(document.body);
69         equals( test.selector, "", "Body Selector" );
70         equals( test.context, document.body, "Body Context" );
71         
72         test = jQuery("#main");
73         equals( test.selector, "#main", "#main Selector" );
74         equals( test.context, document, "#main Context" );
75         
76         test = jQuery("#main", document);
77         equals( test.selector, "#main", "#main Selector" );
78         equals( test.context, document, "#main Context" );
79         
80         test = jQuery("#main", document.body);
81         equals( test.selector, "#main", "#main Selector" );
82         equals( test.context, document.body, "#main Context" );
83         
84         test = jQuery(document.body).find("#main");
85         equals( test.selector, "#main", "#main find Selector" );
86         equals( test.context, document.body, "#main find Context" );
87
88         test = jQuery("#main").filter("div");
89         equals( test.selector, "#main.filter(div)", "#main filter Selector" );
90         equals( test.context, document, "#main filter Context" );
91         
92         test = jQuery("#main").not("div");
93         equals( test.selector, "#main.not(div)", "#main not Selector" );
94         equals( test.context, document, "#main not Context" );
95         
96         test = jQuery("#main").filter("div").not("div");
97         equals( test.selector, "#main.filter(div).not(div)", "#main filter, not Selector" );
98         equals( test.context, document, "#main filter, not Context" );
99         
100         test = jQuery("#main").filter("div").not("div").end();
101         equals( test.selector, "#main.filter(div)", "#main filter, not, end Selector" );
102         equals( test.context, document, "#main filter, not, end Context" );
103         
104         test = jQuery("#main").parent("body");
105         equals( test.selector, "#main.parent(body)", "#main parent Selector" );
106         equals( test.context, document, "#main parent Context" );
107         
108         test = jQuery("#main").eq(0);
109         equals( test.selector, "#main.slice(0,1)", "#main eq Selector" );
110         equals( test.context, document, "#main eq Context" );
111 });\r
112 \r
113 test("browser", function() {\r
114         expect(13);\r
115         var browsers = {\r
116                 //Internet Explorer\r
117                 "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0",\r
118                 "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)": "7.0",\r
119                 /** Failing #1876\r
120                  * "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30)": "7.0",\r
121                  */\r
122                 //Browsers with Gecko engine\r
123                 //Mozilla\r
124                 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915" : "1.7.12",\r
125                 //Firefox\r
126                 "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3": "1.8.1.3",\r
127                 //Netscape\r
128                 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20070321 Netscape/8.1.3" : "1.7.5",\r
129                 //Flock\r
130                 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.11) Gecko/20070321 Firefox/1.5.0.11 Flock/0.7.12" : "1.8.0.11",\r
131                 //Opera browser\r
132                 "Opera/9.20 (X11; Linux x86_64; U; en)": "9.20",\r
133                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.20" : "9.20",\r
134                 "Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20",\r
135                 //WebKit engine\r
136                 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "418.9",\r
137                 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" : "418.8",\r
138                 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.8",\r
139                 //Other user agent string\r
140                 "Other browser's user agent 1.0":null\r
141         };\r
142         for (var i in browsers) {\r
143                 var v = i.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ); // RegEx from Core jQuery.browser.version check\r
144                 version = v ? v[1] : null;\r
145                 equals( version, browsers[i], "Checking UA string" );\r
146         }\r
147 });\r
148 \r
149 test("noConflict", function() {\r
150         expect(6);\r
151 \r
152         var $$ = jQuery;\r
153 \r
154         equals( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );\r
155         equals( jQuery, $$, "Make sure jQuery wasn't touched." );\r
156         equals( $, original$, "Make sure $ was reverted." );\r
157 \r
158         jQuery = $ = $$;\r
159 \r
160         equals( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" );\r
161         equals( jQuery, originaljQuery, "Make sure jQuery was reverted." );\r
162         equals( $, original$, "Make sure $ was reverted." );\r
163 \r
164         jQuery = $$;\r
165 });\r
166 \r
167 test("isFunction", function() {\r
168         expect(19);\r
169 \r
170         // Make sure that false values return false\r
171         ok( !jQuery.isFunction(), "No Value" );\r
172         ok( !jQuery.isFunction( null ), "null Value" );\r
173         ok( !jQuery.isFunction( undefined ), "undefined Value" );\r
174         ok( !jQuery.isFunction( "" ), "Empty String Value" );\r
175         ok( !jQuery.isFunction( 0 ), "0 Value" );\r
176 \r
177         // Check built-ins\r
178         // Safari uses "(Internal Function)"\r
179         ok( jQuery.isFunction(String), "String Function("+String+")" );\r
180         ok( jQuery.isFunction(Array), "Array Function("+Array+")" );\r
181         ok( jQuery.isFunction(Object), "Object Function("+Object+")" );\r
182         ok( jQuery.isFunction(Function), "Function Function("+Function+")" );\r
183 \r
184         // When stringified, this could be misinterpreted\r
185         var mystr = "function";\r
186         ok( !jQuery.isFunction(mystr), "Function String" );\r
187 \r
188         // When stringified, this could be misinterpreted\r
189         var myarr = [ "function" ];\r
190         ok( !jQuery.isFunction(myarr), "Function Array" );\r
191 \r
192         // When stringified, this could be misinterpreted\r
193         var myfunction = { "function": "test" };\r
194         ok( !jQuery.isFunction(myfunction), "Function Object" );\r
195 \r
196         // Make sure normal functions still work\r
197         var fn = function(){};\r
198         ok( jQuery.isFunction(fn), "Normal Function" );\r
199 \r
200         var obj = document.createElement("object");\r
201 \r
202         // Firefox says this is a function\r
203         ok( !jQuery.isFunction(obj), "Object Element" );\r
204 \r
205         // IE says this is an object\r
206         // Since 1.3, this isn't supported (#2968)\r
207         //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );\r
208 \r
209         var nodes = document.body.childNodes;\r
210 \r
211         // Safari says this is a function\r
212         ok( !jQuery.isFunction(nodes), "childNodes Property" );\r
213 \r
214         var first = document.body.firstChild;\r
215 \r
216         // Normal elements are reported ok everywhere\r
217         ok( !jQuery.isFunction(first), "A normal DOM Element" );\r
218 \r
219         var input = document.createElement("input");\r
220         input.type = "text";\r
221         document.body.appendChild( input );\r
222 \r
223         // IE says this is an object\r
224         // Since 1.3, this isn't supported (#2968)\r
225         //ok( jQuery.isFunction(input.focus), "A default function property" );\r
226 \r
227         document.body.removeChild( input );\r
228 \r
229         var a = document.createElement("a");\r
230         a.href = "some-function";\r
231         document.body.appendChild( a );\r
232 \r
233         // This serializes with the word 'function' in it\r
234         ok( !jQuery.isFunction(a), "Anchor Element" );\r
235 \r
236         document.body.removeChild( a );\r
237 \r
238         // Recursive function calls have lengths and array-like properties\r
239         function callme(callback){\r
240                 function fn(response){\r
241                         callback(response);\r
242                 }\r
243 \r
244                 ok( jQuery.isFunction(fn), "Recursive Function Call" );\r
245 \r
246                 fn({ some: "data" });\r
247         };\r
248 \r
249         callme(function(){\r
250                 callme(function(){});\r
251         });\r
252 });\r
253 \r
254 var foo = false;\r
255 \r
256 test("jQuery('html')", function() {\r
257         expect(6);\r
258 \r
259         reset();\r
260         foo = false;\r
261         var s = jQuery("<script>foo='test';</script>")[0];\r
262         ok( s, "Creating a script" );\r
263         ok( !foo, "Make sure the script wasn't executed prematurely" );\r
264         jQuery("body").append("<script>foo='test';</script>");\r
265         ok( foo, "Executing a scripts contents in the right context" );\r
266 \r
267         reset();\r
268         ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );\r
269 \r
270         reset();\r
271 \r
272         var j = jQuery("<span>hi</span> there <!-- mon ami -->");\r
273         ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );\r
274 \r
275         ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );\r
276 });\r
277 \r
278 test("jQuery('html', context)", function() {\r
279         expect(1);\r
280 \r
281         var $div = jQuery("<div/>");\r
282         var $span = jQuery("<span/>", $div);\r
283         equals($span.length, 1, "Verify a span created with a div context works, #1763");\r
284 });\r
285 \r
286 if ( !isLocal ) {\r
287 test("jQuery(selector, xml).text(str) - Loaded via XML document", function() {\r
288         expect(2);\r
289         stop();\r
290         jQuery.get('data/dashboard.xml', function(xml) {\r
291                 // tests for #1419 where IE was a problem\r
292                 equals( jQuery("tab:first", xml).text(), "blabla", "Verify initial text correct" );\r
293                 jQuery("tab:first", xml).text("newtext");\r
294                 equals( jQuery("tab:first", xml).text(), "newtext", "Verify new text correct" );\r
295                 start();\r
296         });\r
297 });\r
298 }\r
299 \r
300 test("length", function() {\r
301         expect(1);\r
302         equals( jQuery("p").length, 6, "Get Number of Elements Found" );\r
303 });\r
304 \r
305 test("size()", function() {\r
306         expect(1);\r
307         equals( jQuery("p").size(), 6, "Get Number of Elements Found" );\r
308 });\r
309 \r
310 test("get()", function() {\r
311         expect(1);\r
312         isSet( jQuery("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );\r
313 });\r
314 \r
315 test("get(Number)", function() {\r
316         expect(1);\r
317         equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" );\r
318 });\r
319 \r
320 test("add(String|Element|Array|undefined)", function() {\r
321         expect(12);\r
322         isSet( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );\r
323         isSet( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );\r
324         ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" );\r
325 \r
326         // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE\r
327         // use jQuery([]).add(form.elements) instead.\r
328         //equals( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );\r
329 \r
330         var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>")).add(jQuery("<p id='x2'>xxx</p>"));\r
331         equals( x[0].id, "x1", "Check on-the-fly element1" );\r
332         equals( x[1].id, "x2", "Check on-the-fly element2" );\r
333 \r
334         var x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");\r
335         equals( x[0].id, "x1", "Check on-the-fly element1" );\r
336         equals( x[1].id, "x2", "Check on-the-fly element2" );\r
337 \r
338         var notDefined;\r
339         equals( jQuery([]).add(notDefined).length, 0, "Check that undefined adds nothing" );\r
340 \r
341         // Added after #2811\r
342         equals( jQuery([]).add([window,document,document.body,document]).length, 3, "Pass an array" );\r
343         equals( jQuery(document).add(document).length, 1, "Check duplicated elements" );\r
344         equals( jQuery(window).add(window).length, 1, "Check duplicated elements using the window" );\r
345         ok( jQuery([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" );\r
346 });\r
347 \r
348 test("each(Function)", function() {\r
349         expect(1);\r
350         var div = jQuery("div");\r
351         div.each(function(){this.foo = 'zoo';});\r
352         var pass = true;\r
353         for ( var i = 0; i < div.size(); i++ ) {\r
354                 if ( div.get(i).foo != "zoo" ) pass = false;\r
355         }\r
356         ok( pass, "Execute a function, Relative" );\r
357 });\r
358 \r
359 test("index(Object)", function() {\r
360         expect(10);\r
361 \r
362         var elements = jQuery([window, document]),\r
363                 inputElements = jQuery('#radio1,#radio2,#check1,#check2');\r
364 \r
365         equals( elements.index(window), 0, "Check for index of elements" );\r
366         equals( elements.index(document), 1, "Check for index of elements" );\r
367         equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" );\r
368         equals( inputElements.index(document.getElementById('radio2')), 1, "Check for index of elements" );\r
369         equals( inputElements.index(document.getElementById('check1')), 2, "Check for index of elements" );\r
370         equals( inputElements.index(document.getElementById('check2')), 3, "Check for index of elements" );\r
371         equals( inputElements.index(window), -1, "Check for not found index" );\r
372         equals( inputElements.index(document), -1, "Check for not found index" );\r
373 \r
374         // enabled since [5500]\r
375         equals( elements.index( elements ), 0, "Pass in a jQuery object" );\r
376         equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );\r
377 });\r
378 \r
379 test("attr(String)", function() {\r
380         expect(26);\r
381         equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );\r
382         equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );\r
383         equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );\r
384         equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );\r
385         equals( jQuery('#check1').attr('type'), "checkbox", 'Check for type attribute' );\r
386         equals( jQuery('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' );\r
387         equals( jQuery('#google').attr('title'), "Google!", 'Check for title attribute' );\r
388         equals( jQuery('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' );\r
389         equals( jQuery('#en').attr('lang'), "en", 'Check for lang attribute' );\r
390         equals( jQuery('#simon').attr('class'), "blog link", 'Check for class attribute' );\r
391         equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' );\r
392         equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' );\r
393         ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );\r
394         equals( jQuery('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );\r
395         equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );\r
396         equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );\r
397         equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );\r
398         equals( jQuery('#foo').attr('nodeName'), 'DIV', 'Check for nodeName attribute' );\r
399         equals( jQuery('#foo').attr('tagName'), 'DIV', 'Check for tagName attribute' );\r
400 \r
401         jQuery('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path\r
402         equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );\r
403 \r
404 \r
405         // Related to [5574] and [5683]\r
406         var body = document.body, $body = jQuery(body);\r
407 \r
408         ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );\r
409         ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );\r
410         \r
411         body.setAttribute('foo', 'baz');\r
412         equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );\r
413         \r
414         body.foo = 'bar';\r
415         equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );\r
416         \r
417         $body.attr('foo','cool');\r
418         equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );\r
419         \r
420         body.foo = undefined;\r
421         ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );\r
422         \r
423         body.removeAttribute('foo'); // Cleanup\r
424 });\r
425 \r
426 if ( !isLocal ) {\r
427         test("attr(String) in XML Files", function() {\r
428                 expect(2);\r
429                 stop();\r
430                 jQuery.get("data/dashboard.xml", function(xml) {\r
431                         equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );\r
432                         equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );\r
433                         start();\r
434                 });\r
435         });\r
436 }\r
437 \r
438 test("attr(String, Function)", function() {\r
439         expect(2);\r
440         equals( jQuery('#text1').attr('value', function() { return this.id })[0].value, "text1", "Set value from id" );\r
441         equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");\r
442 });\r
443 \r
444 test("attr(Hash)", function() {\r
445         expect(1);\r
446         var pass = true;\r
447         jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){\r
448                 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;\r
449         });\r
450         ok( pass, "Set Multiple Attributes" );\r
451 });\r
452 \r
453 test("attr(String, Object)", function() {\r
454         expect(17);\r
455         var div = jQuery("div").attr("foo", "bar");\r
456                 fail = false;\r
457         for ( var i = 0; i < div.size(); i++ ) {\r
458                 if ( div.get(i).getAttribute('foo') != "bar" ){\r
459                         fail = i;\r
460                         break;\r
461                 }\r
462         }\r
463         equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );\r
464 \r
465         ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );\r
466 \r
467         jQuery("#name").attr('name', 'something');\r
468         equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );\r
469         jQuery("#check2").attr('checked', true);\r
470         equals( document.getElementById('check2').checked, true, 'Set checked attribute' );\r
471         jQuery("#check2").attr('checked', false);\r
472         equals( document.getElementById('check2').checked, false, 'Set checked attribute' );\r
473         jQuery("#text1").attr('readonly', true);\r
474         equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );\r
475         jQuery("#text1").attr('readonly', false);\r
476         equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );\r
477         jQuery("#name").attr('maxlength', '5');\r
478         equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );\r
479         jQuery("#name").attr('maxLength', '10');\r
480         equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );\r
481 \r
482         // for #1070\r
483         jQuery("#name").attr('someAttr', '0');\r
484         equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );\r
485         jQuery("#name").attr('someAttr', 0);\r
486         equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );\r
487         jQuery("#name").attr('someAttr', 1);\r
488         equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );\r
489 \r
490         // using contents will get comments regular, text, and comment nodes\r
491         var j = jQuery("#nonnodes").contents();\r
492 \r
493         j.attr("name", "attrvalue");\r
494         equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );\r
495         j.removeAttr("name");\r
496 \r
497         reset();\r
498 \r
499         var type = jQuery("#check2").attr('type');\r
500         var thrown = false;\r
501         try {\r
502                 jQuery("#check2").attr('type','hidden');\r
503         } catch(e) {\r
504                 thrown = true;\r
505         }\r
506         ok( thrown, "Exception thrown when trying to change type property" );\r
507         equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );\r
508 \r
509         var check = document.createElement("input");\r
510         var thrown = true;\r
511         try {\r
512                 jQuery(check).attr('type','checkbox');\r
513         } catch(e) {\r
514                 thrown = false;\r
515         }\r
516         ok( thrown, "Exception thrown when trying to change type property" );\r
517         equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );\r
518 });\r
519 \r
520 if ( !isLocal ) {\r
521         test("attr(String, Object) - Loaded via XML document", function() {\r
522                 expect(2);\r
523                 stop();\r
524                 jQuery.get('data/dashboard.xml', function(xml) {\r
525                         var titles = [];\r
526                         jQuery('tab', xml).each(function() {\r
527                                 titles.push(jQuery(this).attr('title'));\r
528                         });\r
529                         equals( titles[0], 'Location', 'attr() in XML context: Check first title' );\r
530                         equals( titles[1], 'Users', 'attr() in XML context: Check second title' );\r
531                         start();\r
532                 });\r
533         });\r
534 }\r
535 \r
536 test("css(String|Hash)", function() {\r
537         expect(19);\r
538 \r
539         equals( jQuery('#main').css("display"), 'none', 'Check for css property "display"');\r
540 \r
541         ok( jQuery('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');\r
542         jQuery('#foo').css({display: 'none'});\r
543         ok( !jQuery('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');\r
544         jQuery('#foo').css({display: 'block'});\r
545         ok( jQuery('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');\r
546 \r
547         jQuery('#floatTest').css({styleFloat: 'right'});\r
548         equals( jQuery('#floatTest').css('styleFloat'), 'right', 'Modified CSS float using "styleFloat": Assert float is right');\r
549         jQuery('#floatTest').css({cssFloat: 'left'});\r
550         equals( jQuery('#floatTest').css('cssFloat'), 'left', 'Modified CSS float using "cssFloat": Assert float is left');\r
551         jQuery('#floatTest').css({'float': 'right'});\r
552         equals( jQuery('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right');\r
553         jQuery('#floatTest').css({'font-size': '30px'});\r
554         equals( jQuery('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px');\r
555 \r
556         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {\r
557                 jQuery('#foo').css({opacity: n});\r
558                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );\r
559                 jQuery('#foo').css({opacity: parseFloat(n)});\r
560                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );\r
561         });\r
562         jQuery('#foo').css({opacity: ''});\r
563         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );\r
564 });\r
565 \r
566 test("css(String, Object)", function() {\r
567         expect(21);\r
568         ok( jQuery('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');\r
569         jQuery('#foo').css('display', 'none');\r
570         ok( !jQuery('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');\r
571         jQuery('#foo').css('display', 'block');\r
572         ok( jQuery('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');\r
573 \r
574         jQuery('#floatTest').css('styleFloat', 'left');\r
575         equals( jQuery('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left');\r
576         jQuery('#floatTest').css('cssFloat', 'right');\r
577         equals( jQuery('#floatTest').css('cssFloat'), 'right', 'Modified CSS float using "cssFloat": Assert float is right');\r
578         jQuery('#floatTest').css('float', 'left');\r
579         equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');\r
580         jQuery('#floatTest').css('font-size', '20px');\r
581         equals( jQuery('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');\r
582 \r
583         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {\r
584                 jQuery('#foo').css('opacity', n);\r
585                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );\r
586                 jQuery('#foo').css('opacity', parseFloat(n));\r
587                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );\r
588         });\r
589         jQuery('#foo').css('opacity', '');\r
590         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );\r
591         // for #1438, IE throws JS error when filter exists but doesn't have opacity in it\r
592         if (jQuery.browser.msie) {\r
593                 jQuery('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");\r
594         }\r
595         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );\r
596 \r
597         // using contents will get comments regular, text, and comment nodes\r
598         var j = jQuery("#nonnodes").contents();\r
599         j.css("padding-left", "1px");\r
600         equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );\r
601 \r
602         // opera sometimes doesn't update 'display' correctly, see #2037\r
603         jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML\r
604         equals( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );\r
605 });\r
606 \r
607 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {\r
608         expect(4);\r
609 \r
610         var $checkedtest = jQuery("#checkedtest");\r
611         // IE6 was clearing "checked" in jQuery.css(elem, "height");\r
612         jQuery.css($checkedtest[0], "height");\r
613         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );\r
614         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );\r
615         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );\r
616         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );\r
617 });\r
618 \r
619 test("width()", function() {\r
620         expect(9);\r
621 \r
622         var $div = jQuery("#nothiddendiv");\r
623         $div.width(30);\r
624         equals($div.width(), 30, "Test set to 30 correctly");\r
625         $div.width(-1); // handle negative numbers by ignoring #1599\r
626         equals($div.width(), 30, "Test negative width ignored");\r
627         $div.css("padding", "20px");\r
628         equals($div.width(), 30, "Test padding specified with pixels");\r
629         $div.css("border", "2px solid #fff");\r
630         equals($div.width(), 30, "Test border specified with pixels");\r
631         $div.css("padding", "2em");\r
632         equals($div.width(), 30, "Test padding specified with ems");\r
633         $div.css("border", "1em solid #fff");\r
634         equals($div.width(), 30, "Test border specified with ems");\r
635         $div.css("padding", "2%");\r
636         equals($div.width(), 30, "Test padding specified with percent");\r
637         $div.hide();\r
638         equals($div.width(), 30, "Test hidden div");\r
639 \r
640         $div.css({ display: "", border: "", padding: "" });\r
641 \r
642         jQuery("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" });\r
643         equals(jQuery("#nothiddendivchild").width(), 20, "Test child width with border and padding");\r
644         jQuery("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" });\r
645 });\r
646 \r
647 test("height()", function() {\r
648         expect(8);\r
649 \r
650         var $div = jQuery("#nothiddendiv");\r
651         $div.height(30);\r
652         equals($div.height(), 30, "Test set to 30 correctly");\r
653         $div.height(-1); // handle negative numbers by ignoring #1599\r
654         equals($div.height(), 30, "Test negative height ignored");\r
655         $div.css("padding", "20px");\r
656         equals($div.height(), 30, "Test padding specified with pixels");\r
657         $div.css("border", "2px solid #fff");\r
658         equals($div.height(), 30, "Test border specified with pixels");\r
659         $div.css("padding", "2em");\r
660         equals($div.height(), 30, "Test padding specified with ems");\r
661         $div.css("border", "1em solid #fff");\r
662         equals($div.height(), 30, "Test border specified with ems");\r
663         $div.css("padding", "2%");\r
664         equals($div.height(), 30, "Test padding specified with percent");\r
665         $div.hide();\r
666         equals($div.height(), 30, "Test hidden div");\r
667 \r
668         $div.css({ display: "", border: "", padding: "", height: "1px" });\r
669 });\r
670 \r
671 test("text()", function() {\r
672         expect(1);\r
673         var expected = "This link has class=\"blog\": Simon Willison's Weblog";\r
674         equals( jQuery('#sap').text(), expected, 'Check for merged text of more then one element.' );\r
675 });\r
676 \r
677 test("wrap(String|Element)", function() {\r
678         expect(8);\r
679         var defaultText = 'Try them out:'\r
680         var result = jQuery('#first').wrap('<div class="red"><span></span></div>').text();\r
681         equals( defaultText, result, 'Check for wrapping of on-the-fly html' );\r
682         ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );\r
683 \r
684         reset();\r
685         var defaultText = 'Try them out:'\r
686         var result = jQuery('#first').wrap(document.getElementById('empty')).parent();\r
687         ok( result.is('ol'), 'Check for element wrapping' );\r
688         equals( result.text(), defaultText, 'Check for element wrapping' );\r
689 \r
690         reset();\r
691         jQuery('#check1').click(function() {\r
692                 var checkbox = this;\r
693                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );\r
694                 jQuery(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );\r
695                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );\r
696         }).click();\r
697 \r
698         // using contents will get comments regular, text, and comment nodes\r
699         var j = jQuery("#nonnodes").contents();\r
700         j.wrap("<i></i>");\r
701         equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );\r
702         equals( jQuery("#nonnodes > i").text(), j.text() + j[1].nodeValue, "Check node,textnode,comment wraps doesn't hurt text" );\r
703 });\r
704 \r
705 test("wrapAll(String|Element)", function() {\r
706         expect(8);\r
707         var prev = jQuery("#first")[0].previousSibling;\r
708         var p = jQuery("#first")[0].parentNode;\r
709         var result = jQuery('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>');\r
710         equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );\r
711         ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );\r
712         ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );\r
713         equals( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );\r
714         equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );\r
715 \r
716         reset();\r
717         var prev = jQuery("#first")[0].previousSibling;\r
718         var p = jQuery("#first")[0].parentNode;\r
719         var result = jQuery('#first,#firstp').wrapAll(document.getElementById('empty'));\r
720         equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );\r
721         equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );\r
722         equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );\r
723 });\r
724 \r
725 test("wrapInner(String|Element)", function() {\r
726         expect(6);\r
727         var num = jQuery("#first").children().length;\r
728         var result = jQuery('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');\r
729         equals( jQuery("#first").children().length, 1, "Only one child" );\r
730         ok( jQuery("#first").children().is(".red"), "Verify Right Element" );\r
731         equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );\r
732 \r
733         reset();\r
734         var num = jQuery("#first").children().length;\r
735         var result = jQuery('#first').wrapInner(document.getElementById('empty'));\r
736         equals( jQuery("#first").children().length, 1, "Only one child" );\r
737         ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );\r
738         equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );\r
739 });\r
740 \r
741 test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
742         expect(21);\r
743         var defaultText = 'Try them out:'\r
744         var result = jQuery('#first').append('<b>buga</b>');\r
745         equals( result.text(), defaultText + 'buga', 'Check if text appending works' );\r
746         equals( jQuery('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');\r
747 \r
748         reset();\r
749         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";\r
750         jQuery('#sap').append(document.getElementById('first'));\r
751         equals( expected, jQuery('#sap').text(), "Check for appending of element" );\r
752 \r
753         reset();\r
754         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";\r
755         jQuery('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);\r
756         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );\r
757 \r
758         reset();\r
759         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";\r
760         jQuery('#sap').append(jQuery("#first, #yahoo"));\r
761         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );\r
762 \r
763         reset();\r
764         jQuery("#sap").append( 5 );\r
765         ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );\r
766 \r
767         reset();\r
768         jQuery("#sap").append( " text with spaces " );\r
769         ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );\r
770 \r
771         reset();\r
772         ok( jQuery("#sap").append([]), "Check for appending an empty array." );\r
773         ok( jQuery("#sap").append(""), "Check for appending an empty string." );\r
774         ok( jQuery("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );\r
775 \r
776         reset();\r
777         jQuery("#sap").append(document.getElementById('form'));\r
778         equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910\r
779 \r
780         reset();\r
781         var pass = true;\r
782         try {\r
783                 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append("<div>test</div>");\r
784         } catch(e) {\r
785                 pass = false;\r
786         }\r
787 \r
788         ok( pass, "Test for appending a DOM node to the contents of an IFrame" );\r
789 \r
790         reset();\r
791         jQuery('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');\r
792         t( 'Append legend', '#legend', ['legend'] );\r
793 \r
794         reset();\r
795         jQuery('#select1').append('<OPTION>Test</OPTION>');\r
796         equals( jQuery('#select1 option:last').text(), "Test", "Appending &lt;OPTION&gt; (all caps)" );\r
797 \r
798         jQuery('#table').append('<colgroup></colgroup>');\r
799         ok( jQuery('#table colgroup').length, "Append colgroup" );\r
800 \r
801         jQuery('#table colgroup').append('<col/>');\r
802         ok( jQuery('#table colgroup col').length, "Append col" );\r
803 \r
804         reset();\r
805         jQuery('#table').append('<caption></caption>');\r
806         ok( jQuery('#table caption').length, "Append caption" );\r
807 \r
808         reset();\r
809         jQuery('form:last')\r
810                 .append('<select id="appendSelect1"></select>')\r
811                 .append('<select id="appendSelect2"><option>Test</option></select>');\r
812 \r
813         t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );\r
814 \r
815         // using contents will get comments regular, text, and comment nodes\r
816         var j = jQuery("#nonnodes").contents();\r
817         var d = jQuery("<div/>").appendTo("#nonnodes").append(j);\r
818         equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );\r
819         ok( d.contents().length >= 2, "Check node,textnode,comment append works" );\r
820         d.contents().appendTo("#nonnodes");\r
821         d.remove();\r
822         ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );\r
823 });\r
824 \r
825 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
826         expect(6);\r
827         var defaultText = 'Try them out:'\r
828         jQuery('<b>buga</b>').appendTo('#first');\r
829         equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );\r
830         equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');\r
831 \r
832         reset();\r
833         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";\r
834         jQuery(document.getElementById('first')).appendTo('#sap');\r
835         equals( expected, jQuery('#sap').text(), "Check for appending of element" );\r
836 \r
837         reset();\r
838         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";\r
839         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');\r
840         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );\r
841 \r
842         reset();\r
843         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";\r
844         jQuery("#first, #yahoo").appendTo('#sap');\r
845         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );\r
846 \r
847         reset();\r
848         jQuery('#select1').appendTo('#foo');\r
849         t( 'Append select', '#foo select', ['select1'] );\r
850 });\r
851 \r
852 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
853         expect(5);\r
854         var defaultText = 'Try them out:'\r
855         var result = jQuery('#first').prepend('<b>buga</b>');\r
856         equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );\r
857         equals( jQuery('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');\r
858 \r
859         reset();\r
860         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";\r
861         jQuery('#sap').prepend(document.getElementById('first'));\r
862         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );\r
863 \r
864         reset();\r
865         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";\r
866         jQuery('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);\r
867         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );\r
868 \r
869         reset();\r
870         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";\r
871         jQuery('#sap').prepend(jQuery("#first, #yahoo"));\r
872         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );\r
873 });\r
874 \r
875 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
876         expect(6);\r
877         var defaultText = 'Try them out:'\r
878         jQuery('<b>buga</b>').prependTo('#first');\r
879         equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );\r
880         equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');\r
881 \r
882         reset();\r
883         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";\r
884         jQuery(document.getElementById('first')).prependTo('#sap');\r
885         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );\r
886 \r
887         reset();\r
888         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";\r
889         jQuery([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');\r
890         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );\r
891 \r
892         reset();\r
893         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";\r
894         jQuery("#yahoo, #first").prependTo('#sap');\r
895         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );\r
896 \r
897         reset();\r
898         jQuery('<select id="prependSelect1"></select>').prependTo('form:last');\r
899         jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');\r
900 \r
901         t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );\r
902 });\r
903 \r
904 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
905         expect(4);\r
906         var expected = 'This is a normal link: bugaYahoo';\r
907         jQuery('#yahoo').before('<b>buga</b>');\r
908         equals( expected, jQuery('#en').text(), 'Insert String before' );\r
909 \r
910         reset();\r
911         expected = "This is a normal link: Try them out:Yahoo";\r
912         jQuery('#yahoo').before(document.getElementById('first'));\r
913         equals( expected, jQuery('#en').text(), "Insert element before" );\r
914 \r
915         reset();\r
916         expected = "This is a normal link: Try them out:diveintomarkYahoo";\r
917         jQuery('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);\r
918         equals( expected, jQuery('#en').text(), "Insert array of elements before" );\r
919 \r
920         reset();\r
921         expected = "This is a normal link: Try them out:diveintomarkYahoo";\r
922         jQuery('#yahoo').before(jQuery("#first, #mark"));\r
923         equals( expected, jQuery('#en').text(), "Insert jQuery before" );\r
924 });\r
925 \r
926 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
927         expect(4);\r
928         var expected = 'This is a normal link: bugaYahoo';\r
929         jQuery('<b>buga</b>').insertBefore('#yahoo');\r
930         equals( expected, jQuery('#en').text(), 'Insert String before' );\r
931 \r
932         reset();\r
933         expected = "This is a normal link: Try them out:Yahoo";\r
934         jQuery(document.getElementById('first')).insertBefore('#yahoo');\r
935         equals( expected, jQuery('#en').text(), "Insert element before" );\r
936 \r
937         reset();\r
938         expected = "This is a normal link: Try them out:diveintomarkYahoo";\r
939         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');\r
940         equals( expected, jQuery('#en').text(), "Insert array of elements before" );\r
941 \r
942         reset();\r
943         expected = "This is a normal link: Try them out:diveintomarkYahoo";\r
944         jQuery("#first, #mark").insertBefore('#yahoo');\r
945         equals( expected, jQuery('#en').text(), "Insert jQuery before" );\r
946 });\r
947 \r
948 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
949         expect(4);\r
950         var expected = 'This is a normal link: Yahoobuga';\r
951         jQuery('#yahoo').after('<b>buga</b>');\r
952         equals( expected, jQuery('#en').text(), 'Insert String after' );\r
953 \r
954         reset();\r
955         expected = "This is a normal link: YahooTry them out:";\r
956         jQuery('#yahoo').after(document.getElementById('first'));\r
957         equals( expected, jQuery('#en').text(), "Insert element after" );\r
958 \r
959         reset();\r
960         expected = "This is a normal link: YahooTry them out:diveintomark";\r
961         jQuery('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);\r
962         equals( expected, jQuery('#en').text(), "Insert array of elements after" );\r
963 \r
964         reset();\r
965         expected = "This is a normal link: YahooTry them out:diveintomark";\r
966         jQuery('#yahoo').after(jQuery("#first, #mark"));\r
967         equals( expected, jQuery('#en').text(), "Insert jQuery after" );\r
968 });\r
969 \r
970 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
971         expect(4);\r
972         var expected = 'This is a normal link: Yahoobuga';\r
973         jQuery('<b>buga</b>').insertAfter('#yahoo');\r
974         equals( expected, jQuery('#en').text(), 'Insert String after' );\r
975 \r
976         reset();\r
977         expected = "This is a normal link: YahooTry them out:";\r
978         jQuery(document.getElementById('first')).insertAfter('#yahoo');\r
979         equals( expected, jQuery('#en').text(), "Insert element after" );\r
980 \r
981         reset();\r
982         expected = "This is a normal link: YahooTry them out:diveintomark";\r
983         jQuery([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');\r
984         equals( expected, jQuery('#en').text(), "Insert array of elements after" );\r
985 \r
986         reset();\r
987         expected = "This is a normal link: YahooTry them out:diveintomark";\r
988         jQuery("#mark, #first").insertAfter('#yahoo');\r
989         equals( expected, jQuery('#en').text(), "Insert jQuery after" );\r
990 });\r
991 \r
992 test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
993         expect(10);\r
994         jQuery('#yahoo').replaceWith('<b id="replace">buga</b>');\r
995         ok( jQuery("#replace")[0], 'Replace element with string' );\r
996         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );\r
997 \r
998         reset();\r
999         jQuery('#yahoo').replaceWith(document.getElementById('first'));\r
1000         ok( jQuery("#first")[0], 'Replace element with element' );\r
1001         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );\r
1002 \r
1003         reset();\r
1004         jQuery('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);\r
1005         ok( jQuery("#first")[0], 'Replace element with array of elements' );\r
1006         ok( jQuery("#mark")[0], 'Replace element with array of elements' );\r
1007         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );\r
1008 \r
1009         reset();\r
1010         jQuery('#yahoo').replaceWith(jQuery("#first, #mark"));\r
1011         ok( jQuery("#first")[0], 'Replace element with set of elements' );\r
1012         ok( jQuery("#mark")[0], 'Replace element with set of elements' );\r
1013         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );\r
1014 });\r
1015 \r
1016 test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
1017         expect(10);\r
1018         jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");\r
1019         ok( jQuery("#replace")[0], 'Replace element with string' );\r
1020         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );\r
1021 \r
1022         reset();\r
1023         jQuery(document.getElementById('first')).replaceAll("#yahoo");\r
1024         ok( jQuery("#first")[0], 'Replace element with element' );\r
1025         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );\r
1026 \r
1027         reset();\r
1028         jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");\r
1029         ok( jQuery("#first")[0], 'Replace element with array of elements' );\r
1030         ok( jQuery("#mark")[0], 'Replace element with array of elements' );\r
1031         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );\r
1032 \r
1033         reset();\r
1034         jQuery("#first, #mark").replaceAll("#yahoo");\r
1035         ok( jQuery("#first")[0], 'Replace element with set of elements' );\r
1036         ok( jQuery("#mark")[0], 'Replace element with set of elements' );\r
1037         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );\r
1038 });\r
1039 \r
1040 test("end()", function() {\r
1041         expect(3);\r
1042         equals( 'Yahoo', jQuery('#yahoo').parent().end().text(), 'Check for end' );\r
1043         ok( jQuery('#yahoo').end(), 'Check for end with nothing to end' );\r
1044 \r
1045         var x = jQuery('#yahoo');\r
1046         x.parent();\r
1047         equals( 'Yahoo', jQuery('#yahoo').text(), 'Check for non-destructive behaviour' );\r
1048 });\r
1049 \r
1050 test("find(String)", function() {\r
1051         expect(2);\r
1052         equals( 'Yahoo', jQuery('#foo').find('.blogTest').text(), 'Check for find' );\r
1053 \r
1054         // using contents will get comments regular, text, and comment nodes\r
1055         var j = jQuery("#nonnodes").contents();\r
1056         equals( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" );\r
1057 });\r
1058 \r
1059 test("clone()", function() {\r
1060         expect(20);\r
1061         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );\r
1062         var clone = jQuery('#yahoo').clone();\r
1063         equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );\r
1064         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );\r
1065 \r
1066         var cloneTags = [\r
1067                 "<table/>", "<tr/>", "<td/>", "<div/>",\r
1068                 "<button/>", "<ul/>", "<ol/>", "<li/>",\r
1069                 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",\r
1070                 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"\r
1071         ];\r
1072         for (var i = 0; i < cloneTags.length; i++) {\r
1073                 var j = jQuery(cloneTags[i]);\r
1074                 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a &lt;' + cloneTags[i].substring(1));\r
1075         }\r
1076 \r
1077         // using contents will get comments regular, text, and comment nodes\r
1078         var cl = jQuery("#nonnodes").contents().clone();\r
1079         ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );\r
1080 });\r
1081 \r
1082 if (!isLocal) {\r
1083 test("clone() on XML nodes", function() {\r
1084         expect(2);\r
1085         stop();\r
1086         jQuery.get("data/dashboard.xml", function (xml) {\r
1087                 var root = jQuery(xml.documentElement).clone();\r
1088                 jQuery("tab:first", xml).text("origval");\r
1089                 jQuery("tab:first", root).text("cloneval");\r
1090                 equals(jQuery("tab:first", xml).text(), "origval", "Check original XML node was correctly set");\r
1091                 equals(jQuery("tab:first", root).text(), "cloneval", "Check cloned XML node was correctly set");\r
1092                 start();\r
1093         });\r
1094 });\r
1095 }\r
1096 \r
1097 test("is(String)", function() {\r
1098         expect(26);\r
1099         ok( jQuery('#form').is('form'), 'Check for element: A form must be a form' );\r
1100         ok( !jQuery('#form').is('div'), 'Check for element: A form is not a div' );\r
1101         ok( jQuery('#mark').is('.blog'), 'Check for class: Expected class "blog"' );\r
1102         ok( !jQuery('#mark').is('.link'), 'Check for class: Did not expect class "link"' );\r
1103         ok( jQuery('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );\r
1104         ok( !jQuery('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );\r
1105         ok( jQuery('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );\r
1106         ok( !jQuery('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );\r
1107         ok( jQuery('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );\r
1108         ok( !jQuery('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );\r
1109         ok( jQuery('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );\r
1110         ok( !jQuery('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );\r
1111         ok( jQuery('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );\r
1112         ok( !jQuery('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );\r
1113         ok( jQuery('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );\r
1114         ok( !jQuery('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );\r
1115         ok( jQuery('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );\r
1116         ok( !jQuery('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );\r
1117         ok( !jQuery('#foo').is(0), 'Expected false for an invalid expression - 0' );\r
1118         ok( !jQuery('#foo').is(null), 'Expected false for an invalid expression - null' );\r
1119         ok( !jQuery('#foo').is(''), 'Expected false for an invalid expression - ""' );\r
1120         ok( !jQuery('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );\r
1121 \r
1122         // test is() with comma-seperated expressions\r
1123         ok( jQuery('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );\r
1124         ok( jQuery('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );\r
1125         ok( jQuery('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );\r
1126         ok( jQuery('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );\r
1127 });\r
1128 \r
1129 test("jQuery.extend(Object, Object)", function() {\r
1130         expect(20);\r
1131 \r
1132         var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },\r
1133                 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },\r
1134                 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },\r
1135                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },\r
1136                 deep1 = { foo: { bar: true } },\r
1137                 deep1copy = { foo: { bar: true } },\r
1138                 deep2 = { foo: { baz: true }, foo2: document },\r
1139                 deep2copy = { foo: { baz: true }, foo2: document },\r
1140                 deepmerged = { foo: { bar: true, baz: true }, foo2: document };\r
1141 \r
1142         jQuery.extend(settings, options);\r
1143         isObj( settings, merged, "Check if extended: settings must be extended" );\r
1144         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );\r
1145 \r
1146         jQuery.extend(settings, null, options);\r
1147         isObj( settings, merged, "Check if extended: settings must be extended" );\r
1148         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );\r
1149 \r
1150         jQuery.extend(true, deep1, deep2);\r
1151         isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );\r
1152         isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );\r
1153         equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );\r
1154 \r
1155         var nullUndef;\r
1156         nullUndef = jQuery.extend({}, options, { xnumber2: null });\r
1157         ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");\r
1158 \r
1159         nullUndef = jQuery.extend({}, options, { xnumber2: undefined });\r
1160         ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");\r
1161 \r
1162         nullUndef = jQuery.extend({}, options, { xnumber0: null });\r
1163         ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");\r
1164 \r
1165         var target = {};\r
1166         var recursive = { foo:target, bar:5 };\r
1167         jQuery.extend(true, target, recursive);\r
1168         isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );\r
1169 \r
1170         var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907\r
1171         equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );\r
1172 \r
1173         var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );\r
1174         ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );\r
1175 \r
1176         var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );\r
1177         ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );\r
1178 \r
1179         var obj = { foo:null };\r
1180         jQuery.extend(true, obj, { foo:"notnull" } );\r
1181         equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );\r
1182 \r
1183         function func() {}\r
1184         jQuery.extend(func, { key: "value" } );\r
1185         equals( func.key, "value", "Verify a function can be extended" );\r
1186 \r
1187         var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },\r
1188                 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },\r
1189                 options1 = { xnumber2: 1, xstring2: "x" },\r
1190                 options1Copy = { xnumber2: 1, xstring2: "x" },\r
1191                 options2 = { xstring2: "xx", xxx: "newstringx" },\r
1192                 options2Copy = { xstring2: "xx", xxx: "newstringx" },\r
1193                 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };\r
1194 \r
1195         var settings = jQuery.extend({}, defaults, options1, options2);\r
1196         isObj( settings, merged2, "Check if extended: settings must be extended" );\r
1197         isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );\r
1198         isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );\r
1199         isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );\r
1200 });\r
1201 \r
1202 test("val()", function() {\r
1203         expect(8);\r
1204 \r
1205         equals( jQuery("#text1").val(), "Test", "Check for value of input element" );\r
1206         // ticket #1714 this caused a JS error in IE\r
1207         equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );\r
1208         ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );\r
1209         \r
1210         equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );\r
1211 \r
1212         isSet( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );\r
1213 \r
1214         equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );\r
1215         \r
1216         equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );\r
1217         \r
1218         equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );\r
1219         \r
1220 });\r
1221 \r
1222 test("val(String/Number)", function() {\r
1223         expect(6);\r
1224         document.getElementById('text1').value = "bla";\r
1225         equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );\r
1226         \r
1227         jQuery("#text1").val('test');\r
1228         equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );\r
1229         \r
1230         jQuery("#text1").val(67);\r
1231         equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );\r
1232 \r
1233         jQuery("#select1").val("3");\r
1234         equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );\r
1235 \r
1236         jQuery("#select1").val(2);\r
1237         equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );\r
1238 \r
1239         // using contents will get comments regular, text, and comment nodes\r
1240         var j = jQuery("#nonnodes").contents();\r
1241         j.val("asdf");\r
1242         equals( j.val(), "asdf", "Check node,textnode,comment with val()" );\r
1243         j.removeAttr("value");\r
1244 });\r
1245 \r
1246 var scriptorder = 0;\r
1247 \r
1248 test("html(String)", function() {\r
1249         expect(13);\r
1250         var div = jQuery("#main > div");\r
1251         div.html("<b>test</b>");\r
1252         var pass = true;\r
1253         for ( var i = 0; i < div.size(); i++ ) {\r
1254                 if ( div.get(i).childNodes.length != 1 ) pass = false;\r
1255         }\r
1256         ok( pass, "Set HTML" );\r
1257 \r
1258         reset();\r
1259         // using contents will get comments regular, text, and comment nodes\r
1260         var j = jQuery("#nonnodes").contents();\r
1261         j.html("<b>bold</b>");\r
1262 \r
1263         // this is needed, or the expando added by jQuery unique will yield a different html\r
1264         j.find('b').removeData();\r
1265         equals( j.html().toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );\r
1266 \r
1267         jQuery("#main").html("<select/>");\r
1268         jQuery("#main select").html("<option>O1</option><option selected='selected'>O2</option><option>O3</option>");\r
1269         equals( jQuery("#main select").val(), "O2", "Selected option correct" );\r
1270 \r
1271         var $div = jQuery('<div />');\r
1272         equals( $div.html( 5 ).html(), '5', 'Setting a number as html' );\r
1273         equals( $div.html( 0 ).html(), '0', 'Setting a zero as html' );\r
1274 \r
1275         stop();\r
1276 \r
1277         jQuery("#main").html('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>');\r
1278 \r
1279         jQuery("#main").html('foo <form><script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');\r
1280 \r
1281         // 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\r
1282         jQuery("#main").html("<script>equals(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(scriptorder++, 1, 'Script (nested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equals(scriptorder++, 2, 'Script (unnested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script>");\r
1283 \r
1284         setTimeout( start, 100 );\r
1285 });\r
1286 \r
1287 test("filter()", function() {\r
1288         expect(6);\r
1289         isSet( jQuery("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );\r
1290         isSet( jQuery("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );\r
1291         isSet( jQuery("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );\r
1292         isSet( jQuery("p").filter(function() { return !jQuery("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );\r
1293 \r
1294         // using contents will get comments regular, text, and comment nodes\r
1295         var j = jQuery("#nonnodes").contents();\r
1296         equals( j.filter("span").length, 1, "Check node,textnode,comment to filter the one span" );\r
1297         equals( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" );\r
1298 });\r
1299 \r
1300 test("not()", function() {\r
1301         expect(8);\r
1302         equals( jQuery("#main > p#ap > a").not("#google").length, 2, "not('selector')" );\r
1303         equals( jQuery("#main > p#ap > a").not(document.getElementById("google")).length, 2, "not(DOMElement)" );\r
1304         isSet( jQuery("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );\r
1305         isSet( jQuery("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );\r
1306         isSet( jQuery("p").not(jQuery("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );\r
1307         equals( jQuery("p").not(document.getElementsByTagName("p")).length, 0, "not(Array-like DOM collection)" );\r
1308         isSet( jQuery("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d", "option3e" ), "not('complex selector')");\r
1309 \r
1310         var selects = jQuery("#form select");\r
1311         isSet( selects.not( selects[1] ), q("select1", "select3"), "filter out DOM element");\r
1312 });\r
1313 \r
1314 test("andSelf()", function() {\r
1315         expect(4);\r
1316         isSet( jQuery("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );\r
1317         isSet( jQuery("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );\r
1318         isSet( jQuery("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" );\r
1319         isSet( jQuery("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" );\r
1320 });\r
1321 \r
1322 test("siblings([String])", function() {\r
1323         expect(5);\r
1324         isSet( jQuery("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );\r
1325         isSet( jQuery("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );\r
1326         isSet( jQuery("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );\r
1327         isSet( jQuery("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" );\r
1328         isSet( jQuery("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );\r
1329 });\r
1330 \r
1331 test("children([String])", function() {\r
1332         expect(3);\r
1333         isSet( jQuery("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );\r
1334         isSet( jQuery("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );\r
1335         isSet( jQuery("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );\r
1336 });\r
1337 \r
1338 test("parent([String])", function() {\r
1339         expect(5);\r
1340         equals( jQuery("#groups").parent()[0].id, "ap", "Simple parent check" );\r
1341         equals( jQuery("#groups").parent("p")[0].id, "ap", "Filtered parent check" );\r
1342         equals( jQuery("#groups").parent("div").length, 0, "Filtered parent check, no match" );\r
1343         equals( jQuery("#groups").parent("div, p")[0].id, "ap", "Check for multiple filters" );\r
1344         isSet( jQuery("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );\r
1345 });\r
1346 \r
1347 test("parents([String])", function() {\r
1348         expect(5);\r
1349         equals( jQuery("#groups").parents()[0].id, "ap", "Simple parents check" );\r
1350         equals( jQuery("#groups").parents("p")[0].id, "ap", "Filtered parents check" );\r
1351         equals( jQuery("#groups").parents("div")[0].id, "main", "Filtered parents check2" );\r
1352         isSet( jQuery("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );\r
1353         isSet( jQuery("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );\r
1354 });\r
1355 \r
1356 test("next([String])", function() {\r
1357         expect(4);\r
1358         equals( jQuery("#ap").next()[0].id, "foo", "Simple next check" );\r
1359         equals( jQuery("#ap").next("div")[0].id, "foo", "Filtered next check" );\r
1360         equals( jQuery("#ap").next("p").length, 0, "Filtered next check, no match" );\r
1361         equals( jQuery("#ap").next("div, p")[0].id, "foo", "Multiple filters" );\r
1362 });\r
1363 \r
1364 test("prev([String])", function() {\r
1365         expect(4);\r
1366         equals( jQuery("#foo").prev()[0].id, "ap", "Simple prev check" );\r
1367         equals( jQuery("#foo").prev("p")[0].id, "ap", "Filtered prev check" );\r
1368         equals( jQuery("#foo").prev("div").length, 0, "Filtered prev check, no match" );\r
1369         equals( jQuery("#foo").prev("p, div")[0].id, "ap", "Multiple filters" );\r
1370 });\r
1371 \r
1372 test("show()", function() {\r
1373         expect(15);\r
1374         var pass = true, div = jQuery("div");\r
1375         div.show().each(function(){\r
1376                 if ( this.style.display == "none" ) pass = false;\r
1377         });\r
1378         ok( pass, "Show" );\r
1379 \r
1380         jQuery("#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>');\r
1381         var test = {\r
1382                 "div"      : "block",\r
1383                 "p"        : "block",\r
1384                 "a"        : "inline",\r
1385                 "code"     : "inline",\r
1386                 "pre"      : "block",\r
1387                 "span"     : "inline",\r
1388                 "table"    : jQuery.browser.msie ? "block" : "table",\r
1389                 "thead"    : jQuery.browser.msie ? "block" : "table-header-group",\r
1390                 "tbody"    : jQuery.browser.msie ? "block" : "table-row-group",\r
1391                 "tr"       : jQuery.browser.msie ? "block" : "table-row",\r
1392                 "th"       : jQuery.browser.msie ? "block" : "table-cell",\r
1393                 "td"       : jQuery.browser.msie ? "block" : "table-cell",\r
1394                 "ul"       : "block",\r
1395                 "li"       : jQuery.browser.msie ? "block" : "list-item"\r
1396         };\r
1397 \r
1398         jQuery.each(test, function(selector, expected) {\r
1399                 var elem = jQuery(selector, "#show-tests").show();\r
1400                 equals( elem.css("display"), expected, "Show using correct display type for " + selector );\r
1401         });\r
1402 });\r
1403 \r
1404 test("addClass(String)", function() {\r
1405         expect(2);\r
1406         var div = jQuery("div");\r
1407         div.addClass("test");\r
1408         var pass = true;\r
1409         for ( var i = 0; i < div.size(); i++ ) {\r
1410          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;\r
1411         }\r
1412         ok( pass, "Add Class" );\r
1413 \r
1414         // using contents will get regular, text, and comment nodes\r
1415         var j = jQuery("#nonnodes").contents();\r
1416         j.addClass("asdf");\r
1417         ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );\r
1418 });\r
1419 \r
1420 test("removeClass(String) - simple", function() {\r
1421         expect(4);\r
1422         \r
1423         var $divs = jQuery('div');\r
1424         \r
1425         $divs.addClass("test").removeClass("test");\r
1426                 \r
1427         ok( !$divs.is('.test'), "Remove Class" );\r
1428 \r
1429         reset();\r
1430         \r
1431         $divs.addClass("test").addClass("foo").addClass("bar");\r
1432         $divs.removeClass("test").removeClass("bar").removeClass("foo");\r
1433         \r
1434         ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );\r
1435 \r
1436         reset();\r
1437         \r
1438         $divs.eq(0).addClass("test").removeClass("");\r
1439         ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );\r
1440 \r
1441         // using contents will get regular, text, and comment nodes\r
1442         var j = jQuery("#nonnodes").contents();\r
1443         j.removeClass("asdf");\r
1444         ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );\r
1445 });\r
1446 \r
1447 test("toggleClass(String)", function() {\r
1448         expect(3);\r
1449         var e = jQuery("#firstp");\r
1450         ok( !e.is(".test"), "Assert class not present" );\r
1451         e.toggleClass("test");\r
1452         ok( e.is(".test"), "Assert class present" );\r
1453         e.toggleClass("test");\r
1454         ok( !e.is(".test"), "Assert class not present" );\r
1455 });\r
1456 \r
1457 test("removeAttr(String", function() {\r
1458         expect(1);\r
1459         equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );\r
1460 });\r
1461 \r
1462 test("text(String)", function() {\r
1463         expect(4);\r
1464         equals( jQuery("#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" );\r
1465 \r
1466         // using contents will get comments regular, text, and comment nodes\r
1467         var j = jQuery("#nonnodes").contents();\r
1468         j.text("hi!");\r
1469         equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );\r
1470         equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );\r
1471         equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );\r
1472 });\r
1473 \r
1474 test("jQuery.each(Object,Function)", function() {\r
1475         expect(12);\r
1476         jQuery.each( [0,1,2], function(i, n){\r
1477                 equals( i, n, "Check array iteration" );\r
1478         });\r
1479 \r
1480         jQuery.each( [5,6,7], function(i, n){\r
1481                 equals( i, n - 5, "Check array iteration" );\r
1482         });\r
1483 \r
1484         jQuery.each( { name: "name", lang: "lang" }, function(i, n){\r
1485                 equals( i, n, "Check object iteration" );\r
1486         });\r
1487 \r
1488         var total = 0;\r
1489         jQuery.each([1,2,3], function(i,v){ total += v; });\r
1490         equals( total, 6, "Looping over an array" );\r
1491         total = 0;\r
1492         jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });\r
1493         equals( total, 3, "Looping over an array, with break" );\r
1494         total = 0;\r
1495         jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });\r
1496         equals( total, 6, "Looping over an object" );\r
1497         total = 0;\r
1498         jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });\r
1499         equals( total, 3, "Looping over an object, with break" );\r
1500 });\r
1501 \r
1502 test("jQuery.prop", function() {\r
1503         expect(2);\r
1504         var handle = function() { return this.id };\r
1505         equals( jQuery.prop(jQuery("#ap")[0], handle), "ap", "Check with Function argument" );\r
1506         equals( jQuery.prop(jQuery("#ap")[0], "value"), "value", "Check with value argument" );\r
1507 });\r
1508 \r
1509 test("jQuery.className", function() {\r
1510         expect(6);\r
1511         var x = jQuery("<p>Hi</p>")[0];\r
1512         var c = jQuery.className;\r
1513         c.add(x, "hi");\r
1514         equals( x.className, "hi", "Check single added class" );\r
1515         c.add(x, "foo bar");\r
1516         equals( x.className, "hi foo bar", "Check more added classes" );\r
1517         c.remove(x);\r
1518         equals( x.className, "", "Remove all classes" );\r
1519         c.add(x, "hi foo bar");\r
1520         c.remove(x, "foo");\r
1521         equals( x.className, "hi bar", "Check removal of one class" );\r
1522         ok( c.has(x, "hi"), "Check has1" );\r
1523         ok( c.has(x, "bar"), "Check has2" );\r
1524 });\r
1525 \r
1526 test("jQuery.data", function() {\r
1527         expect(5);\r
1528         var div = jQuery("#foo")[0];\r
1529         equals( jQuery.data(div, "test"), undefined, "Check for no data exists" );\r
1530         jQuery.data(div, "test", "success");\r
1531         equals( jQuery.data(div, "test"), "success", "Check for added data" );\r
1532         jQuery.data(div, "test", "overwritten");\r
1533         equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );\r
1534         jQuery.data(div, "test", undefined);\r
1535         equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");\r
1536         jQuery.data(div, "test", null);\r
1537         ok( jQuery.data(div, "test") === null, "Check for null data");\r
1538 });\r
1539 \r
1540 test(".data()", function() {\r
1541         expect(18);\r
1542         var div = jQuery("#foo");\r
1543         equals( div.data("test"), undefined, "Check for no data exists" );\r
1544         div.data("test", "success");\r
1545         equals( div.data("test"), "success", "Check for added data" );\r
1546         div.data("test", "overwritten");\r
1547         equals( div.data("test"), "overwritten", "Check for overwritten data" );\r
1548         div.data("test", undefined);\r
1549         equals( div.data("test"), "overwritten", "Check that data wasn't removed");\r
1550         div.data("test", null);\r
1551         ok( div.data("test") === null, "Check for null data");\r
1552 \r
1553         div.data("test", "overwritten");\r
1554         var hits = {test:0}, gets = {test:0};\r
1555 \r
1556         div\r
1557                 .bind("setData",function(e,key,value){ hits[key] += value; })\r
1558                 .bind("setData.foo",function(e,key,value){ hits[key] += value; })\r
1559                 .bind("getData",function(e,key){ gets[key] += 1; })\r
1560                 .bind("getData.foo",function(e,key){ gets[key] += 3; });\r
1561 \r
1562         div.data("test.foo", 2);\r
1563         equals( div.data("test"), "overwritten", "Check for original data" );\r
1564         equals( div.data("test.foo"), 2, "Check for namespaced data" );\r
1565         equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );\r
1566         equals( hits.test, 2, "Check triggered setter functions" );\r
1567         equals( gets.test, 5, "Check triggered getter functions" );\r
1568 \r
1569         hits.test = 0;\r
1570         gets.test = 0;\r
1571 \r
1572         div.data("test", 1);\r
1573         equals( div.data("test"), 1, "Check for original data" );\r
1574         equals( div.data("test.foo"), 2, "Check for namespaced data" );\r
1575         equals( div.data("test.bar"), 1, "Check for unmatched namespace" );\r
1576         equals( hits.test, 1, "Check triggered setter functions" );\r
1577         equals( gets.test, 5, "Check triggered getter functions" );\r
1578 \r
1579         hits.test = 0;\r
1580         gets.test = 0;\r
1581 \r
1582         div\r
1583                 .bind("getData",function(e,key){ return key + "root"; })\r
1584                 .bind("getData.foo",function(e,key){ return key + "foo"; });\r
1585 \r
1586         equals( div.data("test"), "testroot", "Check for original data" );\r
1587         equals( div.data("test.foo"), "testfoo", "Check for namespaced data" );\r
1588         equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" );\r
1589 });\r
1590 \r
1591 test("jQuery.removeData", function() {\r
1592         expect(1);\r
1593         var div = jQuery("#foo")[0];\r
1594         jQuery.data(div, "test", "testing");\r
1595         jQuery.removeData(div, "test");\r
1596         equals( jQuery.data(div, "test"), undefined, "Check removal of data" );\r
1597 });\r
1598 \r
1599 test(".removeData()", function() {\r
1600         expect(6);\r
1601         var div = jQuery("#foo");\r
1602         div.data("test", "testing");\r
1603         div.removeData("test");\r
1604         equals( div.data("test"), undefined, "Check removal of data" );\r
1605 \r
1606         div.data("test", "testing");\r
1607         div.data("test.foo", "testing2");\r
1608         div.removeData("test.bar");\r
1609         equals( div.data("test.foo"), "testing2", "Make sure data is intact" );\r
1610         equals( div.data("test"), "testing", "Make sure data is intact" );\r
1611 \r
1612         div.removeData("test");\r
1613         equals( div.data("test.foo"), "testing2", "Make sure data is intact" );\r
1614         equals( div.data("test"), undefined, "Make sure data is intact" );\r
1615 \r
1616         div.removeData("test.foo");\r
1617         equals( div.data("test.foo"), undefined, "Make sure data is intact" );\r
1618 });\r
1619 \r
1620 test("remove()", function() {\r
1621         expect(6);\r
1622         jQuery("#ap").children().remove();\r
1623         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );\r
1624         equals( jQuery("#ap").children().length, 0, "Check remove" );\r
1625 \r
1626         reset();\r
1627         jQuery("#ap").children().remove("a");\r
1628         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );\r
1629         equals( jQuery("#ap").children().length, 1, "Check filtered remove" );\r
1630 \r
1631         // using contents will get comments regular, text, and comment nodes\r
1632         equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );\r
1633         jQuery("#nonnodes").contents().remove();\r
1634         equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );\r
1635 });\r
1636 \r
1637 test("empty()", function() {\r
1638         expect(3);\r
1639         equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );\r
1640         equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );\r
1641 \r
1642         // using contents will get comments regular, text, and comment nodes\r
1643         var j = jQuery("#nonnodes").contents();\r
1644         j.empty();\r
1645         equals( j.html(), "", "Check node,textnode,comment empty works" );\r
1646 });\r
1647 \r
1648 test("slice()", function() {\r
1649         expect(6);\r
1650         \r
1651         var $links = jQuery("#ap a");\r
1652         \r
1653         isSet( $links.slice(1,2), q("groups"), "slice(1,2)" );\r
1654         isSet( $links.slice(1), q("groups", "anchor1", "mark"), "slice(1)" );\r
1655         isSet( $links.slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );\r
1656         isSet( $links.slice(-1), q("mark"), "slice(-1)" );\r
1657 \r
1658         isSet( $links.eq(1), q("groups"), "eq(1)" );\r
1659         \r
1660         isSet( $links.eq('2'), q("anchor1"), "eq('2')" );\r
1661 });\r
1662 \r
1663 test("map()", function() {\r
1664         expect(2);//expect(6);\r
1665 \r
1666         isSet(\r
1667                 jQuery("#ap").map(function(){\r
1668                         return jQuery(this).find("a").get();\r
1669                 }),\r
1670                 q("google", "groups", "anchor1", "mark"),\r
1671                 "Array Map"\r
1672         );\r
1673 \r
1674         isSet(\r
1675                 jQuery("#ap > a").map(function(){\r
1676                         return this.parentNode;\r
1677                 }),\r
1678                 q("ap","ap","ap"),\r
1679                 "Single Map"\r
1680         );\r
1681 \r
1682         return;//these haven't been accepted yet\r
1683 \r
1684         //for #2616\r
1685         var keys = jQuery.map( {a:1,b:2}, function( v, k ){\r
1686                 return k;\r
1687         }, [ ] );\r
1688 \r
1689         equals( keys.join(""), "ab", "Map the keys from a hash to an array" );\r
1690 \r
1691         var values = jQuery.map( {a:1,b:2}, function( v, k ){\r
1692                 return v;\r
1693         }, [ ] );\r
1694 \r
1695         equals( values.join(""), "12", "Map the values from a hash to an array" );\r
1696 \r
1697         var scripts = document.getElementsByTagName("script");\r
1698         var mapped = jQuery.map( scripts, function( v, k ){\r
1699                 return v;\r
1700         }, {length:0} );\r
1701 \r
1702         equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );\r
1703 \r
1704         var flat = jQuery.map( Array(4), function( v, k ){\r
1705                 return k % 2 ? k : [k,k,k];//try mixing array and regular returns\r
1706         });\r
1707 \r
1708         equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" );\r
1709 });\r
1710 \r
1711 test("contents()", function() {\r
1712         expect(12);\r
1713         equals( jQuery("#ap").contents().length, 9, "Check element contents" );\r
1714         ok( jQuery("#iframe").contents()[0], "Check existance of IFrame document" );\r
1715         var ibody = jQuery("#loadediframe").contents()[0].body;\r
1716         ok( ibody, "Check existance of IFrame body" );\r
1717 \r
1718         equals( jQuery("span", ibody).text(), "span text", "Find span in IFrame and check its text" );\r
1719 \r
1720         jQuery(ibody).append("<div>init text</div>");\r
1721         equals( jQuery("div", ibody).length, 2, "Check the original div and the new div are in IFrame" );\r
1722 \r
1723         equals( jQuery("div:last", ibody).text(), "init text", "Add text to div in IFrame" );\r
1724 \r
1725         jQuery("div:last", ibody).text("div text");\r
1726         equals( jQuery("div:last", ibody).text(), "div text", "Add text to div in IFrame" );\r
1727 \r
1728         jQuery("div:last", ibody).remove();\r
1729         equals( jQuery("div", ibody).length, 1, "Delete the div and check only one div left in IFrame" );\r
1730 \r
1731         equals( jQuery("div", ibody).text(), "span text", "Make sure the correct div is still left after deletion in IFrame" );\r
1732 \r
1733         jQuery("<table/>", ibody).append("<tr><td>cell</td></tr>").appendTo(ibody);\r
1734         jQuery("table", ibody).remove();\r
1735         equals( jQuery("div", ibody).length, 1, "Check for JS error on add and delete of a table in IFrame" );\r
1736 \r
1737         // using contents will get comments regular, text, and comment nodes\r
1738         var c = jQuery("#nonnodes").contents().contents();\r
1739         equals( c.length, 1, "Check node,textnode,comment contents is just one" );\r
1740         equals( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );\r
1741 });\r
1742 \r
1743 test("jQuery.makeArray", function(){\r
1744         expect(14);\r
1745 \r
1746         equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName, "HEAD", "Pass makeArray a jQuery object" );\r
1747 \r
1748         equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );\r
1749 \r
1750         equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );\r
1751 \r
1752         equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );\r
1753 \r
1754         equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );\r
1755 \r
1756         equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );\r
1757 \r
1758         equals( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );\r
1759 \r
1760         equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );\r
1761 \r
1762         equals( jQuery.makeArray( document.createElement("div") )[0].nodeName, "DIV", "Pass makeArray a single node" );\r
1763 \r
1764         equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );\r
1765 \r
1766         ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );\r
1767 \r
1768         // function, is tricky as it has length\r
1769         equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );\r
1770         \r
1771         //window, also has length\r
1772         equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );\r
1773 \r
1774         equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );\r
1775 \r
1776         ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );\r
1777 });\r