1 QUnit.module( "core", { teardown: moduleTeardown } );
3 QUnit.test( "Basic requirements", function( assert ) {
5 assert.ok( Array.prototype.push, "Array.push()" );
6 assert.ok( Function.prototype.apply, "Function.apply()" );
7 assert.ok( document.getElementById, "getElementById" );
8 assert.ok( document.getElementsByTagName, "getElementsByTagName" );
9 assert.ok( RegExp, "RegExp" );
10 assert.ok( jQuery, "jQuery" );
14 QUnit.test( "jQuery()", function( assert ) {
17 obj = jQuery( "div" ),
18 code = jQuery( "<code/>" ),
19 img = jQuery( "<img/>" ),
20 div = jQuery( "<div/><hr/><code/><b/>" ),
29 // The $(html, props) signature can stealth-call any $.fn method, check for a
30 // few here but beware of modular builds where these methods may be excluded.
31 if ( jQuery.fn.click ) {
33 attrObj[ "click" ] = function() { assert.ok( exec, "Click executed." ); };
35 if ( jQuery.fn.width ) {
37 attrObj[ "width" ] = 10;
39 if ( jQuery.fn.offset ) {
41 attrObj[ "offset" ] = { "top": 1, "left": 1 };
43 if ( jQuery.fn.css ) {
45 attrObj[ "css" ] = { "paddingLeft": 1, "paddingRight": 1 };
47 if ( jQuery.fn.attr ) {
49 attrObj.attr = { "desired": "very" };
52 assert.expect( expected );
54 // Basic constructor's behavior
55 assert.equal( jQuery().length, 0, "jQuery() === jQuery([])" );
56 assert.equal( jQuery( undefined ).length, 0, "jQuery(undefined) === jQuery([])" );
57 assert.equal( jQuery( null ).length, 0, "jQuery(null) === jQuery([])" );
58 assert.equal( jQuery( "" ).length, 0, "jQuery('') === jQuery([])" );
59 assert.deepEqual( jQuery( obj ).get(), obj.get(), "jQuery(jQueryObj) == jQueryObj" );
61 // Invalid #id goes to Sizzle which will throw an error (gh-1682)
65 assert.ok( true, "Threw an error on #id with no id" );
68 // can actually yield more than one, when iframes are included, the window is an array as well
69 assert.equal( jQuery( window ).length, 1, "Correct number of elements generated for jQuery(window)" );
72 // disabled since this test was doing nothing. i tried to fix it but i'm not sure
73 // what the expected behavior should even be. FF returns "\n" for the text node
74 // make sure this is handled
75 var crlfContainer = jQuery('<p>\r\n</p>');
76 var x = crlfContainer.contents().get(0).nodeValue;
77 assert.equal( x, what???, "Check for \\r and \\n in jQuery()" );
80 /* // Disabled until we add this functionality in
83 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
87 assert.ok( pass, "jQuery('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
89 assert.equal( code.length, 1, "Correct number of elements generated for code" );
90 assert.equal( code.parent().length, 0, "Make sure that the generated HTML has no parent." );
92 assert.equal( img.length, 1, "Correct number of elements generated for img" );
93 assert.equal( img.parent().length, 0, "Make sure that the generated HTML has no parent." );
95 assert.equal( div.length, 4, "Correct number of elements generated for div hr code b" );
96 assert.equal( div.parent().length, 0, "Make sure that the generated HTML has no parent." );
98 assert.equal( jQuery( [ 1,2,3 ] ).get( 1 ), 2, "Test passing an array to the factory" );
100 assert.equal( jQuery( document.body ).get( 0 ), jQuery( "body" ).get( 0 ), "Test passing an html node to the factory" );
102 elem = jQuery( " <em>hello</em>" )[ 0 ];
103 assert.equal( elem.nodeName.toLowerCase(), "em", "leading space" );
105 elem = jQuery( "\n\n<em>world</em>" )[ 0 ];
106 assert.equal( elem.nodeName.toLowerCase(), "em", "leading newlines" );
108 elem = jQuery( "<div/>", attrObj );
110 if ( jQuery.fn.width ) {
111 assert.equal( elem[ 0 ].style.width, "10px", "jQuery() quick setter width" );
114 if ( jQuery.fn.offset ) {
115 assert.equal( elem[ 0 ].style.top, "1px", "jQuery() quick setter offset" );
118 if ( jQuery.fn.css ) {
119 assert.equal( elem[ 0 ].style.paddingLeft, "1px", "jQuery quick setter css" );
120 assert.equal( elem[ 0 ].style.paddingRight, "1px", "jQuery quick setter css" );
123 if ( jQuery.fn.attr ) {
124 assert.equal( elem[ 0 ].getAttribute( "desired" ), "very", "jQuery quick setter attr" );
127 assert.equal( elem[ 0 ].childNodes.length, 1, "jQuery quick setter text" );
128 assert.equal( elem[ 0 ].firstChild.nodeValue, "test", "jQuery quick setter text" );
129 assert.equal( elem[ 0 ].className, "test2", "jQuery() quick setter class" );
130 assert.equal( elem[ 0 ].id, "test3", "jQuery() quick setter id" );
133 elem.trigger( "click" );
135 // manually clean up detached elements
138 for ( i = 0; i < 3; ++i ) {
139 elem = jQuery( "<input type='text' value='TEST' />" );
141 assert.equal( elem[ 0 ].defaultValue, "TEST", "Ensure cached nodes are cloned properly (Bug #6655)" );
143 elem = jQuery( "<input type='hidden'>", {} );
144 assert.strictEqual( elem[ 0 ].ownerDocument, document,
145 "Empty attributes object is not interpreted as a document (trac-8950)" );
148 QUnit[ jQuery.find.compile ? "test" : "skip" ]( "jQuery(selector, context)", function( assert ) {
150 assert.deepEqual( jQuery( "div p", "#qunit-fixture" ).get(), q( "sndp", "en", "sap" ), "Basic selector with string as context" );
151 assert.deepEqual( jQuery( "div p", q( "qunit-fixture" )[ 0 ] ).get(), q( "sndp", "en", "sap" ), "Basic selector with element as context" );
152 assert.deepEqual( jQuery( "div p", jQuery( "#qunit-fixture" ) ).get(), q( "sndp", "en", "sap" ), "Basic selector with jQuery object as context" );
155 QUnit.test( "globalEval", function( assert ) {
157 Globals.register( "globalEvalTest" );
159 jQuery.globalEval( "globalEvalTest = 1;" );
160 assert.equal( window.globalEvalTest, 1, "Test variable assignments are global" );
162 jQuery.globalEval( "var globalEvalTest = 2;" );
163 assert.equal( window.globalEvalTest, 2, "Test variable declarations are global" );
165 jQuery.globalEval( "this.globalEvalTest = 3;" );
166 assert.equal( window.globalEvalTest, 3, "Test context (this) is the window object" );
169 QUnit.test( "globalEval with 'use strict'", function( assert ) {
171 Globals.register( "strictEvalTest" );
173 jQuery.globalEval( "'use strict'; var strictEvalTest = 1;" );
174 assert.equal( window.strictEvalTest, 1, "Test variable declarations are global (strict mode)" );
177 QUnit.test( "globalEval execution after script injection (#7862)", function( assert ) {
181 script = document.createElement( "script" );
183 script.src = "data/longLoadScript.php?sleep=2";
186 document.body.appendChild( script );
188 jQuery.globalEval( "var strictEvalTest = " + jQuery.now() + ";" );
189 assert.ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
192 // This is not run in AMD mode
193 if ( jQuery.noConflict ) {
194 QUnit.test( "noConflict", function( assert ) {
199 assert.strictEqual( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
200 assert.strictEqual( window[ "jQuery" ], $$, "Make sure jQuery wasn't touched." );
201 assert.strictEqual( window[ "$" ], original$, "Make sure $ was reverted." );
205 assert.strictEqual( jQuery.noConflict( true ), $$, "noConflict returned the jQuery object" );
206 assert.strictEqual( window[ "jQuery" ], originaljQuery, "Make sure jQuery was reverted." );
207 assert.strictEqual( window[ "$" ], original$, "Make sure $ was reverted." );
208 assert.ok( $$().pushStack( [] ), "Make sure that jQuery still works." );
210 window[ "jQuery" ] = jQuery = $$;
214 QUnit.test( "trim", function( assert ) {
217 var nbsp = String.fromCharCode( 160 );
219 assert.equal( jQuery.trim( "hello " ), "hello", "trailing space" );
220 assert.equal( jQuery.trim( " hello" ), "hello", "leading space" );
221 assert.equal( jQuery.trim( " hello " ), "hello", "space on both sides" );
222 assert.equal( jQuery.trim( " " + nbsp + "hello " + nbsp + " " ), "hello", " " );
224 assert.equal( jQuery.trim(), "", "Nothing in." );
225 assert.equal( jQuery.trim( undefined ), "", "Undefined" );
226 assert.equal( jQuery.trim( null ), "", "Null" );
227 assert.equal( jQuery.trim( 5 ), "5", "Number" );
228 assert.equal( jQuery.trim( false ), "false", "Boolean" );
230 assert.equal( jQuery.trim( " " ), "", "space should be trimmed" );
231 assert.equal( jQuery.trim( "ipad\xA0" ), "ipad", "nbsp should be trimmed" );
232 assert.equal( jQuery.trim( "\uFEFF" ), "", "zwsp should be trimmed" );
233 assert.equal( jQuery.trim( "\uFEFF \xA0! | \uFEFF" ), "! |", "leading/trailing should be trimmed" );
236 QUnit.test( "type", function( assert ) {
239 assert.equal( jQuery.type( null ), "null", "null" );
240 assert.equal( jQuery.type( undefined ), "undefined", "undefined" );
241 assert.equal( jQuery.type( true ), "boolean", "Boolean" );
242 assert.equal( jQuery.type( false ), "boolean", "Boolean" );
243 assert.equal( jQuery.type( Boolean( true ) ), "boolean", "Boolean" );
244 assert.equal( jQuery.type( 0 ), "number", "Number" );
245 assert.equal( jQuery.type( 1 ), "number", "Number" );
246 assert.equal( jQuery.type( Number( 1 ) ), "number", "Number" );
247 assert.equal( jQuery.type( "" ), "string", "String" );
248 assert.equal( jQuery.type( "a" ), "string", "String" );
249 assert.equal( jQuery.type( String( "a" ) ), "string", "String" );
250 assert.equal( jQuery.type( {} ), "object", "Object" );
251 assert.equal( jQuery.type( /foo/ ), "regexp", "RegExp" );
252 assert.equal( jQuery.type( new RegExp( "asdf" ) ), "regexp", "RegExp" );
253 assert.equal( jQuery.type( [ 1 ] ), "array", "Array" );
254 assert.equal( jQuery.type( new Date() ), "date", "Date" );
255 assert.equal( jQuery.type( new Function( "return;" ) ), "function", "Function" );
256 assert.equal( jQuery.type( function() {} ), "function", "Function" );
257 assert.equal( jQuery.type( new Error() ), "error", "Error" );
258 assert.equal( jQuery.type( window ), "object", "Window" );
259 assert.equal( jQuery.type( document ), "object", "Document" );
260 assert.equal( jQuery.type( document.body ), "object", "Element" );
261 assert.equal( jQuery.type( document.createTextNode( "foo" ) ), "object", "TextNode" );
262 assert.equal( jQuery.type( document.getElementsByTagName( "*" ) ), "object", "NodeList" );
264 // Avoid Lint complaints
265 var MyString = String,
269 assert.equal( jQuery.type( new MyBoolean( true ) ), "boolean", "Boolean" );
270 assert.equal( jQuery.type( new MyNumber( 1 ) ), "number", "Number" );
271 assert.equal( jQuery.type( new MyString( "a" ) ), "string", "String" );
272 assert.equal( jQuery.type( new MyObject() ), "object", "Object" );
275 QUnit.test( "type for `Symbol`", function( assert ) {
276 // Prevent reference errors
277 if( typeof Symbol !== "function" ) {
284 assert.equal( jQuery.type( Symbol() ), "symbol", "Symbol" );
285 assert.equal( jQuery.type( Object( Symbol() ) ), "symbol", "Symbol" );
288 QUnit.asyncTest( "isPlainObject", function( assert ) {
291 var pass, iframe, doc, parentObj, childObj, deep,
294 // The use case that we want to match
295 assert.ok( jQuery.isPlainObject( {} ), "{}" );
296 assert.ok( jQuery.isPlainObject( new window.Object() ), "new Object" );
298 parentObj = { foo: "bar" };
299 childObj = Object.create( parentObj );
301 assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.create({}))" );
302 childObj.bar = "foo";
303 assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.create({}))" );
305 // Not objects shouldn't be matched
306 assert.ok( !jQuery.isPlainObject( "" ), "string" );
307 assert.ok( !jQuery.isPlainObject( 0 ) && !jQuery.isPlainObject( 1 ), "number" );
308 assert.ok( !jQuery.isPlainObject( true ) && !jQuery.isPlainObject( false ), "boolean" );
309 assert.ok( !jQuery.isPlainObject( null ), "null" );
310 assert.ok( !jQuery.isPlainObject( undefined ), "undefined" );
312 // Arrays shouldn't be matched
313 assert.ok( !jQuery.isPlainObject( [] ), "array" );
315 // Instantiated objects shouldn't be matched
316 assert.ok( !jQuery.isPlainObject( new Date() ), "new Date" );
318 // Functions shouldn't be matched
319 assert.ok( !jQuery.isPlainObject( fn ), "fn" );
321 // Again, instantiated objects shouldn't be matched
322 assert.ok( !jQuery.isPlainObject( new fn() ), "new fn (no methods)" );
324 // Makes the function a little more realistic
325 // (and harder to detect, incidentally)
326 fn.prototype[ "someMethod" ] = function() {};
328 // Again, instantiated objects shouldn't be matched
329 assert.ok( !jQuery.isPlainObject( new fn() ), "new fn" );
332 deep = { "foo": { "baz": true }, "foo2": document };
333 assert.ok( jQuery.isPlainObject( deep ), "Object with objects is still plain" );
336 assert.ok( !jQuery.isPlainObject( document.createElement( "div" ) ), "DOM Element" );
339 assert.ok( !jQuery.isPlainObject( window ), "window" );
343 jQuery.isPlainObject( window.location );
346 assert.ok( pass, "Does not throw exceptions on host objects" );
348 // Objects from other windows should be matched
349 Globals.register( "iframeDone" );
350 window.iframeDone = function( otherObject, detail ) {
351 window.iframeDone = undefined;
352 iframe.parentNode.removeChild( iframe );
353 assert.ok( jQuery.isPlainObject( new otherObject() ), "new otherObject" + ( detail ? " - " + detail : "" ) );
358 iframe = jQuery( "#qunit-fixture" )[ 0 ].appendChild( document.createElement( "iframe" ) );
359 doc = iframe.contentDocument || iframe.contentWindow.document;
361 doc.write( "<body onload='window.parent.iframeDone(Object);'>" );
364 window.iframeDone( Object, "iframes not supported" );
369 QUnit[ typeof Symbol === "function" ? "test" : "skip" ]( "isPlainObject(Symbol)", function( assert ) {
372 assert.equal( jQuery.isPlainObject( Symbol() ), false, "Symbol" );
373 assert.equal( jQuery.isPlainObject( Object( Symbol() ) ), false, "Symbol inside an object" );
377 QUnit.test( "isFunction", function( assert ) {
380 var mystr, myarr, myfunction, fn, obj, nodes, first, input, a;
382 // Make sure that false values return false
383 assert.ok( !jQuery.isFunction(), "No Value" );
384 assert.ok( !jQuery.isFunction( null ), "null Value" );
385 assert.ok( !jQuery.isFunction( undefined ), "undefined Value" );
386 assert.ok( !jQuery.isFunction( "" ), "Empty String Value" );
387 assert.ok( !jQuery.isFunction( 0 ), "0 Value" );
390 assert.ok( jQuery.isFunction( String ), "String Function(" + String + ")" );
391 assert.ok( jQuery.isFunction( Array ), "Array Function(" + Array + ")" );
392 assert.ok( jQuery.isFunction( Object ), "Object Function(" + Object + ")" );
393 assert.ok( jQuery.isFunction( Function ), "Function Function(" + Function + ")" );
395 // When stringified, this could be misinterpreted
397 assert.ok( !jQuery.isFunction( mystr ), "Function String" );
399 // When stringified, this could be misinterpreted
400 myarr = [ "function" ];
401 assert.ok( !jQuery.isFunction( myarr ), "Function Array" );
403 // When stringified, this could be misinterpreted
404 myfunction = { "function": "test" };
405 assert.ok( !jQuery.isFunction( myfunction ), "Function Object" );
407 // Make sure normal functions still work
409 assert.ok( jQuery.isFunction( fn ), "Normal Function" );
411 obj = document.createElement( "object" );
413 // Firefox says this is a function
414 assert.ok( !jQuery.isFunction( obj ), "Object Element" );
416 // Since 1.3, this isn't supported (#2968)
417 //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
419 nodes = document.body.childNodes;
421 // Safari says this is a function
422 assert.ok( !jQuery.isFunction( nodes ), "childNodes Property" );
424 first = document.body.firstChild;
426 // Normal elements are reported ok everywhere
427 assert.ok( !jQuery.isFunction( first ), "A normal DOM Element" );
429 input = document.createElement( "input" );
431 document.body.appendChild( input );
433 // Since 1.3, this isn't supported (#2968)
434 //ok( jQuery.isFunction(input.focus), "A default function property" );
436 document.body.removeChild( input );
438 a = document.createElement( "a" );
439 a.href = "some-function";
440 document.body.appendChild( a );
442 // This serializes with the word 'function' in it
443 assert.ok( !jQuery.isFunction( a ), "Anchor Element" );
445 document.body.removeChild( a );
447 // Recursive function calls have lengths and array-like properties
448 function callme( callback ) {
449 function fn( response ) {
450 callback( response );
453 assert.ok( jQuery.isFunction( fn ), "Recursive Function Call" );
455 fn( { some: "data" } );
459 callme( function() {} );
463 QUnit.test( "isNumeric", function( assert ) {
466 var t = jQuery.isNumeric,
467 ToString = function( value ) {
468 this.toString = function() {
469 return String( value );
473 assert.ok( t( "-10" ), "Negative integer string" );
474 assert.ok( t( "0" ), "Zero string" );
475 assert.ok( t( "5" ), "Positive integer string" );
476 assert.ok( t( -16 ), "Negative integer number" );
477 assert.ok( t( 0 ), "Zero integer number" );
478 assert.ok( t( 32 ), "Positive integer number" );
479 assert.ok( t( "-1.6" ), "Negative floating point string" );
480 assert.ok( t( "4.536" ), "Positive floating point string" );
481 assert.ok( t( -2.6 ), "Negative floating point number" );
482 assert.ok( t( 3.1415 ), "Positive floating point number" );
483 assert.ok( t( 1.5999999999999999 ), "Very precise floating point number" );
484 assert.ok( t( 8e5 ), "Exponential notation" );
485 assert.ok( t( "123e-2" ), "Exponential notation string" );
486 assert.ok( t( "040" ), "Legacy octal integer literal string" );
487 assert.ok( t( "0xFF" ), "Hexadecimal integer literal string (0x...)" );
488 assert.ok( t( "0Xba" ), "Hexadecimal integer literal string (0X...)" );
489 assert.ok( t( 0xFFF ), "Hexadecimal integer literal" );
491 if ( +"0b1" === 1 ) {
492 assert.ok( t( "0b111110" ), "Binary integer literal string (0b...)" );
493 assert.ok( t( "0B111110" ), "Binary integer literal string (0B...)" );
495 assert.ok( true, "Browser does not support binary integer literal (0b...)" );
496 assert.ok( true, "Browser does not support binary integer literal (0B...)" );
499 if ( +"0o1" === 1 ) {
500 assert.ok( t( "0o76" ), "Octal integer literal string (0o...)" );
501 assert.ok( t( "0O76" ), "Octal integer literal string (0O...)" );
503 assert.ok( true, "Browser does not support octal integer literal (0o...)" );
504 assert.ok( true, "Browser does not support octal integer literal (0O...)" );
507 assert.equal( t( new ToString( "42" ) ), false, "Only limited to strings and numbers" );
508 assert.equal( t( "" ), false, "Empty string" );
509 assert.equal( t( " " ), false, "Whitespace characters string" );
510 assert.equal( t( "\t\t" ), false, "Tab characters string" );
511 assert.equal( t( "abcdefghijklm1234567890" ), false, "Alphanumeric character string" );
512 assert.equal( t( "xabcdefx" ), false, "Non-numeric character string" );
513 assert.equal( t( true ), false, "Boolean true literal" );
514 assert.equal( t( false ), false, "Boolean false literal" );
515 assert.equal( t( "bcfed5.2" ), false, "Number with preceding non-numeric characters" );
516 assert.equal( t( "7.2acdgs" ), false, "Number with trailing non-numeric characters" );
517 assert.equal( t( undefined ), false, "Undefined value" );
518 assert.equal( t( null ), false, "Null value" );
519 assert.equal( t( NaN ), false, "NaN value" );
520 assert.equal( t( Infinity ), false, "Infinity primitive" );
521 assert.equal( t( Number.POSITIVE_INFINITY ), false, "Positive Infinity" );
522 assert.equal( t( Number.NEGATIVE_INFINITY ), false, "Negative Infinity" );
523 assert.equal( t( new ToString( "Devo" ) ), false, "Custom .toString returning non-number" );
524 assert.equal( t( {} ), false, "Empty object" );
525 assert.equal( t( [] ), false, "Empty array" );
526 assert.equal( t( [ 42 ] ), false, "Array with one number" );
527 assert.equal( t( function() {} ), false, "Instance of a function" );
528 assert.equal( t( new Date() ), false, "Instance of a Date" );
531 QUnit[ typeof Symbol === "function" ? "test" : "skip" ]( "isNumeric(Symbol)", function( assert ) {
534 assert.equal( jQuery.isNumeric( Symbol() ), false, "Symbol" );
535 assert.equal( jQuery.isNumeric( Object( Symbol() ) ), false, "Symbol inside an object" );
538 QUnit.test( "isXMLDoc - HTML", function( assert ) {
541 assert.ok( !jQuery.isXMLDoc( document ), "HTML document" );
542 assert.ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
543 assert.ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
546 iframe = document.createElement( "iframe" );
547 document.body.appendChild( iframe );
550 body = jQuery( iframe ).contents()[ 0 ];
553 assert.ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
555 assert.ok( false, "Iframe body element exception" );
559 assert.ok( true, "Iframe body element - iframe not working correctly" );
562 document.body.removeChild( iframe );
565 QUnit.test( "XSS via location.hash", function( assert ) {
569 jQuery[ "_check9521" ] = function( x ) {
570 assert.ok( x, "script called from #id-like selector with inline handler" );
571 jQuery( "#check9521" ).remove();
572 delete jQuery[ "_check9521" ];
577 // This throws an error because it's processed like an id
578 jQuery( "#<img id='check9521' src='no-such-.gif' onerror='jQuery._check9521(false)'>" ).appendTo( "#qunit-fixture" );
580 jQuery[ "_check9521" ]( true );
584 QUnit.test( "isXMLDoc - XML", function( assert ) {
586 var xml = createDashboardXML();
587 assert.ok( jQuery.isXMLDoc( xml ), "XML document" );
588 assert.ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" );
589 assert.ok( jQuery.isXMLDoc( jQuery( "tab", xml )[ 0 ] ), "XML Tab Element" );
592 QUnit.test( "isWindow", function( assert ) {
595 assert.ok( jQuery.isWindow( window ), "window" );
596 assert.ok( jQuery.isWindow( document.getElementsByTagName( "iframe" )[ 0 ].contentWindow ), "iframe.contentWindow" );
597 assert.ok( !jQuery.isWindow(), "empty" );
598 assert.ok( !jQuery.isWindow( null ), "null" );
599 assert.ok( !jQuery.isWindow( undefined ), "undefined" );
600 assert.ok( !jQuery.isWindow( document ), "document" );
601 assert.ok( !jQuery.isWindow( document.documentElement ), "documentElement" );
602 assert.ok( !jQuery.isWindow( "" ), "string" );
603 assert.ok( !jQuery.isWindow( 1 ), "number" );
604 assert.ok( !jQuery.isWindow( true ), "boolean" );
605 assert.ok( !jQuery.isWindow( {} ), "object" );
606 assert.ok( !jQuery.isWindow( { setInterval: function() {} } ), "fake window" );
607 assert.ok( !jQuery.isWindow( /window/ ), "regexp" );
608 assert.ok( !jQuery.isWindow( function() {} ), "function" );
611 QUnit.test( "jQuery('html')", function( assert ) {
616 jQuery[ "foo" ] = false;
617 s = jQuery( "<script>jQuery.foo='test';</script>" )[ 0 ];
618 assert.ok( s, "Creating a script" );
619 assert.ok( !jQuery[ "foo" ], "Make sure the script wasn't executed prematurely" );
620 jQuery( "body" ).append( "<script>jQuery.foo='test';</script>" );
621 assert.ok( jQuery[ "foo" ], "Executing a script's contents in the right context" );
623 // Test multi-line HTML
624 div = jQuery( "<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>" )[ 0 ];
625 assert.equal( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
626 assert.equal( div.firstChild.nodeType, 3, "Text node." );
627 assert.equal( div.lastChild.nodeType, 3, "Text node." );
628 assert.equal( div.childNodes[ 1 ].nodeType, 1, "Paragraph." );
629 assert.equal( div.childNodes[ 1 ].firstChild.nodeType, 3, "Paragraph text." );
631 assert.ok( jQuery( "<link rel='stylesheet'/>" )[ 0 ], "Creating a link" );
633 assert.ok( !jQuery( "<script/>" )[ 0 ].parentNode, "Create a script" );
635 assert.ok( jQuery( "<input/>" ).attr( "type", "hidden" ), "Create an input and set the type." );
637 j = jQuery( "<span>hi</span> there <!-- mon ami -->" );
638 assert.ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
640 assert.ok( !jQuery( "<option>test</option>" )[ 0 ].selected, "Make sure that options are auto-selected #2050" );
642 assert.ok( jQuery( "<div></div>" )[ 0 ], "Create a div with closing tag." );
643 assert.ok( jQuery( "<table></table>" )[ 0 ], "Create a table with closing tag." );
645 assert.equal( jQuery( "element[attribute='<div></div>']" ).length, 0,
646 "When html is within brackets, do not recognize as html." );
648 //equal( jQuery( "element[attribute=<div></div>]" ).length, 0,
649 // "When html is within brackets, do not recognize as html." );
650 if ( jQuery.find.compile ) {
651 assert.equal( jQuery( "element:not(<div></div>)" ).length, 0,
652 "When html is within parens, do not recognize as html." );
654 assert.ok( "skip", "Complex :not not supported in selector-native" );
656 assert.equal( jQuery( "\\<div\\>" ).length, 0, "Ignore escaped html characters" );
659 QUnit.test( "jQuery(tag-hyphenated elements) gh-1987", function( assert ) {
662 jQuery.each( "thead tbody tfoot colgroup caption tr th td".split( " " ), function( i, name ) {
663 var j = jQuery( "<" + name + "-d></" + name + "-d>" );
664 assert.ok( j[ 0 ], "Create a tag-hyphenated elements" );
665 assert.ok( jQuery.nodeName( j[ 0 ], name.toUpperCase() + "-D" ), "Tag-hyphenated element has expected node name" );
668 var j = jQuery( "<tr-multiple-hyphens></tr-multiple-hyphens>" );
669 assert.ok( jQuery.nodeName( j[ 0 ], "TR-MULTIPLE-HYPHENS" ), "Element with multiple hyphens in its tag has expected node name" );
672 QUnit.test( "jQuery('massive html #7990')", function( assert ) {
676 li = "<li>very very very very large html string</li>",
679 for ( i = 0; i < 30000; i += 1 ) {
680 html[ html.length ] = li;
682 html[ html.length ] = "</ul>";
683 html = jQuery( html.join( "" ) )[ 0 ];
684 assert.equal( html.nodeName.toLowerCase(), "ul" );
685 assert.equal( html.firstChild.nodeName.toLowerCase(), "li" );
686 assert.equal( html.childNodes.length, 30000 );
689 QUnit.test( "jQuery('html', context)", function( assert ) {
692 var $div = jQuery( "<div/>" )[ 0 ],
693 $span = jQuery( "<span/>", $div );
694 assert.equal( $span.length, 1, "verify a span created with a div context works, #1763" );
697 QUnit.test( "jQuery(selector, xml).text(str) - loaded via xml document", function( assert ) {
700 var xml = createDashboardXML(),
702 // tests for #1419 where ie was a problem
703 tab = jQuery( "tab", xml ).eq( 0 );
704 assert.equal( tab.text(), "blabla", "verify initial text correct" );
705 tab.text( "newtext" );
706 assert.equal( tab.text(), "newtext", "verify new text correct" );
709 QUnit.test( "end()", function( assert ) {
711 assert.equal( "Yahoo", jQuery( "#yahoo" ).parent().end().text(), "check for end" );
712 assert.ok( jQuery( "#yahoo" ).end(), "check for end with nothing to end" );
714 var x = jQuery( "#yahoo" );
716 assert.equal( "Yahoo", jQuery( "#yahoo" ).text(), "check for non-destructive behaviour" );
719 QUnit.test( "length", function( assert ) {
721 assert.equal( jQuery( "#qunit-fixture p" ).length, 6, "Get Number of Elements Found" );
724 QUnit.test( "get()", function( assert ) {
726 assert.deepEqual( jQuery( "#qunit-fixture p" ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Get All Elements" );
729 QUnit.test( "toArray()", function( assert ) {
731 assert.deepEqual( jQuery( "#qunit-fixture p" ).toArray(),
732 q( "firstp", "ap", "sndp", "en", "sap", "first" ),
733 "Convert jQuery object to an Array" );
736 QUnit.test( "inArray()", function( assert ) {
740 p: q( "firstp", "sap", "ap", "first" ),
741 em: q( "siblingnext", "siblingfirst" ),
742 div: q( "qunit-testrunner-toolbar", "nothiddendiv", "nothiddendivchild", "foo" ),
743 a: q( "mark", "groups", "google", "simon1" ),
747 p: { elem: jQuery( "#ap" )[ 0 ], index: 2 },
748 em: { elem: jQuery( "#siblingfirst" )[ 0 ], index: 1 },
749 div: { elem: jQuery( "#nothiddendiv" )[ 0 ], index: 1 },
750 a: { elem: jQuery( "#simon1" )[ 0 ], index: 3 }
753 p: jQuery( "#liveSpan1" )[ 0 ],
754 em: jQuery( "#nothiddendiv" )[ 0 ],
758 jQuery.each( tests, function( key, obj ) {
759 assert.equal( jQuery.inArray( obj.elem, selections[ key ] ), obj.index, "elem is in the array of selections of its tag" );
761 // Third argument (fromIndex)
762 assert.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" );
763 assert.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" );
764 assert.equal( !!~jQuery.inArray( obj.elem, selections[ key ], -3 ), true, "elem is in the array of selections given a negative index" );
767 jQuery.each( falseTests, function( key, elem ) {
768 assert.equal( !!~jQuery.inArray( elem, selections[ key ] ), false, "elem is NOT in the array of selections" );
773 QUnit.test( "get(Number)", function( assert ) {
775 assert.equal( jQuery( "#qunit-fixture p" ).get( 0 ), document.getElementById( "firstp" ), "Get A Single Element" );
776 assert.strictEqual( jQuery( "#firstp" ).get( 1 ), undefined, "Try get with index larger elements count" );
779 QUnit.test( "get(-Number)", function( assert ) {
781 assert.equal( jQuery( "p" ).get( -1 ), document.getElementById( "first" ), "Get a single element with negative index" );
782 assert.strictEqual( jQuery( "#firstp" ).get( -2 ), undefined, "Try get with index negative index larger then elements count" );
785 QUnit.test( "each(Function)", function( assert ) {
789 div = jQuery( "div" );
790 div.each( function() {this.foo = "zoo";} );
792 for ( i = 0; i < div.length; i++ ) {
793 if ( div.get( i ).foo !== "zoo" ) {
797 assert.ok( pass, "Execute a function, Relative" );
800 QUnit.test( "slice()", function( assert ) {
803 var $links = jQuery( "#ap a" );
805 assert.deepEqual( $links.slice( 1, 2 ).get(), q( "groups" ), "slice(1,2)" );
806 assert.deepEqual( $links.slice( 1 ).get(), q( "groups", "anchor1", "mark" ), "slice(1)" );
807 assert.deepEqual( $links.slice( 0, 3 ).get(), q( "google", "groups", "anchor1" ), "slice(0,3)" );
808 assert.deepEqual( $links.slice( -1 ).get(), q( "mark" ), "slice(-1)" );
810 assert.deepEqual( $links.eq( 1 ).get(), q( "groups" ), "eq(1)" );
811 assert.deepEqual( $links.eq( "2" ).get(), q( "anchor1" ), "eq('2')" );
812 assert.deepEqual( $links.eq( -1 ).get(), q( "mark" ), "eq(-1)" );
815 QUnit.test( "first()/last()", function( assert ) {
818 var $links = jQuery( "#ap a" ), $none = jQuery( "asdf" );
820 assert.deepEqual( $links.first().get(), q( "google" ), "first()" );
821 assert.deepEqual( $links.last().get(), q( "mark" ), "last()" );
823 assert.deepEqual( $none.first().get(), [], "first() none" );
824 assert.deepEqual( $none.last().get(), [], "last() none" );
827 QUnit.test( "map()", function( assert ) {
831 jQuery( "#ap" ).map( function() {
832 return jQuery( this ).find( "a" ).get();
834 q( "google", "groups", "anchor1", "mark" ),
839 jQuery( "#ap > a" ).map( function() {
840 return this.parentNode;
842 q( "ap", "ap", "ap" ),
847 QUnit.test( "jQuery.map", function( assert ) {
850 var i, label, result, callback;
852 result = jQuery.map( [ 3, 4, 5 ], function( v, k ) {
855 assert.equal( result.join( "" ), "012", "Map the keys from an array" );
857 result = jQuery.map( [ 3, 4, 5 ], function( v ) {
860 assert.equal( result.join( "" ), "345", "Map the values from an array" );
862 result = jQuery.map( { a: 1, b: 2 }, function( v, k ) {
865 assert.equal( result.join( "" ), "ab", "Map the keys from an object" );
867 result = jQuery.map( { a: 1, b: 2 }, function( v ) {
870 assert.equal( result.join( "" ), "12", "Map the values from an object" );
872 result = jQuery.map( [ "a", undefined, null, "b" ], function( v ) {
875 assert.equal( result.join( "" ), "ab", "Array iteration does not include undefined/null results" );
877 result = jQuery.map( { a: "a", b: undefined, c: null, d: "b" }, function( v ) {
880 assert.equal( result.join( "" ), "ab", "Object iteration does not include undefined/null results" );
884 One: function( a ) { a = a; },
885 Two: function( a, b ) { a = a; b = b; }
887 callback = function( v, k ) {
888 assert.equal( k, "foo", label + "-argument function treated like object" );
890 for ( i in result ) {
892 result[ i ].foo = "bar";
893 jQuery.map( result[ i ], callback );
897 "undefined": undefined,
902 "nonempty string": "string",
907 callback = function( v, k ) {
908 assert.equal( k, "length", "Object with " + label + " length treated like object" );
910 for ( i in result ) {
912 jQuery.map( { length: result[ i ] }, callback );
916 "sparse Array": Array( 4 ),
917 "length: 1 plain object": { length: 1, "0": true },
918 "length: 2 plain object": { length: 2, "0": true, "1": true },
919 NodeList: document.getElementsByTagName( "html" )
921 callback = function( v, k ) {
922 if ( result[ label ] ) {
923 delete result[ label ];
924 assert.equal( k, "0", label + " treated like array" );
927 for ( i in result ) {
929 jQuery.map( result[ i ], callback );
933 jQuery.map( { length: 0 }, function() {
936 assert.ok( !result, "length: 0 plain object treated like array" );
939 jQuery.map( document.getElementsByTagName( "asdf" ), function() {
942 assert.ok( !result, "empty NodeList treated like array" );
944 result = jQuery.map( Array( 4 ), function( v, k ) {
945 return k % 2 ? k : [ k,k,k ];
947 assert.equal( result.join( "" ), "00012223", "Array results flattened (#2616)" );
950 QUnit.test( "jQuery.merge()", function( assert ) {
954 jQuery.merge( [], [] ),
960 jQuery.merge( [ 1 ], [ 2 ] ),
962 "Basic (single-element)"
965 jQuery.merge( [ 1, 2 ], [ 3, 4 ] ),
967 "Basic (multiple-element)"
971 jQuery.merge( [ 1, 2 ], [] ),
976 jQuery.merge( [], [ 1, 2 ] ),
981 // Fixed at [5998], #3641
983 jQuery.merge( [ -2, -1 ], [ 0, 1, 2 ] ),
985 "Second array including a zero (falsy)"
988 // After fixing #5527
990 jQuery.merge( [], [ null, undefined ] ),
992 "Second array including null and undefined values"
995 jQuery.merge( { length: 0 }, [ 1, 2 ] ),
996 { length: 2, 0: 1, 1: 2 },
1000 jQuery.merge( [ 1, 2 ], { length: 1, 0: 3 } ),
1006 jQuery.merge( [], document.getElementById( "lengthtest" ).getElementsByTagName( "input" ) ),
1007 [ document.getElementById( "length" ), document.getElementById( "idTest" ) ],
1012 QUnit.test( "jQuery.grep()", function( assert ) {
1015 var searchCriterion = function( value ) {
1016 return value % 2 === 0;
1019 assert.deepEqual( jQuery.grep( [], searchCriterion ), [], "Empty array" );
1020 assert.deepEqual( jQuery.grep( new Array( 4 ), searchCriterion ), [], "Sparse array" );
1023 jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion ),
1025 "Satisfying elements present"
1028 jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion ),
1030 "Satisfying elements absent"
1034 jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, true ),
1036 "Satisfying elements present and grep inverted"
1039 jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, true ),
1041 "Satisfying elements absent and grep inverted"
1045 jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, false ),
1047 "Satisfying elements present but grep explicitly uninverted"
1050 jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, false ),
1052 "Satisfying elements absent and grep explicitly uninverted"
1056 QUnit.test( "jQuery.grep(Array-like)", function( assert ) {
1059 var searchCriterion = function( value ) {
1060 return value % 2 === 0;
1063 assert.deepEqual( jQuery.grep( { length: 0 }, searchCriterion ), [], "Empty array-like" );
1066 jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion ),
1068 "Satisfying elements present and array-like object used"
1071 jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion ),
1073 "Satisfying elements absent and Array-like object used"
1077 jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion, true ),
1079 "Satisfying elements present, array-like object used, and grep inverted"
1082 jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion, true ),
1084 "Satisfying elements absent, array-like object used, and grep inverted"
1088 jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion, false ),
1090 "Satisfying elements present, Array-like object used, but grep explicitly uninverted"
1093 jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion, false ),
1095 "Satisfying elements absent, Array-like object used, and grep explicitly uninverted"
1099 QUnit.test( "jQuery.extend(Object, Object)", function( assert ) {
1100 assert.expect( 28 );
1102 var empty, optionsWithLength, optionsWithDate, myKlass,
1103 customObject, optionsWithCustomObject, MyNumber, ret,
1104 nullUndef, target, recursive, obj,
1105 defaults, defaultsCopy, options1, options1Copy, options2, options2Copy, merged2,
1106 settings = { "xnumber1": 5, "xnumber2": 7, "xstring1": "peter", "xstring2": "pan" },
1107 options = { "xnumber2": 1, "xstring2": "x", "xxx": "newstring" },
1108 optionsCopy = { "xnumber2": 1, "xstring2": "x", "xxx": "newstring" },
1109 merged = { "xnumber1": 5, "xnumber2": 1, "xstring1": "peter", "xstring2": "x", "xxx": "newstring" },
1110 deep1 = { "foo": { "bar": true } },
1111 deep2 = { "foo": { "baz": true }, "foo2": document },
1112 deep2copy = { "foo": { "baz": true }, "foo2": document },
1113 deepmerged = { "foo": { "bar": true, "baz": true }, "foo2": document },
1115 nestedarray = { "arr": arr };
1117 jQuery.extend( settings, options );
1118 assert.deepEqual( settings, merged, "Check if extended: settings must be extended" );
1119 assert.deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
1121 jQuery.extend( settings, null, options );
1122 assert.deepEqual( settings, merged, "Check if extended: settings must be extended" );
1123 assert.deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
1125 jQuery.extend( true, deep1, deep2 );
1126 assert.deepEqual( deep1[ "foo" ], deepmerged[ "foo" ], "Check if foo: settings must be extended" );
1127 assert.deepEqual( deep2[ "foo" ], deep2copy[ "foo" ], "Check if not deep2: options must not be modified" );
1128 assert.equal( deep1[ "foo2" ], document, "Make sure that a deep clone was not attempted on the document" );
1130 assert.ok( jQuery.extend( true, {}, nestedarray )[ "arr" ] !== arr, "Deep extend of object must clone child array" );
1133 assert.ok( jQuery.isArray( jQuery.extend( true, { "arr": {} }, nestedarray )[ "arr" ] ), "Cloned array have to be an Array" );
1134 assert.ok( jQuery.isPlainObject( jQuery.extend( true, { "arr": arr }, { "arr": {} } )[ "arr" ] ), "Cloned object have to be an plain object" );
1137 optionsWithLength = { "foo": { "length": -1 } };
1138 jQuery.extend( true, empty, optionsWithLength );
1139 assert.deepEqual( empty[ "foo" ], optionsWithLength[ "foo" ], "The length property must copy correctly" );
1142 optionsWithDate = { "foo": { "date": new Date() } };
1143 jQuery.extend( true, empty, optionsWithDate );
1144 assert.deepEqual( empty[ "foo" ], optionsWithDate[ "foo" ], "Dates copy correctly" );
1147 myKlass = function() {};
1148 customObject = new myKlass();
1149 optionsWithCustomObject = { "foo": { "date": customObject } };
1151 jQuery.extend( true, empty, optionsWithCustomObject );
1152 assert.ok( empty[ "foo" ] && empty[ "foo" ][ "date" ] === customObject, "Custom objects copy correctly (no methods)" );
1154 // Makes the class a little more realistic
1155 myKlass.prototype = { "someMethod": function() {} };
1157 jQuery.extend( true, empty, optionsWithCustomObject );
1158 assert.ok( empty[ "foo" ] && empty[ "foo" ][ "date" ] === customObject, "Custom objects copy correctly" );
1162 ret = jQuery.extend( true, { "foo": 4 }, { "foo": new MyNumber( 5 ) } );
1163 assert.ok( parseInt( ret.foo, 10 ) === 5, "Wrapped numbers copy correctly" );
1166 nullUndef = jQuery.extend( {}, options, { "xnumber2": null } );
1167 assert.ok( nullUndef[ "xnumber2" ] === null, "Check to make sure null values are copied" );
1169 nullUndef = jQuery.extend( {}, options, { "xnumber2": undefined } );
1170 assert.ok( nullUndef[ "xnumber2" ] === options[ "xnumber2" ], "Check to make sure undefined values are not copied" );
1172 nullUndef = jQuery.extend( {}, options, { "xnumber0": null } );
1173 assert.ok( nullUndef[ "xnumber0" ] === null, "Check to make sure null values are inserted" );
1176 recursive = { foo:target, bar:5 };
1177 jQuery.extend( true, target, recursive );
1178 assert.deepEqual( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
1180 ret = jQuery.extend( true, { foo: [] }, { foo: [ 0 ] } ); // 1907
1181 assert.equal( ret.foo.length, 1, "Check to make sure a value with coercion 'false' copies over when necessary to fix #1907" );
1183 ret = jQuery.extend( true, { foo: "1,2,3" }, { foo: [ 1, 2, 3 ] } );
1184 assert.ok( typeof ret.foo !== "string", "Check to make sure values equal with coercion (but not actually equal) overwrite correctly" );
1186 ret = jQuery.extend( true, { foo:"bar" }, { foo:null } );
1187 assert.ok( typeof ret.foo !== "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" );
1190 jQuery.extend( true, obj, { foo:"notnull" } );
1191 assert.equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );
1194 jQuery.extend( func, { key: "value" } );
1195 assert.equal( func.key, "value", "Verify a function can be extended" );
1197 defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" };
1198 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" };
1199 options1 = { xnumber2: 1, xstring2: "x" };
1200 options1Copy = { xnumber2: 1, xstring2: "x" };
1201 options2 = { xstring2: "xx", xxx: "newstringx" };
1202 options2Copy = { xstring2: "xx", xxx: "newstringx" };
1203 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
1205 settings = jQuery.extend( {}, defaults, options1, options2 );
1206 assert.deepEqual( settings, merged2, "Check if extended: settings must be extended" );
1207 assert.deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
1208 assert.deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" );
1209 assert.deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" );
1212 QUnit.test( "jQuery.extend(Object, Object {created with \"defineProperties\"})", function( assert ) {
1215 var definedObj = Object.defineProperties({}, {
1222 "nonenumerableProp": {
1230 jQuery.extend( accessorObj, definedObj );
1231 assert.equal( accessorObj.enumerableProp, true, "Verify that getters are transferred" );
1232 assert.equal( accessorObj.nonenumerableProp, undefined, "Verify that non-enumerable getters are ignored" );
1235 QUnit.test( "jQuery.extend(true,{},{a:[], o:{}}); deep copy with array, followed by object", function( assert ) {
1238 var result, initial = {
1240 // This will make "copyIsArray" true
1241 array: [ 1, 2, 3, 4 ],
1243 // If "copyIsArray" doesn't get reset to false, the check
1244 // will evaluate true and enter the array copy block
1245 // instead of the object copy block. Since the ternary in the
1246 // "copyIsArray" block will evaluate to false
1247 // (check if operating on an array with ), this will be
1248 // replaced by an empty array.
1252 result = jQuery.extend( true, {}, initial );
1254 assert.deepEqual( result, initial, "The [result] and [initial] have equal shape and values" );
1255 assert.ok( !jQuery.isArray( result.object ), "result.object wasn't paved with an empty array" );
1258 QUnit.test( "jQuery.each(Object,Function)", function( assert ) {
1259 assert.expect( 23 );
1261 var i, label, seen, callback;
1264 jQuery.each( [ 3, 4, 5 ], function( k, v ) {
1267 assert.deepEqual( seen, { "0": 3, "1": 4, "2": 5 }, "Array iteration" );
1270 jQuery.each( { name: "name", lang: "lang" }, function( k, v ) {
1273 assert.deepEqual( seen, { name: "name", lang: "lang" }, "Object iteration" );
1276 jQuery.each( [ 1, 2, 3 ], function( k, v ) {
1282 assert.deepEqual( seen, [ 1, 2 ], "Broken array iteration" );
1285 jQuery.each( { "a": 1, "b": 2,"c": 3 }, function( k, v ) {
1289 assert.deepEqual( seen, [ 1 ], "Broken object iteration" );
1292 Zero: function() {},
1293 One: function( a ) { a = a; },
1294 Two: function( a, b ) { a = a; b = b; }
1296 callback = function( k ) {
1297 assert.equal( k, "foo", label + "-argument function treated like object" );
1301 seen[ i ].foo = "bar";
1302 jQuery.each( seen[ i ], callback );
1306 "undefined": undefined,
1311 "nonempty string": "string",
1312 "string \"0\"": "0",
1316 callback = function( k ) {
1317 assert.equal( k, "length", "Object with " + label + " length treated like object" );
1321 jQuery.each( { length: seen[ i ] }, callback );
1325 "sparse Array": Array( 4 ),
1326 "length: 1 plain object": { length: 1, "0": true },
1327 "length: 2 plain object": { length: 2, "0": true, "1": true },
1328 NodeList: document.getElementsByTagName( "html" )
1330 callback = function( k ) {
1331 if ( seen[ label ] ) {
1332 delete seen[ label ];
1333 assert.equal( k, "0", label + " treated like array" );
1339 jQuery.each( seen[ i ], callback );
1343 jQuery.each( { length: 0 }, function() {
1346 assert.ok( !seen, "length: 0 plain object treated like array" );
1349 jQuery.each( document.getElementsByTagName( "asdf" ), function() {
1352 assert.ok( !seen, "empty NodeList treated like array" );
1355 jQuery.each( document.styleSheets, function() {
1358 assert.equal( i, document.styleSheets.length, "Iteration over document.styleSheets" );
1361 QUnit.test( "jQuery.each/map(undefined/null,Function)", function( assert ) {
1365 jQuery.each( undefined, jQuery.noop );
1366 jQuery.each( null, jQuery.noop );
1367 jQuery.map( undefined, jQuery.noop );
1368 jQuery.map( null, jQuery.noop );
1369 assert.ok( true, "jQuery.each/map( undefined/null, function() {} );" );
1371 assert.ok( false, "each/map must accept null and undefined values" );
1375 QUnit.test( "JIT compilation does not interfere with length retrieval (gh-2145)", function( assert ) {
1380 // Trigger JIT compilation of jQuery.each – and therefore isArraylike – in iOS.
1381 // Convince JSC to use one of its optimizing compilers
1382 // by providing code which can be LICM'd into nothing.
1383 for ( i = 0; i < 1000; i++ ) {
1388 jQuery.each( { 1: "1", 2: "2", 3: "3" }, function( index ) {
1389 assert.equal( ++i, index, "Iteration over object with solely " +
1390 "numeric indices (gh-2145 JIT iOS 8 bug)" );
1392 assert.equal( i, 3, "Iteration over object with solely " +
1393 "numeric indices (gh-2145 JIT iOS 8 bug)" );
1396 QUnit.test( "jQuery.makeArray", function( assert ) {
1397 assert.expect( 15 );
1399 assert.equal( jQuery.makeArray( jQuery( "html>*" ) )[ 0 ].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
1401 assert.equal( jQuery.makeArray( document.getElementsByName( "PWD" ) ).slice( 0, 1 )[ 0 ].name, "PWD", "Pass makeArray a nodelist" );
1403 assert.equal( ( function() { return jQuery.makeArray( arguments ); } )( 1, 2 ).join( "" ), "12", "Pass makeArray an arguments array" );
1405 assert.equal( jQuery.makeArray( [ 1,2,3 ] ).join( "" ), "123", "Pass makeArray a real array" );
1407 assert.equal( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
1409 assert.equal( jQuery.makeArray( 0 )[ 0 ], 0, "Pass makeArray a number" );
1411 assert.equal( jQuery.makeArray( "foo" )[ 0 ], "foo", "Pass makeArray a string" );
1413 assert.equal( jQuery.makeArray( true )[ 0 ].constructor, Boolean, "Pass makeArray a boolean" );
1415 assert.equal( jQuery.makeArray( document.createElement( "div" ) )[ 0 ].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
1417 assert.equal( jQuery.makeArray( { length:2, 0:"a", 1:"b" } ).join( "" ), "ab", "Pass makeArray an array like map (with length)" );
1419 assert.ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice( 0, 1 )[ 0 ].nodeName, "Pass makeArray a childNodes array" );
1421 // function, is tricky as it has length
1422 assert.equal( jQuery.makeArray( function() { return 1;} )[ 0 ](), 1, "Pass makeArray a function" );
1424 //window, also has length
1425 assert.equal( jQuery.makeArray( window )[ 0 ], window, "Pass makeArray the window" );
1427 assert.equal( jQuery.makeArray( /a/ )[ 0 ].constructor, RegExp, "Pass makeArray a regex" );
1429 // Some nodes inherit traits of nodelists
1430 assert.ok( jQuery.makeArray( document.getElementById( "form" ) ).length >= 13,
1431 "Pass makeArray a form (treat as elements)" );
1434 QUnit.test( "jQuery.inArray", function( assert ) {
1437 assert.equal( jQuery.inArray( 0, false ), -1, "Search in 'false' as array returns -1 and doesn't throw exception" );
1439 assert.equal( jQuery.inArray( 0, null ), -1, "Search in 'null' as array returns -1 and doesn't throw exception" );
1441 assert.equal( jQuery.inArray( 0, undefined ), -1, "Search in 'undefined' as array returns -1 and doesn't throw exception" );
1444 QUnit.test( "jQuery.isEmptyObject", function( assert ) {
1447 assert.equal( true, jQuery.isEmptyObject( {} ), "isEmptyObject on empty object literal" );
1448 assert.equal( false, jQuery.isEmptyObject( { a:1 } ), "isEmptyObject on non-empty object literal" );
1450 // What about this ?
1451 // equal(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
1454 QUnit.test( "jQuery.proxy", function( assert ) {
1457 var test2, test3, test4, fn, cb,
1459 assert.equal( this, thisObject, "Make sure that scope is set properly." );
1461 thisObject = { foo: "bar", method: test };
1463 // Make sure normal works
1464 test.call( thisObject );
1467 jQuery.proxy( test, thisObject )();
1469 // Another take on it
1470 jQuery.proxy( thisObject, "method" )();
1472 // Make sure it doesn't freak out
1473 assert.equal( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
1475 // Partial application
1476 test2 = function( a ) {
1477 assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
1479 jQuery.proxy( test2, null, "pre-applied" )();
1481 // Partial application w/ normal arguments
1482 test3 = function( a, b ) {
1483 assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
1485 jQuery.proxy( test3, null, "pre-applied" )( "normal" );
1488 test4 = { "meth": function( a ) {
1489 assert.equal( a, "boom", "Ensure old syntax works." );
1491 jQuery.proxy( test4, "meth" )( "boom" );
1493 // jQuery 1.9 improved currying with `this` object
1495 assert.equal( Array.prototype.join.call( arguments, "," ), "arg1,arg2,arg3", "args passed" );
1496 assert.equal( this.foo, "bar", "this-object passed" );
1498 cb = jQuery.proxy( fn, null, "arg1", "arg2" );
1499 cb.call( thisObject, "arg3" );
1502 QUnit.test( "jQuery.parseHTML", function( assert ) {
1503 assert.expect( 23 );
1507 assert.deepEqual( jQuery.parseHTML(), [], "Without arguments" );
1508 assert.deepEqual( jQuery.parseHTML( undefined ), [], "Undefined" );
1509 assert.deepEqual( jQuery.parseHTML( null ), [], "Null" );
1510 assert.deepEqual( jQuery.parseHTML( false ), [], "Boolean false" );
1511 assert.deepEqual( jQuery.parseHTML( 0 ), [], "Zero" );
1512 assert.deepEqual( jQuery.parseHTML( true ), [], "Boolean true" );
1513 assert.deepEqual( jQuery.parseHTML( 42 ), [], "Positive number" );
1514 assert.deepEqual( jQuery.parseHTML( "" ), [], "Empty string" );
1515 assert.throws( function() {
1516 jQuery.parseHTML( "<div></div>", document.getElementById( "form" ) );
1517 }, "Passing an element as the context raises an exception (context should be a document)" );
1519 nodes = jQuery.parseHTML( jQuery( "body" )[ 0 ].innerHTML );
1520 assert.ok( nodes.length > 4, "Parse a large html string" );
1521 assert.equal( jQuery.type( nodes ), "array", "parseHTML returns an array rather than a nodelist" );
1523 html = "<script>undefined()</script>";
1524 assert.equal( jQuery.parseHTML( html ).length, 0, "Ignore scripts by default" );
1525 assert.equal( jQuery.parseHTML( html, true )[ 0 ].nodeName.toLowerCase(), "script", "Preserve scripts when requested" );
1527 html += "<div></div>";
1528 assert.equal( jQuery.parseHTML( html )[ 0 ].nodeName.toLowerCase(), "div", "Preserve non-script nodes" );
1529 assert.equal( jQuery.parseHTML( html, true )[ 0 ].nodeName.toLowerCase(), "script", "Preserve script position" );
1531 assert.equal( jQuery.parseHTML( "text" )[ 0 ].nodeType, 3, "Parsing text returns a text node" );
1532 assert.equal( jQuery.parseHTML( "\t<div></div>" )[ 0 ].nodeValue, "\t", "Preserve leading whitespace" );
1534 assert.equal( jQuery.parseHTML( " <div/> " )[ 0 ].nodeType, 3, "Leading spaces are treated as text nodes (#11290)" );
1536 html = jQuery.parseHTML( "<div>test div</div>" );
1538 assert.equal( html[ 0 ].parentNode.nodeType, 11, "parentNode should be documentFragment" );
1539 assert.equal( html[ 0 ].innerHTML, "test div", "Content should be preserved" );
1541 assert.equal( jQuery.parseHTML( "<span><span>" ).length, 1, "Incorrect html-strings should not break anything" );
1542 assert.equal( jQuery.parseHTML( "<td><td>" )[ 1 ].parentNode.nodeType, 11,
1543 "parentNode should be documentFragment for wrapMap (variable in manipulation module) elements too" );
1544 assert.ok( jQuery.parseHTML( "<#if><tr><p>This is a test.</p></tr><#/if>" ) || true, "Garbage input should not cause error" );
1547 if ( jQuery.support.createHTMLDocument ) {
1548 QUnit.asyncTest( "jQuery.parseHTML", function( assert ) {
1551 Globals.register( "parseHTMLError" );
1553 jQuery.globalEval( "parseHTMLError = false;" );
1554 jQuery.parseHTML( "<img src=x onerror='parseHTMLError = true'>" );
1556 window.setTimeout( function() {
1558 assert.equal( window.parseHTMLError, false, "onerror eventhandler has not been called." );
1563 QUnit.test( "jQuery.parseXML", function( assert ) {
1568 xml = jQuery.parseXML( "<p>A <b>well-formed</b> xml string</p>" );
1569 tmp = xml.getElementsByTagName( "p" )[ 0 ];
1570 assert.ok( !!tmp, "<p> present in document" );
1571 tmp = tmp.getElementsByTagName( "b" )[ 0 ];
1572 assert.ok( !!tmp, "<b> present in document" );
1573 assert.strictEqual( tmp.childNodes[ 0 ].nodeValue, "well-formed", "<b> text is as expected" );
1575 assert.strictEqual( e, undefined, "unexpected error" );
1578 xml = jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
1579 assert.ok( false, "invalid xml not detected" );
1581 assert.strictEqual( e.message, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "invalid xml detected" );
1584 xml = jQuery.parseXML( "" );
1585 assert.strictEqual( xml, null, "empty string => null document" );
1586 xml = jQuery.parseXML();
1587 assert.strictEqual( xml, null, "undefined string => null document" );
1588 xml = jQuery.parseXML( null );
1589 assert.strictEqual( xml, null, "null string => null document" );
1590 xml = jQuery.parseXML( true );
1591 assert.strictEqual( xml, null, "non-string => null document" );
1593 assert.ok( false, "empty input throws exception" );
1597 QUnit.test( "jQuery.camelCase()", function( assert ) {
1600 "foo-bar": "fooBar",
1601 "foo-bar-baz": "fooBarBaz",
1602 "girl-u-want": "girlUWant",
1603 "the-4th-dimension": "the-4thDimension",
1604 "-o-tannenbaum": "OTannenbaum",
1605 "-moz-illa": "MozIlla",
1606 "-ms-take": "msTake"
1611 jQuery.each( tests, function( key, val ) {
1612 assert.equal( jQuery.camelCase( key ), val, "Converts: " + key + " => " + val );
1616 testIframeWithCallback(
1617 "Conditional compilation compatibility (#13274)",
1619 function( cc_on, errors, $, assert ) {
1621 assert.ok( true, "JScript conditional compilation " + ( cc_on ? "supported" : "not supported" ) );
1622 assert.deepEqual( errors, [], "No errors" );
1623 assert.ok( $(), "jQuery executes" );
1627 // iOS7 doesn't fire the load event if the long-loading iframe gets its source reset to about:blank.
1628 // This makes this test fail but it doesn't seem to cause any real-life problems so blacklisting
1629 // this test there is preferred to complicating the hard-to-test core/ready code further.
1630 if ( !/iphone os 7_/i.test( navigator.userAgent ) ) {
1631 testIframeWithCallback(
1632 "document ready when jQuery loaded asynchronously (#13655)",
1633 "core/dynamic_ready.html",
1634 function( ready, assert ) {
1636 assert.equal( true, ready, "document ready correctly fired when jQuery is loaded after DOMContentLoaded" );
1641 testIframeWithCallback(
1642 "Tolerating alias-masked DOM properties (#14074)",
1643 "core/aliased.html",
1644 function( errors, assert ) {
1646 assert.deepEqual( errors, [], "jQuery loaded" );
1650 testIframeWithCallback(
1651 "Don't call window.onready (#14802)",
1652 "core/onready.html",
1653 function( error, assert ) {
1655 assert.equal( error, false, "no call to user-defined onready" );
1659 QUnit.test( "Iterability of jQuery objects (gh-1693)", function( assert ) {
1660 /* jshint unused: false */
1663 var i, elem, result;
1665 if ( typeof Symbol === "function" ) {
1667 elem = jQuery( "<div></div><span></span><a></a>" );
1671 eval( "for ( i of elem ) { result += i.nodeName; }" );
1673 assert.equal( result, "DIVSPANA", "for-of works on jQuery objects" );
1675 assert.ok( true, "The browser doesn't support Symbols" );