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.test( "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,
294 // The use case that we want to match
295 assert.ok( jQuery.isPlainObject( {} ), "{}" );
297 // Not objects shouldn't be matched
298 assert.ok( !jQuery.isPlainObject( "" ), "string" );
299 assert.ok( !jQuery.isPlainObject( 0 ) && !jQuery.isPlainObject( 1 ), "number" );
300 assert.ok( !jQuery.isPlainObject( true ) && !jQuery.isPlainObject( false ), "boolean" );
301 assert.ok( !jQuery.isPlainObject( null ), "null" );
302 assert.ok( !jQuery.isPlainObject( undefined ), "undefined" );
304 // Arrays shouldn't be matched
305 assert.ok( !jQuery.isPlainObject( [] ), "array" );
307 // Instantiated objects shouldn't be matched
308 assert.ok( !jQuery.isPlainObject( new Date() ), "new Date" );
310 // Functions shouldn't be matched
311 assert.ok( !jQuery.isPlainObject( fn ), "fn" );
313 // Again, instantiated objects shouldn't be matched
314 assert.ok( !jQuery.isPlainObject( new fn() ), "new fn (no methods)" );
316 // Makes the function a little more realistic
317 // (and harder to detect, incidentally)
318 fn.prototype[ "someMethod" ] = function() {};
320 // Again, instantiated objects shouldn't be matched
321 assert.ok( !jQuery.isPlainObject( new fn() ), "new fn" );
324 assert.ok( !jQuery.isPlainObject( document.createElement( "div" ) ), "DOM Element" );
327 assert.ok( !jQuery.isPlainObject( window ), "window" );
331 jQuery.isPlainObject( window.location );
334 assert.ok( pass, "Does not throw exceptions on host objects" );
336 // Objects from other windows should be matched
337 Globals.register( "iframeDone" );
338 window.iframeDone = function( otherObject, detail ) {
339 window.iframeDone = undefined;
340 iframe.parentNode.removeChild( iframe );
341 assert.ok( jQuery.isPlainObject( new otherObject() ), "new otherObject" + ( detail ? " - " + detail : "" ) );
346 iframe = jQuery( "#qunit-fixture" )[ 0 ].appendChild( document.createElement( "iframe" ) );
347 doc = iframe.contentDocument || iframe.contentWindow.document;
349 doc.write( "<body onload='window.parent.iframeDone(Object);'>" );
352 window.iframeDone( Object, "iframes not supported" );
357 QUnit[ typeof Symbol === "function" ? "test" : "skip" ]( "isPlainObject(Symbol)", function( assert ) {
360 assert.equal( jQuery.isPlainObject( Symbol() ), false, "Symbol" );
361 assert.equal( jQuery.isPlainObject( Object( Symbol() ) ), false, "Symbol inside an object" );
365 QUnit.test( "isFunction", function( assert ) {
368 var mystr, myarr, myfunction, fn, obj, nodes, first, input, a;
370 // Make sure that false values return false
371 assert.ok( !jQuery.isFunction(), "No Value" );
372 assert.ok( !jQuery.isFunction( null ), "null Value" );
373 assert.ok( !jQuery.isFunction( undefined ), "undefined Value" );
374 assert.ok( !jQuery.isFunction( "" ), "Empty String Value" );
375 assert.ok( !jQuery.isFunction( 0 ), "0 Value" );
378 assert.ok( jQuery.isFunction( String ), "String Function(" + String + ")" );
379 assert.ok( jQuery.isFunction( Array ), "Array Function(" + Array + ")" );
380 assert.ok( jQuery.isFunction( Object ), "Object Function(" + Object + ")" );
381 assert.ok( jQuery.isFunction( Function ), "Function Function(" + Function + ")" );
383 // When stringified, this could be misinterpreted
385 assert.ok( !jQuery.isFunction( mystr ), "Function String" );
387 // When stringified, this could be misinterpreted
388 myarr = [ "function" ];
389 assert.ok( !jQuery.isFunction( myarr ), "Function Array" );
391 // When stringified, this could be misinterpreted
392 myfunction = { "function": "test" };
393 assert.ok( !jQuery.isFunction( myfunction ), "Function Object" );
395 // Make sure normal functions still work
397 assert.ok( jQuery.isFunction( fn ), "Normal Function" );
399 obj = document.createElement( "object" );
401 // Firefox says this is a function
402 assert.ok( !jQuery.isFunction( obj ), "Object Element" );
404 // Since 1.3, this isn't supported (#2968)
405 //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
407 nodes = document.body.childNodes;
409 // Safari says this is a function
410 assert.ok( !jQuery.isFunction( nodes ), "childNodes Property" );
412 first = document.body.firstChild;
414 // Normal elements are reported ok everywhere
415 assert.ok( !jQuery.isFunction( first ), "A normal DOM Element" );
417 input = document.createElement( "input" );
419 document.body.appendChild( input );
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 a = document.createElement( "a" );
427 a.href = "some-function";
428 document.body.appendChild( a );
430 // This serializes with the word 'function' in it
431 assert.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 ) {
438 callback( response );
441 assert.ok( jQuery.isFunction( fn ), "Recursive Function Call" );
443 fn( { some: "data" } );
447 callme( function() {} );
451 QUnit.test( "isNumeric", function( assert ) {
454 var t = jQuery.isNumeric,
455 ToString = function( value ) {
456 this.toString = function() {
457 return String( value );
461 assert.ok( t( "-10" ), "Negative integer string" );
462 assert.ok( t( "0" ), "Zero string" );
463 assert.ok( t( "5" ), "Positive integer string" );
464 assert.ok( t( -16 ), "Negative integer number" );
465 assert.ok( t( 0 ), "Zero integer number" );
466 assert.ok( t( 32 ), "Positive integer number" );
467 assert.ok( t( "040" ), "Octal integer literal string" );
468 assert.ok( t( "0xFF" ), "Hexadecimal integer literal string" );
469 assert.ok( t( 0xFFF ), "Hexadecimal integer literal" );
470 assert.ok( t( "-1.6" ), "Negative floating point string" );
471 assert.ok( t( "4.536" ), "Positive floating point string" );
472 assert.ok( t( -2.6 ), "Negative floating point number" );
473 assert.ok( t( 3.1415 ), "Positive floating point number" );
474 assert.ok( t( 1.5999999999999999 ), "Very precise floating point number" );
475 assert.ok( t( 8e5 ), "Exponential notation" );
476 assert.ok( t( "123e-2" ), "Exponential notation string" );
478 assert.equal( t( new ToString( "42" ) ), false, "Custom .toString returning number" );
479 assert.equal( t( "" ), false, "Empty string" );
480 assert.equal( t( " " ), false, "Whitespace characters string" );
481 assert.equal( t( "\t\t" ), false, "Tab characters string" );
482 assert.equal( t( "abcdefghijklm1234567890" ), false, "Alphanumeric character string" );
483 assert.equal( t( "xabcdefx" ), false, "Non-numeric character string" );
484 assert.equal( t( true ), false, "Boolean true literal" );
485 assert.equal( t( false ), false, "Boolean false literal" );
486 assert.equal( t( "bcfed5.2" ), false, "Number with preceding non-numeric characters" );
487 assert.equal( t( "7.2acdgs" ), false, "Number with trailling non-numeric characters" );
488 assert.equal( t( undefined ), false, "Undefined value" );
489 assert.equal( t( null ), false, "Null value" );
490 assert.equal( t( NaN ), false, "NaN value" );
491 assert.equal( t( Infinity ), false, "Infinity primitive" );
492 assert.equal( t( Number.POSITIVE_INFINITY ), false, "Positive Infinity" );
493 assert.equal( t( Number.NEGATIVE_INFINITY ), false, "Negative Infinity" );
494 assert.equal( t( new ToString( "Devo" ) ), false, "Custom .toString returning non-number" );
495 assert.equal( t( {} ), false, "Empty object" );
496 assert.equal( t( [] ), false, "Empty array" );
497 assert.equal( t( [ 42 ] ), false, "Array with one number" );
498 assert.equal( t( function() {} ), false, "Instance of a function" );
499 assert.equal( t( new Date() ), false, "Instance of a Date" );
502 QUnit[ typeof Symbol === "function" ? "test" : "skip" ]( "isNumeric(Symbol)", function( assert ) {
505 assert.equal( jQuery.isNumeric( Symbol() ), false, "Symbol" );
506 assert.equal( jQuery.isNumeric( Object( Symbol() ) ), false, "Symbol inside an object" );
509 QUnit.test( "isXMLDoc - HTML", function( assert ) {
512 assert.ok( !jQuery.isXMLDoc( document ), "HTML document" );
513 assert.ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
514 assert.ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
517 iframe = document.createElement( "iframe" );
518 document.body.appendChild( iframe );
521 body = jQuery( iframe ).contents()[ 0 ];
524 assert.ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
526 assert.ok( false, "Iframe body element exception" );
530 assert.ok( true, "Iframe body element - iframe not working correctly" );
533 document.body.removeChild( iframe );
536 QUnit.test( "XSS via location.hash", function( assert ) {
540 jQuery[ "_check9521" ] = function( x ) {
541 assert.ok( x, "script called from #id-like selector with inline handler" );
542 jQuery( "#check9521" ).remove();
543 delete jQuery[ "_check9521" ];
548 // This throws an error because it's processed like an id
549 jQuery( "#<img id='check9521' src='no-such-.gif' onerror='jQuery._check9521(false)'>" ).appendTo( "#qunit-fixture" );
551 jQuery[ "_check9521" ]( true );
555 QUnit.test( "isXMLDoc - XML", function( assert ) {
557 var xml = createDashboardXML();
558 assert.ok( jQuery.isXMLDoc( xml ), "XML document" );
559 assert.ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" );
560 assert.ok( jQuery.isXMLDoc( jQuery( "tab", xml )[ 0 ] ), "XML Tab Element" );
563 QUnit.test( "isWindow", function( assert ) {
566 assert.ok( jQuery.isWindow( window ), "window" );
567 assert.ok( jQuery.isWindow( document.getElementsByTagName( "iframe" )[ 0 ].contentWindow ), "iframe.contentWindow" );
568 assert.ok( !jQuery.isWindow(), "empty" );
569 assert.ok( !jQuery.isWindow( null ), "null" );
570 assert.ok( !jQuery.isWindow( undefined ), "undefined" );
571 assert.ok( !jQuery.isWindow( document ), "document" );
572 assert.ok( !jQuery.isWindow( document.documentElement ), "documentElement" );
573 assert.ok( !jQuery.isWindow( "" ), "string" );
574 assert.ok( !jQuery.isWindow( 1 ), "number" );
575 assert.ok( !jQuery.isWindow( true ), "boolean" );
576 assert.ok( !jQuery.isWindow( {} ), "object" );
577 assert.ok( !jQuery.isWindow( { setInterval: function() {} } ), "fake window" );
578 assert.ok( !jQuery.isWindow( /window/ ), "regexp" );
579 assert.ok( !jQuery.isWindow( function() {} ), "function" );
582 QUnit.test( "jQuery('html')", function( assert ) {
587 jQuery[ "foo" ] = false;
588 s = jQuery( "<script>jQuery.foo='test';</script>" )[ 0 ];
589 assert.ok( s, "Creating a script" );
590 assert.ok( !jQuery[ "foo" ], "Make sure the script wasn't executed prematurely" );
591 jQuery( "body" ).append( "<script>jQuery.foo='test';</script>" );
592 assert.ok( jQuery[ "foo" ], "Executing a scripts contents in the right context" );
594 // Test multi-line HTML
595 div = jQuery( "<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>" )[ 0 ];
596 assert.equal( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
597 assert.equal( div.firstChild.nodeType, 3, "Text node." );
598 assert.equal( div.lastChild.nodeType, 3, "Text node." );
599 assert.equal( div.childNodes[ 1 ].nodeType, 1, "Paragraph." );
600 assert.equal( div.childNodes[ 1 ].firstChild.nodeType, 3, "Paragraph text." );
602 assert.ok( jQuery( "<link rel='stylesheet'/>" )[ 0 ], "Creating a link" );
604 assert.ok( !jQuery( "<script/>" )[ 0 ].parentNode, "Create a script" );
606 assert.ok( jQuery( "<input/>" ).attr( "type", "hidden" ), "Create an input and set the type." );
608 j = jQuery( "<span>hi</span> there <!-- mon ami -->" );
609 assert.ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
611 assert.ok( !jQuery( "<option>test</option>" )[ 0 ].selected, "Make sure that options are auto-selected #2050" );
613 assert.ok( jQuery( "<div></div>" )[ 0 ], "Create a div with closing tag." );
614 assert.ok( jQuery( "<table></table>" )[ 0 ], "Create a table with closing tag." );
616 assert.equal( jQuery( "element[attribute='<div></div>']" ).length, 0,
617 "When html is within brackets, do not recognize as html." );
619 //equal( jQuery( "element[attribute=<div></div>]" ).length, 0,
620 // "When html is within brackets, do not recognize as html." );
621 assert.equal( jQuery( "element:not(<div></div>)" ).length, 0,
622 "When html is within parens, do not recognize as html." );
623 assert.equal( jQuery( "\\<div\\>" ).length, 0, "Ignore escaped html characters" );
626 QUnit.test( "jQuery(tag-hyphenated elements) gh-1987", function( assert ) {
629 jQuery.each( "thead tbody tfoot colgroup caption tr th td".split( " " ), function( i, name ) {
630 var j = jQuery( "<" + name + "-d></" + name + "-d>" );
631 assert.ok( j[ 0 ], "Create a tag-hyphenated elements" );
632 assert.ok( jQuery.nodeName( j[ 0 ], name.toUpperCase() + "-D" ), "Tag-hyphenated element has expected node name" );
635 var j = jQuery( "<tr-multiple-hyphens></tr-multiple-hyphens>" );
636 assert.ok( jQuery.nodeName( j[ 0 ], "TR-MULTIPLE-HYPHENS" ), "Element with multiple hyphens in its tag has expected node name" );
639 QUnit.test( "jQuery('massive html #7990')", function( assert ) {
643 li = "<li>very very very very large html string</li>",
646 for ( i = 0; i < 30000; i += 1 ) {
647 html[ html.length ] = li;
649 html[ html.length ] = "</ul>";
650 html = jQuery( html.join( "" ) )[ 0 ];
651 assert.equal( html.nodeName.toLowerCase(), "ul" );
652 assert.equal( html.firstChild.nodeName.toLowerCase(), "li" );
653 assert.equal( html.childNodes.length, 30000 );
656 QUnit.test( "jQuery('html', context)", function( assert ) {
659 var $div = jQuery( "<div/>" )[ 0 ],
660 $span = jQuery( "<span/>", $div );
661 assert.equal( $span.length, 1, "verify a span created with a div context works, #1763" );
664 QUnit.test( "jQuery(selector, xml).text(str) - loaded via xml document", function( assert ) {
667 var xml = createDashboardXML(),
669 // tests for #1419 where ie was a problem
670 tab = jQuery( "tab", xml ).eq( 0 );
671 assert.equal( tab.text(), "blabla", "verify initial text correct" );
672 tab.text( "newtext" );
673 assert.equal( tab.text(), "newtext", "verify new text correct" );
676 QUnit.test( "end()", function( assert ) {
678 assert.equal( "Yahoo", jQuery( "#yahoo" ).parent().end().text(), "check for end" );
679 assert.ok( jQuery( "#yahoo" ).end(), "check for end with nothing to end" );
681 var x = jQuery( "#yahoo" );
683 assert.equal( "Yahoo", jQuery( "#yahoo" ).text(), "check for non-destructive behaviour" );
686 QUnit.test( "length", function( assert ) {
688 assert.equal( jQuery( "#qunit-fixture p" ).length, 6, "Get Number of Elements Found" );
691 QUnit.test( "get()", function( assert ) {
693 assert.deepEqual( jQuery( "#qunit-fixture p" ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Get All Elements" );
696 QUnit.test( "toArray()", function( assert ) {
698 assert.deepEqual( jQuery( "#qunit-fixture p" ).toArray(),
699 q( "firstp", "ap", "sndp", "en", "sap", "first" ),
700 "Convert jQuery object to an Array" );
703 QUnit.test( "inArray()", function( assert ) {
707 p: q( "firstp", "sap", "ap", "first" ),
708 em: q( "siblingnext", "siblingfirst" ),
709 div: q( "qunit-testrunner-toolbar", "nothiddendiv", "nothiddendivchild", "foo" ),
710 a: q( "mark", "groups", "google", "simon1" ),
714 p: { elem: jQuery( "#ap" )[ 0 ], index: 2 },
715 em: { elem: jQuery( "#siblingfirst" )[ 0 ], index: 1 },
716 div: { elem: jQuery( "#nothiddendiv" )[ 0 ], index: 1 },
717 a: { elem: jQuery( "#simon1" )[ 0 ], index: 3 }
720 p: jQuery( "#liveSpan1" )[ 0 ],
721 em: jQuery( "#nothiddendiv" )[ 0 ],
725 jQuery.each( tests, function( key, obj ) {
726 assert.equal( jQuery.inArray( obj.elem, selections[ key ] ), obj.index, "elem is in the array of selections of its tag" );
728 // Third argument (fromIndex)
729 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" );
730 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" );
731 assert.equal( !!~jQuery.inArray( obj.elem, selections[ key ], -3 ), true, "elem is in the array of selections given a negative index" );
734 jQuery.each( falseTests, function( key, elem ) {
735 assert.equal( !!~jQuery.inArray( elem, selections[ key ] ), false, "elem is NOT in the array of selections" );
740 QUnit.test( "get(Number)", function( assert ) {
742 assert.equal( jQuery( "#qunit-fixture p" ).get( 0 ), document.getElementById( "firstp" ), "Get A Single Element" );
743 assert.strictEqual( jQuery( "#firstp" ).get( 1 ), undefined, "Try get with index larger elements count" );
746 QUnit.test( "get(-Number)", function( assert ) {
748 assert.equal( jQuery( "p" ).get( -1 ), document.getElementById( "first" ), "Get a single element with negative index" );
749 assert.strictEqual( jQuery( "#firstp" ).get( -2 ), undefined, "Try get with index negative index larger then elements count" );
752 QUnit.test( "each(Function)", function( assert ) {
756 div = jQuery( "div" );
757 div.each( function() {this.foo = "zoo";} );
759 for ( i = 0; i < div.length; i++ ) {
760 if ( div.get( i ).foo !== "zoo" ) {
764 assert.ok( pass, "Execute a function, Relative" );
767 QUnit.test( "slice()", function( assert ) {
770 var $links = jQuery( "#ap a" );
772 assert.deepEqual( $links.slice( 1, 2 ).get(), q( "groups" ), "slice(1,2)" );
773 assert.deepEqual( $links.slice( 1 ).get(), q( "groups", "anchor1", "mark" ), "slice(1)" );
774 assert.deepEqual( $links.slice( 0, 3 ).get(), q( "google", "groups", "anchor1" ), "slice(0,3)" );
775 assert.deepEqual( $links.slice( -1 ).get(), q( "mark" ), "slice(-1)" );
777 assert.deepEqual( $links.eq( 1 ).get(), q( "groups" ), "eq(1)" );
778 assert.deepEqual( $links.eq( "2" ).get(), q( "anchor1" ), "eq('2')" );
779 assert.deepEqual( $links.eq( -1 ).get(), q( "mark" ), "eq(-1)" );
782 QUnit.test( "first()/last()", function( assert ) {
785 var $links = jQuery( "#ap a" ), $none = jQuery( "asdf" );
787 assert.deepEqual( $links.first().get(), q( "google" ), "first()" );
788 assert.deepEqual( $links.last().get(), q( "mark" ), "last()" );
790 assert.deepEqual( $none.first().get(), [], "first() none" );
791 assert.deepEqual( $none.last().get(), [], "last() none" );
794 QUnit.test( "map()", function( assert ) {
798 jQuery( "#ap" ).map( function() {
799 return jQuery( this ).find( "a" ).get();
801 q( "google", "groups", "anchor1", "mark" ),
806 jQuery( "#ap > a" ).map( function() {
807 return this.parentNode;
809 q( "ap", "ap", "ap" ),
814 QUnit.test( "jQuery.map", function( assert ) {
817 var i, label, result, callback;
819 result = jQuery.map( [ 3, 4, 5 ], function( v, k ) {
822 assert.equal( result.join( "" ), "012", "Map the keys from an array" );
824 result = jQuery.map( [ 3, 4, 5 ], function( v ) {
827 assert.equal( result.join( "" ), "345", "Map the values from an array" );
829 result = jQuery.map( { a: 1, b: 2 }, function( v, k ) {
832 assert.equal( result.join( "" ), "ab", "Map the keys from an object" );
834 result = jQuery.map( { a: 1, b: 2 }, function( v ) {
837 assert.equal( result.join( "" ), "12", "Map the values from an object" );
839 result = jQuery.map( [ "a", undefined, null, "b" ], function( v ) {
842 assert.equal( result.join( "" ), "ab", "Array iteration does not include undefined/null results" );
844 result = jQuery.map( { a: "a", b: undefined, c: null, d: "b" }, function( v ) {
847 assert.equal( result.join( "" ), "ab", "Object iteration does not include undefined/null results" );
851 One: function( a ) { a = a; },
852 Two: function( a, b ) { a = a; b = b; }
854 callback = function( v, k ) {
855 assert.equal( k, "foo", label + "-argument function treated like object" );
857 for ( i in result ) {
859 result[ i ].foo = "bar";
860 jQuery.map( result[ i ], callback );
864 "undefined": undefined,
869 "nonempty string": "string",
874 callback = function( v, k ) {
875 assert.equal( k, "length", "Object with " + label + " length treated like object" );
877 for ( i in result ) {
879 jQuery.map( { length: result[ i ] }, callback );
883 "sparse Array": Array( 4 ),
884 "length: 1 plain object": { length: 1, "0": true },
885 "length: 2 plain object": { length: 2, "0": true, "1": true },
886 NodeList: document.getElementsByTagName( "html" )
888 callback = function( v, k ) {
889 if ( result[ label ] ) {
890 delete result[ label ];
891 assert.equal( k, "0", label + " treated like array" );
894 for ( i in result ) {
896 jQuery.map( result[ i ], callback );
900 jQuery.map( { length: 0 }, function() {
903 assert.ok( !result, "length: 0 plain object treated like array" );
906 jQuery.map( document.getElementsByTagName( "asdf" ), function() {
909 assert.ok( !result, "empty NodeList treated like array" );
911 result = jQuery.map( Array( 4 ), function( v, k ) {
912 return k % 2 ? k : [ k,k,k ];
914 assert.equal( result.join( "" ), "00012223", "Array results flattened (#2616)" );
917 QUnit.test( "jQuery.merge()", function( assert ) {
921 jQuery.merge( [], [] ),
927 jQuery.merge( [ 1 ], [ 2 ] ),
929 "Basic (single-element)"
932 jQuery.merge( [ 1, 2 ], [ 3, 4 ] ),
934 "Basic (multiple-element)"
938 jQuery.merge( [ 1, 2 ], [] ),
943 jQuery.merge( [], [ 1, 2 ] ),
948 // Fixed at [5998], #3641
950 jQuery.merge( [ -2, -1 ], [ 0, 1, 2 ] ),
952 "Second array including a zero (falsy)"
955 // After fixing #5527
957 jQuery.merge( [], [ null, undefined ] ),
959 "Second array including null and undefined values"
962 jQuery.merge( { length: 0 }, [ 1, 2 ] ),
963 { length: 2, 0: 1, 1: 2 },
967 jQuery.merge( [ 1, 2 ], { length: 1, 0: 3 } ),
973 jQuery.merge( [], document.getElementById( "lengthtest" ).getElementsByTagName( "input" ) ),
974 [ document.getElementById( "length" ), document.getElementById( "idTest" ) ],
979 QUnit.test( "jQuery.grep()", function( assert ) {
982 var searchCriterion = function( value ) {
983 return value % 2 === 0;
986 assert.deepEqual( jQuery.grep( [], searchCriterion ), [], "Empty array" );
987 assert.deepEqual( jQuery.grep( new Array( 4 ), searchCriterion ), [], "Sparse array" );
990 jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion ),
992 "Satisfying elements present"
995 jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion ),
997 "Satisfying elements absent"
1001 jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, true ),
1003 "Satisfying elements present and grep inverted"
1006 jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, true ),
1008 "Satisfying elements absent and grep inverted"
1012 jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, false ),
1014 "Satisfying elements present but grep explicitly uninverted"
1017 jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, false ),
1019 "Satisfying elements absent and grep explicitly uninverted"
1023 QUnit.test( "jQuery.grep(Array-like)", function( assert ) {
1026 var searchCriterion = function( value ) {
1027 return value % 2 === 0;
1030 assert.deepEqual( jQuery.grep( { length: 0 }, searchCriterion ), [], "Empty array-like" );
1033 jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion ),
1035 "Satisfying elements present and array-like object used"
1038 jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion ),
1040 "Satisfying elements absent and Array-like object used"
1044 jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion, true ),
1046 "Satisfying elements present, array-like object used, and grep inverted"
1049 jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion, true ),
1051 "Satisfying elements absent, array-like object used, and grep inverted"
1055 jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion, false ),
1057 "Satisfying elements present, Array-like object used, but grep explicitly uninverted"
1060 jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion, false ),
1062 "Satisfying elements absent, Array-like object used, and grep explicitly uninverted"
1066 QUnit.test( "jQuery.extend(Object, Object)", function( assert ) {
1067 assert.expect( 28 );
1069 var empty, optionsWithLength, optionsWithDate, myKlass,
1070 customObject, optionsWithCustomObject, MyNumber, ret,
1071 nullUndef, target, recursive, obj,
1072 defaults, defaultsCopy, options1, options1Copy, options2, options2Copy, merged2,
1073 settings = { "xnumber1": 5, "xnumber2": 7, "xstring1": "peter", "xstring2": "pan" },
1074 options = { "xnumber2": 1, "xstring2": "x", "xxx": "newstring" },
1075 optionsCopy = { "xnumber2": 1, "xstring2": "x", "xxx": "newstring" },
1076 merged = { "xnumber1": 5, "xnumber2": 1, "xstring1": "peter", "xstring2": "x", "xxx": "newstring" },
1077 deep1 = { "foo": { "bar": true } },
1078 deep2 = { "foo": { "baz": true }, "foo2": document },
1079 deep2copy = { "foo": { "baz": true }, "foo2": document },
1080 deepmerged = { "foo": { "bar": true, "baz": true }, "foo2": document },
1082 nestedarray = { "arr": arr };
1084 jQuery.extend( settings, options );
1085 assert.deepEqual( settings, merged, "Check if extended: settings must be extended" );
1086 assert.deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
1088 jQuery.extend( settings, null, options );
1089 assert.deepEqual( settings, merged, "Check if extended: settings must be extended" );
1090 assert.deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
1092 jQuery.extend( true, deep1, deep2 );
1093 assert.deepEqual( deep1[ "foo" ], deepmerged[ "foo" ], "Check if foo: settings must be extended" );
1094 assert.deepEqual( deep2[ "foo" ], deep2copy[ "foo" ], "Check if not deep2: options must not be modified" );
1095 assert.equal( deep1[ "foo2" ], document, "Make sure that a deep clone was not attempted on the document" );
1097 assert.ok( jQuery.extend( true, {}, nestedarray )[ "arr" ] !== arr, "Deep extend of object must clone child array" );
1100 assert.ok( jQuery.isArray( jQuery.extend( true, { "arr": {} }, nestedarray )[ "arr" ] ), "Cloned array have to be an Array" );
1101 assert.ok( jQuery.isPlainObject( jQuery.extend( true, { "arr": arr }, { "arr": {} } )[ "arr" ] ), "Cloned object have to be an plain object" );
1104 optionsWithLength = { "foo": { "length": -1 } };
1105 jQuery.extend( true, empty, optionsWithLength );
1106 assert.deepEqual( empty[ "foo" ], optionsWithLength[ "foo" ], "The length property must copy correctly" );
1109 optionsWithDate = { "foo": { "date": new Date() } };
1110 jQuery.extend( true, empty, optionsWithDate );
1111 assert.deepEqual( empty[ "foo" ], optionsWithDate[ "foo" ], "Dates copy correctly" );
1114 myKlass = function() {};
1115 customObject = new myKlass();
1116 optionsWithCustomObject = { "foo": { "date": customObject } };
1118 jQuery.extend( true, empty, optionsWithCustomObject );
1119 assert.ok( empty[ "foo" ] && empty[ "foo" ][ "date" ] === customObject, "Custom objects copy correctly (no methods)" );
1121 // Makes the class a little more realistic
1122 myKlass.prototype = { "someMethod": function() {} };
1124 jQuery.extend( true, empty, optionsWithCustomObject );
1125 assert.ok( empty[ "foo" ] && empty[ "foo" ][ "date" ] === customObject, "Custom objects copy correctly" );
1129 ret = jQuery.extend( true, { "foo": 4 }, { "foo": new MyNumber( 5 ) } );
1130 assert.ok( parseInt( ret.foo, 10 ) === 5, "Wrapped numbers copy correctly" );
1133 nullUndef = jQuery.extend( {}, options, { "xnumber2": null } );
1134 assert.ok( nullUndef[ "xnumber2" ] === null, "Check to make sure null values are copied" );
1136 nullUndef = jQuery.extend( {}, options, { "xnumber2": undefined } );
1137 assert.ok( nullUndef[ "xnumber2" ] === options[ "xnumber2" ], "Check to make sure undefined values are not copied" );
1139 nullUndef = jQuery.extend( {}, options, { "xnumber0": null } );
1140 assert.ok( nullUndef[ "xnumber0" ] === null, "Check to make sure null values are inserted" );
1143 recursive = { foo:target, bar:5 };
1144 jQuery.extend( true, target, recursive );
1145 assert.deepEqual( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
1147 ret = jQuery.extend( true, { foo: [] }, { foo: [ 0 ] } ); // 1907
1148 assert.equal( ret.foo.length, 1, "Check to make sure a value with coercion 'false' copies over when necessary to fix #1907" );
1150 ret = jQuery.extend( true, { foo: "1,2,3" }, { foo: [ 1, 2, 3 ] } );
1151 assert.ok( typeof ret.foo !== "string", "Check to make sure values equal with coercion (but not actually equal) overwrite correctly" );
1153 ret = jQuery.extend( true, { foo:"bar" }, { foo:null } );
1154 assert.ok( typeof ret.foo !== "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" );
1157 jQuery.extend( true, obj, { foo:"notnull" } );
1158 assert.equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );
1161 jQuery.extend( func, { key: "value" } );
1162 assert.equal( func.key, "value", "Verify a function can be extended" );
1164 defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" };
1165 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" };
1166 options1 = { xnumber2: 1, xstring2: "x" };
1167 options1Copy = { xnumber2: 1, xstring2: "x" };
1168 options2 = { xstring2: "xx", xxx: "newstringx" };
1169 options2Copy = { xstring2: "xx", xxx: "newstringx" };
1170 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
1172 settings = jQuery.extend( {}, defaults, options1, options2 );
1173 assert.deepEqual( settings, merged2, "Check if extended: settings must be extended" );
1174 assert.deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
1175 assert.deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" );
1176 assert.deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" );
1179 QUnit.test( "jQuery.extend(Object, Object {created with \"defineProperties\"})", function( assert ) {
1182 var definedObj = Object.defineProperties({}, {
1189 "nonenumerableProp": {
1197 jQuery.extend( accessorObj, definedObj );
1198 assert.equal( accessorObj.enumerableProp, true, "Verify that getters are transferred" );
1199 assert.equal( accessorObj.nonenumerableProp, undefined, "Verify that non-enumerable getters are ignored" );
1202 QUnit.test( "jQuery.extend(true,{},{a:[], o:{}}); deep copy with array, followed by object", function( assert ) {
1205 var result, initial = {
1207 // This will make "copyIsArray" true
1208 array: [ 1, 2, 3, 4 ],
1210 // If "copyIsArray" doesn't get reset to false, the check
1211 // will evaluate true and enter the array copy block
1212 // instead of the object copy block. Since the ternary in the
1213 // "copyIsArray" block will will evaluate to false
1214 // (check if operating on an array with ), this will be
1215 // replaced by an empty array.
1219 result = jQuery.extend( true, {}, initial );
1221 assert.deepEqual( result, initial, "The [result] and [initial] have equal shape and values" );
1222 assert.ok( !jQuery.isArray( result.object ), "result.object wasn't paved with an empty array" );
1225 QUnit.test( "jQuery.each(Object,Function)", function( assert ) {
1226 assert.expect( 23 );
1228 var i, label, seen, callback;
1231 jQuery.each( [ 3, 4, 5 ], function( k, v ) {
1234 assert.deepEqual( seen, { "0": 3, "1": 4, "2": 5 }, "Array iteration" );
1237 jQuery.each( { name: "name", lang: "lang" }, function( k, v ) {
1240 assert.deepEqual( seen, { name: "name", lang: "lang" }, "Object iteration" );
1243 jQuery.each( [ 1, 2, 3 ], function( k, v ) {
1249 assert.deepEqual( seen, [ 1, 2 ], "Broken array iteration" );
1252 jQuery.each( { "a": 1, "b": 2,"c": 3 }, function( k, v ) {
1256 assert.deepEqual( seen, [ 1 ], "Broken object iteration" );
1259 Zero: function() {},
1260 One: function( a ) { a = a; },
1261 Two: function( a, b ) { a = a; b = b; }
1263 callback = function( k ) {
1264 assert.equal( k, "foo", label + "-argument function treated like object" );
1268 seen[ i ].foo = "bar";
1269 jQuery.each( seen[ i ], callback );
1273 "undefined": undefined,
1278 "nonempty string": "string",
1279 "string \"0\"": "0",
1283 callback = function( k ) {
1284 assert.equal( k, "length", "Object with " + label + " length treated like object" );
1288 jQuery.each( { length: seen[ i ] }, callback );
1292 "sparse Array": Array( 4 ),
1293 "length: 1 plain object": { length: 1, "0": true },
1294 "length: 2 plain object": { length: 2, "0": true, "1": true },
1295 NodeList: document.getElementsByTagName( "html" )
1297 callback = function( k ) {
1298 if ( seen[ label ] ) {
1299 delete seen[ label ];
1300 assert.equal( k, "0", label + " treated like array" );
1306 jQuery.each( seen[ i ], callback );
1310 jQuery.each( { length: 0 }, function() {
1313 assert.ok( !seen, "length: 0 plain object treated like array" );
1316 jQuery.each( document.getElementsByTagName( "asdf" ), function() {
1319 assert.ok( !seen, "empty NodeList treated like array" );
1322 jQuery.each( document.styleSheets, function() {
1325 assert.equal( i, document.styleSheets.length, "Iteration over document.styleSheets" );
1328 QUnit.test( "jQuery.each/map(undefined/null,Function)", function( assert ) {
1332 jQuery.each( undefined, jQuery.noop );
1333 jQuery.each( null, jQuery.noop );
1334 jQuery.map( undefined, jQuery.noop );
1335 jQuery.map( null, jQuery.noop );
1336 assert.ok( true, "jQuery.each/map( undefined/null, function() {} );" );
1338 assert.ok( false, "each/map must accept null and undefined values" );
1342 QUnit.test( "JIT compilation does not interfere with length retrieval (gh-2145)", function( assert ) {
1347 // Trigger JIT compilation of jQuery.each – and therefore isArraylike – in iOS.
1348 // Convince JSC to use one of its optimizing compilers
1349 // by providing code which can be LICM'd into nothing.
1350 for ( i = 0; i < 1000; i++ ) {
1355 jQuery.each( { 1: "1", 2: "2", 3: "3" }, function( index ) {
1356 assert.equal( ++i, index, "Iteration over object with solely " +
1357 "numeric indices (gh-2145 JIT iOS 8 bug)" );
1359 assert.equal( i, 3, "Iteration over object with solely " +
1360 "numeric indices (gh-2145 JIT iOS 8 bug)" );
1363 QUnit.test( "jQuery.makeArray", function( assert ) {
1364 assert.expect( 15 );
1366 assert.equal( jQuery.makeArray( jQuery( "html>*" ) )[ 0 ].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
1368 assert.equal( jQuery.makeArray( document.getElementsByName( "PWD" ) ).slice( 0, 1 )[ 0 ].name, "PWD", "Pass makeArray a nodelist" );
1370 assert.equal( ( function() { return jQuery.makeArray( arguments ); } )( 1, 2 ).join( "" ), "12", "Pass makeArray an arguments array" );
1372 assert.equal( jQuery.makeArray( [ 1,2,3 ] ).join( "" ), "123", "Pass makeArray a real array" );
1374 assert.equal( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
1376 assert.equal( jQuery.makeArray( 0 )[ 0 ], 0, "Pass makeArray a number" );
1378 assert.equal( jQuery.makeArray( "foo" )[ 0 ], "foo", "Pass makeArray a string" );
1380 assert.equal( jQuery.makeArray( true )[ 0 ].constructor, Boolean, "Pass makeArray a boolean" );
1382 assert.equal( jQuery.makeArray( document.createElement( "div" ) )[ 0 ].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
1384 assert.equal( jQuery.makeArray( { length:2, 0:"a", 1:"b" } ).join( "" ), "ab", "Pass makeArray an array like map (with length)" );
1386 assert.ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice( 0, 1 )[ 0 ].nodeName, "Pass makeArray a childNodes array" );
1388 // function, is tricky as it has length
1389 assert.equal( jQuery.makeArray( function() { return 1;} )[ 0 ](), 1, "Pass makeArray a function" );
1391 //window, also has length
1392 assert.equal( jQuery.makeArray( window )[ 0 ], window, "Pass makeArray the window" );
1394 assert.equal( jQuery.makeArray( /a/ )[ 0 ].constructor, RegExp, "Pass makeArray a regex" );
1396 // Some nodes inherit traits of nodelists
1397 assert.ok( jQuery.makeArray( document.getElementById( "form" ) ).length >= 13,
1398 "Pass makeArray a form (treat as elements)" );
1401 QUnit.test( "jQuery.inArray", function( assert ) {
1404 assert.equal( jQuery.inArray( 0, false ), -1, "Search in 'false' as array returns -1 and doesn't throw exception" );
1406 assert.equal( jQuery.inArray( 0, null ), -1, "Search in 'null' as array returns -1 and doesn't throw exception" );
1408 assert.equal( jQuery.inArray( 0, undefined ), -1, "Search in 'undefined' as array returns -1 and doesn't throw exception" );
1411 QUnit.test( "jQuery.isEmptyObject", function( assert ) {
1414 assert.equal( true, jQuery.isEmptyObject( {} ), "isEmptyObject on empty object literal" );
1415 assert.equal( false, jQuery.isEmptyObject( { a:1 } ), "isEmptyObject on non-empty object literal" );
1417 // What about this ?
1418 // equal(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
1421 QUnit.test( "jQuery.proxy", function( assert ) {
1424 var test2, test3, test4, fn, cb,
1426 assert.equal( this, thisObject, "Make sure that scope is set properly." );
1428 thisObject = { foo: "bar", method: test };
1430 // Make sure normal works
1431 test.call( thisObject );
1434 jQuery.proxy( test, thisObject )();
1436 // Another take on it
1437 jQuery.proxy( thisObject, "method" )();
1439 // Make sure it doesn't freak out
1440 assert.equal( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
1442 // Partial application
1443 test2 = function( a ) {
1444 assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
1446 jQuery.proxy( test2, null, "pre-applied" )();
1448 // Partial application w/ normal arguments
1449 test3 = function( a, b ) {
1450 assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
1452 jQuery.proxy( test3, null, "pre-applied" )( "normal" );
1455 test4 = { "meth": function( a ) {
1456 assert.equal( a, "boom", "Ensure old syntax works." );
1458 jQuery.proxy( test4, "meth" )( "boom" );
1460 // jQuery 1.9 improved currying with `this` object
1462 assert.equal( Array.prototype.join.call( arguments, "," ), "arg1,arg2,arg3", "args passed" );
1463 assert.equal( this.foo, "bar", "this-object passed" );
1465 cb = jQuery.proxy( fn, null, "arg1", "arg2" );
1466 cb.call( thisObject, "arg3" );
1469 QUnit.test( "jQuery.parseHTML", function( assert ) {
1470 assert.expect( 23 );
1474 assert.deepEqual( jQuery.parseHTML(), [], "Without arguments" );
1475 assert.deepEqual( jQuery.parseHTML( undefined ), [], "Undefined" );
1476 assert.deepEqual( jQuery.parseHTML( null ), [], "Null" );
1477 assert.deepEqual( jQuery.parseHTML( false ), [], "Boolean false" );
1478 assert.deepEqual( jQuery.parseHTML( 0 ), [], "Zero" );
1479 assert.deepEqual( jQuery.parseHTML( true ), [], "Boolean true" );
1480 assert.deepEqual( jQuery.parseHTML( 42 ), [], "Positive number" );
1481 assert.deepEqual( jQuery.parseHTML( "" ), [], "Empty string" );
1482 assert.throws( function() {
1483 jQuery.parseHTML( "<div></div>", document.getElementById( "form" ) );
1484 }, "Passing an element as the context raises an exception (context should be a document)" );
1486 nodes = jQuery.parseHTML( jQuery( "body" )[ 0 ].innerHTML );
1487 assert.ok( nodes.length > 4, "Parse a large html string" );
1488 assert.equal( jQuery.type( nodes ), "array", "parseHTML returns an array rather than a nodelist" );
1490 html = "<script>undefined()</script>";
1491 assert.equal( jQuery.parseHTML( html ).length, 0, "Ignore scripts by default" );
1492 assert.equal( jQuery.parseHTML( html, true )[ 0 ].nodeName.toLowerCase(), "script", "Preserve scripts when requested" );
1494 html += "<div></div>";
1495 assert.equal( jQuery.parseHTML( html )[ 0 ].nodeName.toLowerCase(), "div", "Preserve non-script nodes" );
1496 assert.equal( jQuery.parseHTML( html, true )[ 0 ].nodeName.toLowerCase(), "script", "Preserve script position" );
1498 assert.equal( jQuery.parseHTML( "text" )[ 0 ].nodeType, 3, "Parsing text returns a text node" );
1499 assert.equal( jQuery.parseHTML( "\t<div></div>" )[ 0 ].nodeValue, "\t", "Preserve leading whitespace" );
1501 assert.equal( jQuery.parseHTML( " <div/> " )[ 0 ].nodeType, 3, "Leading spaces are treated as text nodes (#11290)" );
1503 html = jQuery.parseHTML( "<div>test div</div>" );
1505 assert.equal( html[ 0 ].parentNode.nodeType, 11, "parentNode should be documentFragment" );
1506 assert.equal( html[ 0 ].innerHTML, "test div", "Content should be preserved" );
1508 assert.equal( jQuery.parseHTML( "<span><span>" ).length, 1, "Incorrect html-strings should not break anything" );
1509 assert.equal( jQuery.parseHTML( "<td><td>" )[ 1 ].parentNode.nodeType, 11,
1510 "parentNode should be documentFragment for wrapMap (variable in manipulation module) elements too" );
1511 assert.ok( jQuery.parseHTML( "<#if><tr><p>This is a test.</p></tr><#/if>" ) || true, "Garbage input should not cause error" );
1514 if ( jQuery.support.createHTMLDocument ) {
1515 QUnit.asyncTest( "jQuery.parseHTML", function( assert ) {
1518 Globals.register( "parseHTMLError" );
1520 jQuery.globalEval( "parseHTMLError = false;" );
1521 jQuery.parseHTML( "<img src=x onerror='parseHTMLError = true'>" );
1523 window.setTimeout( function() {
1525 assert.equal( window.parseHTMLError, false, "onerror eventhandler has not been called." );
1530 QUnit.test( "jQuery.parseJSON", function( assert ) {
1531 assert.expect( 20 );
1533 assert.strictEqual( jQuery.parseJSON( null ), null, "primitive null" );
1534 assert.strictEqual( jQuery.parseJSON( "0.88" ), 0.88, "Number" );
1536 jQuery.parseJSON( "\" \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u007E \\u263a \"" ),
1537 " \" \\ / \b \f \n \r \t ~ \u263A ",
1540 assert.deepEqual( jQuery.parseJSON( "{}" ), {}, "Empty object" );
1541 assert.deepEqual( jQuery.parseJSON( "{\"test\":1}" ), { "test": 1 }, "Plain object" );
1542 assert.deepEqual( jQuery.parseJSON( "[0]" ), [ 0 ], "Simple array" );
1545 jQuery.parseJSON( "[ \"string\", -4.2, 2.7180e0, 3.14E-1, {}, [], true, false, null ]" ),
1546 [ "string", -4.2, 2.718, 0.314, {}, [], true, false, null ],
1547 "Array of all data types"
1550 jQuery.parseJSON( "{ \"string\": \"\", \"number\": 4.2e+1, \"object\": {}," +
1551 "\"array\": [[]], \"boolean\": [ true, false ], \"null\": null }" ),
1552 { string: "", number: 42, object: {}, array: [ [] ], "boolean": [ true, false ], "null": null },
1553 "Dictionary of all data types"
1556 assert.deepEqual( jQuery.parseJSON( "\n{\"test\":1}\t" ), { "test": 1 },
1557 "Leading and trailing whitespace are ignored" );
1559 assert.throws( function() {
1561 }, null, "Undefined raises an error" );
1562 assert.throws( function() {
1563 jQuery.parseJSON( "" );
1564 }, null, "Empty string raises an error" );
1565 assert.throws( function() {
1566 jQuery.parseJSON( "''" );
1567 }, null, "Single-quoted string raises an error" );
1571 assert.throws(function() {
1572 jQuery.parseJSON("\" \\a \"");
1573 }, null, "Invalid string escape raises an error" );
1575 // Broken on IE8, Safari 5.1 Windows
1576 assert.throws(function() {
1577 jQuery.parseJSON("\"\t\"");
1578 }, null, "Unescaped control character raises an error" );
1581 assert.throws(function() {
1582 jQuery.parseJSON(".123");
1583 }, null, "Number with no integer component raises an error" );
1586 assert.throws( function() {
1587 var result = jQuery.parseJSON( "0101" );
1590 // Ensure base-10 interpretation on browsers that erroneously accept leading-zero numbers
1591 if ( result === 101 ) {
1592 throw new Error( "close enough" );
1594 }, null, "Leading-zero number raises an error or is parsed as decimal" );
1595 assert.throws( function() {
1596 jQuery.parseJSON( "{a:1}" );
1597 }, null, "Unquoted property raises an error" );
1598 assert.throws( function() {
1599 jQuery.parseJSON( "{'a':1}" );
1600 }, null, "Single-quoted property raises an error" );
1601 assert.throws( function() {
1602 jQuery.parseJSON( "[,]" );
1603 }, null, "Array element elision raises an error" );
1604 assert.throws( function() {
1605 jQuery.parseJSON( "{},[]" );
1606 }, null, "Comma expression raises an error" );
1607 assert.throws( function() {
1608 jQuery.parseJSON( "[]\n,{}" );
1609 }, null, "Newline-containing comma expression raises an error" );
1610 assert.throws( function() {
1611 jQuery.parseJSON( "\"\"\n\"\"" );
1612 }, null, "Automatic semicolon insertion raises an error" );
1614 assert.strictEqual( jQuery.parseJSON( [ 0 ] ), 0, "Input cast to string" );
1617 QUnit.test( "jQuery.parseXML", function( assert ) {
1622 xml = jQuery.parseXML( "<p>A <b>well-formed</b> xml string</p>" );
1623 tmp = xml.getElementsByTagName( "p" )[ 0 ];
1624 assert.ok( !!tmp, "<p> present in document" );
1625 tmp = tmp.getElementsByTagName( "b" )[ 0 ];
1626 assert.ok( !!tmp, "<b> present in document" );
1627 assert.strictEqual( tmp.childNodes[ 0 ].nodeValue, "well-formed", "<b> text is as expected" );
1629 assert.strictEqual( e, undefined, "unexpected error" );
1632 xml = jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
1633 assert.ok( false, "invalid xml not detected" );
1635 assert.strictEqual( e.message, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "invalid xml detected" );
1638 xml = jQuery.parseXML( "" );
1639 assert.strictEqual( xml, null, "empty string => null document" );
1640 xml = jQuery.parseXML();
1641 assert.strictEqual( xml, null, "undefined string => null document" );
1642 xml = jQuery.parseXML( null );
1643 assert.strictEqual( xml, null, "null string => null document" );
1644 xml = jQuery.parseXML( true );
1645 assert.strictEqual( xml, null, "non-string => null document" );
1647 assert.ok( false, "empty input throws exception" );
1651 QUnit.test( "jQuery.camelCase()", function( assert ) {
1654 "foo-bar": "fooBar",
1655 "foo-bar-baz": "fooBarBaz",
1656 "girl-u-want": "girlUWant",
1657 "the-4th-dimension": "the-4thDimension",
1658 "-o-tannenbaum": "OTannenbaum",
1659 "-moz-illa": "MozIlla",
1660 "-ms-take": "msTake"
1665 jQuery.each( tests, function( key, val ) {
1666 assert.equal( jQuery.camelCase( key ), val, "Converts: " + key + " => " + val );
1670 testIframeWithCallback(
1671 "Conditional compilation compatibility (#13274)",
1673 function( cc_on, errors, $, assert ) {
1675 assert.ok( true, "JScript conditional compilation " + ( cc_on ? "supported" : "not supported" ) );
1676 assert.deepEqual( errors, [], "No errors" );
1677 assert.ok( $(), "jQuery executes" );
1681 // iOS7 doesn't fire the load event if the long-loading iframe gets its source reset to about:blank.
1682 // This makes this test fail but it doesn't seem to cause any real-life problems so blacklisting
1683 // this test there is preferred to complicating the hard-to-test core/ready code further.
1684 if ( !/iphone os 7_/i.test( navigator.userAgent ) ) {
1685 testIframeWithCallback(
1686 "document ready when jQuery loaded asynchronously (#13655)",
1687 "core/dynamic_ready.html",
1688 function( ready, assert ) {
1690 assert.equal( true, ready, "document ready correctly fired when jQuery is loaded after DOMContentLoaded" );
1695 testIframeWithCallback(
1696 "Tolerating alias-masked DOM properties (#14074)",
1697 "core/aliased.html",
1698 function( errors, assert ) {
1700 assert.deepEqual( errors, [], "jQuery loaded" );
1704 testIframeWithCallback(
1705 "Don't call window.onready (#14802)",
1706 "core/onready.html",
1707 function( error, assert ) {
1709 assert.equal( error, false, "no call to user-defined onready" );
1713 QUnit.test( "Iterability of jQuery objects (gh-1693)", function( assert ) {
1714 /* jshint unused: false */
1717 var i, elem, result;
1719 if ( typeof Symbol === "function" ) {
1721 elem = jQuery( "<div></div><span></span><a></a>" );
1725 eval( "for ( i of elem ) { result += i.nodeName; }" );
1727 assert.equal( result, "DIVSPANA", "for-of works on jQuery objects" );
1729 assert.ok( true, "The browser doesn't support Symbols" );