1 QUnit
.module( "core", {
2 beforeEach: function() {
3 this.sandbox
= sinon
.sandbox
.create();
5 afterEach: function() {
6 this.sandbox
.restore();
7 return moduleTeardown
.apply( this, arguments
);
11 QUnit
.test( "Basic requirements", function( assert
) {
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" );
22 QUnit
.test( "jQuery()", function( assert
) {
25 obj
= jQuery( "div" ),
26 code
= jQuery( "<code/>" ),
27 img
= jQuery( "<img/>" ),
28 div
= jQuery( "<div/><hr/><code/><b/>" ),
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
) {
41 attrObj
[ "click" ] = function() { assert
.ok( exec
, "Click executed." ); };
43 if ( jQuery
.fn
.width
) {
45 attrObj
[ "width" ] = 10;
47 if ( jQuery
.fn
.offset
) {
49 attrObj
[ "offset" ] = { "top": 1, "left": 1 };
51 if ( jQuery
.fn
.css
) {
53 attrObj
[ "css" ] = { "paddingLeft": 1, "paddingRight": 1 };
55 if ( jQuery
.fn
.attr
) {
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)
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
91 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
95 assert.ok( pass, "jQuery('<tag>') 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" );
141 elem
.trigger( "click" );
143 // manually clean up detached elements
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)" );
156 QUnit
[ jQuery
.find
.compile
? "test" : "skip" ]( "jQuery(selector, context)", function( assert
) {
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" );
163 QUnit
.test( "globalEval", function( assert
) {
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" );
177 QUnit
.test( "globalEval with 'use strict'", function( assert
) {
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)" );
185 QUnit
.test( "globalEval execution after script injection (#7862)", function( assert
) {
189 script
= document
.createElement( "script" );
191 script
.src
= baseURL
+ "mock.php?action=wait&wait=2&script=1";
194 document
.body
.appendChild( script
);
196 jQuery
.globalEval( "var strictEvalTest = " + Date
.now() + ";" );
197 assert
.ok( window
.strictEvalTest
- now
< 500, "Code executed synchronously" );
200 QUnit
.test( "noConflict", function( assert
) {
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." );
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
= $$;
219 QUnit
.test( "trim", function( assert
) {
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", " " );
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" );
241 QUnit
.test( "isPlainObject", function( assert
) {
242 var done
= assert
.async();
246 var pass
, iframe
, doc
, parentObj
, childObj
, deep
,
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" );
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" );
296 deep
= { "foo": { "baz": true }, "foo2": document
};
297 assert
.ok( jQuery
.isPlainObject( deep
), "Object with objects is still plain" );
300 assert
.ok( !jQuery
.isPlainObject( document
.createElement( "div" ) ), "DOM Element" );
303 assert
.ok( !jQuery
.isPlainObject( window
), "window" );
307 jQuery
.isPlainObject( window
.location
);
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
: "" ) );
322 iframe
= jQuery( "#qunit-fixture" )[ 0 ].appendChild( document
.createElement( "iframe" ) );
323 doc
= iframe
.contentDocument
|| iframe
.contentWindow
.document
;
325 doc
.write( "<body onload='window.parent.iframeDone(Object);'>" );
328 window
.iframeDone( Object
, "iframes not supported" );
332 QUnit
[ typeof Symbol
=== "function" ? "test" : "skip" ]( "isPlainObject(Symbol)", function( assert
) {
335 assert
.equal( jQuery
.isPlainObject( Symbol() ), false, "Symbol" );
336 assert
.equal( jQuery
.isPlainObject( Object( Symbol() ) ), false, "Symbol inside an object" );
339 QUnit
.test( "isPlainObject(localStorage)", function( assert
) {
342 assert
.equal( jQuery
.isPlainObject( localStorage
), false );
345 QUnit
[ "assign" in Object
? "test" : "skip" ]( "isPlainObject(Object.assign(...))",
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
) {
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" );
364 iframe
= document
.createElement( "iframe" );
365 document
.body
.appendChild( iframe
);
368 body
= jQuery( iframe
).contents()[ 0 ];
371 assert
.ok( !jQuery
.isXMLDoc( body
), "Iframe body element" );
373 assert
.ok( false, "Iframe body element exception" );
377 assert
.ok( true, "Iframe body element - iframe not working correctly" );
380 document
.body
.removeChild( iframe
);
383 QUnit
.test( "XSS via location.hash", function( assert
) {
384 var done
= assert
.async();
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" ];
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" );
398 jQuery
[ "_check9521" ]( true );
402 QUnit
.test( "isXMLDoc - XML", function( assert
) {
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" );
410 QUnit
.test( "jQuery('html')", function( assert
) {
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." );
453 assert
.ok( "skip", "Complex :not not supported in selector-native" );
455 assert
.equal( jQuery( "\\<div\\>" ).length
, 0, "Ignore escaped html characters" );
458 QUnit
.test( "jQuery(element with non-alphanumeric name)", function( assert
) {
461 jQuery
.each( [ "-", ":" ], function( i
, symbol
) {
462 jQuery
.each( [ "thead", "tbody", "tfoot", "colgroup", "caption", "tr", "th", "td" ],
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" );
480 QUnit
.test( "jQuery('massive html #7990')", function( assert
) {
484 li
= "<li>very very very very large html string</li>",
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 );
497 QUnit
.test( "jQuery('html', context)", function( assert
) {
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" );
505 QUnit
.test( "jQuery(selector, xml).text(str) - loaded via xml document", function( assert
) {
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" );
517 QUnit
.test( "end()", function( assert
) {
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" );
524 assert
.equal( "Yahoo", jQuery( "#yahoo" ).text(), "check for non-destructive behavior" );
527 QUnit
.test( "length", function( assert
) {
529 assert
.equal( jQuery( "#qunit-fixture p" ).length
, 6, "Get Number of Elements Found" );
532 QUnit
.test( "get()", function( assert
) {
534 assert
.deepEqual( jQuery( "#qunit-fixture p" ).get(), q( "firstp", "ap", "sndp", "en", "sap", "first" ), "Get All Elements" );
537 QUnit
.test( "toArray()", function( assert
) {
539 assert
.deepEqual( jQuery( "#qunit-fixture p" ).toArray(),
540 q( "firstp", "ap", "sndp", "en", "sap", "first" ),
541 "Convert jQuery object to an Array" );
544 QUnit
.test( "inArray()", function( assert
) {
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" ),
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 }
561 p
: jQuery( "#liveSpan1" )[ 0 ],
562 em
: jQuery( "#nothiddendiv" )[ 0 ],
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" );
575 jQuery
.each( falseTests
, function( key
, elem
) {
576 assert
.equal( !!~jQuery
.inArray( elem
, selections
[ key
] ), false, "elem is NOT in the array of selections" );
581 QUnit
.test( "get(Number)", function( assert
) {
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" );
587 QUnit
.test( "get(-Number)", function( assert
) {
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" );
593 QUnit
.test( "each(Function)", function( assert
) {
597 div
= jQuery( "div" );
598 div
.each( function() {this.foo
= "zoo";} );
600 for ( i
= 0; i
< div
.length
; i
++ ) {
601 if ( div
.get( i
).foo
!== "zoo" ) {
605 assert
.ok( pass
, "Execute a function, Relative" );
608 QUnit
.test( "slice()", function( assert
) {
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)" );
623 QUnit
.test( "first()/last()", function( assert
) {
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" );
635 QUnit
.test( "map()", function( assert
) {
639 jQuery( "#ap" ).map( function() {
640 return jQuery( this ).find( "a" ).get();
642 q( "google", "groups", "anchor1", "mark" ),
647 jQuery( "#ap > a" ).map( function() {
648 return this.parentNode
;
650 q( "ap", "ap", "ap" ),
655 QUnit
.test( "jQuery.map", function( assert
) {
658 var i
, label
, result
, callback
;
660 result
= jQuery
.map( [ 3, 4, 5 ], function( v
, k
) {
663 assert
.equal( result
.join( "" ), "012", "Map the keys from an array" );
665 result
= jQuery
.map( [ 3, 4, 5 ], function( v
) {
668 assert
.equal( result
.join( "" ), "345", "Map the values from an array" );
670 result
= jQuery
.map( { a
: 1, b
: 2 }, function( v
, k
) {
673 assert
.equal( result
.join( "" ), "ab", "Map the keys from an object" );
675 result
= jQuery
.map( { a
: 1, b
: 2 }, function( v
) {
678 assert
.equal( result
.join( "" ), "12", "Map the values from an object" );
680 result
= jQuery
.map( [ "a", undefined, null, "b" ], function( v
) {
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
) {
688 assert
.equal( result
.join( "" ), "ab", "Object iteration does not include undefined/null results" );
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
) {
700 result
[ i
].foo
= "bar";
701 jQuery
.map( result
[ i
], callback
);
705 "undefined": undefined,
710 "nonempty string": "string",
715 callback = function( v
, k
) {
716 assert
.equal( k
, "length", "Object with " + label
+ " length treated like object" );
718 for ( i
in result
) {
720 jQuery
.map( { length
: result
[ i
] }, callback
);
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
) {
737 jQuery
.map( result
[ i
], callback
);
741 jQuery
.map( { length
: 0 }, function() {
744 assert
.ok( !result
, "length: 0 plain object treated like array" );
747 jQuery
.map( document
.getElementsByTagName( "asdf" ), function() {
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
];
755 assert
.equal( result
.join( "" ), "00012223", "Array results flattened (#2616)" );
758 QUnit
.test( "jQuery.merge()", function( assert
) {
762 jQuery
.merge( [], [] ),
768 jQuery
.merge( [ 1 ], [ 2 ] ),
770 "Basic (single-element)"
773 jQuery
.merge( [ 1, 2 ], [ 3, 4 ] ),
775 "Basic (multiple-element)"
779 jQuery
.merge( [ 1, 2 ], [] ),
784 jQuery
.merge( [], [ 1, 2 ] ),
789 // Fixed at [5998], #3641
791 jQuery
.merge( [ -2, -1 ], [ 0, 1, 2 ] ),
793 "Second array including a zero (falsy)"
796 // After fixing #5527
798 jQuery
.merge( [], [ null, undefined ] ),
800 "Second array including null and undefined values"
803 jQuery
.merge( { length
: 0 }, [ 1, 2 ] ),
804 { length
: 2, 0: 1, 1: 2 },
808 jQuery
.merge( [ 1, 2 ], { length
: 1, 0: 3 } ),
814 jQuery
.merge( [], document
.getElementById( "lengthtest" ).getElementsByTagName( "input" ) ),
815 [ document
.getElementById( "length" ), document
.getElementById( "idTest" ) ],
820 QUnit
.test( "jQuery.grep()", function( assert
) {
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" );
831 jQuery
.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion
),
833 "Satisfying elements present"
836 jQuery
.grep( [ 1, 3, 5, 7 ], searchCriterion
),
838 "Satisfying elements absent"
842 jQuery
.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion
, true ),
844 "Satisfying elements present and grep inverted"
847 jQuery
.grep( [ 1, 3, 5, 7 ], searchCriterion
, true ),
849 "Satisfying elements absent and grep inverted"
853 jQuery
.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion
, false ),
855 "Satisfying elements present but grep explicitly uninverted"
858 jQuery
.grep( [ 1, 3, 5, 7 ], searchCriterion
, false ),
860 "Satisfying elements absent and grep explicitly uninverted"
864 QUnit
.test( "jQuery.grep(Array-like)", function( assert
) {
867 var searchCriterion = function( value
) {
868 return value
% 2 === 0;
871 assert
.deepEqual( jQuery
.grep( { length
: 0 }, searchCriterion
), [], "Empty array-like" );
874 jQuery
.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length
: 6 }, searchCriterion
),
876 "Satisfying elements present and array-like object used"
879 jQuery
.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length
: 4 }, searchCriterion
),
881 "Satisfying elements absent and Array-like object used"
885 jQuery
.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length
: 6 }, searchCriterion
, true ),
887 "Satisfying elements present, array-like object used, and grep inverted"
890 jQuery
.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length
: 4 }, searchCriterion
, true ),
892 "Satisfying elements absent, array-like object used, and grep inverted"
896 jQuery
.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length
: 6 }, searchCriterion
, false ),
898 "Satisfying elements present, Array-like object used, but grep explicitly uninverted"
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"
907 QUnit
.test( "jQuery.extend(Object, Object)", function( assert
) {
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
},
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" );
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" );
945 optionsWithLength
= { "foo": { "length": -1 } };
946 jQuery
.extend( true, empty
, optionsWithLength
);
947 assert
.deepEqual( empty
[ "foo" ], optionsWithLength
[ "foo" ], "The length property must copy correctly" );
950 optionsWithDate
= { "foo": { "date": new Date() } };
951 jQuery
.extend( true, empty
, optionsWithDate
);
952 assert
.deepEqual( empty
[ "foo" ], optionsWithDate
[ "foo" ], "Dates copy correctly" );
955 myKlass = function() {};
956 customObject
= new myKlass();
957 optionsWithCustomObject
= { "foo": { "date": customObject
} };
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() {} };
965 jQuery
.extend( true, empty
, optionsWithCustomObject
);
966 assert
.ok( empty
[ "foo" ] && empty
[ "foo" ][ "date" ] === customObject
, "Custom objects copy correctly" );
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" );
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" );
997 jQuery
.extend( true, obj
, { foo
:"notnull" } );
998 assert
.equal( obj
.foo
, "notnull", "Make sure a null value can be overwritten" );
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" );
1019 QUnit
.test( "jQuery.extend(Object, Object {created with \"defineProperties\"})", function( assert
) {
1022 var definedObj
= Object
.defineProperties( {}, {
1029 "nonenumerableProp": {
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" );
1042 QUnit
.test( "jQuery.extend(true,{},{a:[], o:{}}); deep copy with array, followed by object", function( assert
) {
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.
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" );
1065 QUnit
.test( "jQuery.extend( true, ... ) Object.prototype pollution", function( assert
) {
1068 jQuery
.extend( true, {}, JSON
.parse( "{\"__proto__\": {\"devMode\": true}}" ) );
1069 assert
.ok( !( "devMode" in {} ), "Object.prototype not polluted" );
1072 QUnit
.test( "jQuery.each(Object,Function)", function( assert
) {
1073 assert
.expect( 23 );
1075 var i
, label
, seen
, callback
;
1078 jQuery
.each( [ 3, 4, 5 ], function( k
, v
) {
1081 assert
.deepEqual( seen
, { "0": 3, "1": 4, "2": 5 }, "Array iteration" );
1084 jQuery
.each( { name
: "name", lang
: "lang" }, function( k
, v
) {
1087 assert
.deepEqual( seen
, { name
: "name", lang
: "lang" }, "Object iteration" );
1090 jQuery
.each( [ 1, 2, 3 ], function( k
, v
) {
1096 assert
.deepEqual( seen
, [ 1, 2 ], "Broken array iteration" );
1099 jQuery
.each( { "a": 1, "b": 2, "c": 3 }, function( k
, v
) {
1103 assert
.deepEqual( seen
, [ 1 ], "Broken object iteration" );
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" );
1115 seen
[ i
].foo
= "bar";
1116 jQuery
.each( seen
[ i
], callback
);
1120 "undefined": undefined,
1125 "nonempty string": "string",
1126 "string \"0\"": "0",
1130 callback = function( k
) {
1131 assert
.equal( k
, "length", "Object with " + label
+ " length treated like object" );
1135 jQuery
.each( { length
: seen
[ i
] }, callback
);
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" );
1153 jQuery
.each( seen
[ i
], callback
);
1157 jQuery
.each( { length
: 0 }, function() {
1160 assert
.ok( !seen
, "length: 0 plain object treated like array" );
1163 jQuery
.each( document
.getElementsByTagName( "asdf" ), function() {
1166 assert
.ok( !seen
, "empty NodeList treated like array" );
1169 jQuery
.each( document
.styleSheets
, function() {
1172 assert
.equal( i
, document
.styleSheets
.length
, "Iteration over document.styleSheets" );
1175 QUnit
.test( "jQuery.each/map(undefined/null,Function)", function( assert
) {
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() {} );" );
1185 assert
.ok( false, "each/map must accept null and undefined values" );
1189 QUnit
.test( "JIT compilation does not interfere with length retrieval (gh-2145)", function( assert
) {
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
++ ) {
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)" );
1206 assert
.equal( i
, 3, "Iteration over object with solely " +
1207 "numeric indices (gh-2145 JIT iOS 8 bug)" );
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)" );
1248 QUnit
.test( "jQuery.inArray", function( assert
) {
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" );
1258 QUnit
.test( "jQuery.isEmptyObject", function( assert
) {
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" );
1268 QUnit
.test( "jQuery.parseHTML", function( assert
) {
1269 assert
.expect( 23 );
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" );
1313 QUnit
.test( "jQuery.parseHTML(<a href>) - gh-2965", function( assert
) {
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" );
1322 if ( jQuery
.support
.createHTMLDocument
) {
1323 QUnit
.test( "jQuery.parseHTML", function( assert
) {
1324 var done
= assert
.async();
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." );
1339 QUnit
.test( "jQuery.parseXML", function( assert
) {
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" );
1351 assert
.strictEqual( e
, undefined, "unexpected error" );
1354 xml
= jQuery
.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
1355 assert
.ok( false, "invalid xml not detected" );
1357 assert
.strictEqual( e
.message
, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "invalid xml detected" );
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" );
1369 assert
.ok( false, "empty input throws exception" );
1374 "Conditional compilation compatibility (#13274)",
1376 function( assert
, jQuery
, window
, document
, cc_on
, errors
) {
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
) ) {
1389 "document ready when jQuery loaded asynchronously (#13655)",
1390 "core/dynamic_ready.html",
1391 function( assert
, jQuery
, window
, document
, ready
) {
1393 assert
.equal( true, ready
, "document ready correctly fired when jQuery is loaded after DOMContentLoaded" );
1399 "Tolerating alias-masked DOM properties (#14074)",
1400 "core/aliased.html",
1401 function( assert
, jQuery
, window
, document
, errors
) {
1403 assert
.deepEqual( errors
, [], "jQuery loaded" );
1408 "Don't call window.onready (#14802)",
1409 "core/onready.html",
1410 function( assert
, jQuery
, window
, document
, error
) {
1412 assert
.equal( error
, false, "no call to user-defined onready" );
1416 QUnit
.test( "Iterability of jQuery objects (gh-1693)", function( assert
) {
1419 var i
, elem
, result
;
1421 if ( typeof Symbol
=== "function" ) {
1423 elem
= jQuery( "<div></div><span></span><a></a>" );
1427 eval( "for ( i of elem ) { result += i.nodeName; }" );
1429 assert
.equal( result
, "DIVSPANA", "for-of works on jQuery objects" );
1431 assert
.ok( true, "The browser doesn't support Symbols" );
1436 "Iterability of jQuery objects with Symbol polyfill (gh-1693)",
1437 "core/jquery-iterability-transpiled.html",
1438 function( assert
, jQuery
, window
, document
, testString
) {
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
) {
1451 this.sandbox
.stub( window
, "setTimeout", function( fn
) {
1455 message
= error
.message
;
1459 jQuery( function() {
1460 throw new Error( "Error in jQuery ready" );
1464 "Error in jQuery ready",
1465 "The error should have been thrown in a timeout"
1469 QUnit
[ jQuery
.Deferred
? "test" : "skip" ]( "jQuery.readyException (custom)", function( assert
) {
1472 var done
= assert
.async();
1474 this.sandbox
.stub( jQuery
, "readyException", function( error
) {
1477 "Error in jQuery ready",
1478 "The custom jQuery.readyException should have been called"
1483 jQuery( function() {
1484 throw new Error( "Error in jQuery ready" );