1 module("core", { teardown: moduleTeardown });
3 test("Basic requirements", function() {
5 ok( Array.prototype.push, "Array.push()" );
6 ok( Function.prototype.apply, "Function.apply()" );
7 ok( document.getElementById, "getElementById" );
8 ok( document.getElementsByTagName, "getElementsByTagName" );
9 ok( RegExp, "RegExp" );
10 ok( jQuery, "jQuery" );
14 test("jQuery()", function() {
18 code = jQuery("<code/>"),
19 img = jQuery("<img/>"),
20 div = jQuery("<div/><hr/><code/><b/>"),
29 // The $(html, props) signature can stealth-call any $.fn method, check for a
30 // few here but beware of modular builds where these methods may be excluded.
31 if ( jQuery.fn.click ) {
33 attrObj["click"] = function() { ok( exec, "Click executed." ); };
35 if ( jQuery.fn.width ) {
37 attrObj["width"] = 10;
39 if ( jQuery.fn.offset ) {
41 attrObj["offset"] = { "top": 1, "left": 1 };
43 if ( jQuery.fn.css ) {
45 attrObj["css"] = { "paddingLeft": 1, "paddingRight": 1 };
47 if ( jQuery.fn.attr ) {
49 attrObj.attr = { "desired": "very" };
54 // Basic constructor's behavior
55 equal( jQuery().length, 0, "jQuery() === jQuery([])" );
56 equal( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" );
57 equal( jQuery(null).length, 0, "jQuery(null) === jQuery([])" );
58 equal( jQuery("").length, 0, "jQuery('') === jQuery([])" );
59 deepEqual( jQuery(obj).get(), obj.get(), "jQuery(jQueryObj) == jQueryObj" );
61 // Invalid #id goes to Sizzle which will throw an error (gh-1682)
65 ok( true, "Threw an error on #id with no id" );
68 // can actually yield more than one, when iframes are included, the window is an array as well
69 equal( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" );
72 // disabled since this test was doing nothing. i tried to fix it but i'm not sure
73 // what the expected behavior should even be. FF returns "\n" for the text node
74 // make sure this is handled
75 var crlfContainer = jQuery('<p>\r\n</p>');
76 var x = crlfContainer.contents().get(0).nodeValue;
77 equal( x, what???, "Check for \\r and \\n in jQuery()" );
80 /* // Disabled until we add this functionality in
83 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
87 ok( pass, "jQuery('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
89 equal( code.length, 1, "Correct number of elements generated for code" );
90 equal( code.parent().length, 0, "Make sure that the generated HTML has no parent." );
92 equal( img.length, 1, "Correct number of elements generated for img" );
93 equal( img.parent().length, 0, "Make sure that the generated HTML has no parent." );
95 equal( div.length, 4, "Correct number of elements generated for div hr code b" );
96 equal( div.parent().length, 0, "Make sure that the generated HTML has no parent." );
98 equal( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
100 equal( jQuery(document.body).get(0), jQuery("body").get(0), "Test passing an html node to the factory" );
102 elem = jQuery(" <em>hello</em>")[0];
103 equal( elem.nodeName.toLowerCase(), "em", "leading space" );
105 elem = jQuery("\n\n<em>world</em>")[0];
106 equal( elem.nodeName.toLowerCase(), "em", "leading newlines" );
108 elem = jQuery("<div/>", attrObj );
110 if ( jQuery.fn.width ) {
111 equal( elem[0].style.width, "10px", "jQuery() quick setter width");
114 if ( jQuery.fn.offset ) {
115 equal( elem[0].style.top, "1px", "jQuery() quick setter offset");
118 if ( jQuery.fn.css ) {
119 equal( elem[0].style.paddingLeft, "1px", "jQuery quick setter css");
120 equal( elem[0].style.paddingRight, "1px", "jQuery quick setter css");
123 if ( jQuery.fn.attr ) {
124 equal( elem[0].getAttribute("desired"), "very", "jQuery quick setter attr");
127 equal( elem[0].childNodes.length, 1, "jQuery quick setter text");
128 equal( elem[0].firstChild.nodeValue, "test", "jQuery quick setter text");
129 equal( elem[0].className, "test2", "jQuery() quick setter class");
130 equal( elem[0].id, "test3", "jQuery() quick setter id");
133 elem.trigger("click");
135 // manually clean up detached elements
138 for ( i = 0; i < 3; ++i ) {
139 elem = jQuery("<input type='text' value='TEST' />");
141 equal( elem[0].defaultValue, "TEST", "Ensure cached nodes are cloned properly (Bug #6655)" );
143 elem = jQuery( "<input type='hidden'>", {} );
144 strictEqual( elem[ 0 ].ownerDocument, document,
145 "Empty attributes object is not interpreted as a document (trac-8950)" );
148 test("jQuery(selector, context)", function() {
150 deepEqual( jQuery("div p", "#qunit-fixture").get(), q("sndp", "en", "sap"), "Basic selector with string as context" );
151 deepEqual( jQuery("div p", q("qunit-fixture")[0]).get(), q("sndp", "en", "sap"), "Basic selector with element as context" );
152 deepEqual( jQuery("div p", jQuery("#qunit-fixture")).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
155 test( "globalEval", function() {
157 Globals.register("globalEvalTest");
159 jQuery.globalEval("globalEvalTest = 1;");
160 equal( window.globalEvalTest, 1, "Test variable assignments are global" );
162 jQuery.globalEval("var globalEvalTest = 2;");
163 equal( window.globalEvalTest, 2, "Test variable declarations are global" );
165 jQuery.globalEval("this.globalEvalTest = 3;");
166 equal( window.globalEvalTest, 3, "Test context (this) is the window object" );
169 test( "globalEval with 'use strict'", function() {
171 Globals.register("strictEvalTest");
173 jQuery.globalEval("'use strict'; var strictEvalTest = 1;");
174 equal( window.strictEvalTest, 1, "Test variable declarations are global (strict mode)" );
177 test( "globalEval execution after script injection (#7862)", 1, function() {
179 script = document.createElement( "script" );
181 script.src = "data/longLoadScript.php?sleep=2";
184 document.body.appendChild( script );
186 jQuery.globalEval( "var strictEvalTest = " + jQuery.now() + ";");
187 ok( window.strictEvalTest - now < 500, "Code executed synchronously" );
190 // This is not run in AMD mode
191 if ( jQuery.noConflict ) {
192 test("noConflict", function() {
197 strictEqual( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
198 strictEqual( window["jQuery"], $$, "Make sure jQuery wasn't touched." );
199 strictEqual( window["$"], original$, "Make sure $ was reverted." );
203 strictEqual( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" );
204 strictEqual( window["jQuery"], originaljQuery, "Make sure jQuery was reverted." );
205 strictEqual( window["$"], original$, "Make sure $ was reverted." );
206 ok( $$().pushStack([]), "Make sure that jQuery still works." );
208 window["jQuery"] = jQuery = $$;
212 test("trim", function() {
215 var nbsp = String.fromCharCode(160);
217 equal( jQuery.trim("hello "), "hello", "trailing space" );
218 equal( jQuery.trim(" hello"), "hello", "leading space" );
219 equal( jQuery.trim(" hello "), "hello", "space on both sides" );
220 equal( jQuery.trim(" " + nbsp + "hello " + nbsp + " "), "hello", " " );
222 equal( jQuery.trim(), "", "Nothing in." );
223 equal( jQuery.trim( undefined ), "", "Undefined" );
224 equal( jQuery.trim( null ), "", "Null" );
225 equal( jQuery.trim( 5 ), "5", "Number" );
226 equal( jQuery.trim( false ), "false", "Boolean" );
228 equal( jQuery.trim(" "), "", "space should be trimmed" );
229 equal( jQuery.trim("ipad\xA0"), "ipad", "nbsp should be trimmed" );
230 equal( jQuery.trim("\uFEFF"), "", "zwsp should be trimmed" );
231 equal( jQuery.trim("\uFEFF \xA0! | \uFEFF"), "! |", "leading/trailing should be trimmed" );
234 test("type", function() {
237 equal( jQuery.type(null), "null", "null" );
238 equal( jQuery.type(undefined), "undefined", "undefined" );
239 equal( jQuery.type(true), "boolean", "Boolean" );
240 equal( jQuery.type(false), "boolean", "Boolean" );
241 equal( jQuery.type(Boolean(true)), "boolean", "Boolean" );
242 equal( jQuery.type(0), "number", "Number" );
243 equal( jQuery.type(1), "number", "Number" );
244 equal( jQuery.type(Number(1)), "number", "Number" );
245 equal( jQuery.type(""), "string", "String" );
246 equal( jQuery.type("a"), "string", "String" );
247 equal( jQuery.type(String("a")), "string", "String" );
248 equal( jQuery.type({}), "object", "Object" );
249 equal( jQuery.type(/foo/), "regexp", "RegExp" );
250 equal( jQuery.type(new RegExp("asdf")), "regexp", "RegExp" );
251 equal( jQuery.type([1]), "array", "Array" );
252 equal( jQuery.type(new Date()), "date", "Date" );
253 equal( jQuery.type(new Function("return;")), "function", "Function" );
254 equal( jQuery.type(function(){}), "function", "Function" );
255 equal( jQuery.type(new Error()), "error", "Error" );
256 equal( jQuery.type(window), "object", "Window" );
257 equal( jQuery.type(document), "object", "Document" );
258 equal( jQuery.type(document.body), "object", "Element" );
259 equal( jQuery.type(document.createTextNode("foo")), "object", "TextNode" );
260 equal( jQuery.type(document.getElementsByTagName("*")), "object", "NodeList" );
262 // Avoid Lint complaints
263 var MyString = String,
267 equal( jQuery.type(new MyBoolean(true)), "boolean", "Boolean" );
268 equal( jQuery.type(new MyNumber(1)), "number", "Number" );
269 equal( jQuery.type(new MyString("a")), "string", "String" );
270 equal( jQuery.type(new MyObject()), "object", "Object" );
273 asyncTest("isPlainObject", function() {
276 var pass, iframe, doc,
279 // The use case that we want to match
280 ok( jQuery.isPlainObject({}), "{}" );
282 // Not objects shouldn't be matched
283 ok( !jQuery.isPlainObject(""), "string" );
284 ok( !jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number" );
285 ok( !jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean" );
286 ok( !jQuery.isPlainObject(null), "null" );
287 ok( !jQuery.isPlainObject(undefined), "undefined" );
289 // Arrays shouldn't be matched
290 ok( !jQuery.isPlainObject([]), "array" );
292 // Instantiated objects shouldn't be matched
293 ok( !jQuery.isPlainObject(new Date()), "new Date" );
295 // Functions shouldn't be matched
296 ok( !jQuery.isPlainObject(fn), "fn" );
298 // Again, instantiated objects shouldn't be matched
299 ok( !jQuery.isPlainObject(new fn()), "new fn (no methods)" );
301 // Makes the function a little more realistic
302 // (and harder to detect, incidentally)
303 fn.prototype["someMethod"] = function(){};
305 // Again, instantiated objects shouldn't be matched
306 ok( !jQuery.isPlainObject(new fn()), "new fn" );
309 ok( !jQuery.isPlainObject( document.createElement("div") ), "DOM Element" );
312 ok( !jQuery.isPlainObject( window ), "window" );
316 jQuery.isPlainObject( window.location );
319 ok( pass, "Does not throw exceptions on host objects" );
321 // Objects from other windows should be matched
322 Globals.register("iframeDone");
323 window.iframeDone = function( otherObject, detail ) {
324 window.iframeDone = undefined;
325 iframe.parentNode.removeChild( iframe );
326 ok( jQuery.isPlainObject(new otherObject()), "new otherObject" + ( detail ? " - " + detail : "" ) );
331 iframe = jQuery("#qunit-fixture")[0].appendChild( document.createElement("iframe") );
332 doc = iframe.contentDocument || iframe.contentWindow.document;
334 doc.write("<body onload='window.parent.iframeDone(Object);'>");
337 window.iframeDone( Object, "iframes not supported" );
341 test("isFunction", function() {
344 var mystr, myarr, myfunction, fn, obj, nodes, first, input, a;
346 // Make sure that false values return false
347 ok( !jQuery.isFunction(), "No Value" );
348 ok( !jQuery.isFunction( null ), "null Value" );
349 ok( !jQuery.isFunction( undefined ), "undefined Value" );
350 ok( !jQuery.isFunction( "" ), "Empty String Value" );
351 ok( !jQuery.isFunction( 0 ), "0 Value" );
354 ok( jQuery.isFunction(String), "String Function("+String+")" );
355 ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
356 ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
357 ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
359 // When stringified, this could be misinterpreted
361 ok( !jQuery.isFunction(mystr), "Function String" );
363 // When stringified, this could be misinterpreted
364 myarr = [ "function" ];
365 ok( !jQuery.isFunction(myarr), "Function Array" );
367 // When stringified, this could be misinterpreted
368 myfunction = { "function": "test" };
369 ok( !jQuery.isFunction(myfunction), "Function Object" );
371 // Make sure normal functions still work
373 ok( jQuery.isFunction(fn), "Normal Function" );
375 obj = document.createElement("object");
377 // Firefox says this is a function
378 ok( !jQuery.isFunction(obj), "Object Element" );
380 // Since 1.3, this isn't supported (#2968)
381 //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
383 nodes = document.body.childNodes;
385 // Safari says this is a function
386 ok( !jQuery.isFunction(nodes), "childNodes Property" );
388 first = document.body.firstChild;
390 // Normal elements are reported ok everywhere
391 ok( !jQuery.isFunction(first), "A normal DOM Element" );
393 input = document.createElement("input");
395 document.body.appendChild( input );
397 // Since 1.3, this isn't supported (#2968)
398 //ok( jQuery.isFunction(input.focus), "A default function property" );
400 document.body.removeChild( input );
402 a = document.createElement("a");
403 a.href = "some-function";
404 document.body.appendChild( a );
406 // This serializes with the word 'function' in it
407 ok( !jQuery.isFunction(a), "Anchor Element" );
409 document.body.removeChild( a );
411 // Recursive function calls have lengths and array-like properties
412 function callme(callback){
413 function fn(response){
417 ok( jQuery.isFunction(fn), "Recursive Function Call" );
419 fn({ some: "data" });
423 callme(function(){});
427 test( "isNumeric", function() {
430 var t = jQuery.isNumeric,
431 ToString = function( value ) {
432 this.toString = function() {
433 return String( value );
437 ok( t( "-10" ), "Negative integer string" );
438 ok( t( "0" ), "Zero string" );
439 ok( t( "5" ), "Positive integer string" );
440 ok( t( -16 ), "Negative integer number" );
441 ok( t( 0 ), "Zero integer number" );
442 ok( t( 32 ), "Positive integer number" );
443 ok( t( "040" ), "Octal integer literal string" );
444 ok( t( "0xFF" ), "Hexadecimal integer literal string" );
445 ok( t( 0xFFF ), "Hexadecimal integer literal" );
446 ok( t( "-1.6" ), "Negative floating point string" );
447 ok( t( "4.536" ), "Positive floating point string" );
448 ok( t( -2.6 ), "Negative floating point number" );
449 ok( t( 3.1415 ), "Positive floating point number" );
450 ok( t( 1.5999999999999999 ), "Very precise floating point number" );
451 ok( t( 8e5 ), "Exponential notation" );
452 ok( t( "123e-2" ), "Exponential notation string" );
453 ok( t( new ToString( "42" ) ), "Custom .toString returning number" );
455 equal( t( "" ), false, "Empty string" );
456 equal( t( " " ), false, "Whitespace characters string" );
457 equal( t( "\t\t" ), false, "Tab characters string" );
458 equal( t( "abcdefghijklm1234567890" ), false, "Alphanumeric character string" );
459 equal( t( "xabcdefx" ), false, "Non-numeric character string" );
460 equal( t( true ), false, "Boolean true literal" );
461 equal( t( false ), false, "Boolean false literal" );
462 equal( t( "bcfed5.2" ), false, "Number with preceding non-numeric characters" );
463 equal( t( "7.2acdgs" ), false, "Number with trailling non-numeric characters" );
464 equal( t( undefined ), false, "Undefined value" );
465 equal( t( null ), false, "Null value" );
466 equal( t( NaN ), false, "NaN value" );
467 equal( t( Infinity ), false, "Infinity primitive" );
468 equal( t( Number.POSITIVE_INFINITY ), false, "Positive Infinity" );
469 equal( t( Number.NEGATIVE_INFINITY ), false, "Negative Infinity" );
470 equal( t( new ToString( "Devo" ) ), false, "Custom .toString returning non-number" );
471 equal( t( {} ), false, "Empty object" );
472 equal( t( [] ), false, "Empty array" );
473 equal( t( [ 42 ] ), false, "Array with one number" );
474 equal( t( function(){} ), false, "Instance of a function" );
475 equal( t( new Date() ), false, "Instance of a Date" );
478 test("isXMLDoc - HTML", function() {
481 ok( !jQuery.isXMLDoc( document ), "HTML document" );
482 ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
483 ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
486 iframe = document.createElement("iframe");
487 document.body.appendChild( iframe );
490 body = jQuery(iframe).contents()[0];
493 ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
495 ok( false, "Iframe body element exception" );
499 ok( true, "Iframe body element - iframe not working correctly" );
502 document.body.removeChild( iframe );
505 test("XSS via location.hash", function() {
509 jQuery["_check9521"] = function(x){
510 ok( x, "script called from #id-like selector with inline handler" );
511 jQuery("#check9521").remove();
512 delete jQuery["_check9521"];
516 // This throws an error because it's processed like an id
517 jQuery( "#<img id='check9521' src='no-such-.gif' onerror='jQuery._check9521(false)'>" ).appendTo("#qunit-fixture");
519 jQuery["_check9521"](true);
523 test("isXMLDoc - XML", function() {
525 var xml = createDashboardXML();
526 ok( jQuery.isXMLDoc( xml ), "XML document" );
527 ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" );
528 ok( jQuery.isXMLDoc( jQuery("tab", xml)[0] ), "XML Tab Element" );
531 test("isWindow", function() {
534 ok( jQuery.isWindow(window), "window" );
535 ok( jQuery.isWindow(document.getElementsByTagName("iframe")[0].contentWindow), "iframe.contentWindow" );
536 ok( !jQuery.isWindow(), "empty" );
537 ok( !jQuery.isWindow(null), "null" );
538 ok( !jQuery.isWindow(undefined), "undefined" );
539 ok( !jQuery.isWindow(document), "document" );
540 ok( !jQuery.isWindow(document.documentElement), "documentElement" );
541 ok( !jQuery.isWindow(""), "string" );
542 ok( !jQuery.isWindow(1), "number" );
543 ok( !jQuery.isWindow(true), "boolean" );
544 ok( !jQuery.isWindow({}), "object" );
545 ok( !jQuery.isWindow({ setInterval: function(){} }), "fake window" );
546 ok( !jQuery.isWindow(/window/), "regexp" );
547 ok( !jQuery.isWindow(function(){}), "function" );
550 test("jQuery('html')", function() {
555 jQuery["foo"] = false;
556 s = jQuery("<script>jQuery.foo='test';</script>")[0];
557 ok( s, "Creating a script" );
558 ok( !jQuery["foo"], "Make sure the script wasn't executed prematurely" );
559 jQuery("body").append("<script>jQuery.foo='test';</script>");
560 ok( jQuery["foo"], "Executing a scripts contents in the right context" );
562 // Test multi-line HTML
563 div = jQuery("<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>")[0];
564 equal( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
565 equal( div.firstChild.nodeType, 3, "Text node." );
566 equal( div.lastChild.nodeType, 3, "Text node." );
567 equal( div.childNodes[1].nodeType, 1, "Paragraph." );
568 equal( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." );
570 ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
572 ok( !jQuery("<script/>")[0].parentNode, "Create a script" );
574 ok( jQuery("<input/>").attr("type", "hidden"), "Create an input and set the type." );
576 j = jQuery("<span>hi</span> there <!-- mon ami -->");
577 ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
579 ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
581 ok( jQuery("<div></div>")[0], "Create a div with closing tag." );
582 ok( jQuery("<table></table>")[0], "Create a table with closing tag." );
584 equal( jQuery( "element[attribute='<div></div>']" ).length, 0,
585 "When html is within brackets, do not recognize as html." );
586 //equal( jQuery( "element[attribute=<div></div>]" ).length, 0,
587 // "When html is within brackets, do not recognize as html." );
588 equal( jQuery( "element:not(<div></div>)" ).length, 0,
589 "When html is within parens, do not recognize as html." );
590 equal( jQuery( "\\<div\\>" ).length, 0, "Ignore escaped html characters" );
593 test("jQuery(tag-hyphenated elements) gh-1987", function() {
596 jQuery.each( "thead tbody tfoot colgroup caption tr th td".split(" "), function( i, name ) {
597 var j = jQuery("<" + name + "-d></" + name + "-d>");
598 ok( j[0], "Create a tag-hyphenated elements" );
599 ok( jQuery.nodeName(j[0], name.toUpperCase() + "-D"), "Tag-hyphenated element has expected node name" );
602 var j = jQuery("<tr-multiple-hyphens></tr-multiple-hyphens>");
603 ok( jQuery.nodeName(j[0], "TR-MULTIPLE-HYPHENS"), "Element with multiple hyphens in its tag has expected node name" );
606 test("jQuery('massive html #7990')", function() {
610 li = "<li>very very very very large html string</li>",
613 for ( i = 0; i < 30000; i += 1 ) {
614 html[html.length] = li;
616 html[html.length] = "</ul>";
617 html = jQuery(html.join(""))[0];
618 equal( html.nodeName.toLowerCase(), "ul");
619 equal( html.firstChild.nodeName.toLowerCase(), "li");
620 equal( html.childNodes.length, 30000 );
623 test("jQuery('html', context)", function() {
626 var $div = jQuery("<div/>")[0],
627 $span = jQuery("<span/>", $div);
628 equal($span.length, 1, "verify a span created with a div context works, #1763");
631 test("jQuery(selector, xml).text(str) - loaded via xml document", function() {
634 var xml = createDashboardXML(),
635 // tests for #1419 where ie was a problem
636 tab = jQuery("tab", xml).eq(0);
637 equal( tab.text(), "blabla", "verify initial text correct" );
639 equal( tab.text(), "newtext", "verify new text correct" );
642 test("end()", function() {
644 equal( "Yahoo", jQuery("#yahoo").parent().end().text(), "check for end" );
645 ok( jQuery("#yahoo").end(), "check for end with nothing to end" );
647 var x = jQuery("#yahoo");
649 equal( "Yahoo", jQuery("#yahoo").text(), "check for non-destructive behaviour" );
652 test("length", function() {
654 equal( jQuery("#qunit-fixture p").length, 6, "Get Number of Elements Found" );
657 test("get()", function() {
659 deepEqual( jQuery("#qunit-fixture p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
662 test("toArray()", function() {
664 deepEqual( jQuery("#qunit-fixture p").toArray(),
665 q("firstp","ap","sndp","en","sap","first"),
666 "Convert jQuery object to an Array" );
669 test("inArray()", function() {
673 p: q("firstp", "sap", "ap", "first"),
674 em: q("siblingnext", "siblingfirst"),
675 div: q("qunit-testrunner-toolbar", "nothiddendiv", "nothiddendivchild", "foo"),
676 a: q("mark", "groups", "google", "simon1"),
680 p: { elem: jQuery("#ap")[0], index: 2 },
681 em: { elem: jQuery("#siblingfirst")[0], index: 1 },
682 div: { elem: jQuery("#nothiddendiv")[0], index: 1 },
683 a: { elem: jQuery("#simon1")[0], index: 3 }
686 p: jQuery("#liveSpan1")[0],
687 em: jQuery("#nothiddendiv")[0],
691 jQuery.each( tests, function( key, obj ) {
692 equal( jQuery.inArray( obj.elem, selections[ key ] ), obj.index, "elem is in the array of selections of its tag" );
693 // Third argument (fromIndex)
694 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" );
695 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" );
696 equal( !!~jQuery.inArray( obj.elem, selections[ key ], -3 ), true, "elem is in the array of selections given a negative index" );
699 jQuery.each( falseTests, function( key, elem ) {
700 equal( !!~jQuery.inArray( elem, selections[ key ] ), false, "elem is NOT in the array of selections" );
705 test("get(Number)", function() {
707 equal( jQuery("#qunit-fixture p").get(0), document.getElementById("firstp"), "Get A Single Element" );
708 strictEqual( jQuery("#firstp").get(1), undefined, "Try get with index larger elements count" );
711 test("get(-Number)",function() {
713 equal( jQuery("p").get(-1), document.getElementById("first"), "Get a single element with negative index" );
714 strictEqual( jQuery("#firstp").get(-2), undefined, "Try get with index negative index larger then elements count" );
717 test("each(Function)", function() {
722 div.each(function(){this.foo = "zoo";});
724 for ( i = 0; i < div.length; i++ ) {
725 if ( div.get(i).foo !== "zoo" ) {
729 ok( pass, "Execute a function, Relative" );
732 test("slice()", function() {
735 var $links = jQuery("#ap a");
737 deepEqual( $links.slice(1,2).get(), q("groups"), "slice(1,2)" );
738 deepEqual( $links.slice(1).get(), q("groups", "anchor1", "mark"), "slice(1)" );
739 deepEqual( $links.slice(0,3).get(), q("google", "groups", "anchor1"), "slice(0,3)" );
740 deepEqual( $links.slice(-1).get(), q("mark"), "slice(-1)" );
742 deepEqual( $links.eq(1).get(), q("groups"), "eq(1)" );
743 deepEqual( $links.eq("2").get(), q("anchor1"), "eq('2')" );
744 deepEqual( $links.eq(-1).get(), q("mark"), "eq(-1)" );
747 test("first()/last()", function() {
750 var $links = jQuery("#ap a"), $none = jQuery("asdf");
752 deepEqual( $links.first().get(), q("google"), "first()" );
753 deepEqual( $links.last().get(), q("mark"), "last()" );
755 deepEqual( $none.first().get(), [], "first() none" );
756 deepEqual( $none.last().get(), [], "last() none" );
759 test("map()", function() {
763 jQuery("#ap").map(function() {
764 return jQuery( this ).find("a").get();
766 q( "google", "groups", "anchor1", "mark" ),
771 jQuery("#ap > a").map(function() {
772 return this.parentNode;
779 test("jQuery.map", function() {
782 var i, label, result, callback;
784 result = jQuery.map( [ 3, 4, 5 ], function( v, k ) {
787 equal( result.join(""), "012", "Map the keys from an array" );
789 result = jQuery.map( [ 3, 4, 5 ], function( v ) {
792 equal( result.join(""), "345", "Map the values from an array" );
794 result = jQuery.map( { a: 1, b: 2 }, function( v, k ) {
797 equal( result.join(""), "ab", "Map the keys from an object" );
799 result = jQuery.map( { a: 1, b: 2 }, function( v ) {
802 equal( result.join(""), "12", "Map the values from an object" );
804 result = jQuery.map( [ "a", undefined, null, "b" ], function( v ) {
807 equal( result.join(""), "ab", "Array iteration does not include undefined/null results" );
809 result = jQuery.map( { a: "a", b: undefined, c: null, d: "b" }, function( v ) {
812 equal( result.join(""), "ab", "Object iteration does not include undefined/null results" );
816 One: function( a ) { a = a; },
817 Two: function( a, b ) { a = a; b = b; }
819 callback = function( v, k ) {
820 equal( k, "foo", label + "-argument function treated like object" );
822 for ( i in result ) {
824 result[ i ].foo = "bar";
825 jQuery.map( result[ i ], callback );
829 "undefined": undefined,
834 "nonempty string": "string",
839 callback = function( v, k ) {
840 equal( k, "length", "Object with " + label + " length treated like object" );
842 for ( i in result ) {
844 jQuery.map( { length: result[ i ] }, callback );
848 "sparse Array": Array( 4 ),
849 "length: 1 plain object": { length: 1, "0": true },
850 "length: 2 plain object": { length: 2, "0": true, "1": true },
851 NodeList: document.getElementsByTagName("html")
853 callback = function( v, k ) {
854 if ( result[ label ] ) {
855 delete result[ label ];
856 equal( k, "0", label + " treated like array" );
859 for ( i in result ) {
861 jQuery.map( result[ i ], callback );
865 jQuery.map( { length: 0 }, function() {
868 ok( !result, "length: 0 plain object treated like array" );
871 jQuery.map( document.getElementsByTagName("asdf"), function() {
874 ok( !result, "empty NodeList treated like array" );
876 result = jQuery.map( Array(4), function( v, k ){
877 return k % 2 ? k : [k,k,k];
879 equal( result.join(""), "00012223", "Array results flattened (#2616)" );
882 test("jQuery.merge()", function() {
886 jQuery.merge( [], [] ),
892 jQuery.merge( [ 1 ], [ 2 ] ),
894 "Basic (single-element)"
897 jQuery.merge( [ 1, 2 ], [ 3, 4 ] ),
899 "Basic (multiple-element)"
903 jQuery.merge( [ 1, 2 ], [] ),
908 jQuery.merge( [], [ 1, 2 ] ),
913 // Fixed at [5998], #3641
915 jQuery.merge( [ -2, -1 ], [ 0, 1, 2 ] ),
916 [ -2, -1 , 0, 1, 2 ],
917 "Second array including a zero (falsy)"
920 // After fixing #5527
922 jQuery.merge( [], [ null, undefined ] ),
924 "Second array including null and undefined values"
927 jQuery.merge( { length: 0 }, [ 1, 2 ] ),
928 { length: 2, 0: 1, 1: 2 },
932 jQuery.merge( [ 1, 2 ], { length: 1, 0: 3 } ),
938 jQuery.merge( [], document.getElementById("lengthtest").getElementsByTagName("input") ),
939 [ document.getElementById("length"), document.getElementById("idTest") ],
944 test("jQuery.grep()", function() {
947 var searchCriterion = function( value ) {
948 return value % 2 === 0;
951 deepEqual( jQuery.grep( [], searchCriterion ), [], "Empty array" );
952 deepEqual( jQuery.grep( new Array(4), searchCriterion ), [], "Sparse array" );
954 deepEqual( jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion ), [ 2, 4, 6 ], "Satisfying elements present" );
955 deepEqual( jQuery.grep( [ 1, 3, 5, 7], searchCriterion ), [], "Satisfying elements absent" );
957 deepEqual( jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, true ), [ 1, 3, 5 ], "Satisfying elements present and grep inverted" );
958 deepEqual( jQuery.grep( [ 1, 3, 5, 7], searchCriterion, true ), [1, 3, 5, 7], "Satisfying elements absent and grep inverted" );
960 deepEqual( jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, false ), [ 2, 4, 6 ], "Satisfying elements present but grep explicitly uninverted" );
961 deepEqual( jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, false ), [], "Satisfying elements absent and grep explicitly uninverted" );
964 test("jQuery.extend(Object, Object)", function() {
967 var empty, optionsWithLength, optionsWithDate, myKlass,
968 customObject, optionsWithCustomObject, MyNumber, ret,
969 nullUndef, target, recursive, obj,
970 defaults, defaultsCopy, options1, options1Copy, options2, options2Copy, merged2,
971 settings = { "xnumber1": 5, "xnumber2": 7, "xstring1": "peter", "xstring2": "pan" },
972 options = { "xnumber2": 1, "xstring2": "x", "xxx": "newstring" },
973 optionsCopy = { "xnumber2": 1, "xstring2": "x", "xxx": "newstring" },
974 merged = { "xnumber1": 5, "xnumber2": 1, "xstring1": "peter", "xstring2": "x", "xxx": "newstring" },
975 deep1 = { "foo": { "bar": true } },
976 deep2 = { "foo": { "baz": true }, "foo2": document },
977 deep2copy = { "foo": { "baz": true }, "foo2": document },
978 deepmerged = { "foo": { "bar": true, "baz": true }, "foo2": document },
980 nestedarray = { "arr": arr };
982 jQuery.extend(settings, options);
983 deepEqual( settings, merged, "Check if extended: settings must be extended" );
984 deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
986 jQuery.extend(settings, null, options);
987 deepEqual( settings, merged, "Check if extended: settings must be extended" );
988 deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
990 jQuery.extend(true, deep1, deep2);
991 deepEqual( deep1["foo"], deepmerged["foo"], "Check if foo: settings must be extended" );
992 deepEqual( deep2["foo"], deep2copy["foo"], "Check if not deep2: options must not be modified" );
993 equal( deep1["foo2"], document, "Make sure that a deep clone was not attempted on the document" );
995 ok( jQuery.extend(true, {}, nestedarray)["arr"] !== arr, "Deep extend of object must clone child array" );
998 ok( jQuery.isArray( jQuery.extend(true, { "arr": {} }, nestedarray)["arr"] ), "Cloned array have to be an Array" );
999 ok( jQuery.isPlainObject( jQuery.extend(true, { "arr": arr }, { "arr": {} })["arr"] ), "Cloned object have to be an plain object" );
1002 optionsWithLength = { "foo": { "length": -1 } };
1003 jQuery.extend(true, empty, optionsWithLength);
1004 deepEqual( empty["foo"], optionsWithLength["foo"], "The length property must copy correctly" );
1007 optionsWithDate = { "foo": { "date": new Date() } };
1008 jQuery.extend(true, empty, optionsWithDate);
1009 deepEqual( empty["foo"], optionsWithDate["foo"], "Dates copy correctly" );
1012 myKlass = function() {};
1013 customObject = new myKlass();
1014 optionsWithCustomObject = { "foo": { "date": customObject } };
1016 jQuery.extend(true, empty, optionsWithCustomObject);
1017 ok( empty["foo"] && empty["foo"]["date"] === customObject, "Custom objects copy correctly (no methods)" );
1019 // Makes the class a little more realistic
1020 myKlass.prototype = { "someMethod": function(){} };
1022 jQuery.extend(true, empty, optionsWithCustomObject);
1023 ok( empty["foo"] && empty["foo"]["date"] === customObject, "Custom objects copy correctly" );
1027 ret = jQuery.extend(true, { "foo": 4 }, { "foo": new MyNumber(5) } );
1028 ok( parseInt(ret.foo, 10) === 5, "Wrapped numbers copy correctly" );
1031 nullUndef = jQuery.extend({}, options, { "xnumber2": null });
1032 ok( nullUndef["xnumber2"] === null, "Check to make sure null values are copied");
1034 nullUndef = jQuery.extend({}, options, { "xnumber2": undefined });
1035 ok( nullUndef["xnumber2"] === options["xnumber2"], "Check to make sure undefined values are not copied");
1037 nullUndef = jQuery.extend({}, options, { "xnumber0": null });
1038 ok( nullUndef["xnumber0"] === null, "Check to make sure null values are inserted");
1041 recursive = { foo:target, bar:5 };
1042 jQuery.extend(true, target, recursive);
1043 deepEqual( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
1045 ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
1046 equal( ret.foo.length, 1, "Check to make sure a value with coercion 'false' copies over when necessary to fix #1907" );
1048 ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
1049 ok( typeof ret.foo !== "string", "Check to make sure values equal with coercion (but not actually equal) overwrite correctly" );
1051 ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
1052 ok( typeof ret.foo !== "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" );
1055 jQuery.extend(true, obj, { foo:"notnull" } );
1056 equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );
1059 jQuery.extend(func, { key: "value" } );
1060 equal( func.key, "value", "Verify a function can be extended" );
1062 defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" };
1063 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" };
1064 options1 = { xnumber2: 1, xstring2: "x" };
1065 options1Copy = { xnumber2: 1, xstring2: "x" };
1066 options2 = { xstring2: "xx", xxx: "newstringx" };
1067 options2Copy = { xstring2: "xx", xxx: "newstringx" };
1068 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
1070 settings = jQuery.extend({}, defaults, options1, options2);
1071 deepEqual( settings, merged2, "Check if extended: settings must be extended" );
1072 deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
1073 deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" );
1074 deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" );
1077 test("jQuery.extend(true,{},{a:[], o:{}}); deep copy with array, followed by object", function() {
1080 var result, initial = {
1081 // This will make "copyIsArray" true
1082 array: [ 1, 2, 3, 4 ],
1083 // If "copyIsArray" doesn't get reset to false, the check
1084 // will evaluate true and enter the array copy block
1085 // instead of the object copy block. Since the ternary in the
1086 // "copyIsArray" block will will evaluate to false
1087 // (check if operating on an array with ), this will be
1088 // replaced by an empty array.
1092 result = jQuery.extend( true, {}, initial );
1094 deepEqual( result, initial, "The [result] and [initial] have equal shape and values" );
1095 ok( !jQuery.isArray( result.object ), "result.object wasn't paved with an empty array" );
1098 test("jQuery.each(Object,Function)", function() {
1101 var i, label, seen, callback;
1104 jQuery.each( [ 3, 4, 5 ], function( k, v ) {
1107 deepEqual( seen, { "0": 3, "1": 4, "2": 5 }, "Array iteration" );
1110 jQuery.each( { name: "name", lang: "lang" }, function( k, v ) {
1113 deepEqual( seen, { name: "name", lang: "lang" }, "Object iteration" );
1116 jQuery.each( [ 1, 2, 3 ], function( k, v ) {
1122 deepEqual( seen, [ 1, 2 ] , "Broken array iteration" );
1125 jQuery.each( {"a": 1, "b": 2,"c": 3 }, function( k, v ) {
1129 deepEqual( seen, [ 1 ], "Broken object iteration" );
1132 Zero: function() {},
1133 One: function( a ) { a = a; },
1134 Two: function( a, b ) { a = a; b = b; }
1136 callback = function( k ) {
1137 equal( k, "foo", label + "-argument function treated like object" );
1141 seen[ i ].foo = "bar";
1142 jQuery.each( seen[ i ], callback );
1146 "undefined": undefined,
1151 "nonempty string": "string",
1152 "string \"0\"": "0",
1156 callback = function( k ) {
1157 equal( k, "length", "Object with " + label + " length treated like object" );
1161 jQuery.each( { length: seen[ i ] }, callback );
1165 "sparse Array": Array( 4 ),
1166 "length: 1 plain object": { length: 1, "0": true },
1167 "length: 2 plain object": { length: 2, "0": true, "1": true },
1168 NodeList: document.getElementsByTagName("html")
1170 callback = function( k ) {
1171 if ( seen[ label ] ) {
1172 delete seen[ label ];
1173 equal( k, "0", label + " treated like array" );
1179 jQuery.each( seen[ i ], callback );
1183 jQuery.each( { length: 0 }, function() {
1186 ok( !seen, "length: 0 plain object treated like array" );
1189 jQuery.each( document.getElementsByTagName("asdf"), function() {
1192 ok( !seen, "empty NodeList treated like array" );
1195 jQuery.each( document.styleSheets, function() {
1198 equal( i, document.styleSheets.length, "Iteration over document.styleSheets" );
1201 test( "JIT compilation does not interfere with length retrieval (gh-2145)", function() {
1206 // Trigger JIT compilation of jQuery.each – and therefore isArraylike – in iOS.
1207 // Convince JSC to use one of its optimizing compilers
1208 // by providing code which can be LICM'd into nothing.
1209 for ( i = 0; i < 1000; i++ ) {
1214 jQuery.each( { 1: "1", 2: "2", 3: "3" }, function( index ) {
1215 equal( ++i, index, "Iteration over object with solely " +
1216 "numeric indices (gh-2145 JIT iOS 8 bug)" );
1218 equal( i, 3, "Iteration over object with solely " +
1219 "numeric indices (gh-2145 JIT iOS 8 bug)" );
1222 test("jQuery.makeArray", function(){
1225 equal( jQuery.makeArray(jQuery("html>*"))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
1227 equal( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
1229 equal( (function() { return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
1231 equal( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
1233 equal( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
1235 equal( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
1237 equal( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
1239 equal( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
1241 equal( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
1243 equal( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
1245 ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );
1247 // function, is tricky as it has length
1248 equal( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
1250 //window, also has length
1251 equal( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
1253 equal( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
1255 // Some nodes inherit traits of nodelists
1256 ok( jQuery.makeArray(document.getElementById("form")).length >= 13,
1257 "Pass makeArray a form (treat as elements)" );
1260 test("jQuery.inArray", function(){
1263 equal( jQuery.inArray( 0, false ), -1 , "Search in 'false' as array returns -1 and doesn't throw exception" );
1265 equal( jQuery.inArray( 0, null ), -1 , "Search in 'null' as array returns -1 and doesn't throw exception" );
1267 equal( jQuery.inArray( 0, undefined ), -1 , "Search in 'undefined' as array returns -1 and doesn't throw exception" );
1270 test("jQuery.isEmptyObject", function(){
1273 equal(true, jQuery.isEmptyObject({}), "isEmptyObject on empty object literal" );
1274 equal(false, jQuery.isEmptyObject({a:1}), "isEmptyObject on non-empty object literal" );
1276 // What about this ?
1277 // equal(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
1280 test("jQuery.proxy", function(){
1283 var test2, test3, test4, fn, cb,
1284 test = function(){ equal( this, thisObject, "Make sure that scope is set properly." ); },
1285 thisObject = { foo: "bar", method: test };
1287 // Make sure normal works
1288 test.call( thisObject );
1291 jQuery.proxy( test, thisObject )();
1293 // Another take on it
1294 jQuery.proxy( thisObject, "method" )();
1296 // Make sure it doesn't freak out
1297 equal( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
1299 // Partial application
1300 test2 = function( a ){ equal( a, "pre-applied", "Ensure arguments can be pre-applied." ); };
1301 jQuery.proxy( test2, null, "pre-applied" )();
1303 // Partial application w/ normal arguments
1304 test3 = function( a, b ){ equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." ); };
1305 jQuery.proxy( test3, null, "pre-applied" )( "normal" );
1308 test4 = { "meth": function( a ){ equal( a, "boom", "Ensure old syntax works." ); } };
1309 jQuery.proxy( test4, "meth" )( "boom" );
1311 // jQuery 1.9 improved currying with `this` object
1313 equal( Array.prototype.join.call( arguments, "," ), "arg1,arg2,arg3", "args passed" );
1314 equal( this.foo, "bar", "this-object passed" );
1316 cb = jQuery.proxy( fn, null, "arg1", "arg2" );
1317 cb.call( thisObject, "arg3" );
1320 test("jQuery.parseHTML", function() {
1325 deepEqual( jQuery.parseHTML(), [], "Without arguments" );
1326 deepEqual( jQuery.parseHTML( undefined ), [], "Undefined" );
1327 deepEqual( jQuery.parseHTML( null ), [], "Null" );
1328 deepEqual( jQuery.parseHTML( false ), [], "Boolean false" );
1329 deepEqual( jQuery.parseHTML( 0 ), [], "Zero" );
1330 deepEqual( jQuery.parseHTML( true ), [], "Boolean true" );
1331 deepEqual( jQuery.parseHTML( 42 ), [], "Positive number" );
1332 deepEqual( jQuery.parseHTML( "" ), [], "Empty string" );
1334 jQuery.parseHTML( "<div></div>", document.getElementById("form") );
1335 }, "Passing an element as the context raises an exception (context should be a document)");
1337 nodes = jQuery.parseHTML( jQuery("body")[0].innerHTML );
1338 ok( nodes.length > 4, "Parse a large html string" );
1339 equal( jQuery.type( nodes ), "array", "parseHTML returns an array rather than a nodelist" );
1341 html = "<script>undefined()</script>";
1342 equal( jQuery.parseHTML( html ).length, 0, "Ignore scripts by default" );
1343 equal( jQuery.parseHTML( html, true )[0].nodeName.toLowerCase(), "script", "Preserve scripts when requested" );
1345 html += "<div></div>";
1346 equal( jQuery.parseHTML( html )[0].nodeName.toLowerCase(), "div", "Preserve non-script nodes" );
1347 equal( jQuery.parseHTML( html, true )[0].nodeName.toLowerCase(), "script", "Preserve script position");
1349 equal( jQuery.parseHTML("text")[0].nodeType, 3, "Parsing text returns a text node" );
1350 equal( jQuery.parseHTML( "\t<div></div>" )[0].nodeValue, "\t", "Preserve leading whitespace" );
1352 equal( jQuery.parseHTML(" <div/> ")[0].nodeType, 3, "Leading spaces are treated as text nodes (#11290)" );
1354 html = jQuery.parseHTML( "<div>test div</div>" );
1356 equal( html[ 0 ].parentNode.nodeType, 11, "parentNode should be documentFragment" );
1357 equal( html[ 0 ].innerHTML, "test div", "Content should be preserved" );
1359 equal( jQuery.parseHTML("<span><span>").length, 1, "Incorrect html-strings should not break anything" );
1360 equal( jQuery.parseHTML("<td><td>")[ 1 ].parentNode.nodeType, 11,
1361 "parentNode should be documentFragment for wrapMap (variable in manipulation module) elements too" );
1362 ok( jQuery.parseHTML("<#if><tr><p>This is a test.</p></tr><#/if>") || true, "Garbage input should not cause error" );
1365 if ( jQuery.support.createHTMLDocument ) {
1366 asyncTest("jQuery.parseHTML", function() {
1369 Globals.register("parseHTMLError");
1371 jQuery.globalEval("parseHTMLError = false;");
1372 jQuery.parseHTML( "<img src=x onerror='parseHTMLError = true'>" );
1374 window.setTimeout(function() {
1376 equal( window.parseHTMLError, false, "onerror eventhandler has not been called." );
1381 test("jQuery.parseJSON", function() {
1384 strictEqual( jQuery.parseJSON( null ), null, "primitive null" );
1385 strictEqual( jQuery.parseJSON("0.88"), 0.88, "Number" );
1387 jQuery.parseJSON("\" \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u007E \\u263a \""),
1388 " \" \\ / \b \f \n \r \t ~ \u263A ",
1391 deepEqual( jQuery.parseJSON("{}"), {}, "Empty object" );
1392 deepEqual( jQuery.parseJSON("{\"test\":1}"), { "test": 1 }, "Plain object" );
1393 deepEqual( jQuery.parseJSON("[0]"), [ 0 ], "Simple array" );
1396 jQuery.parseJSON("[ \"string\", -4.2, 2.7180e0, 3.14E-1, {}, [], true, false, null ]"),
1397 [ "string", -4.2, 2.718, 0.314, {}, [], true, false, null ],
1398 "Array of all data types"
1401 jQuery.parseJSON( "{ \"string\": \"\", \"number\": 4.2e+1, \"object\": {}," +
1402 "\"array\": [[]], \"boolean\": [ true, false ], \"null\": null }"),
1403 { string: "", number: 42, object: {}, array: [[]], boolean: [ true, false ], "null": null },
1404 "Dictionary of all data types"
1407 deepEqual( jQuery.parseJSON("\n{\"test\":1}\t"), { "test": 1 },
1408 "Leading and trailing whitespace are ignored" );
1412 }, null, "Undefined raises an error" );
1414 jQuery.parseJSON( "" );
1415 }, null, "Empty string raises an error" );
1417 jQuery.parseJSON("''");
1418 }, null, "Single-quoted string raises an error" );
1423 jQuery.parseJSON("\" \\a \"");
1424 }, null, "Invalid string escape raises an error" );
1426 // Broken on IE8, Safari 5.1 Windows
1428 jQuery.parseJSON("\"\t\"");
1429 }, null, "Unescaped control character raises an error" );
1433 jQuery.parseJSON(".123");
1434 }, null, "Number with no integer component raises an error" );
1438 var result = jQuery.parseJSON("0101");
1441 // Ensure base-10 interpretation on browsers that erroneously accept leading-zero numbers
1442 if ( result === 101 ) {
1443 throw new Error("close enough");
1445 }, null, "Leading-zero number raises an error or is parsed as decimal" );
1447 jQuery.parseJSON("{a:1}");
1448 }, null, "Unquoted property raises an error" );
1450 jQuery.parseJSON("{'a':1}");
1451 }, null, "Single-quoted property raises an error" );
1453 jQuery.parseJSON("[,]");
1454 }, null, "Array element elision raises an error" );
1456 jQuery.parseJSON("{},[]");
1457 }, null, "Comma expression raises an error" );
1459 jQuery.parseJSON("[]\n,{}");
1460 }, null, "Newline-containing comma expression raises an error" );
1462 jQuery.parseJSON("\"\"\n\"\"");
1463 }, null, "Automatic semicolon insertion raises an error" );
1465 strictEqual( jQuery.parseJSON([ 0 ]), 0, "Input cast to string" );
1468 test("jQuery.parseXML", 8, function(){
1471 xml = jQuery.parseXML( "<p>A <b>well-formed</b> xml string</p>" );
1472 tmp = xml.getElementsByTagName( "p" )[ 0 ];
1473 ok( !!tmp, "<p> present in document" );
1474 tmp = tmp.getElementsByTagName( "b" )[ 0 ];
1475 ok( !!tmp, "<b> present in document" );
1476 strictEqual( tmp.childNodes[ 0 ].nodeValue, "well-formed", "<b> text is as expected" );
1478 strictEqual( e, undefined, "unexpected error" );
1481 xml = jQuery.parseXML( "<p>Not a <<b>well-formed</b> xml string</p>" );
1482 ok( false, "invalid xml not detected" );
1484 strictEqual( e.message, "Invalid XML: <p>Not a <<b>well-formed</b> xml string</p>", "invalid xml detected" );
1487 xml = jQuery.parseXML( "" );
1488 strictEqual( xml, null, "empty string => null document" );
1489 xml = jQuery.parseXML();
1490 strictEqual( xml, null, "undefined string => null document" );
1491 xml = jQuery.parseXML( null );
1492 strictEqual( xml, null, "null string => null document" );
1493 xml = jQuery.parseXML( true );
1494 strictEqual( xml, null, "non-string => null document" );
1496 ok( false, "empty input throws exception" );
1500 test("jQuery.camelCase()", function() {
1503 "foo-bar": "fooBar",
1504 "foo-bar-baz": "fooBarBaz",
1505 "girl-u-want": "girlUWant",
1506 "the-4th-dimension": "the-4thDimension",
1507 "-o-tannenbaum": "OTannenbaum",
1508 "-moz-illa": "MozIlla",
1509 "-ms-take": "msTake"
1514 jQuery.each( tests, function( key, val ) {
1515 equal( jQuery.camelCase( key ), val, "Converts: " + key + " => " + val );
1519 testIframeWithCallback( "Conditional compilation compatibility (#13274)", "core/cc_on.html", function( cc_on, errors, $ ) {
1521 ok( true, "JScript conditional compilation " + ( cc_on ? "supported" : "not supported" ) );
1522 deepEqual( errors, [], "No errors" );
1523 ok( $(), "jQuery executes" );
1526 // iOS7 doesn't fire the load event if the long-loading iframe gets its source reset to about:blank.
1527 // This makes this test fail but it doesn't seem to cause any real-life problems so blacklisting
1528 // this test there is preferred to complicating the hard-to-test core/ready code further.
1529 if ( !/iphone os 7_/i.test( navigator.userAgent ) ) {
1530 testIframeWithCallback( "document ready when jQuery loaded asynchronously (#13655)", "core/dynamic_ready.html", function( ready ) {
1532 equal( true, ready, "document ready correctly fired when jQuery is loaded after DOMContentLoaded" );
1536 testIframeWithCallback( "Tolerating alias-masked DOM properties (#14074)", "core/aliased.html",
1537 function( errors ) {
1539 deepEqual( errors, [], "jQuery loaded" );
1543 testIframeWithCallback( "Don't call window.onready (#14802)", "core/onready.html",
1546 equal( error, false, "no call to user-defined onready" );