3 test("Basic requirements", function() {
5 ok( Array.prototype.push, "Array.push()" );
6 ok( Function.prototype.apply, "Function.apply()" );
7 ok( document.getElementById, "getElementById" );
8 ok( document.getElementsByTagName, "getElementsByTagName" );
9 ok( RegExp, "RegExp" );
10 ok( jQuery, "jQuery" );
14 test("jQuery()", function() {
17 var main = jQuery("#main");
18 isSet( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
21 // disabled since this test was doing nothing. i tried to fix it but i'm not sure
22 // what the expected behavior should even be. FF returns "\n" for the text node
23 // make sure this is handled
24 var crlfContainer = jQuery('<p>\r\n</p>');
25 var x = crlfContainer.contents().get(0).nodeValue;
26 equals( x, what???, "Check for \\r and \\n in jQuery()" );
29 /* // Disabled until we add this functionality in
32 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
36 ok( pass, "jQuery('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
38 var code = jQuery("<code/>");
39 equals( code.length, 1, "Correct number of elements generated for code" );
40 var img = jQuery("<img/>");
41 equals( img.length, 1, "Correct number of elements generated for img" );
42 var div = jQuery("<div/><hr/><code/><b/>");
43 equals( div.length, 4, "Correct number of elements generated for div hr code b" );
45 // can actually yield more than one, when iframes are included, the window is an array as well
46 equals( jQuery(window).length, 1, "Correct number of elements generated for window" );
48 equals( jQuery(document).length, 1, "Correct number of elements generated for document" );
50 equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
52 equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" );
55 test("selector state", function() {
61 equals( test.selector, "", "Empty jQuery Selector" );
62 equals( test.context, document, "Empty jQuery Context" );
64 test = jQuery(document);
65 equals( test.selector, "", "Document Selector" );
66 equals( test.context, document, "Document Context" );
68 test = jQuery(document.body);
69 equals( test.selector, "", "Body Selector" );
70 equals( test.context, document.body, "Body Context" );
72 test = jQuery("#main");
73 equals( test.selector, "#main", "#main Selector" );
74 equals( test.context, document, "#main Context" );
76 test = jQuery("#notfoundnono");
77 equals( test.selector, "#notfoundnono", "#notfoundnono Selector" );
78 equals( test.context, document, "#notfoundnono Context" );
80 test = jQuery("#main", document);
81 equals( test.selector, "#main", "#main Selector" );
82 equals( test.context, document, "#main Context" );
84 test = jQuery("#main", document.body);
85 equals( test.selector, "#main", "#main Selector" );
86 equals( test.context, document.body, "#main Context" );
90 equals( test.selector, "#main", "#main Selector" );
91 equals( test.context, document.body, "#main Context" );
93 test = jQuery(document.body).find("#main");
94 equals( test.selector, "#main", "#main find Selector" );
95 equals( test.context, document.body, "#main find Context" );
97 test = jQuery("#main").filter("div");
98 equals( test.selector, "#main.filter(div)", "#main filter Selector" );
99 equals( test.context, document, "#main filter Context" );
101 test = jQuery("#main").not("div");
102 equals( test.selector, "#main.not(div)", "#main not Selector" );
103 equals( test.context, document, "#main not Context" );
105 test = jQuery("#main").filter("div").not("div");
106 equals( test.selector, "#main.filter(div).not(div)", "#main filter, not Selector" );
107 equals( test.context, document, "#main filter, not Context" );
109 test = jQuery("#main").filter("div").not("div").end();
110 equals( test.selector, "#main.filter(div)", "#main filter, not, end Selector" );
111 equals( test.context, document, "#main filter, not, end Context" );
113 test = jQuery("#main").parent("body");
114 equals( test.selector, "#main.parent(body)", "#main parent Selector" );
115 equals( test.context, document, "#main parent Context" );
117 test = jQuery("#main").eq(0);
118 equals( test.selector, "#main.slice(0,1)", "#main eq Selector" );
119 equals( test.context, document, "#main eq Context" );
122 test("browser", function() {
126 "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0",
127 "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",
129 * "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",
131 //Browsers with Gecko engine
133 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915" : "1.7.12",
135 "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",
137 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20070321 Netscape/8.1.3" : "1.7.5",
139 "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",
141 "Opera/9.20 (X11; Linux x86_64; U; en)": "9.20",
142 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.20" : "9.20",
143 "Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20",
145 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "418.9",
146 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" : "418.8",
147 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.8",
148 //Other user agent string
149 "Other browser's user agent 1.0":null
151 for (var i in browsers) {
152 var v = i.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ); // RegEx from Core jQuery.browser.version check
153 var version = v ? v[1] : null;
154 equals( version, browsers[i], "Checking UA string" );
158 test("noConflict", function() {
163 equals( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
164 equals( jQuery, $$, "Make sure jQuery wasn't touched." );
165 equals( $, original$, "Make sure $ was reverted." );
169 equals( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" );
170 equals( jQuery, originaljQuery, "Make sure jQuery was reverted." );
171 equals( $, original$, "Make sure $ was reverted." );
176 test("isFunction", function() {
179 // Make sure that false values return false
180 ok( !jQuery.isFunction(), "No Value" );
181 ok( !jQuery.isFunction( null ), "null Value" );
182 ok( !jQuery.isFunction( undefined ), "undefined Value" );
183 ok( !jQuery.isFunction( "" ), "Empty String Value" );
184 ok( !jQuery.isFunction( 0 ), "0 Value" );
187 // Safari uses "(Internal Function)"
188 ok( jQuery.isFunction(String), "String Function("+String+")" );
189 ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
190 ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
191 ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
193 // When stringified, this could be misinterpreted
194 var mystr = "function";
195 ok( !jQuery.isFunction(mystr), "Function String" );
197 // When stringified, this could be misinterpreted
198 var myarr = [ "function" ];
199 ok( !jQuery.isFunction(myarr), "Function Array" );
201 // When stringified, this could be misinterpreted
202 var myfunction = { "function": "test" };
203 ok( !jQuery.isFunction(myfunction), "Function Object" );
205 // Make sure normal functions still work
206 var fn = function(){};
207 ok( jQuery.isFunction(fn), "Normal Function" );
209 var obj = document.createElement("object");
211 // Firefox says this is a function
212 ok( !jQuery.isFunction(obj), "Object Element" );
214 // IE says this is an object
215 // Since 1.3, this isn't supported (#2968)
216 //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
218 var nodes = document.body.childNodes;
220 // Safari says this is a function
221 ok( !jQuery.isFunction(nodes), "childNodes Property" );
223 var first = document.body.firstChild;
225 // Normal elements are reported ok everywhere
226 ok( !jQuery.isFunction(first), "A normal DOM Element" );
228 var input = document.createElement("input");
230 document.body.appendChild( input );
232 // IE says this is an object
233 // Since 1.3, this isn't supported (#2968)
234 //ok( jQuery.isFunction(input.focus), "A default function property" );
236 document.body.removeChild( input );
238 var a = document.createElement("a");
239 a.href = "some-function";
240 document.body.appendChild( a );
242 // This serializes with the word 'function' in it
243 ok( !jQuery.isFunction(a), "Anchor Element" );
245 document.body.removeChild( a );
247 // Recursive function calls have lengths and array-like properties
248 function callme(callback){
249 function fn(response){
253 ok( jQuery.isFunction(fn), "Recursive Function Call" );
255 fn({ some: "data" });
259 callme(function(){});
263 test("jQuery('html')", function() {
268 var s = jQuery("<script>jQuery.foo='test';</script>")[0];
269 ok( s, "Creating a script" );
270 ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
271 jQuery("body").append("<script>jQuery.foo='test';</script>");
272 ok( jQuery.foo, "Executing a scripts contents in the right context" );
275 ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
277 ok( !jQuery("<script/>")[0].parentNode, "Create a script" );
279 ok( jQuery("<input/>").attr("type", "hidden"), "Create an input and set the type." );
281 var j = jQuery("<span>hi</span> there <!-- mon ami -->");
282 ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
284 ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
287 test("jQuery('html', context)", function() {
290 var $div = jQuery("<div/>");
291 var $span = jQuery("<span/>", $div);
292 equals($span.length, 1, "Verify a span created with a div context works, #1763");
296 test("jQuery(selector, xml).text(str) - Loaded via XML document", function() {
299 jQuery.get('data/dashboard.xml', function(xml) {
300 // tests for #1419 where IE was a problem
301 var tab = jQuery("tab", xml).eq(0);
302 equals( tab.text(), "blabla", "Verify initial text correct" );
304 equals( tab.text(), "newtext", "Verify new text correct" );
310 test("length", function() {
312 equals( jQuery("p").length, 6, "Get Number of Elements Found" );
315 test("size()", function() {
317 equals( jQuery("p").size(), 6, "Get Number of Elements Found" );
320 test("get()", function() {
322 isSet( jQuery("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
325 test("get(Number)", function() {
327 equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" );
330 test("add(String|Element|Array|undefined)", function() {
332 isSet( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
333 isSet( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
334 ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" );
336 // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE
337 // use jQuery([]).add(form.elements) instead.
338 //equals( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );
340 var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>")).add(jQuery("<p id='x2'>xxx</p>"));
341 equals( x[0].id, "x1", "Check on-the-fly element1" );
342 equals( x[1].id, "x2", "Check on-the-fly element2" );
344 var x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
345 equals( x[0].id, "x1", "Check on-the-fly element1" );
346 equals( x[1].id, "x2", "Check on-the-fly element2" );
349 equals( jQuery([]).add(notDefined).length, 0, "Check that undefined adds nothing" );
352 equals( jQuery([]).add([window,document,document.body,document]).length, 3, "Pass an array" );
353 equals( jQuery(document).add(document).length, 1, "Check duplicated elements" );
354 equals( jQuery(window).add(window).length, 1, "Check duplicated elements using the window" );
355 ok( jQuery([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" );
358 test("each(Function)", function() {
360 var div = jQuery("div");
361 div.each(function(){this.foo = 'zoo';});
363 for ( var i = 0; i < div.size(); i++ ) {
364 if ( div.get(i).foo != "zoo" ) pass = false;
366 ok( pass, "Execute a function, Relative" );
369 test("index(Object)", function() {
372 var elements = jQuery([window, document]),
373 inputElements = jQuery('#radio1,#radio2,#check1,#check2');
375 equals( elements.index(window), 0, "Check for index of elements" );
376 equals( elements.index(document), 1, "Check for index of elements" );
377 equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" );
378 equals( inputElements.index(document.getElementById('radio2')), 1, "Check for index of elements" );
379 equals( inputElements.index(document.getElementById('check1')), 2, "Check for index of elements" );
380 equals( inputElements.index(document.getElementById('check2')), 3, "Check for index of elements" );
381 equals( inputElements.index(window), -1, "Check for not found index" );
382 equals( inputElements.index(document), -1, "Check for not found index" );
384 // enabled since [5500]
385 equals( elements.index( elements ), 0, "Pass in a jQuery object" );
386 equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
389 test("attr(String)", function() {
391 equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
392 equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );
393 equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );
394 equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );
395 equals( jQuery('#check1').attr('type'), "checkbox", 'Check for type attribute' );
396 equals( jQuery('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' );
397 equals( jQuery('#google').attr('title'), "Google!", 'Check for title attribute' );
398 equals( jQuery('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' );
399 equals( jQuery('#en').attr('lang'), "en", 'Check for lang attribute' );
400 equals( jQuery('#simon').attr('class'), "blog link", 'Check for class attribute' );
401 equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' );
402 equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' );
403 ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
404 equals( jQuery('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );
405 equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
406 equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
407 equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
408 equals( jQuery('#foo').attr('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' );
409 equals( jQuery('#foo').attr('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' );
411 jQuery('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
412 equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
414 equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
417 // Related to [5574] and [5683]
418 var body = document.body, $body = jQuery(body);
420 ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );
421 ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );
423 body.setAttribute('foo', 'baz');
424 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
427 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
429 $body.attr('foo','cool');
430 equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );
432 body.foo = undefined;
433 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
435 body.removeAttribute('foo'); // Cleanup
439 test("attr(String) in XML Files", function() {
442 jQuery.get("data/dashboard.xml", function(xml) {
443 equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
444 equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
450 test("attr(String, Function)", function() {
452 equals( jQuery('#text1').attr('value', function() { return this.id })[0].value, "text1", "Set value from id" );
453 equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
456 test("attr(Hash)", function() {
459 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
460 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
462 ok( pass, "Set Multiple Attributes" );
465 test("attr(String, Object)", function() {
467 var div = jQuery("div").attr("foo", "bar"),
469 for ( var i = 0; i < div.size(); i++ ) {
470 if ( div.get(i).getAttribute('foo') != "bar" ){
475 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
477 ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
479 jQuery("#name").attr('name', 'something');
480 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
481 jQuery("#check2").attr('checked', true);
482 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
483 jQuery("#check2").attr('checked', false);
484 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
485 jQuery("#text1").attr('readonly', true);
486 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
487 jQuery("#text1").attr('readonly', false);
488 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
489 jQuery("#name").attr('maxlength', '5');
490 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
491 jQuery("#name").attr('maxLength', '10');
492 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
495 jQuery("#name").attr('someAttr', '0');
496 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
497 jQuery("#name").attr('someAttr', 0);
498 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
499 jQuery("#name").attr('someAttr', 1);
500 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
502 // using contents will get comments regular, text, and comment nodes
503 var j = jQuery("#nonnodes").contents();
505 j.attr("name", "attrvalue");
506 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
507 j.removeAttr("name");
511 var type = jQuery("#check2").attr('type');
514 jQuery("#check2").attr('type','hidden');
518 ok( thrown, "Exception thrown when trying to change type property" );
519 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
521 var check = document.createElement("input");
524 jQuery(check).attr('type','checkbox');
528 ok( thrown, "Exception thrown when trying to change type property" );
529 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
531 var check = jQuery("<input />");
534 check.attr('type','checkbox');
538 ok( thrown, "Exception thrown when trying to change type property" );
539 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
543 test("attr(String, Object) - Loaded via XML document", function() {
546 jQuery.get('data/dashboard.xml', function(xml) {
548 jQuery('tab', xml).each(function() {
549 titles.push(jQuery(this).attr('title'));
551 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
552 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
558 test("attr('tabindex')", function() {
561 // elements not natively tabbable
562 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
563 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
566 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
567 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
568 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
570 // anchor without href
571 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
572 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
573 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
576 test("attr('tabindex', value)", function() {
579 var element = jQuery('#divWithNoTabIndex');
580 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
582 // set a positive string
583 element.attr('tabindex', '1');
584 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
587 element.attr('tabindex', '0');
588 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
590 // set a negative string
591 element.attr('tabindex', '-1');
592 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
594 // set a positive number
595 element.attr('tabindex', 1);
596 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
599 element.attr('tabindex', 0);
600 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
602 // set a negative number
603 element.attr('tabindex', -1);
604 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
606 element = jQuery('#linkWithTabIndex');
607 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
609 element.attr('tabindex', -1);
610 equals(element.attr('tabindex'), -1, 'set negative tabindex');
613 test("css(String|Hash)", function() {
616 equals( jQuery('#main').css("display"), 'none', 'Check for css property "display"');
618 ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible');
619 jQuery('#nothiddendiv').css({display: 'none'});
620 ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden');
621 jQuery('#nothiddendiv').css({display: 'block'});
622 ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
624 jQuery('#floatTest').css({styleFloat: 'right'});
625 equals( jQuery('#floatTest').css('styleFloat'), 'right', 'Modified CSS float using "styleFloat": Assert float is right');
626 jQuery('#floatTest').css({cssFloat: 'left'});
627 equals( jQuery('#floatTest').css('cssFloat'), 'left', 'Modified CSS float using "cssFloat": Assert float is left');
628 jQuery('#floatTest').css({'float': 'right'});
629 equals( jQuery('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right');
630 jQuery('#floatTest').css({'font-size': '30px'});
631 equals( jQuery('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px');
633 jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
634 jQuery('#foo').css({opacity: n});
635 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
636 jQuery('#foo').css({opacity: parseFloat(n)});
637 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
639 jQuery('#foo').css({opacity: ''});
640 equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
643 test("css(String, Object)", function() {
645 ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible');
646 jQuery('#nothiddendiv').css("display", 'none');
647 ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden');
648 jQuery('#nothiddendiv').css("display", 'block');
649 ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
651 jQuery('#floatTest').css('styleFloat', 'left');
652 equals( jQuery('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left');
653 jQuery('#floatTest').css('cssFloat', 'right');
654 equals( jQuery('#floatTest').css('cssFloat'), 'right', 'Modified CSS float using "cssFloat": Assert float is right');
655 jQuery('#floatTest').css('float', 'left');
656 equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');
657 jQuery('#floatTest').css('font-size', '20px');
658 equals( jQuery('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');
660 jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
661 jQuery('#foo').css('opacity', n);
662 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
663 jQuery('#foo').css('opacity', parseFloat(n));
664 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
666 jQuery('#foo').css('opacity', '');
667 equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
668 // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
669 if (jQuery.browser.msie) {
670 jQuery('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
672 equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
674 // using contents will get comments regular, text, and comment nodes
675 var j = jQuery("#nonnodes").contents();
676 j.css("padding-left", "1px");
677 equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
679 // opera sometimes doesn't update 'display' correctly, see #2037
680 jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML
681 equals( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
684 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
687 var $checkedtest = jQuery("#checkedtest");
688 // IE6 was clearing "checked" in jQuery.css(elem, "height");
689 jQuery.css($checkedtest[0], "height");
690 ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
691 ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
692 ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
693 ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
696 test("width()", function() {
699 var $div = jQuery("#nothiddendiv");
701 equals($div.width(), 30, "Test set to 30 correctly");
703 equals($div.width(), 30, "Test hidden div");
705 $div.width(-1); // handle negative numbers by ignoring #1599
706 equals($div.width(), 30, "Test negative width ignored");
707 $div.css("padding", "20px");
708 equals($div.width(), 30, "Test padding specified with pixels");
709 $div.css("border", "2px solid #fff");
710 equals($div.width(), 30, "Test border specified with pixels");
711 //$div.css("padding", "2em");
712 //equals($div.width(), 30, "Test padding specified with ems");
713 //$div.css("border", "1em solid #fff");
714 //DISABLED - Opera 9.6 fails this test, returns 8
715 //equals($div.width(), 30, "Test border specified with ems");
716 //$div.css("padding", "2%");
717 //equals($div.width(), 30, "Test padding specified with percent");
719 $div.css({ display: "", border: "", padding: "" });
721 jQuery("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" });
722 equals(jQuery("#nothiddendivchild").width(), 20, "Test child width with border and padding");
723 jQuery("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" });
726 test("height()", function() {
729 var $div = jQuery("#nothiddendiv");
731 equals($div.height(), 30, "Test set to 30 correctly");
733 equals($div.height(), 30, "Test hidden div");
735 $div.height(-1); // handle negative numbers by ignoring #1599
736 equals($div.height(), 30, "Test negative height ignored");
737 $div.css("padding", "20px");
738 equals($div.height(), 30, "Test padding specified with pixels");
739 $div.css("border", "2px solid #fff");
740 equals($div.height(), 30, "Test border specified with pixels");
741 //$div.css("padding", "2em");
742 //equals($div.height(), 30, "Test padding specified with ems");
743 //$div.css("border", "1em solid #fff");
744 //DISABLED - Opera 9.6 fails this test, returns 8
745 //equals($div.height(), 30, "Test border specified with ems");
746 //$div.css("padding", "2%");
747 //equals($div.height(), 30, "Test padding specified with percent");
749 $div.css({ display: "", border: "", padding: "", height: "1px" });
752 test("text()", function() {
754 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
755 equals( jQuery('#sap').text(), expected, 'Check for merged text of more then one element.' );
758 test("wrap(String|Element)", function() {
760 var defaultText = 'Try them out:'
761 var result = jQuery('#first').wrap('<div class="red"><span></span></div>').text();
762 equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
763 ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
766 var defaultText = 'Try them out:'
767 var result = jQuery('#first').wrap(document.getElementById('empty')).parent();
768 ok( result.is('ol'), 'Check for element wrapping' );
769 equals( result.text(), defaultText, 'Check for element wrapping' );
772 jQuery('#check1').click(function() {
774 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
775 jQuery(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
776 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
779 // using contents will get comments regular, text, and comment nodes
780 var j = jQuery("#nonnodes").contents();
782 equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
783 equals( jQuery("#nonnodes > i").text(), j.text() + j[1].nodeValue, "Check node,textnode,comment wraps doesn't hurt text" );
785 // Try wrapping a disconnected node
786 j = jQuery("<label/>").wrap("<li/>");
787 equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
788 equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
791 test("wrapAll(String|Element)", function() {
793 var prev = jQuery("#firstp")[0].previousSibling;
794 var p = jQuery("#firstp,#first")[0].parentNode;
795 var result = jQuery('#firstp,#first').wrapAll('<div class="red"><div id="tmp"></div></div>');
796 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
797 ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
798 ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
799 equals( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
800 equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
803 var prev = jQuery("#firstp")[0].previousSibling;
804 var p = jQuery("#first")[0].parentNode;
805 var result = jQuery('#firstp,#first').wrapAll(document.getElementById('empty'));
806 equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
807 equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
808 equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
811 test("wrapInner(String|Element)", function() {
813 var num = jQuery("#first").children().length;
814 var result = jQuery('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
815 equals( jQuery("#first").children().length, 1, "Only one child" );
816 ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
817 equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
820 var num = jQuery("#first").children().length;
821 var result = jQuery('#first').wrapInner(document.getElementById('empty'));
822 equals( jQuery("#first").children().length, 1, "Only one child" );
823 ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
824 equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
827 test("append(String|Element|Array<Element>|jQuery)", function() {
829 var defaultText = 'Try them out:'
830 var result = jQuery('#first').append('<b>buga</b>');
831 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
832 equals( jQuery('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
835 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
836 jQuery('#sap').append(document.getElementById('first'));
837 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
840 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
841 jQuery('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
842 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
845 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
846 jQuery('#sap').append(jQuery("#first, #yahoo"));
847 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
850 jQuery("#sap").append( 5 );
851 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
854 jQuery("#sap").append( " text with spaces " );
855 ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
858 ok( jQuery("#sap").append([]), "Check for appending an empty array." );
859 ok( jQuery("#sap").append(""), "Check for appending an empty string." );
860 ok( jQuery("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
863 jQuery("#sap").append(document.getElementById('form'));
864 equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
869 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append("<div>test</div>");
874 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
877 jQuery('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
878 t( 'Append legend', '#legend', ['legend'] );
881 jQuery('#select1').append('<OPTION>Test</OPTION>');
882 equals( jQuery('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
884 jQuery('#table').append('<colgroup></colgroup>');
885 ok( jQuery('#table colgroup').length, "Append colgroup" );
887 jQuery('#table colgroup').append('<col/>');
888 ok( jQuery('#table colgroup col').length, "Append col" );
891 jQuery('#table').append('<caption></caption>');
892 ok( jQuery('#table caption').length, "Append caption" );
896 .append('<select id="appendSelect1"></select>')
897 .append('<select id="appendSelect2"><option>Test</option></select>');
899 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
901 // using contents will get comments regular, text, and comment nodes
902 var j = jQuery("#nonnodes").contents();
903 var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
904 equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
905 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
906 d.contents().appendTo("#nonnodes");
908 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
911 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
913 var defaultText = 'Try them out:'
914 jQuery('<b>buga</b>').appendTo('#first');
915 equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
916 equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
919 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
920 jQuery(document.getElementById('first')).appendTo('#sap');
921 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
924 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
925 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
926 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
929 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
932 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
933 jQuery("#first, #yahoo").appendTo('#sap');
934 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
937 jQuery('#select1').appendTo('#foo');
938 t( 'Append select', '#foo select', ['select1'] );
941 var div = jQuery("<div/>").click(function(){
942 ok(true, "Running a cloned click.");
944 div.appendTo("#main, #moretests");
946 jQuery("#main div:last").click();
947 jQuery("#moretests div:last").click();
950 var div = jQuery("<div/>").appendTo("#main, #moretests");
952 equals( div.length, 2, "appendTo returns the inserted elements" );
954 div.addClass("test");
956 ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
957 ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
962 test("prepend(String|Element|Array<Element>|jQuery)", function() {
964 var defaultText = 'Try them out:'
965 var result = jQuery('#first').prepend('<b>buga</b>');
966 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
967 equals( jQuery('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
970 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
971 jQuery('#sap').prepend(document.getElementById('first'));
972 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
975 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
976 jQuery('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
977 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
980 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
981 jQuery('#sap').prepend(jQuery("#first, #yahoo"));
982 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
985 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
987 var defaultText = 'Try them out:'
988 jQuery('<b>buga</b>').prependTo('#first');
989 equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
990 equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
993 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
994 jQuery(document.getElementById('first')).prependTo('#sap');
995 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
998 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
999 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
1000 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
1003 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
1004 jQuery("#first, #yahoo").prependTo('#sap');
1005 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
1008 jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
1009 jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
1011 t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
1014 test("before(String|Element|Array<Element>|jQuery)", function() {
1016 var expected = 'This is a normal link: bugaYahoo';
1017 jQuery('#yahoo').before('<b>buga</b>');
1018 equals( expected, jQuery('#en').text(), 'Insert String before' );
1021 expected = "This is a normal link: Try them out:Yahoo";
1022 jQuery('#yahoo').before(document.getElementById('first'));
1023 equals( expected, jQuery('#en').text(), "Insert element before" );
1026 expected = "This is a normal link: Try them out:diveintomarkYahoo";
1027 jQuery('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
1028 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
1031 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
1032 jQuery('#yahoo').before(jQuery("#first, #mark"));
1033 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
1036 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
1038 var expected = 'This is a normal link: bugaYahoo';
1039 jQuery('<b>buga</b>').insertBefore('#yahoo');
1040 equals( expected, jQuery('#en').text(), 'Insert String before' );
1043 expected = "This is a normal link: Try them out:Yahoo";
1044 jQuery(document.getElementById('first')).insertBefore('#yahoo');
1045 equals( expected, jQuery('#en').text(), "Insert element before" );
1048 expected = "This is a normal link: Try them out:diveintomarkYahoo";
1049 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
1050 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
1053 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
1054 jQuery("#first, #mark").insertBefore('#yahoo');
1055 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
1058 test("after(String|Element|Array<Element>|jQuery)", function() {
1060 var expected = 'This is a normal link: Yahoobuga';
1061 jQuery('#yahoo').after('<b>buga</b>');
1062 equals( expected, jQuery('#en').text(), 'Insert String after' );
1065 expected = "This is a normal link: YahooTry them out:";
1066 jQuery('#yahoo').after(document.getElementById('first'));
1067 equals( expected, jQuery('#en').text(), "Insert element after" );
1070 expected = "This is a normal link: YahooTry them out:diveintomark";
1071 jQuery('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
1072 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
1075 expected = "This is a normal link: YahoodiveintomarkTry them out:";
1076 jQuery('#yahoo').after(jQuery("#first, #mark"));
1077 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
1080 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
1082 var expected = 'This is a normal link: Yahoobuga';
1083 jQuery('<b>buga</b>').insertAfter('#yahoo');
1084 equals( expected, jQuery('#en').text(), 'Insert String after' );
1087 expected = "This is a normal link: YahooTry them out:";
1088 jQuery(document.getElementById('first')).insertAfter('#yahoo');
1089 equals( expected, jQuery('#en').text(), "Insert element after" );
1092 expected = "This is a normal link: YahooTry them out:diveintomark";
1093 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
1094 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
1097 expected = "This is a normal link: YahoodiveintomarkTry them out:";
1098 jQuery("#first, #mark").insertAfter('#yahoo');
1099 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
1102 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
1104 jQuery('#yahoo').replaceWith('<b id="replace">buga</b>');
1105 ok( jQuery("#replace")[0], 'Replace element with string' );
1106 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
1109 jQuery('#yahoo').replaceWith(document.getElementById('first'));
1110 ok( jQuery("#first")[0], 'Replace element with element' );
1111 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
1114 jQuery('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
1115 ok( jQuery("#first")[0], 'Replace element with array of elements' );
1116 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
1117 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
1120 jQuery('#yahoo').replaceWith(jQuery("#first, #mark"));
1121 ok( jQuery("#first")[0], 'Replace element with set of elements' );
1122 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
1123 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
1126 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
1128 jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
1129 ok( jQuery("#replace")[0], 'Replace element with string' );
1130 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
1133 jQuery(document.getElementById('first')).replaceAll("#yahoo");
1134 ok( jQuery("#first")[0], 'Replace element with element' );
1135 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
1138 jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
1139 ok( jQuery("#first")[0], 'Replace element with array of elements' );
1140 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
1141 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
1144 jQuery("#first, #mark").replaceAll("#yahoo");
1145 ok( jQuery("#first")[0], 'Replace element with set of elements' );
1146 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
1147 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
1150 test("end()", function() {
1152 equals( 'Yahoo', jQuery('#yahoo').parent().end().text(), 'Check for end' );
1153 ok( jQuery('#yahoo').end(), 'Check for end with nothing to end' );
1155 var x = jQuery('#yahoo');
1157 equals( 'Yahoo', jQuery('#yahoo').text(), 'Check for non-destructive behaviour' );
1160 test("find(String)", function() {
1162 equals( 'Yahoo', jQuery('#foo').find('.blogTest').text(), 'Check for find' );
1164 // using contents will get comments regular, text, and comment nodes
1165 var j = jQuery("#nonnodes").contents();
1166 equals( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" );
1169 test("clone()", function() {
1171 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
1172 var clone = jQuery('#yahoo').clone();
1173 equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
1174 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
1177 "<table/>", "<tr/>", "<td/>", "<div/>",
1178 "<button/>", "<ul/>", "<ol/>", "<li/>",
1179 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
1180 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
1182 for (var i = 0; i < cloneTags.length; i++) {
1183 var j = jQuery(cloneTags[i]);
1184 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1));
1187 // using contents will get comments regular, text, and comment nodes
1188 var cl = jQuery("#nonnodes").contents().clone();
1189 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
1191 var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
1192 ok( true, "Bound event still exists." );
1195 div = div.clone(true).clone(true);
1196 equals( div.length, 1, "One element cloned" );
1197 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1198 div.trigger("click");
1200 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
1201 div.find("table").click(function(){
1202 ok( true, "Bound event still exists." );
1205 div = div.clone(true);
1206 equals( div.length, 1, "One element cloned" );
1207 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1208 div.find("table:last").trigger("click");
1210 div = jQuery("<div/>").html('<object height="355" width="425"> <param name="movie" value="http://www.youtube.com/v/JikaHBDoV3k&hl=en"> <param name="wmode" value="transparent"> </object>');
1212 div = div.clone(true);
1213 equals( div.length, 1, "One element cloned" );
1214 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1218 test("clone() on XML nodes", function() {
1221 jQuery.get("data/dashboard.xml", function (xml) {
1222 var root = jQuery(xml.documentElement).clone();
1223 var origTab = jQuery("tab", xml).eq(0);
1224 var cloneTab = jQuery("tab", root).eq(0);
1225 origTab.text("origval");
1226 cloneTab.text("cloneval");
1227 equals(origTab.text(), "origval", "Check original XML node was correctly set");
1228 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
1234 test("is(String)", function() {
1236 ok( jQuery('#form').is('form'), 'Check for element: A form must be a form' );
1237 ok( !jQuery('#form').is('div'), 'Check for element: A form is not a div' );
1238 ok( jQuery('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
1239 ok( !jQuery('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
1240 ok( jQuery('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
1241 ok( !jQuery('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
1242 ok( jQuery('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
1243 ok( !jQuery('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
1244 ok( jQuery('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
1245 ok( !jQuery('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
1246 ok( jQuery('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
1247 ok( !jQuery('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
1248 ok( jQuery('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
1249 ok( !jQuery('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
1250 ok( jQuery('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );
1251 ok( !jQuery('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );
1252 ok( jQuery('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );
1253 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"' );
1254 ok( !jQuery('#foo').is(0), 'Expected false for an invalid expression - 0' );
1255 ok( !jQuery('#foo').is(null), 'Expected false for an invalid expression - null' );
1256 ok( !jQuery('#foo').is(''), 'Expected false for an invalid expression - ""' );
1257 ok( !jQuery('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
1259 // test is() with comma-seperated expressions
1260 ok( jQuery('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
1261 ok( jQuery('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
1262 ok( jQuery('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
1263 ok( jQuery('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
1266 test("jQuery.merge()", function() {
1269 var parse = jQuery.merge;
1271 same( parse([],[]), [], "Empty arrays" );
1273 same( parse([1],[2]), [1,2], "Basic" );
1274 same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
1276 same( parse([1,2],[]), [1,2], "Second empty" );
1277 same( parse([],[1,2]), [1,2], "First empty" );
1279 // Fixed at [5998], #3641
1280 same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
1283 test("jQuery.extend(Object, Object)", function() {
1286 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
1287 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
1288 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
1289 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
1290 deep1 = { foo: { bar: true } },
1291 deep1copy = { foo: { bar: true } },
1292 deep2 = { foo: { baz: true }, foo2: document },
1293 deep2copy = { foo: { baz: true }, foo2: document },
1294 deepmerged = { foo: { bar: true, baz: true }, foo2: document };
1296 jQuery.extend(settings, options);
1297 isObj( settings, merged, "Check if extended: settings must be extended" );
1298 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
1300 jQuery.extend(settings, null, options);
1301 isObj( settings, merged, "Check if extended: settings must be extended" );
1302 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
1304 jQuery.extend(true, deep1, deep2);
1305 isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
1306 isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
1307 equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
1310 nullUndef = jQuery.extend({}, options, { xnumber2: null });
1311 ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
1313 nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
1314 ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
1316 nullUndef = jQuery.extend({}, options, { xnumber0: null });
1317 ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
1320 var recursive = { foo:target, bar:5 };
1321 jQuery.extend(true, target, recursive);
1322 isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
1324 var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
1325 equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
1327 var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
1328 ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
1330 var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
1331 ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
1333 var obj = { foo:null };
1334 jQuery.extend(true, obj, { foo:"notnull" } );
1335 equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
1338 jQuery.extend(func, { key: "value" } );
1339 equals( func.key, "value", "Verify a function can be extended" );
1341 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
1342 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
1343 options1 = { xnumber2: 1, xstring2: "x" },
1344 options1Copy = { xnumber2: 1, xstring2: "x" },
1345 options2 = { xstring2: "xx", xxx: "newstringx" },
1346 options2Copy = { xstring2: "xx", xxx: "newstringx" },
1347 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
1349 var settings = jQuery.extend({}, defaults, options1, options2);
1350 isObj( settings, merged2, "Check if extended: settings must be extended" );
1351 isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
1352 isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
1353 isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
1356 test("val()", function() {
1359 equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
1360 // ticket #1714 this caused a JS error in IE
1361 equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
1362 ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
1364 equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
1366 isSet( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
1368 equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
1370 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
1372 equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
1376 test("val(String/Number)", function() {
1378 document.getElementById('text1').value = "bla";
1379 equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
1381 jQuery("#text1").val('test');
1382 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
1384 jQuery("#text1").val(67);
1385 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
1387 jQuery("#select1").val("3");
1388 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
1390 jQuery("#select1").val(2);
1391 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
1393 // using contents will get comments regular, text, and comment nodes
1394 var j = jQuery("#nonnodes").contents();
1396 equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
1397 j.removeAttr("value");
1400 test("html(String)", function() {
1403 jQuery.scriptorder = 0;
1405 var div = jQuery("#main > div");
1406 div.html("<b>test</b>");
1408 for ( var i = 0; i < div.size(); i++ ) {
1409 if ( div.get(i).childNodes.length != 1 ) pass = false;
1411 ok( pass, "Set HTML" );
1414 // using contents will get comments regular, text, and comment nodes
1415 var j = jQuery("#nonnodes").contents();
1416 j.html("<b>bold</b>");
1418 // this is needed, or the expando added by jQuery unique will yield a different html
1419 j.find('b').removeData();
1420 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
1422 jQuery("#main").html("<select/>");
1423 jQuery("#main select").html("<option>O1</option><option selected='selected'>O2</option><option>O3</option>");
1424 equals( jQuery("#main select").val(), "O2", "Selected option correct" );
1426 var $div = jQuery('<div />');
1427 equals( $div.html( 5 ).html(), '5', 'Setting a number as html' );
1428 equals( $div.html( 0 ).html(), '0', 'Setting a zero as html' );
1432 jQuery("#main").html('<script type="something/else">ok( false, "Non-script evaluated." );</script><script type="text/javascript">ok( true, "text/javascript is evaluated." );</script><script>ok( true, "No type is evaluated." );</script><div><script type="text/javascript">ok( true, "Inner text/javascript is evaluated." );</script><script>ok( true, "Inner No type is evaluated." );</script><script type="something/else">ok( false, "Non-script evaluated." );</script></div>');
1436 jQuery("#main").html('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>');
1438 jQuery("#main").html('foo <form><script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
1440 // 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
1441 jQuery("#main").html("<script>equals(jQuery.scriptorder++, 0, 'Script is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html (even though appears before)')<\/script><span id='scriptorder'><script>equals(jQuery.scriptorder++, 1, 'Script (nested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equals(jQuery.scriptorder++, 2, 'Script (unnested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script>");
1443 setTimeout( start, 100 );
1446 test("filter()", function() {
1448 isSet( jQuery("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
1449 isSet( jQuery("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
1450 isSet( jQuery("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
1451 isSet( jQuery("p").filter(function() { return !jQuery("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
1453 // using contents will get comments regular, text, and comment nodes
1454 var j = jQuery("#nonnodes").contents();
1455 equals( j.filter("span").length, 1, "Check node,textnode,comment to filter the one span" );
1456 equals( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" );
1459 test("closest()", function() {
1461 isSet( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
1462 isSet( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
1463 isSet( jQuery("body").closest("div").get(), [], "closest(div)" );
1464 isSet( jQuery("#main").closest("span,#html").get(), q("html"), "closest(span,#html)" );
1466 isSet( jQuery("div:eq(1)").closest("div:first").get(), [], "closest(div:first)" );
1467 isSet( jQuery("div").closest("body:first div:last").get(), q("fx-tests"), "closest(body:first div:last)" );
1470 test("not()", function() {
1472 equals( jQuery("#main > p#ap > a").not("#google").length, 2, "not('selector')" );
1473 equals( jQuery("#main > p#ap > a").not(document.getElementById("google")).length, 2, "not(DOMElement)" );
1474 isSet( jQuery("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );
1475 isSet( jQuery("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
1476 isSet( jQuery("p").not(jQuery("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
1477 equals( jQuery("p").not(document.getElementsByTagName("p")).length, 0, "not(Array-like DOM collection)" );
1478 isSet( jQuery("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d", "option3e" ), "not('complex selector')");
1480 var selects = jQuery("#form select");
1481 isSet( selects.not( selects[1] ), q("select1", "select3"), "filter out DOM element");
1483 isSet( jQuery('#ap *').not('code'), q("google", "groups", "anchor1", "mark"), "not('tag selector')" );
1484 isSet( jQuery('#ap *').not('code, #mark'), q("google", "groups", "anchor1"), "not('tag, ID selector')" );
1485 isSet( jQuery('#ap *').not('#mark, code'), q("google", "groups", "anchor1"), "not('ID, tag selector')");
1488 test("andSelf()", function() {
1490 isSet( jQuery("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
1491 isSet( jQuery("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
1492 isSet( jQuery("#sndp, #en").parent().andSelf().get(), q("foo","sndp","en"), "Check for parent and self" );
1493 isSet( jQuery("#groups").parents("p, div").andSelf().get(), q("main", "ap", "groups"), "Check for parents and self" );
1496 test("siblings([String])", function() {
1498 isSet( jQuery("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
1499 isSet( jQuery("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" );
1500 isSet( jQuery("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
1501 isSet( jQuery("#foo").siblings("form, b").get(), q("form", "floatTest", "lengthtest", "name-tests", "testForm"), "Check for multiple filters" );
1502 var set = q("en", "sap", "sndp");
1503 isSet( jQuery("#en, #sndp").siblings().get(), set, "Check for unique results from siblings" );
1506 test("children([String])", function() {
1508 isSet( jQuery("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
1509 isSet( jQuery("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
1510 isSet( jQuery("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
1513 test("parent([String])", function() {
1515 equals( jQuery("#groups").parent()[0].id, "ap", "Simple parent check" );
1516 equals( jQuery("#groups").parent("p")[0].id, "ap", "Filtered parent check" );
1517 equals( jQuery("#groups").parent("div").length, 0, "Filtered parent check, no match" );
1518 equals( jQuery("#groups").parent("div, p")[0].id, "ap", "Check for multiple filters" );
1519 isSet( jQuery("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
1522 test("parents([String])", function() {
1524 equals( jQuery("#groups").parents()[0].id, "ap", "Simple parents check" );
1525 equals( jQuery("#groups").parents("p")[0].id, "ap", "Filtered parents check" );
1526 equals( jQuery("#groups").parents("div")[0].id, "main", "Filtered parents check2" );
1527 isSet( jQuery("#groups").parents("p, div").get(), q("main", "ap"), "Check for multiple filters" );
1528 isSet( jQuery("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );
1531 test("next([String])", function() {
1533 equals( jQuery("#ap").next()[0].id, "foo", "Simple next check" );
1534 equals( jQuery("#ap").next("div")[0].id, "foo", "Filtered next check" );
1535 equals( jQuery("#ap").next("p").length, 0, "Filtered next check, no match" );
1536 equals( jQuery("#ap").next("div, p")[0].id, "foo", "Multiple filters" );
1539 test("prev([String])", function() {
1541 equals( jQuery("#foo").prev()[0].id, "ap", "Simple prev check" );
1542 equals( jQuery("#foo").prev("p")[0].id, "ap", "Filtered prev check" );
1543 equals( jQuery("#foo").prev("div").length, 0, "Filtered prev check, no match" );
1544 equals( jQuery("#foo").prev("p, div")[0].id, "ap", "Multiple filters" );
1547 test("show()", function() {
1549 var pass = true, div = jQuery("#main div");
1550 div.show().each(function(){
1551 if ( this.style.display == "none" ) pass = false;
1555 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>');
1557 var old = jQuery("#show-tests table").show().css("display") !== "table";
1566 "table" : old ? "block" : "table",
1567 "thead" : old ? "block" : "table-header-group",
1568 "tbody" : old ? "block" : "table-row-group",
1569 "tr" : old ? "block" : "table-row",
1570 "th" : old ? "block" : "table-cell",
1571 "td" : old ? "block" : "table-cell",
1573 "li" : old ? "block" : "list-item"
1576 jQuery.each(test, function(selector, expected) {
1577 var elem = jQuery(selector, "#show-tests").show();
1578 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
1582 test("addClass(String)", function() {
1584 var div = jQuery("div");
1585 div.addClass("test");
1587 for ( var i = 0; i < div.size(); i++ ) {
1588 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
1590 ok( pass, "Add Class" );
1592 // using contents will get regular, text, and comment nodes
1593 var j = jQuery("#nonnodes").contents();
1595 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
1598 test("removeClass(String) - simple", function() {
1601 var $divs = jQuery('div');
1603 $divs.addClass("test").removeClass("test");
1605 ok( !$divs.is('.test'), "Remove Class" );
1609 $divs.addClass("test").addClass("foo").addClass("bar");
1610 $divs.removeClass("test").removeClass("bar").removeClass("foo");
1612 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
1616 // Make sure that a null value doesn't cause problems
1617 $divs.eq(0).addClass("test").removeClass(null);
1618 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
1620 $divs.eq(0).addClass("test").removeClass("");
1621 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
1623 // using contents will get regular, text, and comment nodes
1624 var j = jQuery("#nonnodes").contents();
1625 j.removeClass("asdf");
1626 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
1629 test("toggleClass(String)", function() {
1631 var e = jQuery("#firstp");
1632 ok( !e.is(".test"), "Assert class not present" );
1633 e.toggleClass("test");
1634 ok( e.is(".test"), "Assert class present" );
1635 e.toggleClass("test");
1636 ok( !e.is(".test"), "Assert class not present" );
1638 e.toggleClass("test", false);
1639 ok( !e.is(".test"), "Assert class not present" );
1640 e.toggleClass("test", true);
1641 ok( e.is(".test"), "Assert class present" );
1642 e.toggleClass("test", false);
1643 ok( !e.is(".test"), "Assert class not present" );
1646 test("removeAttr(String", function() {
1648 equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );
1651 test("text(String)", function() {
1653 equals( jQuery("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
1655 // using contents will get comments regular, text, and comment nodes
1656 var j = jQuery("#nonnodes").contents();
1658 equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
1659 equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
1660 equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
1663 test("jQuery.each(Object,Function)", function() {
1665 jQuery.each( [0,1,2], function(i, n){
1666 equals( i, n, "Check array iteration" );
1669 jQuery.each( [5,6,7], function(i, n){
1670 equals( i, n - 5, "Check array iteration" );
1673 jQuery.each( { name: "name", lang: "lang" }, function(i, n){
1674 equals( i, n, "Check object iteration" );
1678 jQuery.each([1,2,3], function(i,v){ total += v; });
1679 equals( total, 6, "Looping over an array" );
1681 jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
1682 equals( total, 3, "Looping over an array, with break" );
1684 jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
1685 equals( total, 6, "Looping over an object" );
1687 jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
1688 equals( total, 3, "Looping over an object, with break" );
1691 test("jQuery.prop", function() {
1693 var handle = function() { return this.id };
1694 equals( jQuery.prop(jQuery("#ap")[0], handle), "ap", "Check with Function argument" );
1695 equals( jQuery.prop(jQuery("#ap")[0], "value"), "value", "Check with value argument" );
1698 test("jQuery.className", function() {
1700 var x = jQuery("<p>Hi</p>")[0];
1701 var c = jQuery.className;
1703 equals( x.className, "hi", "Check single added class" );
1704 c.add(x, "foo bar");
1705 equals( x.className, "hi foo bar", "Check more added classes" );
1707 equals( x.className, "", "Remove all classes" );
1708 c.add(x, "hi foo bar");
1710 equals( x.className, "hi bar", "Check removal of one class" );
1711 ok( c.has(x, "hi"), "Check has1" );
1712 ok( c.has(x, "bar"), "Check has2" );
1715 test("remove()", function() {
1717 jQuery("#ap").children().remove();
1718 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1719 equals( jQuery("#ap").children().length, 0, "Check remove" );
1722 jQuery("#ap").children().remove("a");
1723 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1724 equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
1726 jQuery("#ap").children().remove("a, code");
1727 equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
1729 // using contents will get comments regular, text, and comment nodes
1730 equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
1731 jQuery("#nonnodes").contents().remove();
1732 equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1735 test("empty()", function() {
1737 equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
1738 equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
1740 // using contents will get comments regular, text, and comment nodes
1741 var j = jQuery("#nonnodes").contents();
1743 equals( j.html(), "", "Check node,textnode,comment empty works" );
1746 test("slice()", function() {
1749 var $links = jQuery("#ap a");
1751 isSet( $links.slice(1,2), q("groups"), "slice(1,2)" );
1752 isSet( $links.slice(1), q("groups", "anchor1", "mark"), "slice(1)" );
1753 isSet( $links.slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
1754 isSet( $links.slice(-1), q("mark"), "slice(-1)" );
1756 isSet( $links.eq(1), q("groups"), "eq(1)" );
1758 isSet( $links.eq('2'), q("anchor1"), "eq('2')" );
1761 test("map()", function() {
1762 expect(2);//expect(6);
1765 jQuery("#ap").map(function(){
1766 return jQuery(this).find("a").get();
1768 q("google", "groups", "anchor1", "mark"),
1773 jQuery("#ap > a").map(function(){
1774 return this.parentNode;
1780 return;//these haven't been accepted yet
1783 var keys = jQuery.map( {a:1,b:2}, function( v, k ){
1787 equals( keys.join(""), "ab", "Map the keys from a hash to an array" );
1789 var values = jQuery.map( {a:1,b:2}, function( v, k ){
1793 equals( values.join(""), "12", "Map the values from a hash to an array" );
1795 var scripts = document.getElementsByTagName("script");
1796 var mapped = jQuery.map( scripts, function( v, k ){
1800 equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );
1802 var flat = jQuery.map( Array(4), function( v, k ){
1803 return k % 2 ? k : [k,k,k];//try mixing array and regular returns
1806 equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" );
1809 test("contents()", function() {
1811 equals( jQuery("#ap").contents().length, 9, "Check element contents" );
1812 ok( jQuery("#iframe").contents()[0], "Check existance of IFrame document" );
1813 var ibody = jQuery("#loadediframe").contents()[0].body;
1814 ok( ibody, "Check existance of IFrame body" );
1816 equals( jQuery("span", ibody).text(), "span text", "Find span in IFrame and check its text" );
1818 jQuery(ibody).append("<div>init text</div>");
1819 equals( jQuery("div", ibody).length, 2, "Check the original div and the new div are in IFrame" );
1821 equals( jQuery("div:last", ibody).text(), "init text", "Add text to div in IFrame" );
1823 jQuery("div:last", ibody).text("div text");
1824 equals( jQuery("div:last", ibody).text(), "div text", "Add text to div in IFrame" );
1826 jQuery("div:last", ibody).remove();
1827 equals( jQuery("div", ibody).length, 1, "Delete the div and check only one div left in IFrame" );
1829 equals( jQuery("div", ibody).text(), "span text", "Make sure the correct div is still left after deletion in IFrame" );
1831 jQuery("<table/>", ibody).append("<tr><td>cell</td></tr>").appendTo(ibody);
1832 jQuery("table", ibody).remove();
1833 equals( jQuery("div", ibody).length, 1, "Check for JS error on add and delete of a table in IFrame" );
1835 // using contents will get comments regular, text, and comment nodes
1836 var c = jQuery("#nonnodes").contents().contents();
1837 equals( c.length, 1, "Check node,textnode,comment contents is just one" );
1838 equals( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" );
1841 test("jQuery.makeArray", function(){
1844 equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
1846 equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
1848 equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
1850 equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
1852 equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
1854 equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
1856 equals( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
1858 equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
1860 equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
1862 equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
1864 ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );
1866 // function, is tricky as it has length
1867 equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
1869 //window, also has length
1870 equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
1872 equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
1874 ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );