Core: Prevent Object.prototype pollution for $.extend( true, ... )
[jquery.git] / test / unit / core.js
blob28f40ab56a6349b10435e91fe809f292d51a39c7
1 QUnit.module( "core", {
2 beforeEach: function() {
3 this.sandbox = sinon.sandbox.create();
4 },
5 afterEach: function() {
6 this.sandbox.restore();
7 return moduleTeardown.apply( this, arguments );
9 } );
11 QUnit.test( "Basic requirements", function( assert ) {
12 assert.expect( 7 );
13 assert.ok( Array.prototype.push, "Array.push()" );
14 assert.ok( Function.prototype.apply, "Function.apply()" );
15 assert.ok( document.getElementById, "getElementById" );
16 assert.ok( document.getElementsByTagName, "getElementsByTagName" );
17 assert.ok( RegExp, "RegExp" );
18 assert.ok( jQuery, "jQuery" );
19 assert.ok( $, "$" );
20 } );
22 QUnit.test( "jQuery()", function( assert ) {
24 var elem, i,
25 obj = jQuery( "div" ),
26 code = jQuery( "<code/>" ),
27 img = jQuery( "<img/>" ),
28 div = jQuery( "<div/><hr/><code/><b/>" ),
29 exec = false,
30 expected = 23,
31 attrObj = {
32 "text": "test",
33 "class": "test2",
34 "id": "test3"
37 // The $(html, props) signature can stealth-call any $.fn method, check for a
38 // few here but beware of modular builds where these methods may be excluded.
39 if ( jQuery.fn.click ) {
40 expected++;
41 attrObj[ "click" ] = function() { assert.ok( exec, "Click executed." ); };
43 if ( jQuery.fn.width ) {
44 expected++;
45 attrObj[ "width" ] = 10;
47 if ( jQuery.fn.offset ) {
48 expected++;
49 attrObj[ "offset" ] = { "top": 1, "left": 1 };
51 if ( jQuery.fn.css ) {
52 expected += 2;
53 attrObj[ "css" ] = { "paddingLeft": 1, "paddingRight": 1 };
55 if ( jQuery.fn.attr ) {
56 expected++;
57 attrObj.attr = { "desired": "very" };
60 assert.expect( expected );
62 // Basic constructor's behavior
63 assert.equal( jQuery().length, 0, "jQuery() === jQuery([])" );
64 assert.equal( jQuery( undefined ).length, 0, "jQuery(undefined) === jQuery([])" );
65 assert.equal( jQuery( null ).length, 0, "jQuery(null) === jQuery([])" );
66 assert.equal( jQuery( "" ).length, 0, "jQuery('') === jQuery([])" );
67 assert.deepEqual( jQuery( obj ).get(), obj.get(), "jQuery(jQueryObj) == jQueryObj" );
69 // Invalid #id goes to Sizzle which will throw an error (gh-1682)
70 try {
71 jQuery( "#" );
72 } catch ( e ) {
73 assert.ok( true, "Threw an error on #id with no id" );
76 // can actually yield more than one, when iframes are included, the window is an array as well
77 assert.equal( jQuery( window ).length, 1, "Correct number of elements generated for jQuery(window)" );
80 // disabled since this test was doing nothing. i tried to fix it but i'm not sure
81 // what the expected behavior should even be. FF returns "\n" for the text node
82 // make sure this is handled
83 var crlfContainer = jQuery('<p>\r\n</p>');
84 var x = crlfContainer.contents().get(0).nodeValue;
85 assert.equal( x, what???, "Check for \\r and \\n in jQuery()" );
88 /* // Disabled until we add this functionality in
89 var pass = true;
90 try {
91 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
92 } catch(e){
93 pass = false;
95 assert.ok( pass, "jQuery('&lt;tag&gt;') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
97 assert.equal( code.length, 1, "Correct number of elements generated for code" );
98 assert.equal( code.parent().length, 0, "Make sure that the generated HTML has no parent." );
100 assert.equal( img.length, 1, "Correct number of elements generated for img" );
101 assert.equal( img.parent().length, 0, "Make sure that the generated HTML has no parent." );
103 assert.equal( div.length, 4, "Correct number of elements generated for div hr code b" );
104 assert.equal( div.parent().length, 0, "Make sure that the generated HTML has no parent." );
106 assert.equal( jQuery( [ 1, 2, 3 ] ).get( 1 ), 2, "Test passing an array to the factory" );
108 assert.equal( jQuery( document.body ).get( 0 ), jQuery( "body" ).get( 0 ), "Test passing an html node to the factory" );
110 elem = jQuery( " <em>hello</em>" )[ 0 ];
111 assert.equal( elem.nodeName.toLowerCase(), "em", "leading space" );
113 elem = jQuery( "\n\n<em>world</em>" )[ 0 ];
114 assert.equal( elem.nodeName.toLowerCase(), "em", "leading newlines" );
116 elem = jQuery( "<div/>", attrObj );
118 if ( jQuery.fn.width ) {
119 assert.equal( elem[ 0 ].style.width, "10px", "jQuery() quick setter width" );
122 if ( jQuery.fn.offset ) {
123 assert.equal( elem[ 0 ].style.top, "1px", "jQuery() quick setter offset" );
126 if ( jQuery.fn.css ) {
127 assert.equal( elem[ 0 ].style.paddingLeft, "1px", "jQuery quick setter css" );
128 assert.equal( elem[ 0 ].style.paddingRight, "1px", "jQuery quick setter css" );
131 if ( jQuery.fn.attr ) {
132 assert.equal( elem[ 0 ].getAttribute( "desired" ), "very", "jQuery quick setter attr" );
135 assert.equal( elem[ 0 ].childNodes.length, 1, "jQuery quick setter text" );
136 assert.equal( elem[ 0 ].firstChild.nodeValue, "test", "jQuery quick setter text" );
137 assert.equal( elem[ 0 ].className, "test2", "jQuery() quick setter class" );
138 assert.equal( elem[ 0 ].id, "test3", "jQuery() quick setter id" );
140 exec = true;
141 elem.trigger( "click" );
143 // manually clean up detached elements
144 elem.remove();
146 for ( i = 0; i < 3; ++i ) {
147 elem = jQuery( "<input type='text' value='TEST' />" );
149 assert.equal( elem[ 0 ].defaultValue, "TEST", "Ensure cached nodes are cloned properly (Bug #6655)" );
151 elem = jQuery( "<input type='hidden'>", {} );
152 assert.strictEqual( elem[ 0 ].ownerDocument, document,
153 "Empty attributes object is not interpreted as a document (trac-8950)" );
154 } );
156 QUnit[ jQuery.find.compile ? "test" : "skip" ]( "jQuery(selector, context)", function( assert ) {
157 assert.expect( 3 );
158 assert.deepEqual( jQuery( "div p", "#qunit-fixture" ).get(), q( "sndp", "en", "sap" ), "Basic selector with string as context" );
159 assert.deepEqual( jQuery( "div p", q( "qunit-fixture" )[ 0 ] ).get(), q( "sndp", "en", "sap" ), "Basic selector with element as context" );
160 assert.deepEqual( jQuery( "div p", jQuery( "#qunit-fixture" ) ).get(), q( "sndp", "en", "sap" ), "Basic selector with jQuery object as context" );
161 } );
163 QUnit.test( "globalEval", function( assert ) {
164 assert.expect( 3 );
165 Globals.register( "globalEvalTest" );
167 jQuery.globalEval( "globalEvalTest = 1;" );
168 assert.equal( window.globalEvalTest, 1, "Test variable assignments are global" );
170 jQuery.globalEval( "var globalEvalTest = 2;" );
171 assert.equal( window.globalEvalTest, 2, "Test variable declarations are global" );
173 jQuery.globalEval( "this.globalEvalTest = 3;" );
174 assert.equal( window.globalEvalTest, 3, "Test context (this) is the window object" );
175 } );
177 QUnit.test( "globalEval with 'use strict'", function( assert ) {
178 assert.expect( 1 );
179 Globals.register( "strictEvalTest" );
181 jQuery.globalEval( "'use strict'; var strictEvalTest = 1;" );
182 assert.equal( window.strictEvalTest, 1, "Test variable declarations are global (strict mode)" );
183 } );
185 QUnit.test( "globalEval execution after script injection (#7862)", function( assert ) {
186 assert.expect( 1 );
188 var now,
189 script = document.createElement( "script" );
191 script.src = baseURL + "mock.php?action=wait&wait=2&script=1";
193 now = Date.now();
194 document.body.appendChild( script );
196 jQuery.globalEval( "var strictEvalTest = " + Date.now() + ";" );
197 assert.ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
198 } );
200 QUnit.test( "noConflict", function( assert ) {
201 assert.expect( 7 );
203 var $$ = jQuery;
205 assert.strictEqual( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
206 assert.strictEqual( window[ "jQuery" ], $$, "Make sure jQuery wasn't touched." );
207 assert.strictEqual( window[ "$" ], original$, "Make sure $ was reverted." );
209 jQuery = $ = $$;
211 assert.strictEqual( jQuery.noConflict( true ), $$, "noConflict returned the jQuery object" );
212 assert.strictEqual( window[ "jQuery" ], originaljQuery, "Make sure jQuery was reverted." );
213 assert.strictEqual( window[ "$" ], original$, "Make sure $ was reverted." );
214 assert.ok( $$().pushStack( [] ), "Make sure that jQuery still works." );
216 window[ "jQuery" ] = jQuery = $$;
217 } );
219 QUnit.test( "trim", function( assert ) {
220 assert.expect( 13 );
222 var nbsp = String.fromCharCode( 160 );
224 assert.equal( jQuery.trim( "hello " ), "hello", "trailing space" );
225 assert.equal( jQuery.trim( " hello" ), "hello", "leading space" );
226 assert.equal( jQuery.trim( " hello " ), "hello", "space on both sides" );
227 assert.equal( jQuery.trim( " " + nbsp + "hello " + nbsp + " " ), "hello", "&nbsp;" );
229 assert.equal( jQuery.trim(), "", "Nothing in." );
230 assert.equal( jQuery.trim( undefined ), "", "Undefined" );
231 assert.equal( jQuery.trim( null ), "", "Null" );
232 assert.equal( jQuery.trim( 5 ), "5", "Number" );
233 assert.equal( jQuery.trim( false ), "false", "Boolean" );
235 assert.equal( jQuery.trim( " " ), "", "space should be trimmed" );
236 assert.equal( jQuery.trim( "ipad\xA0" ), "ipad", "nbsp should be trimmed" );
237 assert.equal( jQuery.trim( "\uFEFF" ), "", "zwsp should be trimmed" );
238 assert.equal( jQuery.trim( "\uFEFF \xA0! | \uFEFF" ), "! |", "leading/trailing should be trimmed" );
239 } );
241 QUnit.test( "isPlainObject", function( assert ) {
242 var done = assert.async();
244 assert.expect( 23 );
246 var pass, iframe, doc, parentObj, childObj, deep,
247 fn = function() {};
249 // The use case that we want to match
250 assert.ok( jQuery.isPlainObject( {} ), "{}" );
251 assert.ok( jQuery.isPlainObject( new window.Object() ), "new Object" );
252 assert.ok( jQuery.isPlainObject( { constructor: fn } ),
253 "plain object with constructor property" );
254 assert.ok( jQuery.isPlainObject( { constructor: "foo" } ),
255 "plain object with primitive constructor property" );
257 parentObj = {};
258 childObj = Object.create( parentObj );
259 assert.ok( !jQuery.isPlainObject( childObj ), "Object.create({})" );
260 parentObj.foo = "bar";
261 assert.ok( !jQuery.isPlainObject( childObj ), "Object.create({...})" );
262 childObj.bar = "foo";
263 assert.ok( !jQuery.isPlainObject( childObj ), "extend(Object.create({...}), ...)" );
265 // Not objects shouldn't be matched
266 assert.ok( !jQuery.isPlainObject( "" ), "string" );
267 assert.ok( !jQuery.isPlainObject( 0 ) && !jQuery.isPlainObject( 1 ), "number" );
268 assert.ok( !jQuery.isPlainObject( true ) && !jQuery.isPlainObject( false ), "boolean" );
269 assert.ok( !jQuery.isPlainObject( null ), "null" );
270 assert.ok( !jQuery.isPlainObject( undefined ), "undefined" );
272 // Arrays shouldn't be matched
273 assert.ok( !jQuery.isPlainObject( [] ), "array" );
275 // Instantiated objects shouldn't be matched
276 assert.ok( !jQuery.isPlainObject( new Date() ), "new Date" );
278 // Functions shouldn't be matched
279 assert.ok( !jQuery.isPlainObject( fn ), "fn" );
281 // Again, instantiated objects shouldn't be matched
282 assert.ok( !jQuery.isPlainObject( new fn() ), "new fn (no methods)" );
284 // Makes the function a little more realistic
285 // (and harder to detect, incidentally)
286 fn.prototype[ "someMethod" ] = function() {};
288 // Again, instantiated objects shouldn't be matched
289 assert.ok( !jQuery.isPlainObject( new fn() ), "new fn" );
291 // Instantiated objects with primitive constructors shouldn't be matched
292 fn.prototype.constructor = "foo";
293 assert.ok( !jQuery.isPlainObject( new fn() ), "new fn with primitive constructor" );
295 // Deep object
296 deep = { "foo": { "baz": true }, "foo2": document };
297 assert.ok( jQuery.isPlainObject( deep ), "Object with objects is still plain" );
299 // DOM Element
300 assert.ok( !jQuery.isPlainObject( document.createElement( "div" ) ), "DOM Element" );
302 // Window
303 assert.ok( !jQuery.isPlainObject( window ), "window" );
305 pass = false;
306 try {
307 jQuery.isPlainObject( window.location );
308 pass = true;
309 } catch ( e ) {}
310 assert.ok( pass, "Does not throw exceptions on host objects" );
312 // Objects from other windows should be matched
313 Globals.register( "iframeDone" );
314 window.iframeDone = function( otherObject, detail ) {
315 window.iframeDone = undefined;
316 iframe.parentNode.removeChild( iframe );
317 assert.ok( jQuery.isPlainObject( new otherObject() ), "new otherObject" + ( detail ? " - " + detail : "" ) );
318 done();
321 try {
322 iframe = jQuery( "#qunit-fixture" )[ 0 ].appendChild( document.createElement( "iframe" ) );
323 doc = iframe.contentDocument || iframe.contentWindow.document;
324 doc.open();
325 doc.write( "<body onload='window.parent.iframeDone(Object);'>" );
326 doc.close();
327 } catch ( e ) {
328 window.iframeDone( Object, "iframes not supported" );
330 } );
332 QUnit[ typeof Symbol === "function" ? "test" : "skip" ]( "isPlainObject(Symbol)", function( assert ) {
333 assert.expect( 2 );
335 assert.equal( jQuery.isPlainObject( Symbol() ), false, "Symbol" );
336 assert.equal( jQuery.isPlainObject( Object( Symbol() ) ), false, "Symbol inside an object" );
337 } );
339 QUnit.test( "isPlainObject(localStorage)", function( assert ) {
340 assert.expect( 1 );
342 assert.equal( jQuery.isPlainObject( localStorage ), false );
343 } );
345 QUnit[ "assign" in Object ? "test" : "skip" ]( "isPlainObject(Object.assign(...))",
346 function( assert ) {
347 assert.expect( 1 );
349 var parentObj = { foo: "bar" };
350 var childObj = Object.assign( Object.create( parentObj ), { bar: "foo" } );
352 assert.ok( !jQuery.isPlainObject( childObj ), "isPlainObject(Object.assign(...))" );
356 QUnit.test( "isXMLDoc - HTML", function( assert ) {
357 assert.expect( 4 );
359 assert.ok( !jQuery.isXMLDoc( document ), "HTML document" );
360 assert.ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
361 assert.ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
363 var body,
364 iframe = document.createElement( "iframe" );
365 document.body.appendChild( iframe );
367 try {
368 body = jQuery( iframe ).contents()[ 0 ];
370 try {
371 assert.ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
372 } catch ( e ) {
373 assert.ok( false, "Iframe body element exception" );
376 } catch ( e ) {
377 assert.ok( true, "Iframe body element - iframe not working correctly" );
380 document.body.removeChild( iframe );
381 } );
383 QUnit.test( "XSS via location.hash", function( assert ) {
384 var done = assert.async();
385 assert.expect( 1 );
387 jQuery[ "_check9521" ] = function( x ) {
388 assert.ok( x, "script called from #id-like selector with inline handler" );
389 jQuery( "#check9521" ).remove();
390 delete jQuery[ "_check9521" ];
391 done();
393 try {
395 // This throws an error because it's processed like an id
396 jQuery( "#<img id='check9521' src='no-such-.gif' onerror='jQuery._check9521(false)'>" ).appendTo( "#qunit-fixture" );
397 } catch ( err ) {
398 jQuery[ "_check9521" ]( true );
400 } );
402 QUnit.test( "isXMLDoc - XML", function( assert ) {
403 assert.expect( 3 );
404 var xml = createDashboardXML();
405 assert.ok( jQuery.isXMLDoc( xml ), "XML document" );
406 assert.ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" );
407 assert.ok( jQuery.isXMLDoc( jQuery( "tab", xml )[ 0 ] ), "XML Tab Element" );
408 } );
410 QUnit.test( "jQuery('html')", function( assert ) {
411 assert.expect( 18 );
413 var s, div, j;
415 jQuery[ "foo" ] = false;
416 s = jQuery( "<script>jQuery.foo='test';</script>" )[ 0 ];
417 assert.ok( s, "Creating a script" );
418 assert.ok( !jQuery[ "foo" ], "Make sure the script wasn't executed prematurely" );
419 jQuery( "body" ).append( "<script>jQuery.foo='test';</script>" );
420 assert.ok( jQuery[ "foo" ], "Executing a script's contents in the right context" );
422 // Test multi-line HTML
423 div = jQuery( "<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>" )[ 0 ];
424 assert.equal( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
425 assert.equal( div.firstChild.nodeType, 3, "Text node." );
426 assert.equal( div.lastChild.nodeType, 3, "Text node." );
427 assert.equal( div.childNodes[ 1 ].nodeType, 1, "Paragraph." );
428 assert.equal( div.childNodes[ 1 ].firstChild.nodeType, 3, "Paragraph text." );
430 assert.ok( jQuery( "<link rel='stylesheet'/>" )[ 0 ], "Creating a link" );
432 assert.ok( !jQuery( "<script/>" )[ 0 ].parentNode, "Create a script" );
434 assert.ok( jQuery( "<input/>" ).attr( "type", "hidden" ), "Create an input and set the type." );
436 j = jQuery( "<span>hi</span> there <!-- mon ami -->" );
437 assert.ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
439 assert.ok( !jQuery( "<option>test</option>" )[ 0 ].selected, "Make sure that options are auto-selected #2050" );
441 assert.ok( jQuery( "<div></div>" )[ 0 ], "Create a div with closing tag." );
442 assert.ok( jQuery( "<table></table>" )[ 0 ], "Create a table with closing tag." );
444 assert.equal( jQuery( "element[attribute='<div></div>']" ).length, 0,
445 "When html is within brackets, do not recognize as html." );
447 //equal( jQuery( "element[attribute=<div></div>]" ).length, 0,
448 // "When html is within brackets, do not recognize as html." );
449 if ( jQuery.find.compile ) {
450 assert.equal( jQuery( "element:not(<div></div>)" ).length, 0,
451 "When html is within parens, do not recognize as html." );
452 } else {
453 assert.ok( "skip", "Complex :not not supported in selector-native" );
455 assert.equal( jQuery( "\\<div\\>" ).length, 0, "Ignore escaped html characters" );
456 } );
458 QUnit.test( "jQuery(element with non-alphanumeric name)", function( assert ) {
459 assert.expect( 36 );
461 jQuery.each( [ "-", ":" ], function( i, symbol ) {
462 jQuery.each( [ "thead", "tbody", "tfoot", "colgroup", "caption", "tr", "th", "td" ],
463 function( j, tag ) {
464 var tagName = tag + symbol + "test";
465 var el = jQuery( "<" + tagName + "></" + tagName + ">" );
466 assert.ok( el[ 0 ], "Create a " + tagName + " element" );
467 assert.ok( el[ 0 ].nodeName === tagName.toUpperCase(),
468 tagName + " element has expected node name" );
472 var tagName = [ "tr", "multiple", "symbol" ].join( symbol );
473 var el = jQuery( "<" + tagName + "></" + tagName + ">" );
474 assert.ok( el[ 0 ], "Create a " + tagName + " element" );
475 assert.ok( el[ 0 ].nodeName === tagName.toUpperCase(),
476 tagName + " element has expected node name" );
477 } );
478 } );
480 QUnit.test( "jQuery('massive html #7990')", function( assert ) {
481 assert.expect( 3 );
483 var i,
484 li = "<li>very very very very large html string</li>",
485 html = [ "<ul>" ];
487 for ( i = 0; i < 30000; i += 1 ) {
488 html[ html.length ] = li;
490 html[ html.length ] = "</ul>";
491 html = jQuery( html.join( "" ) )[ 0 ];
492 assert.equal( html.nodeName.toLowerCase(), "ul" );
493 assert.equal( html.firstChild.nodeName.toLowerCase(), "li" );
494 assert.equal( html.childNodes.length, 30000 );
495 } );
497 QUnit.test( "jQuery('html', context)", function( assert ) {
498 assert.expect( 1 );
500 var $div = jQuery( "<div/>" )[ 0 ],
501 $span = jQuery( "<span/>", $div );
502 assert.equal( $span.length, 1, "verify a span created with a div context works, #1763" );
503 } );
505 QUnit.test( "jQuery(selector, xml).text(str) - loaded via xml document", function( assert ) {
506 assert.expect( 2 );
508 var xml = createDashboardXML(),
510 // tests for #1419 where ie was a problem
511 tab = jQuery( "tab", xml ).eq( 0 );
512 assert.equal( tab.text(), "blabla", "verify initial text correct" );
513 tab.text( "newtext" );
514 assert.equal( tab.text(), "newtext", "verify new text correct" );
515 } );
517 QUnit.test( "end()", function( assert ) {
518 assert.expect( 3 );
519 assert.equal( "Yahoo", jQuery( "#yahoo" ).parent().end().text(), "check for end" );
520 assert.ok( jQuery( "#yahoo" ).end(), "check for end with nothing to end" );
522 var x = jQuery( "#yahoo" );
523 x.parent();
524 assert.equal( "Yahoo", jQuery( "#yahoo" ).text(), "check for non-destructive behavior" );
525 } );
527 QUnit.test( "length", function( assert ) {
528 assert.expect( 1 );
529 assert.equal( jQuery( "#qunit-fixture p" ).length, 6, "Get Number of Elements Found" );
530 } );
532 QUnit.test( "get()", function( assert ) {
533 assert.expect( 1 );
534 assert.deepEqual( jQuery( "#qunit-fixture p" ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Get All Elements" );
535 } );
537 QUnit.test( "toArray()", function( assert ) {
538 assert.expect( 1 );
539 assert.deepEqual( jQuery( "#qunit-fixture p" ).toArray(),
540 q( "firstp", "ap", "sndp", "en", "sap", "first" ),
541 "Convert jQuery object to an Array" );
542 } );
544 QUnit.test( "inArray()", function( assert ) {
545 assert.expect( 19 );
547 var selections = {
548 p: q( "firstp", "sap", "ap", "first" ),
549 em: q( "siblingnext", "siblingfirst" ),
550 div: q( "qunit-testrunner-toolbar", "nothiddendiv", "nothiddendivchild", "foo" ),
551 a: q( "mark", "groups", "google", "simon1" ),
552 empty: []
554 tests = {
555 p: { elem: jQuery( "#ap" )[ 0 ], index: 2 },
556 em: { elem: jQuery( "#siblingfirst" )[ 0 ], index: 1 },
557 div: { elem: jQuery( "#nothiddendiv" )[ 0 ], index: 1 },
558 a: { elem: jQuery( "#simon1" )[ 0 ], index: 3 }
560 falseTests = {
561 p: jQuery( "#liveSpan1" )[ 0 ],
562 em: jQuery( "#nothiddendiv" )[ 0 ],
563 empty: ""
566 jQuery.each( tests, function( key, obj ) {
567 assert.equal( jQuery.inArray( obj.elem, selections[ key ] ), obj.index, "elem is in the array of selections of its tag" );
569 // Third argument (fromIndex)
570 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" );
571 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" );
572 assert.equal( !!~jQuery.inArray( obj.elem, selections[ key ], -3 ), true, "elem is in the array of selections given a negative index" );
573 } );
575 jQuery.each( falseTests, function( key, elem ) {
576 assert.equal( !!~jQuery.inArray( elem, selections[ key ] ), false, "elem is NOT in the array of selections" );
577 } );
579 } );
581 QUnit.test( "get(Number)", function( assert ) {
582 assert.expect( 2 );
583 assert.equal( jQuery( "#qunit-fixture p" ).get( 0 ), document.getElementById( "firstp" ), "Get A Single Element" );
584 assert.strictEqual( jQuery( "#firstp" ).get( 1 ), undefined, "Try get with index larger elements count" );
585 } );
587 QUnit.test( "get(-Number)", function( assert ) {
588 assert.expect( 2 );
589 assert.equal( jQuery( "p" ).get( -1 ), document.getElementById( "first" ), "Get a single element with negative index" );
590 assert.strictEqual( jQuery( "#firstp" ).get( -2 ), undefined, "Try get with index negative index larger then elements count" );
591 } );
593 QUnit.test( "each(Function)", function( assert ) {
594 assert.expect( 1 );
595 var div, pass, i;
597 div = jQuery( "div" );
598 div.each( function() {this.foo = "zoo";} );
599 pass = true;
600 for ( i = 0; i < div.length; i++ ) {
601 if ( div.get( i ).foo !== "zoo" ) {
602 pass = false;
605 assert.ok( pass, "Execute a function, Relative" );
606 } );
608 QUnit.test( "slice()", function( assert ) {
609 assert.expect( 7 );
611 var $links = jQuery( "#ap a" );
613 assert.deepEqual( $links.slice( 1, 2 ).get(), q( "groups" ), "slice(1,2)" );
614 assert.deepEqual( $links.slice( 1 ).get(), q( "groups", "anchor1", "mark" ), "slice(1)" );
615 assert.deepEqual( $links.slice( 0, 3 ).get(), q( "google", "groups", "anchor1" ), "slice(0,3)" );
616 assert.deepEqual( $links.slice( -1 ).get(), q( "mark" ), "slice(-1)" );
618 assert.deepEqual( $links.eq( 1 ).get(), q( "groups" ), "eq(1)" );
619 assert.deepEqual( $links.eq( "2" ).get(), q( "anchor1" ), "eq('2')" );
620 assert.deepEqual( $links.eq( -1 ).get(), q( "mark" ), "eq(-1)" );
621 } );
623 QUnit.test( "first()/last()", function( assert ) {
624 assert.expect( 4 );
626 var $links = jQuery( "#ap a" ), $none = jQuery( "asdf" );
628 assert.deepEqual( $links.first().get(), q( "google" ), "first()" );
629 assert.deepEqual( $links.last().get(), q( "mark" ), "last()" );
631 assert.deepEqual( $none.first().get(), [], "first() none" );
632 assert.deepEqual( $none.last().get(), [], "last() none" );
633 } );
635 QUnit.test( "map()", function( assert ) {
636 assert.expect( 2 );
638 assert.deepEqual(
639 jQuery( "#ap" ).map( function() {
640 return jQuery( this ).find( "a" ).get();
641 } ).get(),
642 q( "google", "groups", "anchor1", "mark" ),
643 "Array Map"
646 assert.deepEqual(
647 jQuery( "#ap > a" ).map( function() {
648 return this.parentNode;
649 } ).get(),
650 q( "ap", "ap", "ap" ),
651 "Single Map"
653 } );
655 QUnit.test( "jQuery.map", function( assert ) {
656 assert.expect( 25 );
658 var i, label, result, callback;
660 result = jQuery.map( [ 3, 4, 5 ], function( v, k ) {
661 return k;
662 } );
663 assert.equal( result.join( "" ), "012", "Map the keys from an array" );
665 result = jQuery.map( [ 3, 4, 5 ], function( v ) {
666 return v;
667 } );
668 assert.equal( result.join( "" ), "345", "Map the values from an array" );
670 result = jQuery.map( { a: 1, b: 2 }, function( v, k ) {
671 return k;
672 } );
673 assert.equal( result.join( "" ), "ab", "Map the keys from an object" );
675 result = jQuery.map( { a: 1, b: 2 }, function( v ) {
676 return v;
677 } );
678 assert.equal( result.join( "" ), "12", "Map the values from an object" );
680 result = jQuery.map( [ "a", undefined, null, "b" ], function( v ) {
681 return v;
682 } );
683 assert.equal( result.join( "" ), "ab", "Array iteration does not include undefined/null results" );
685 result = jQuery.map( { a: "a", b: undefined, c: null, d: "b" }, function( v ) {
686 return v;
687 } );
688 assert.equal( result.join( "" ), "ab", "Object iteration does not include undefined/null results" );
690 result = {
691 Zero: function() {},
692 One: function( a ) { a = a; },
693 Two: function( a, b ) { a = a; b = b; }
695 callback = function( v, k ) {
696 assert.equal( k, "foo", label + "-argument function treated like object" );
698 for ( i in result ) {
699 label = i;
700 result[ i ].foo = "bar";
701 jQuery.map( result[ i ], callback );
704 result = {
705 "undefined": undefined,
706 "null": null,
707 "false": false,
708 "true": true,
709 "empty string": "",
710 "nonempty string": "string",
711 "string \"0\"": "0",
712 "negative": -1,
713 "excess": 1
715 callback = function( v, k ) {
716 assert.equal( k, "length", "Object with " + label + " length treated like object" );
718 for ( i in result ) {
719 label = i;
720 jQuery.map( { length: result[ i ] }, callback );
723 result = {
724 "sparse Array": Array( 4 ),
725 "length: 1 plain object": { length: 1, "0": true },
726 "length: 2 plain object": { length: 2, "0": true, "1": true },
727 NodeList: document.getElementsByTagName( "html" )
729 callback = function( v, k ) {
730 if ( result[ label ] ) {
731 delete result[ label ];
732 assert.equal( k, "0", label + " treated like array" );
735 for ( i in result ) {
736 label = i;
737 jQuery.map( result[ i ], callback );
740 result = false;
741 jQuery.map( { length: 0 }, function() {
742 result = true;
743 } );
744 assert.ok( !result, "length: 0 plain object treated like array" );
746 result = false;
747 jQuery.map( document.getElementsByTagName( "asdf" ), function() {
748 result = true;
749 } );
750 assert.ok( !result, "empty NodeList treated like array" );
752 result = jQuery.map( Array( 4 ), function( v, k ) {
753 return k % 2 ? k : [ k, k, k ];
754 } );
755 assert.equal( result.join( "" ), "00012223", "Array results flattened (#2616)" );
756 } );
758 QUnit.test( "jQuery.merge()", function( assert ) {
759 assert.expect( 10 );
761 assert.deepEqual(
762 jQuery.merge( [], [] ),
764 "Empty arrays"
767 assert.deepEqual(
768 jQuery.merge( [ 1 ], [ 2 ] ),
769 [ 1, 2 ],
770 "Basic (single-element)"
772 assert.deepEqual(
773 jQuery.merge( [ 1, 2 ], [ 3, 4 ] ),
774 [ 1, 2, 3, 4 ],
775 "Basic (multiple-element)"
778 assert.deepEqual(
779 jQuery.merge( [ 1, 2 ], [] ),
780 [ 1, 2 ],
781 "Second empty"
783 assert.deepEqual(
784 jQuery.merge( [], [ 1, 2 ] ),
785 [ 1, 2 ],
786 "First empty"
789 // Fixed at [5998], #3641
790 assert.deepEqual(
791 jQuery.merge( [ -2, -1 ], [ 0, 1, 2 ] ),
792 [ -2, -1, 0, 1, 2 ],
793 "Second array including a zero (falsy)"
796 // After fixing #5527
797 assert.deepEqual(
798 jQuery.merge( [], [ null, undefined ] ),
799 [ null, undefined ],
800 "Second array including null and undefined values"
802 assert.deepEqual(
803 jQuery.merge( { length: 0 }, [ 1, 2 ] ),
804 { length: 2, 0: 1, 1: 2 },
805 "First array like"
807 assert.deepEqual(
808 jQuery.merge( [ 1, 2 ], { length: 1, 0: 3 } ),
809 [ 1, 2, 3 ],
810 "Second array like"
813 assert.deepEqual(
814 jQuery.merge( [], document.getElementById( "lengthtest" ).getElementsByTagName( "input" ) ),
815 [ document.getElementById( "length" ), document.getElementById( "idTest" ) ],
816 "Second NodeList"
818 } );
820 QUnit.test( "jQuery.grep()", function( assert ) {
821 assert.expect( 8 );
823 var searchCriterion = function( value ) {
824 return value % 2 === 0;
827 assert.deepEqual( jQuery.grep( [], searchCriterion ), [], "Empty array" );
828 assert.deepEqual( jQuery.grep( new Array( 4 ), searchCriterion ), [], "Sparse array" );
830 assert.deepEqual(
831 jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion ),
832 [ 2, 4, 6 ],
833 "Satisfying elements present"
835 assert.deepEqual(
836 jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion ),
838 "Satisfying elements absent"
841 assert.deepEqual(
842 jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, true ),
843 [ 1, 3, 5 ],
844 "Satisfying elements present and grep inverted"
846 assert.deepEqual(
847 jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, true ),
848 [ 1, 3, 5, 7 ],
849 "Satisfying elements absent and grep inverted"
852 assert.deepEqual(
853 jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, false ),
854 [ 2, 4, 6 ],
855 "Satisfying elements present but grep explicitly uninverted"
857 assert.deepEqual(
858 jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, false ),
860 "Satisfying elements absent and grep explicitly uninverted"
862 } );
864 QUnit.test( "jQuery.grep(Array-like)", function( assert ) {
865 assert.expect( 7 );
867 var searchCriterion = function( value ) {
868 return value % 2 === 0;
871 assert.deepEqual( jQuery.grep( { length: 0 }, searchCriterion ), [], "Empty array-like" );
873 assert.deepEqual(
874 jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion ),
875 [ 2, 4, 6 ],
876 "Satisfying elements present and array-like object used"
878 assert.deepEqual(
879 jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion ),
881 "Satisfying elements absent and Array-like object used"
884 assert.deepEqual(
885 jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion, true ),
886 [ 1, 3, 5 ],
887 "Satisfying elements present, array-like object used, and grep inverted"
889 assert.deepEqual(
890 jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion, true ),
891 [ 1, 3, 5, 7 ],
892 "Satisfying elements absent, array-like object used, and grep inverted"
895 assert.deepEqual(
896 jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion, false ),
897 [ 2, 4, 6 ],
898 "Satisfying elements present, Array-like object used, but grep explicitly uninverted"
900 assert.deepEqual(
901 jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion, false ),
903 "Satisfying elements absent, Array-like object used, and grep explicitly uninverted"
905 } );
907 QUnit.test( "jQuery.extend(Object, Object)", function( assert ) {
908 assert.expect( 28 );
910 var empty, optionsWithLength, optionsWithDate, myKlass,
911 customObject, optionsWithCustomObject, MyNumber, ret,
912 nullUndef, target, recursive, obj,
913 defaults, defaultsCopy, options1, options1Copy, options2, options2Copy, merged2,
914 settings = { "xnumber1": 5, "xnumber2": 7, "xstring1": "peter", "xstring2": "pan" },
915 options = { "xnumber2": 1, "xstring2": "x", "xxx": "newstring" },
916 optionsCopy = { "xnumber2": 1, "xstring2": "x", "xxx": "newstring" },
917 merged = { "xnumber1": 5, "xnumber2": 1, "xstring1": "peter", "xstring2": "x", "xxx": "newstring" },
918 deep1 = { "foo": { "bar": true } },
919 deep2 = { "foo": { "baz": true }, "foo2": document },
920 deep2copy = { "foo": { "baz": true }, "foo2": document },
921 deepmerged = { "foo": { "bar": true, "baz": true }, "foo2": document },
922 arr = [ 1, 2, 3 ],
923 nestedarray = { "arr": arr };
925 jQuery.extend( settings, options );
926 assert.deepEqual( settings, merged, "Check if extended: settings must be extended" );
927 assert.deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
929 jQuery.extend( settings, null, options );
930 assert.deepEqual( settings, merged, "Check if extended: settings must be extended" );
931 assert.deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
933 jQuery.extend( true, deep1, deep2 );
934 assert.deepEqual( deep1[ "foo" ], deepmerged[ "foo" ], "Check if foo: settings must be extended" );
935 assert.deepEqual( deep2[ "foo" ], deep2copy[ "foo" ], "Check if not deep2: options must not be modified" );
936 assert.equal( deep1[ "foo2" ], document, "Make sure that a deep clone was not attempted on the document" );
938 assert.ok( jQuery.extend( true, {}, nestedarray )[ "arr" ] !== arr, "Deep extend of object must clone child array" );
940 // #5991
941 assert.ok( Array.isArray( jQuery.extend( true, { "arr": {} }, nestedarray )[ "arr" ] ), "Cloned array have to be an Array" );
942 assert.ok( jQuery.isPlainObject( jQuery.extend( true, { "arr": arr }, { "arr": {} } )[ "arr" ] ), "Cloned object have to be an plain object" );
944 empty = {};
945 optionsWithLength = { "foo": { "length": -1 } };
946 jQuery.extend( true, empty, optionsWithLength );
947 assert.deepEqual( empty[ "foo" ], optionsWithLength[ "foo" ], "The length property must copy correctly" );
949 empty = {};
950 optionsWithDate = { "foo": { "date": new Date() } };
951 jQuery.extend( true, empty, optionsWithDate );
952 assert.deepEqual( empty[ "foo" ], optionsWithDate[ "foo" ], "Dates copy correctly" );
954 /** @constructor */
955 myKlass = function() {};
956 customObject = new myKlass();
957 optionsWithCustomObject = { "foo": { "date": customObject } };
958 empty = {};
959 jQuery.extend( true, empty, optionsWithCustomObject );
960 assert.ok( empty[ "foo" ] && empty[ "foo" ][ "date" ] === customObject, "Custom objects copy correctly (no methods)" );
962 // Makes the class a little more realistic
963 myKlass.prototype = { "someMethod": function() {} };
964 empty = {};
965 jQuery.extend( true, empty, optionsWithCustomObject );
966 assert.ok( empty[ "foo" ] && empty[ "foo" ][ "date" ] === customObject, "Custom objects copy correctly" );
968 MyNumber = Number;
970 ret = jQuery.extend( true, { "foo": 4 }, { "foo": new MyNumber( 5 ) } );
971 assert.ok( parseInt( ret.foo, 10 ) === 5, "Wrapped numbers copy correctly" );
973 nullUndef = jQuery.extend( {}, options, { "xnumber2": null } );
974 assert.ok( nullUndef[ "xnumber2" ] === null, "Check to make sure null values are copied" );
976 nullUndef = jQuery.extend( {}, options, { "xnumber2": undefined } );
977 assert.ok( nullUndef[ "xnumber2" ] === options[ "xnumber2" ], "Check to make sure undefined values are not copied" );
979 nullUndef = jQuery.extend( {}, options, { "xnumber0": null } );
980 assert.ok( nullUndef[ "xnumber0" ] === null, "Check to make sure null values are inserted" );
982 target = {};
983 recursive = { foo:target, bar:5 };
984 jQuery.extend( true, target, recursive );
985 assert.deepEqual( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
987 ret = jQuery.extend( true, { foo: [] }, { foo: [ 0 ] } ); // 1907
988 assert.equal( ret.foo.length, 1, "Check to make sure a value with coercion 'false' copies over when necessary to fix #1907" );
990 ret = jQuery.extend( true, { foo: "1,2,3" }, { foo: [ 1, 2, 3 ] } );
991 assert.ok( typeof ret.foo !== "string", "Check to make sure values equal with coercion (but not actually equal) overwrite correctly" );
993 ret = jQuery.extend( true, { foo:"bar" }, { foo:null } );
994 assert.ok( typeof ret.foo !== "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" );
996 obj = { foo:null };
997 jQuery.extend( true, obj, { foo:"notnull" } );
998 assert.equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );
1000 function func() {}
1001 jQuery.extend( func, { key: "value" } );
1002 assert.equal( func.key, "value", "Verify a function can be extended" );
1004 defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" };
1005 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" };
1006 options1 = { xnumber2: 1, xstring2: "x" };
1007 options1Copy = { xnumber2: 1, xstring2: "x" };
1008 options2 = { xstring2: "xx", xxx: "newstringx" };
1009 options2Copy = { xstring2: "xx", xxx: "newstringx" };
1010 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
1012 settings = jQuery.extend( {}, defaults, options1, options2 );
1013 assert.deepEqual( settings, merged2, "Check if extended: settings must be extended" );
1014 assert.deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
1015 assert.deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" );
1016 assert.deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" );
1017 } );
1019 QUnit.test( "jQuery.extend(Object, Object {created with \"defineProperties\"})", function( assert ) {
1020 assert.expect( 2 );
1022 var definedObj = Object.defineProperties( {}, {
1023 "enumerableProp": {
1024 get: function() {
1025 return true;
1027 enumerable: true
1029 "nonenumerableProp": {
1030 get: function() {
1031 return true;
1034 } ),
1035 accessorObj = {};
1037 jQuery.extend( accessorObj, definedObj );
1038 assert.equal( accessorObj.enumerableProp, true, "Verify that getters are transferred" );
1039 assert.equal( accessorObj.nonenumerableProp, undefined, "Verify that non-enumerable getters are ignored" );
1040 } );
1042 QUnit.test( "jQuery.extend(true,{},{a:[], o:{}}); deep copy with array, followed by object", function( assert ) {
1043 assert.expect( 2 );
1045 var result, initial = {
1047 // This will make "copyIsArray" true
1048 array: [ 1, 2, 3, 4 ],
1050 // If "copyIsArray" doesn't get reset to false, the check
1051 // will evaluate true and enter the array copy block
1052 // instead of the object copy block. Since the ternary in the
1053 // "copyIsArray" block will evaluate to false
1054 // (check if operating on an array with ), this will be
1055 // replaced by an empty array.
1056 object: {}
1059 result = jQuery.extend( true, {}, initial );
1061 assert.deepEqual( result, initial, "The [result] and [initial] have equal shape and values" );
1062 assert.ok( !Array.isArray( result.object ), "result.object wasn't paved with an empty array" );
1063 } );
1065 QUnit.test( "jQuery.extend( true, ... ) Object.prototype pollution", function( assert ) {
1066 assert.expect( 1 );
1068 jQuery.extend( true, {}, JSON.parse( "{\"__proto__\": {\"devMode\": true}}" ) );
1069 assert.ok( !( "devMode" in {} ), "Object.prototype not polluted" );
1070 } );
1072 QUnit.test( "jQuery.each(Object,Function)", function( assert ) {
1073 assert.expect( 23 );
1075 var i, label, seen, callback;
1077 seen = {};
1078 jQuery.each( [ 3, 4, 5 ], function( k, v ) {
1079 seen[ k ] = v;
1080 } );
1081 assert.deepEqual( seen, { "0": 3, "1": 4, "2": 5 }, "Array iteration" );
1083 seen = {};
1084 jQuery.each( { name: "name", lang: "lang" }, function( k, v ) {
1085 seen[ k ] = v;
1086 } );
1087 assert.deepEqual( seen, { name: "name", lang: "lang" }, "Object iteration" );
1089 seen = [];
1090 jQuery.each( [ 1, 2, 3 ], function( k, v ) {
1091 seen.push( v );
1092 if ( k === 1 ) {
1093 return false;
1095 } );
1096 assert.deepEqual( seen, [ 1, 2 ], "Broken array iteration" );
1098 seen = [];
1099 jQuery.each( { "a": 1, "b": 2, "c": 3 }, function( k, v ) {
1100 seen.push( v );
1101 return false;
1102 } );
1103 assert.deepEqual( seen, [ 1 ], "Broken object iteration" );
1105 seen = {
1106 Zero: function() {},
1107 One: function( a ) { a = a; },
1108 Two: function( a, b ) { a = a; b = b; }
1110 callback = function( k ) {
1111 assert.equal( k, "foo", label + "-argument function treated like object" );
1113 for ( i in seen ) {
1114 label = i;
1115 seen[ i ].foo = "bar";
1116 jQuery.each( seen[ i ], callback );
1119 seen = {
1120 "undefined": undefined,
1121 "null": null,
1122 "false": false,
1123 "true": true,
1124 "empty string": "",
1125 "nonempty string": "string",
1126 "string \"0\"": "0",
1127 "negative": -1,
1128 "excess": 1
1130 callback = function( k ) {
1131 assert.equal( k, "length", "Object with " + label + " length treated like object" );
1133 for ( i in seen ) {
1134 label = i;
1135 jQuery.each( { length: seen[ i ] }, callback );
1138 seen = {
1139 "sparse Array": Array( 4 ),
1140 "length: 1 plain object": { length: 1, "0": true },
1141 "length: 2 plain object": { length: 2, "0": true, "1": true },
1142 NodeList: document.getElementsByTagName( "html" )
1144 callback = function( k ) {
1145 if ( seen[ label ] ) {
1146 delete seen[ label ];
1147 assert.equal( k, "0", label + " treated like array" );
1148 return false;
1151 for ( i in seen ) {
1152 label = i;
1153 jQuery.each( seen[ i ], callback );
1156 seen = false;
1157 jQuery.each( { length: 0 }, function() {
1158 seen = true;
1159 } );
1160 assert.ok( !seen, "length: 0 plain object treated like array" );
1162 seen = false;
1163 jQuery.each( document.getElementsByTagName( "asdf" ), function() {
1164 seen = true;
1165 } );
1166 assert.ok( !seen, "empty NodeList treated like array" );
1168 i = 0;
1169 jQuery.each( document.styleSheets, function() {
1170 i++;
1171 } );
1172 assert.equal( i, document.styleSheets.length, "Iteration over document.styleSheets" );
1173 } );
1175 QUnit.test( "jQuery.each/map(undefined/null,Function)", function( assert ) {
1176 assert.expect( 1 );
1178 try {
1179 jQuery.each( undefined, jQuery.noop );
1180 jQuery.each( null, jQuery.noop );
1181 jQuery.map( undefined, jQuery.noop );
1182 jQuery.map( null, jQuery.noop );
1183 assert.ok( true, "jQuery.each/map( undefined/null, function() {} );" );
1184 } catch ( e ) {
1185 assert.ok( false, "each/map must accept null and undefined values" );
1187 } );
1189 QUnit.test( "JIT compilation does not interfere with length retrieval (gh-2145)", function( assert ) {
1190 assert.expect( 4 );
1192 var i;
1194 // Trigger JIT compilation of jQuery.each – and therefore isArraylike – in iOS.
1195 // Convince JSC to use one of its optimizing compilers
1196 // by providing code which can be LICM'd into nothing.
1197 for ( i = 0; i < 1000; i++ ) {
1198 jQuery.each( [] );
1201 i = 0;
1202 jQuery.each( { 1: "1", 2: "2", 3: "3" }, function( index ) {
1203 assert.equal( ++i, index, "Iteration over object with solely " +
1204 "numeric indices (gh-2145 JIT iOS 8 bug)" );
1205 } );
1206 assert.equal( i, 3, "Iteration over object with solely " +
1207 "numeric indices (gh-2145 JIT iOS 8 bug)" );
1208 } );
1210 QUnit.test( "jQuery.makeArray", function( assert ) {
1211 assert.expect( 15 );
1213 assert.equal( jQuery.makeArray( jQuery( "html>*" ) )[ 0 ].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
1215 assert.equal( jQuery.makeArray( document.getElementsByName( "PWD" ) ).slice( 0, 1 )[ 0 ].name, "PWD", "Pass makeArray a nodelist" );
1217 assert.equal( ( function() { return jQuery.makeArray( arguments ); } )( 1, 2 ).join( "" ), "12", "Pass makeArray an arguments array" );
1219 assert.equal( jQuery.makeArray( [ 1, 2, 3 ] ).join( "" ), "123", "Pass makeArray a real array" );
1221 assert.equal( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
1223 assert.equal( jQuery.makeArray( 0 )[ 0 ], 0, "Pass makeArray a number" );
1225 assert.equal( jQuery.makeArray( "foo" )[ 0 ], "foo", "Pass makeArray a string" );
1227 assert.equal( jQuery.makeArray( true )[ 0 ].constructor, Boolean, "Pass makeArray a boolean" );
1229 assert.equal( jQuery.makeArray( document.createElement( "div" ) )[ 0 ].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
1231 assert.equal( jQuery.makeArray( { length:2, 0:"a", 1:"b" } ).join( "" ), "ab", "Pass makeArray an array like map (with length)" );
1233 assert.ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice( 0, 1 )[ 0 ].nodeName, "Pass makeArray a childNodes array" );
1235 // function, is tricky as it has length
1236 assert.equal( jQuery.makeArray( function() { return 1;} )[ 0 ](), 1, "Pass makeArray a function" );
1238 //window, also has length
1239 assert.equal( jQuery.makeArray( window )[ 0 ], window, "Pass makeArray the window" );
1241 assert.equal( jQuery.makeArray( /a/ )[ 0 ].constructor, RegExp, "Pass makeArray a regex" );
1243 // Some nodes inherit traits of nodelists
1244 assert.ok( jQuery.makeArray( document.getElementById( "form" ) ).length >= 13,
1245 "Pass makeArray a form (treat as elements)" );
1246 } );
1248 QUnit.test( "jQuery.inArray", function( assert ) {
1249 assert.expect( 3 );
1251 assert.equal( jQuery.inArray( 0, false ), -1, "Search in 'false' as array returns -1 and doesn't throw exception" );
1253 assert.equal( jQuery.inArray( 0, null ), -1, "Search in 'null' as array returns -1 and doesn't throw exception" );
1255 assert.equal( jQuery.inArray( 0, undefined ), -1, "Search in 'undefined' as array returns -1 and doesn't throw exception" );
1256 } );
1258 QUnit.test( "jQuery.isEmptyObject", function( assert ) {
1259 assert.expect( 2 );
1261 assert.equal( true, jQuery.isEmptyObject( {} ), "isEmptyObject on empty object literal" );
1262 assert.equal( false, jQuery.isEmptyObject( { a:1 } ), "isEmptyObject on non-empty object literal" );
1264 // What about this ?
1265 // equal(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
1266 } );
1268 QUnit.test( "jQuery.parseHTML", function( assert ) {
1269 assert.expect( 23 );
1271 var html, nodes;
1273 assert.deepEqual( jQuery.parseHTML(), [], "Without arguments" );
1274 assert.deepEqual( jQuery.parseHTML( undefined ), [], "Undefined" );
1275 assert.deepEqual( jQuery.parseHTML( null ), [], "Null" );
1276 assert.deepEqual( jQuery.parseHTML( false ), [], "Boolean false" );
1277 assert.deepEqual( jQuery.parseHTML( 0 ), [], "Zero" );
1278 assert.deepEqual( jQuery.parseHTML( true ), [], "Boolean true" );
1279 assert.deepEqual( jQuery.parseHTML( 42 ), [], "Positive number" );
1280 assert.deepEqual( jQuery.parseHTML( "" ), [], "Empty string" );
1281 assert.throws( function() {
1282 jQuery.parseHTML( "<div></div>", document.getElementById( "form" ) );
1283 }, "Passing an element as the context raises an exception (context should be a document)" );
1285 nodes = jQuery.parseHTML( jQuery( "body" )[ 0 ].innerHTML );
1286 assert.ok( nodes.length > 4, "Parse a large html string" );
1287 assert.ok( Array.isArray( nodes ), "parseHTML returns an array rather than a nodelist" );
1289 html = "<script>undefined()</script>";
1290 assert.equal( jQuery.parseHTML( html ).length, 0, "Ignore scripts by default" );
1291 assert.equal( jQuery.parseHTML( html, true )[ 0 ].nodeName.toLowerCase(), "script", "Preserve scripts when requested" );
1293 html += "<div></div>";
1294 assert.equal( jQuery.parseHTML( html )[ 0 ].nodeName.toLowerCase(), "div", "Preserve non-script nodes" );
1295 assert.equal( jQuery.parseHTML( html, true )[ 0 ].nodeName.toLowerCase(), "script", "Preserve script position" );
1297 assert.equal( jQuery.parseHTML( "text" )[ 0 ].nodeType, 3, "Parsing text returns a text node" );
1298 assert.equal( jQuery.parseHTML( "\t<div></div>" )[ 0 ].nodeValue, "\t", "Preserve leading whitespace" );
1300 assert.equal( jQuery.parseHTML( " <div/> " )[ 0 ].nodeType, 3, "Leading spaces are treated as text nodes (#11290)" );
1302 html = jQuery.parseHTML( "<div>test div</div>" );
1304 assert.equal( html[ 0 ].parentNode.nodeType, 11, "parentNode should be documentFragment" );
1305 assert.equal( html[ 0 ].innerHTML, "test div", "Content should be preserved" );
1307 assert.equal( jQuery.parseHTML( "<span><span>" ).length, 1, "Incorrect html-strings should not break anything" );
1308 assert.equal( jQuery.parseHTML( "<td><td>" )[ 1 ].parentNode.nodeType, 11,
1309 "parentNode should be documentFragment for wrapMap (variable in manipulation module) elements too" );
1310 assert.ok( jQuery.parseHTML( "<#if><tr><p>This is a test.</p></tr><#/if>" ) || true, "Garbage input should not cause error" );
1311 } );
1313 QUnit.test( "jQuery.parseHTML(<a href>) - gh-2965", function( assert ) {
1314 assert.expect( 1 );
1316 var html = "<a href='example.html'></a>",
1317 href = jQuery.parseHTML( html )[ 0 ].href;
1319 assert.ok( /\/example\.html$/.test( href ), "href is not lost after parsing anchor" );
1320 } );
1322 if ( jQuery.support.createHTMLDocument ) {
1323 QUnit.test( "jQuery.parseHTML", function( assert ) {
1324 var done = assert.async();
1325 assert.expect( 1 );
1327 Globals.register( "parseHTMLError" );
1329 jQuery.globalEval( "parseHTMLError = false;" );
1330 jQuery.parseHTML( "<img src=x onerror='parseHTMLError = true'>" );
1332 window.setTimeout( function() {
1333 assert.equal( window.parseHTMLError, false, "onerror eventhandler has not been called." );
1334 done();
1335 }, 2000 );
1336 } );
1339 QUnit.test( "jQuery.parseXML", function( assert ) {
1340 assert.expect( 8 );
1342 var xml, tmp;
1343 try {
1344 xml = jQuery.parseXML( "<p>A <b>well-formed</b> xml string</p>" );
1345 tmp = xml.getElementsByTagName( "p" )[ 0 ];
1346 assert.ok( !!tmp, "<p> present in document" );
1347 tmp = tmp.getElementsByTagName( "b" )[ 0 ];
1348 assert.ok( !!tmp, "<b> present in document" );
1349 assert.strictEqual( tmp.childNodes[ 0 ].nodeValue, "well-formed", "<b> text is as expected" );
1350 } catch ( e ) {
1351 assert.strictEqual( e, undefined, "unexpected error" );
1353 try {
1354 xml = jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
1355 assert.ok( false, "invalid xml not detected" );
1356 } catch ( e ) {
1357 assert.strictEqual( e.message, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "invalid xml detected" );
1359 try {
1360 xml = jQuery.parseXML( "" );
1361 assert.strictEqual( xml, null, "empty string => null document" );
1362 xml = jQuery.parseXML();
1363 assert.strictEqual( xml, null, "undefined string => null document" );
1364 xml = jQuery.parseXML( null );
1365 assert.strictEqual( xml, null, "null string => null document" );
1366 xml = jQuery.parseXML( true );
1367 assert.strictEqual( xml, null, "non-string => null document" );
1368 } catch ( e ) {
1369 assert.ok( false, "empty input throws exception" );
1371 } );
1373 testIframe(
1374 "Conditional compilation compatibility (#13274)",
1375 "core/cc_on.html",
1376 function( assert, jQuery, window, document, cc_on, errors ) {
1377 assert.expect( 3 );
1378 assert.ok( true, "JScript conditional compilation " + ( cc_on ? "supported" : "not supported" ) );
1379 assert.deepEqual( errors, [], "No errors" );
1380 assert.ok( jQuery(), "jQuery executes" );
1384 // iOS7 doesn't fire the load event if the long-loading iframe gets its source reset to about:blank.
1385 // This makes this test fail but it doesn't seem to cause any real-life problems so blacklisting
1386 // this test there is preferred to complicating the hard-to-test core/ready code further.
1387 if ( !/iphone os 7_/i.test( navigator.userAgent ) ) {
1388 testIframe(
1389 "document ready when jQuery loaded asynchronously (#13655)",
1390 "core/dynamic_ready.html",
1391 function( assert, jQuery, window, document, ready ) {
1392 assert.expect( 1 );
1393 assert.equal( true, ready, "document ready correctly fired when jQuery is loaded after DOMContentLoaded" );
1398 testIframe(
1399 "Tolerating alias-masked DOM properties (#14074)",
1400 "core/aliased.html",
1401 function( assert, jQuery, window, document, errors ) {
1402 assert.expect( 1 );
1403 assert.deepEqual( errors, [], "jQuery loaded" );
1407 testIframe(
1408 "Don't call window.onready (#14802)",
1409 "core/onready.html",
1410 function( assert, jQuery, window, document, error ) {
1411 assert.expect( 1 );
1412 assert.equal( error, false, "no call to user-defined onready" );
1416 QUnit.test( "Iterability of jQuery objects (gh-1693)", function( assert ) {
1417 assert.expect( 1 );
1419 var i, elem, result;
1421 if ( typeof Symbol === "function" ) {
1423 elem = jQuery( "<div></div><span></span><a></a>" );
1424 result = "";
1426 try {
1427 eval( "for ( i of elem ) { result += i.nodeName; }" );
1428 } catch ( e ) {}
1429 assert.equal( result, "DIVSPANA", "for-of works on jQuery objects" );
1430 } else {
1431 assert.ok( true, "The browser doesn't support Symbols" );
1433 } );
1435 testIframe(
1436 "Iterability of jQuery objects with Symbol polyfill (gh-1693)",
1437 "core/jquery-iterability-transpiled.html",
1438 function( assert, jQuery, window, document, testString ) {
1439 assert.expect( 1 );
1441 assert.strictEqual( testString, "DIVSPANA",
1442 "for-of works on jQuery objects with Symbol polyfilled" );
1446 QUnit[ jQuery.Deferred ? "test" : "skip" ]( "jQuery.readyException (original)", function( assert ) {
1447 assert.expect( 1 );
1449 var message;
1451 this.sandbox.stub( window, "setTimeout", function( fn ) {
1452 try {
1453 fn();
1454 } catch ( error ) {
1455 message = error.message;
1457 } );
1459 jQuery( function() {
1460 throw new Error( "Error in jQuery ready" );
1461 } );
1462 assert.strictEqual(
1463 message,
1464 "Error in jQuery ready",
1465 "The error should have been thrown in a timeout"
1467 } );
1469 QUnit[ jQuery.Deferred ? "test" : "skip" ]( "jQuery.readyException (custom)", function( assert ) {
1470 assert.expect( 1 );
1472 var done = assert.async();
1474 this.sandbox.stub( jQuery, "readyException", function( error ) {
1475 assert.strictEqual(
1476 error.message,
1477 "Error in jQuery ready",
1478 "The custom jQuery.readyException should have been called"
1480 done();
1481 } );
1483 jQuery( function() {
1484 throw new Error( "Error in jQuery ready" );
1485 } );
1486 } );