1 module("core", { teardown: moduleTeardown });
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 // Basic constructor's behavior
19 equal( jQuery().length, 0, "jQuery() === jQuery([])" );
20 equal( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" );
21 equal( jQuery(null).length, 0, "jQuery(null) === jQuery([])" );
22 equal( jQuery("").length, 0, "jQuery('') === jQuery([])" );
23 equal( jQuery("#").length, 0, "jQuery('#') === jQuery([])" );
25 var obj = jQuery("div");
26 equal( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" );
28 // can actually yield more than one, when iframes are included, the window is an array as well
29 equal( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" );
32 var main = jQuery("#qunit-fixture");
33 deepEqual( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
36 // disabled since this test was doing nothing. i tried to fix it but i'm not sure
37 // what the expected behavior should even be. FF returns "\n" for the text node
38 // make sure this is handled
39 var crlfContainer = jQuery('<p>\r\n</p>');
40 var x = crlfContainer.contents().get(0).nodeValue;
41 equal( x, what???, "Check for \\r and \\n in jQuery()" );
44 /* // Disabled until we add this functionality in
47 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
51 ok( pass, "jQuery('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
53 var code = jQuery("<code/>");
54 equal( code.length, 1, "Correct number of elements generated for code" );
55 equal( code.parent().length, 0, "Make sure that the generated HTML has no parent." );
56 var img = jQuery("<img/>");
57 equal( img.length, 1, "Correct number of elements generated for img" );
58 equal( img.parent().length, 0, "Make sure that the generated HTML has no parent." );
59 var div = jQuery("<div/><hr/><code/><b/>");
60 equal( div.length, 4, "Correct number of elements generated for div hr code b" );
61 equal( div.parent().length, 0, "Make sure that the generated HTML has no parent." );
63 equal( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
65 equal( jQuery(document.body).get(0), jQuery("body").get(0), "Test passing an html node to the factory" );
69 var elem = jQuery("<div/>", {
71 css: { paddingLeft:1, paddingRight:1 },
72 click: function(){ ok(exec, "Click executed."); },
78 equal( elem[0].style.width, "10px", "jQuery() quick setter width");
79 equal( elem[0].style.paddingLeft, "1px", "jQuery quick setter css");
80 equal( elem[0].style.paddingRight, "1px", "jQuery quick setter css");
81 equal( elem[0].childNodes.length, 1, "jQuery quick setter text");
82 equal( elem[0].firstChild.nodeValue, "test", "jQuery quick setter text");
83 equal( elem[0].className, "test2", "jQuery() quick setter class");
84 equal( elem[0].id, "test3", "jQuery() quick setter id");
89 // manually clean up detached elements
92 for ( var i = 0; i < 3; ++i ) {
93 elem = jQuery("<input type='text' value='TEST' />");
95 equal( elem[0].defaultValue, "TEST", "Ensure cached nodes are cloned properly (Bug #6655)" );
97 // manually clean up detached elements
100 equal( jQuery(" <div/> ").length, 1, "Make sure whitespace is trimmed." );
101 equal( jQuery(" a<div/>b ").length, 1, "Make sure whitespace and other characters are trimmed." );
104 for ( var i = 0; i < 128; i++ ) {
108 equal( jQuery(" <div>" + long + "</div> ").length, 1, "Make sure whitespace is trimmed on long strings." );
109 equal( jQuery(" a<div>" + long + "</div>b ").length, 1, "Make sure whitespace and other characters are trimmed on long strings." );
112 test("selector state", function() {
117 test = jQuery(undefined);
118 equal( test.selector, "", "Empty jQuery Selector" );
119 equal( test.context, undefined, "Empty jQuery Context" );
121 test = jQuery(document);
122 equal( test.selector, "", "Document Selector" );
123 equal( test.context, document, "Document Context" );
125 test = jQuery(document.body);
126 equal( test.selector, "", "Body Selector" );
127 equal( test.context, document.body, "Body Context" );
129 test = jQuery("#qunit-fixture");
130 equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
131 equal( test.context, document, "#qunit-fixture Context" );
133 test = jQuery("#notfoundnono");
134 equal( test.selector, "#notfoundnono", "#notfoundnono Selector" );
135 equal( test.context, document, "#notfoundnono Context" );
137 test = jQuery("#qunit-fixture", document);
138 equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
139 equal( test.context, document, "#qunit-fixture Context" );
141 test = jQuery("#qunit-fixture", document.body);
142 equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
143 equal( test.context, document.body, "#qunit-fixture Context" );
147 equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" );
148 equal( test.context, document.body, "#qunit-fixture Context" );
150 test = jQuery(document.body).find("#qunit-fixture");
151 equal( test.selector, "#qunit-fixture", "#qunit-fixture find Selector" );
152 equal( test.context, document.body, "#qunit-fixture find Context" );
154 test = jQuery("#qunit-fixture").filter("div");
155 equal( test.selector, "#qunit-fixture.filter(div)", "#qunit-fixture filter Selector" );
156 equal( test.context, document, "#qunit-fixture filter Context" );
158 test = jQuery("#qunit-fixture").not("div");
159 equal( test.selector, "#qunit-fixture.not(div)", "#qunit-fixture not Selector" );
160 equal( test.context, document, "#qunit-fixture not Context" );
162 test = jQuery("#qunit-fixture").filter("div").not("div");
163 equal( test.selector, "#qunit-fixture.filter(div).not(div)", "#qunit-fixture filter, not Selector" );
164 equal( test.context, document, "#qunit-fixture filter, not Context" );
166 test = jQuery("#qunit-fixture").filter("div").not("div").end();
167 equal( test.selector, "#qunit-fixture.filter(div)", "#qunit-fixture filter, not, end Selector" );
168 equal( test.context, document, "#qunit-fixture filter, not, end Context" );
170 test = jQuery("#qunit-fixture").parent("body");
171 equal( test.selector, "#qunit-fixture.parent(body)", "#qunit-fixture parent Selector" );
172 equal( test.context, document, "#qunit-fixture parent Context" );
174 test = jQuery("#qunit-fixture").eq(0);
175 equal( test.selector, "#qunit-fixture.slice(0,1)", "#qunit-fixture eq Selector" );
176 equal( test.context, document, "#qunit-fixture eq Context" );
180 jQuery(d).appendTo(jQuery(d)).selector,
181 jQuery(d).appendTo(d).selector,
182 "manipulation methods make same selector for jQuery objects"
186 test( "globalEval", function() {
190 jQuery.globalEval( "var globalEvalTest = true;" );
191 ok( window.globalEvalTest, "Test variable declarations are global" );
193 window.globalEvalTest = false;
195 jQuery.globalEval( "globalEvalTest = true;" );
196 ok( window.globalEvalTest, "Test variable assignments are global" );
198 window.globalEvalTest = false;
200 jQuery.globalEval( "this.globalEvalTest = true;" );
201 ok( window.globalEvalTest, "Test context (this) is the window object" );
203 window.globalEvalTest = undefined;
207 test("browser", function() {
210 jQuery.get("data/ua.txt", function(data){
211 var uas = data.split("\n");
212 expect( (uas.length - 1) * 2 );
214 jQuery.each(uas, function(){
215 var parts = this.split("\t");
217 var ua = jQuery.uaMatch( parts[2] );
218 equal( ua.browser, parts[0], "Checking browser for " + parts[2] );
219 equal( ua.version, parts[1], "Checking version string for " + parts[2] );
228 test("noConflict", function() {
233 equal( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
234 equal( jQuery, $$, "Make sure jQuery wasn't touched." );
235 equal( $, original$, "Make sure $ was reverted." );
239 equal( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" );
240 equal( jQuery, originaljQuery, "Make sure jQuery was reverted." );
241 equal( $, original$, "Make sure $ was reverted." );
242 ok( $$("#qunit-fixture").html("test"), "Make sure that jQuery still works." );
247 test("trim", function() {
250 var nbsp = String.fromCharCode(160);
252 equal( jQuery.trim("hello "), "hello", "trailing space" );
253 equal( jQuery.trim(" hello"), "hello", "leading space" );
254 equal( jQuery.trim(" hello "), "hello", "space on both sides" );
255 equal( jQuery.trim(" " + nbsp + "hello " + nbsp + " "), "hello", " " );
257 equal( jQuery.trim(), "", "Nothing in." );
258 equal( jQuery.trim( undefined ), "", "Undefined" );
259 equal( jQuery.trim( null ), "", "Null" );
260 equal( jQuery.trim( 5 ), "5", "Number" );
261 equal( jQuery.trim( false ), "false", "Boolean" );
264 test("type", function() {
267 equal( jQuery.type(null), "null", "null" );
268 equal( jQuery.type(undefined), "undefined", "undefined" );
269 equal( jQuery.type(true), "boolean", "Boolean" );
270 equal( jQuery.type(false), "boolean", "Boolean" );
271 equal( jQuery.type(Boolean(true)), "boolean", "Boolean" );
272 equal( jQuery.type(0), "number", "Number" );
273 equal( jQuery.type(1), "number", "Number" );
274 equal( jQuery.type(Number(1)), "number", "Number" );
275 equal( jQuery.type(""), "string", "String" );
276 equal( jQuery.type("a"), "string", "String" );
277 equal( jQuery.type(String("a")), "string", "String" );
278 equal( jQuery.type({}), "object", "Object" );
279 equal( jQuery.type(/foo/), "regexp", "RegExp" );
280 equal( jQuery.type(new RegExp("asdf")), "regexp", "RegExp" );
281 equal( jQuery.type([1]), "array", "Array" );
282 equal( jQuery.type(new Date()), "date", "Date" );
283 equal( jQuery.type(new Function("return;")), "function", "Function" );
284 equal( jQuery.type(function(){}), "function", "Function" );
285 equal( jQuery.type(window), "object", "Window" );
286 equal( jQuery.type(document), "object", "Document" );
287 equal( jQuery.type(document.body), "object", "Element" );
288 equal( jQuery.type(document.createTextNode("foo")), "object", "TextNode" );
289 equal( jQuery.type(document.getElementsByTagName("*")), "object", "NodeList" );
292 test("isPlainObject", function() {
297 // The use case that we want to match
298 ok(jQuery.isPlainObject({}), "{}");
300 // Not objects shouldn't be matched
301 ok(!jQuery.isPlainObject(""), "string");
302 ok(!jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number");
303 ok(!jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean");
304 ok(!jQuery.isPlainObject(null), "null");
305 ok(!jQuery.isPlainObject(undefined), "undefined");
307 // Arrays shouldn't be matched
308 ok(!jQuery.isPlainObject([]), "array");
310 // Instantiated objects shouldn't be matched
311 ok(!jQuery.isPlainObject(new Date), "new Date");
313 var fn = function(){};
315 // Functions shouldn't be matched
316 ok(!jQuery.isPlainObject(fn), "fn");
318 // Again, instantiated objects shouldn't be matched
319 ok(!jQuery.isPlainObject(new fn), "new fn (no methods)");
321 // Makes the function a little more realistic
322 // (and harder to detect, incidentally)
323 fn.prototype = {someMethod: function(){}};
325 // Again, instantiated objects shouldn't be matched
326 ok(!jQuery.isPlainObject(new fn), "new fn");
329 ok(!jQuery.isPlainObject(document.createElement("div")), "DOM Element");
332 ok(!jQuery.isPlainObject(window), "window");
335 jQuery.isPlainObject( window.location );
336 ok( true, "Does not throw exceptions on host objects");
338 ok( false, "Does not throw exceptions on host objects -- FAIL");
342 var iframe = document.createElement("iframe");
343 document.body.appendChild(iframe);
345 window.iframeDone = function(otherObject){
346 // Objects from other windows should be matched
347 ok(jQuery.isPlainObject(new otherObject), "new otherObject");
348 document.body.removeChild( iframe );
352 var doc = iframe.contentDocument || iframe.contentWindow.document;
354 doc.write("<body onload='window.parent.iframeDone(Object);'>");
357 document.body.removeChild( iframe );
359 ok(true, "new otherObject - iframes not supported");
364 test("isFunction", function() {
367 // Make sure that false values return false
368 ok( !jQuery.isFunction(), "No Value" );
369 ok( !jQuery.isFunction( null ), "null Value" );
370 ok( !jQuery.isFunction( undefined ), "undefined Value" );
371 ok( !jQuery.isFunction( "" ), "Empty String Value" );
372 ok( !jQuery.isFunction( 0 ), "0 Value" );
375 // Safari uses "(Internal Function)"
376 ok( jQuery.isFunction(String), "String Function("+String+")" );
377 ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
378 ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
379 ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
381 // When stringified, this could be misinterpreted
382 var mystr = "function";
383 ok( !jQuery.isFunction(mystr), "Function String" );
385 // When stringified, this could be misinterpreted
386 var myarr = [ "function" ];
387 ok( !jQuery.isFunction(myarr), "Function Array" );
389 // When stringified, this could be misinterpreted
390 var myfunction = { "function": "test" };
391 ok( !jQuery.isFunction(myfunction), "Function Object" );
393 // Make sure normal functions still work
394 var fn = function(){};
395 ok( jQuery.isFunction(fn), "Normal Function" );
397 var obj = document.createElement("object");
399 // Firefox says this is a function
400 ok( !jQuery.isFunction(obj), "Object Element" );
402 // IE says this is an object
403 // Since 1.3, this isn't supported (#2968)
404 //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
406 var nodes = document.body.childNodes;
408 // Safari says this is a function
409 ok( !jQuery.isFunction(nodes), "childNodes Property" );
411 var first = document.body.firstChild;
413 // Normal elements are reported ok everywhere
414 ok( !jQuery.isFunction(first), "A normal DOM Element" );
416 var input = document.createElement("input");
418 document.body.appendChild( input );
420 // IE says this is an object
421 // Since 1.3, this isn't supported (#2968)
422 //ok( jQuery.isFunction(input.focus), "A default function property" );
424 document.body.removeChild( input );
426 var a = document.createElement("a");
427 a.href = "some-function";
428 document.body.appendChild( a );
430 // This serializes with the word 'function' in it
431 ok( !jQuery.isFunction(a), "Anchor Element" );
433 document.body.removeChild( a );
435 // Recursive function calls have lengths and array-like properties
436 function callme(callback){
437 function fn(response){
441 ok( jQuery.isFunction(fn), "Recursive Function Call" );
443 fn({ some: "data" });
447 callme(function(){});
451 test( "isNumeric", function() {
454 var t = jQuery.isNumeric,
455 Traditionalists = function(n) {
457 this.toString = function(){
458 return String(this.value);
461 answer = new Traditionalists( "42" ),
462 rong = new Traditionalists( "Devo" );
464 ok( t("-10"), "Negative integer string");
465 ok( t("0"), "Zero string");
466 ok( t("5"), "Positive integer string");
467 ok( t(-16), "Negative integer number");
468 ok( t(0), "Zero integer number");
469 ok( t(32), "Positive integer number");
470 ok( t("040"), "Octal integer literal string");
471 ok( t(0144), "Octal integer literal");
472 ok( t("0xFF"), "Hexadecimal integer literal string");
473 ok( t(0xFFF), "Hexadecimal integer literal");
474 ok( t("-1.6"), "Negative floating point string");
475 ok( t("4.536"), "Positive floating point string");
476 ok( t(-2.6), "Negative floating point number");
477 ok( t(3.1415), "Positive floating point number");
478 ok( t(8e5), "Exponential notation");
479 ok( t("123e-2"), "Exponential notation string");
480 ok( t(answer), "Custom .toString returning number");
481 equal( t(""), false, "Empty string");
482 equal( t(" "), false, "Whitespace characters string");
483 equal( t("\t\t"), false, "Tab characters string");
484 equal( t("abcdefghijklm1234567890"), false, "Alphanumeric character string");
485 equal( t("xabcdefx"), false, "Non-numeric character string");
486 equal( t(true), false, "Boolean true literal");
487 equal( t(false), false, "Boolean false literal");
488 equal( t("bcfed5.2"), false, "Number with preceding non-numeric characters");
489 equal( t("7.2acdgs"), false, "Number with trailling non-numeric characters");
490 equal( t(undefined), false, "Undefined value");
491 equal( t(null), false, "Null value");
492 equal( t(NaN), false, "NaN value");
493 equal( t(Infinity), false, "Infinity primitive");
494 equal( t(Number.POSITIVE_INFINITY), false, "Positive Infinity");
495 equal( t(Number.NEGATIVE_INFINITY), false, "Negative Infinity");
496 equal( t(rong), false, "Custom .toString returning non-number");
497 equal( t({}), false, "Empty object");
498 equal( t(function(){} ), false, "Instance of a function");
499 equal( t( new Date ), false, "Instance of a Date");
500 equal( t(function(){} ), false, "Instance of a function");
503 test("isXMLDoc - HTML", function() {
506 ok( !jQuery.isXMLDoc( document ), "HTML document" );
507 ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
508 ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
510 var iframe = document.createElement("iframe");
511 document.body.appendChild( iframe );
514 var body = jQuery(iframe).contents()[0];
517 ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
519 ok( false, "Iframe body element exception" );
523 ok( true, "Iframe body element - iframe not working correctly" );
526 document.body.removeChild( iframe );
529 test("XSS via location.hash", function() {
533 jQuery._check9521 = function(x){
534 ok( x, "script called from #id-like selector with inline handler" );
535 jQuery("#check9521").remove();
536 delete jQuery._check9521;
540 // This throws an error because it's processed like an id
541 jQuery( '#<img id="check9521" src="no-such-.gif" onerror="jQuery._check9521(false)">' ).appendTo("#qunit-fixture");
543 jQuery._check9521(true);
548 test("isXMLDoc - XML", function() {
551 jQuery.get("data/dashboard.xml", function(xml) {
552 ok( jQuery.isXMLDoc( xml ), "XML document" );
553 ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" );
554 ok( jQuery.isXMLDoc( jQuery("tab", xml)[0] ), "XML Tab Element" );
560 test("isWindow", function() {
563 ok( jQuery.isWindow(window), "window" );
564 ok( !jQuery.isWindow(), "empty" );
565 ok( !jQuery.isWindow(null), "null" );
566 ok( !jQuery.isWindow(undefined), "undefined" );
567 ok( !jQuery.isWindow(document), "document" );
568 ok( !jQuery.isWindow(document.documentElement), "documentElement" );
569 ok( !jQuery.isWindow(""), "string" );
570 ok( !jQuery.isWindow(1), "number" );
571 ok( !jQuery.isWindow(true), "boolean" );
572 ok( !jQuery.isWindow({}), "object" );
574 // ok( !jQuery.isWindow({ setInterval: function(){} }), "fake window" );
575 ok( !jQuery.isWindow(/window/), "regexp" );
576 ok( !jQuery.isWindow(function(){}), "function" );
579 test("jQuery('html')", function() {
584 var s = jQuery("<script>jQuery.foo='test';</script>")[0];
585 ok( s, "Creating a script" );
586 ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
587 jQuery("body").append("<script>jQuery.foo='test';</script>");
588 ok( jQuery.foo, "Executing a scripts contents in the right context" );
590 // Test multi-line HTML
591 var div = jQuery("<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>")[0];
592 equal( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
593 equal( div.firstChild.nodeType, 3, "Text node." );
594 equal( div.lastChild.nodeType, 3, "Text node." );
595 equal( div.childNodes[1].nodeType, 1, "Paragraph." );
596 equal( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." );
599 ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
601 ok( !jQuery("<script/>")[0].parentNode, "Create a script" );
603 ok( jQuery("<input/>").attr("type", "hidden"), "Create an input and set the type." );
605 var j = jQuery("<span>hi</span> there <!-- mon ami -->");
606 ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
608 ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
610 ok( jQuery("<div></div>")[0], "Create a div with closing tag." );
611 ok( jQuery("<table></table>")[0], "Create a table with closing tag." );
613 // Test very large html string #7990
615 var li = "<li>very large html string</li>";
617 for ( i = 0; i < 50000; i += 1 ) {
621 html = jQuery(html.join(""))[0];
622 equal( html.nodeName.toUpperCase(), "UL");
623 equal( html.firstChild.nodeName.toUpperCase(), "LI");
624 equal( html.childNodes.length, 50000 );
627 test("jQuery('html', context)", function() {
630 var $div = jQuery("<div/>")[0];
631 var $span = jQuery("<span/>", $div);
632 equal($span.length, 1, "Verify a span created with a div context works, #1763");
636 test("jQuery(selector, xml).text(str) - Loaded via XML document", function() {
639 jQuery.get("data/dashboard.xml", function(xml) {
640 // tests for #1419 where IE was a problem
641 var tab = jQuery("tab", xml).eq(0);
642 equal( tab.text(), "blabla", "Verify initial text correct" );
644 equal( tab.text(), "newtext", "Verify new text correct" );
650 test("end()", function() {
652 equal( "Yahoo", jQuery("#yahoo").parent().end().text(), "Check for end" );
653 ok( jQuery("#yahoo").end(), "Check for end with nothing to end" );
655 var x = jQuery("#yahoo");
657 equal( "Yahoo", jQuery("#yahoo").text(), "Check for non-destructive behaviour" );
660 test("length", function() {
662 equal( jQuery("#qunit-fixture p").length, 6, "Get Number of Elements Found" );
665 test("size()", function() {
667 equal( jQuery("#qunit-fixture p").size(), 6, "Get Number of Elements Found" );
670 test("get()", function() {
672 deepEqual( jQuery("#qunit-fixture p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
675 test("toArray()", function() {
677 deepEqual( jQuery("#qunit-fixture p").toArray(),
678 q("firstp","ap","sndp","en","sap","first"),
679 "Convert jQuery object to an Array" )
682 test("inArray()", function() {
686 p: q("firstp", "sap", "ap", "first"),
687 em: q("siblingnext", "siblingfirst"),
688 div: q("qunit-testrunner-toolbar", "nothiddendiv", "nothiddendivchild", "foo"),
689 a: q("mark", "groups", "google", "simon1"),
693 p: { elem: jQuery("#ap")[0], index: 2 },
694 em: { elem: jQuery("#siblingfirst")[0], index: 1 },
695 div: { elem: jQuery("#nothiddendiv")[0], index: 1 },
696 a: { elem: jQuery("#simon1")[0], index: 3 }
699 p: jQuery("#liveSpan1")[0],
700 em: jQuery("#nothiddendiv")[0],
704 jQuery.each( tests, function( key, obj ) {
705 equal( jQuery.inArray( obj.elem, selections[ key ] ), obj.index, "elem is in the array of selections of its tag" );
706 // Third argument (fromIndex)
707 equal( !!~jQuery.inArray( obj.elem, selections[ key ], 5 ), false, "elem is NOT in the array of selections given a starting index greater than its position" );
708 equal( !!~jQuery.inArray( obj.elem, selections[ key ], 1 ), true, "elem is in the array of selections given a starting index less than or equal to its position" );
709 equal( !!~jQuery.inArray( obj.elem, selections[ key ], -3 ), true, "elem is in the array of selections given a negative index" );
712 jQuery.each( falseTests, function( key, elem ) {
713 equal( !!~jQuery.inArray( elem, selections[ key ] ), false, "elem is NOT in the array of selections" );
718 test("get(Number)", function() {
720 equal( jQuery("#qunit-fixture p").get(0), document.getElementById("firstp"), "Get A Single Element" );
721 strictEqual( jQuery("#firstp").get(1), undefined, "Try get with index larger elements count" );
724 test("get(-Number)",function() {
726 equal( jQuery("p").get(-1), document.getElementById("first"), "Get a single element with negative index" );
727 strictEqual( jQuery("#firstp").get(-2), undefined, "Try get with index negative index larger then elements count" );
730 test("each(Function)", function() {
732 var div = jQuery("div");
733 div.each(function(){this.foo = "zoo";});
735 for ( var i = 0; i < div.size(); i++ ) {
736 if ( div.get(i).foo != "zoo" ) pass = false;
738 ok( pass, "Execute a function, Relative" );
741 test("slice()", function() {
744 var $links = jQuery("#ap a");
746 deepEqual( $links.slice(1,2).get(), q("groups"), "slice(1,2)" );
747 deepEqual( $links.slice(1).get(), q("groups", "anchor1", "mark"), "slice(1)" );
748 deepEqual( $links.slice(0,3).get(), q("google", "groups", "anchor1"), "slice(0,3)" );
749 deepEqual( $links.slice(-1).get(), q("mark"), "slice(-1)" );
751 deepEqual( $links.eq(1).get(), q("groups"), "eq(1)" );
752 deepEqual( $links.eq("2").get(), q("anchor1"), "eq('2')" );
753 deepEqual( $links.eq(-1).get(), q("mark"), "eq(-1)" );
756 test("first()/last()", function() {
759 var $links = jQuery("#ap a"), $none = jQuery("asdf");
761 deepEqual( $links.first().get(), q("google"), "first()" );
762 deepEqual( $links.last().get(), q("mark"), "last()" );
764 deepEqual( $none.first().get(), [], "first() none" );
765 deepEqual( $none.last().get(), [], "last() none" );
768 test("map()", function() {
772 jQuery("#ap").map(function(){
773 return jQuery(this).find("a").get();
775 q("google", "groups", "anchor1", "mark"),
780 jQuery("#ap > a").map(function(){
781 return this.parentNode;
788 var keys = jQuery.map( {a:1,b:2}, function( v, k ){
791 equal( keys.join(""), "ab", "Map the keys from a hash to an array" );
793 var values = jQuery.map( {a:1,b:2}, function( v, k ){
796 equal( values.join(""), "12", "Map the values from a hash to an array" );
798 // object with length prop
799 var values = jQuery.map( {a:1,b:2, length:3}, function( v, k ){
802 equal( values.join(""), "123", "Map the values from a hash with a length property to an array" );
804 var scripts = document.getElementsByTagName("script");
805 var mapped = jQuery.map( scripts, function( v, k ){
808 equal( mapped.length, scripts.length, "Map an array(-like) to a hash" );
810 var nonsense = document.getElementsByTagName("asdf");
811 var mapped = jQuery.map( nonsense, function( v, k ){
814 equal( mapped.length, nonsense.length, "Map an empty array(-like) to a hash" );
816 var flat = jQuery.map( Array(4), function( v, k ){
817 return k % 2 ? k : [k,k,k];//try mixing array and regular returns
819 equal( flat.join(""), "00012223", "try the new flatten technique(#2616)" );
822 test("jQuery.merge()", function() {
825 var parse = jQuery.merge;
827 deepEqual( parse([],[]), [], "Empty arrays" );
829 deepEqual( parse([1],[2]), [1,2], "Basic" );
830 deepEqual( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
832 deepEqual( parse([1,2],[]), [1,2], "Second empty" );
833 deepEqual( parse([],[1,2]), [1,2], "First empty" );
835 // Fixed at [5998], #3641
836 deepEqual( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
838 // After fixing #5527
839 deepEqual( parse([], [null, undefined]), [null, undefined], "Second array including null and undefined values");
840 deepEqual( parse({length:0}, [1,2]), {length:2, 0:1, 1:2}, "First array like");
843 test("jQuery.extend(Object, Object)", function() {
846 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
847 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
848 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
849 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
850 deep1 = { foo: { bar: true } },
851 deep1copy = { foo: { bar: true } },
852 deep2 = { foo: { baz: true }, foo2: document },
853 deep2copy = { foo: { baz: true }, foo2: document },
854 deepmerged = { foo: { bar: true, baz: true }, foo2: document },
856 nestedarray = { arr: arr };
858 jQuery.extend(settings, options);
859 deepEqual( settings, merged, "Check if extended: settings must be extended" );
860 deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
862 jQuery.extend(settings, null, options);
863 deepEqual( settings, merged, "Check if extended: settings must be extended" );
864 deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
866 jQuery.extend(true, deep1, deep2);
867 deepEqual( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
868 deepEqual( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
869 equal( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
871 ok( jQuery.extend(true, {}, nestedarray).arr !== arr, "Deep extend of object must clone child array" );
874 ok( jQuery.isArray( jQuery.extend(true, { arr: {} }, nestedarray).arr ), "Cloned array heve to be an Array" );
875 ok( jQuery.isPlainObject( jQuery.extend(true, { arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" );
878 var optionsWithLength = { foo: { length: -1 } };
879 jQuery.extend(true, empty, optionsWithLength);
880 deepEqual( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
883 var optionsWithDate = { foo: { date: new Date } };
884 jQuery.extend(true, empty, optionsWithDate);
885 deepEqual( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
887 var myKlass = function() {};
888 var customObject = new myKlass();
889 var optionsWithCustomObject = { foo: { date: customObject } };
891 jQuery.extend(true, empty, optionsWithCustomObject);
892 ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
894 // Makes the class a little more realistic
895 myKlass.prototype = { someMethod: function(){} };
897 jQuery.extend(true, empty, optionsWithCustomObject);
898 ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" );
900 var ret = jQuery.extend(true, { foo: 4 }, { foo: new Number(5) } );
901 ok( ret.foo == 5, "Wrapped numbers copy correctly" );
904 nullUndef = jQuery.extend({}, options, { xnumber2: null });
905 ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
907 nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
908 ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
910 nullUndef = jQuery.extend({}, options, { xnumber0: null });
911 ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
914 var recursive = { foo:target, bar:5 };
915 jQuery.extend(true, target, recursive);
916 deepEqual( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
918 var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
919 equal( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
921 var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
922 ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
924 var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
925 ok( typeof ret.foo !== "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" );
927 var obj = { foo:null };
928 jQuery.extend(true, obj, { foo:"notnull" } );
929 equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );
932 jQuery.extend(func, { key: "value" } );
933 equal( func.key, "value", "Verify a function can be extended" );
935 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
936 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
937 options1 = { xnumber2: 1, xstring2: "x" },
938 options1Copy = { xnumber2: 1, xstring2: "x" },
939 options2 = { xstring2: "xx", xxx: "newstringx" },
940 options2Copy = { xstring2: "xx", xxx: "newstringx" },
941 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
943 var settings = jQuery.extend({}, defaults, options1, options2);
944 deepEqual( settings, merged2, "Check if extended: settings must be extended" );
945 deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
946 deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" );
947 deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" );
950 test("jQuery.each(Object,Function)", function() {
952 jQuery.each( [0,1,2], function(i, n){
953 equal( i, n, "Check array iteration" );
956 jQuery.each( [5,6,7], function(i, n){
957 equal( i, n - 5, "Check array iteration" );
960 jQuery.each( { name: "name", lang: "lang" }, function(i, n){
961 equal( i, n, "Check object iteration" );
965 jQuery.each([1,2,3], function(i,v){ total += v; });
966 equal( total, 6, "Looping over an array" );
968 jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
969 equal( total, 3, "Looping over an array, with break" );
971 jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
972 equal( total, 6, "Looping over an object" );
974 jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
975 equal( total, 3, "Looping over an object, with break" );
977 var f = function(){};
979 jQuery.each(f, function(i){
982 equal( "baz", f.foo, "Loop over a function" );
984 var stylesheet_count = 0;
985 jQuery.each(document.styleSheets, function(i){
988 equal(stylesheet_count, 2, "should not throw an error in IE while looping over document.styleSheets and return proper amount");
992 test("jQuery.makeArray", function(){
995 equal( jQuery.makeArray(jQuery("html>*"))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
997 equal( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
999 equal( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
1001 equal( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
1003 equal( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
1005 equal( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
1007 equal( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
1009 equal( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
1011 equal( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
1013 equal( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
1015 ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );
1017 // function, is tricky as it has length
1018 equal( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
1020 //window, also has length
1021 equal( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
1023 equal( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
1025 ok( jQuery.makeArray(document.getElementById("form")).length >= 13, "Pass makeArray a form (treat as elements)" );
1028 deepEqual( jQuery.makeArray({length: "0"}), [], "Make sure object is coerced properly.");
1029 deepEqual( jQuery.makeArray({length: "5"}), [], "Make sure object is coerced properly.");
1032 test("jQuery.inArray", function(){
1035 equal( jQuery.inArray( 0, false ), -1 , "Search in 'false' as array returns -1 and doesn't throw exception" );
1037 equal( jQuery.inArray( 0, null ), -1 , "Search in 'null' as array returns -1 and doesn't throw exception" );
1039 equal( jQuery.inArray( 0, undefined ), -1 , "Search in 'undefined' as array returns -1 and doesn't throw exception" );
1042 test("jQuery.isEmptyObject", function(){
1045 equal(true, jQuery.isEmptyObject({}), "isEmptyObject on empty object literal" );
1046 equal(false, jQuery.isEmptyObject({a:1}), "isEmptyObject on non-empty object literal" );
1048 // What about this ?
1049 // equal(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
1052 test("jQuery.proxy", function(){
1055 var test = function(){ equal( this, thisObject, "Make sure that scope is set properly." ); };
1056 var thisObject = { foo: "bar", method: test };
1058 // Make sure normal works
1059 test.call( thisObject );
1062 jQuery.proxy( test, thisObject )();
1064 // Another take on it
1065 jQuery.proxy( thisObject, "method" )();
1067 // Make sure it doesn't freak out
1068 equal( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
1070 // Partial application
1071 var test2 = function( a ){ equal( a, "pre-applied", "Ensure arguments can be pre-applied." ); };
1072 jQuery.proxy( test2, null, "pre-applied" )();
1074 // Partial application w/ normal arguments
1075 var test3 = function( a, b ){ equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." ); };
1076 jQuery.proxy( test3, null, "pre-applied" )( "normal" );
1079 var test4 = { meth: function( a ){ equal( a, "boom", "Ensure old syntax works." ); } };
1080 jQuery.proxy( test4, "meth" )( "boom" );
1083 test("jQuery.parseJSON", function(){
1086 equal( jQuery.parseJSON(), null, "Nothing in, null out." );
1087 equal( jQuery.parseJSON( null ), null, "Nothing in, null out." );
1088 equal( jQuery.parseJSON( "" ), null, "Nothing in, null out." );
1090 deepEqual( jQuery.parseJSON("{}"), {}, "Plain object parsing." );
1091 deepEqual( jQuery.parseJSON("{\"test\":1}"), {"test":1}, "Plain object parsing." );
1093 deepEqual( jQuery.parseJSON("\n{\"test\":1}"), {"test":1}, "Make sure leading whitespaces are handled." );
1096 jQuery.parseJSON("{a:1}");
1097 ok( false, "Test malformed JSON string." );
1099 ok( true, "Test malformed JSON string." );
1103 jQuery.parseJSON("{'a':1}");
1104 ok( false, "Test malformed JSON string." );
1106 ok( true, "Test malformed JSON string." );
1110 test("jQuery.parseXML", 4, function(){
1113 xml = jQuery.parseXML( "<p>A <b>well-formed</b> xml string</p>" );
1114 tmp = xml.getElementsByTagName( "p" )[ 0 ];
1115 ok( !!tmp, "<p> present in document" );
1116 tmp = tmp.getElementsByTagName( "b" )[ 0 ];
1117 ok( !!tmp, "<b> present in document" );
1118 strictEqual( tmp.childNodes[ 0 ].nodeValue, "well-formed", "<b> text is as expected" );
1120 strictEqual( e, undefined, "unexpected error" );
1123 xml = jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
1124 ok( false, "invalid xml not detected" );
1126 strictEqual( e.message, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "invalid xml detected" );
1130 test("jQuery.sub() - Static Methods", function(){
1132 var Subclass = jQuery.sub();
1134 topLevelMethod: function() {return this.debug;},
1139 setup: function(config) {
1140 this.extend(true, this.config, config);
1143 Subclass.fn.extend({subClassMethod: function() { return this;}});
1145 //Test Simple Subclass
1146 ok(Subclass.topLevelMethod() === false, "Subclass.topLevelMethod thought debug was true");
1147 ok(Subclass.config.locale == "en_US", Subclass.config.locale + " is wrong!");
1148 deepEqual(Subclass.config.test, undefined, "Subclass.config.test is set incorrectly");
1149 equal(jQuery.ajax, Subclass.ajax, "The subclass failed to get all top level methods");
1151 //Create a SubSubclass
1152 var SubSubclass = Subclass.sub();
1154 //Make Sure the SubSubclass inherited properly
1155 ok(SubSubclass.topLevelMethod() === false, "SubSubclass.topLevelMethod thought debug was true");
1156 ok(SubSubclass.config.locale == "en_US", SubSubclass.config.locale + " is wrong!");
1157 deepEqual(SubSubclass.config.test, undefined, "SubSubclass.config.test is set incorrectly");
1158 equal(jQuery.ajax, SubSubclass.ajax, "The subsubclass failed to get all top level methods");
1160 //Modify The Subclass and test the Modifications
1161 SubSubclass.fn.extend({subSubClassMethod: function() { return this;}});
1162 SubSubclass.setup({locale: "es_MX", test: "worked"});
1163 SubSubclass.debug = true;
1164 SubSubclass.ajax = function() {return false;};
1165 ok(SubSubclass.topLevelMethod(), "SubSubclass.topLevelMethod thought debug was false");
1166 deepEqual(SubSubclass(document).subClassMethod, Subclass.fn.subClassMethod, "Methods Differ!");
1167 ok(SubSubclass.config.locale == "es_MX", SubSubclass.config.locale + " is wrong!");
1168 ok(SubSubclass.config.test == "worked", "SubSubclass.config.test is set incorrectly");
1169 notEqual(jQuery.ajax, SubSubclass.ajax, "The subsubclass failed to get all top level methods");
1171 //This shows that the modifications to the SubSubClass did not bubble back up to it's superclass
1172 ok(Subclass.topLevelMethod() === false, "Subclass.topLevelMethod thought debug was true");
1173 ok(Subclass.config.locale == "en_US", Subclass.config.locale + " is wrong!");
1174 deepEqual(Subclass.config.test, undefined, "Subclass.config.test is set incorrectly");
1175 deepEqual(Subclass(document).subSubClassMethod, undefined, "subSubClassMethod set incorrectly");
1176 equal(jQuery.ajax, Subclass.ajax, "The subclass failed to get all top level methods");
1179 test("jQuery.sub() - .fn Methods", function(){
1182 var Subclass = jQuery.sub(),
1183 SubclassSubclass = Subclass.sub(),
1184 jQueryDocument = jQuery(document),
1185 selectors, contexts, methods, method, arg, description;
1187 jQueryDocument.toString = function(){ return "jQueryDocument"; };
1189 Subclass.fn.subclassMethod = function(){};
1190 SubclassSubclass.fn.subclassSubclassMethod = function(){};
1198 methods = [ // all methods that return a new jQuery instance
1204 ["filter", document],
1208 contexts = [undefined, document, jQueryDocument];
1210 jQuery.each(selectors, function(i, selector){
1212 jQuery.each(methods, function(){
1216 jQuery.each(contexts, function(i, context){
1218 description = "(\""+selector+"\", "+context+")."+method+"("+(arg||"")+")";
1221 jQuery(selector, context)[method](arg).subclassMethod, undefined,
1222 "jQuery"+description+" doesn't have Subclass methods"
1225 jQuery(selector, context)[method](arg).subclassSubclassMethod, undefined,
1226 "jQuery"+description+" doesn't have SubclassSubclass methods"
1229 Subclass(selector, context)[method](arg).subclassMethod, Subclass.fn.subclassMethod,
1230 "Subclass"+description+" has Subclass methods"
1233 Subclass(selector, context)[method](arg).subclassSubclassMethod, undefined,
1234 "Subclass"+description+" doesn't have SubclassSubclass methods"
1237 SubclassSubclass(selector, context)[method](arg).subclassMethod, Subclass.fn.subclassMethod,
1238 "SubclassSubclass"+description+" has Subclass methods"
1241 SubclassSubclass(selector, context)[method](arg).subclassSubclassMethod, SubclassSubclass.fn.subclassSubclassMethod,
1242 "SubclassSubclass"+description+" has SubclassSubclass methods"
1251 test("jQuery.camelCase()", function() {
1254 "foo-bar": "fooBar",
1255 "foo-bar-baz": "fooBarBaz",
1256 "girl-u-want": "girlUWant",
1257 "the-4th-dimension": "the4thDimension",
1258 "-o-tannenbaum": "OTannenbaum",
1259 "-moz-illa": "MozIlla",
1260 "-ms-take": "msTake"
1265 jQuery.each( tests, function( key, val ) {
1266 equal( jQuery.camelCase( key ), val, "Converts: " + key + " => " + val );