module("core"); test("Basic requirements", function() { expect(7); ok( Array.prototype.push, "Array.push()" ); ok( Function.prototype.apply, "Function.apply()" ); ok( document.getElementById, "getElementById" ); ok( document.getElementsByTagName, "getElementsByTagName" ); ok( RegExp, "RegExp" ); ok( jQuery, "jQuery" ); ok( $, "$" ); }); test("jQuery()", function() { expect(23); // Basic constructor's behavior equals( jQuery().length, 0, "jQuery() === jQuery([])" ); equals( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" ); equals( jQuery(null).length, 0, "jQuery(null) === jQuery([])" ); equals( jQuery("").length, 0, "jQuery('') === jQuery([])" ); var obj = jQuery("div") equals( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" ); // can actually yield more than one, when iframes are included, the window is an array as well equals( 1, jQuery(window).length, "Correct number of elements generated for jQuery(window)" ); var main = jQuery("#main"); same( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" ); /* // disabled since this test was doing nothing. i tried to fix it but i'm not sure // what the expected behavior should even be. FF returns "\n" for the text node // make sure this is handled var crlfContainer = jQuery('

\r\n

'); var x = crlfContainer.contents().get(0).nodeValue; equals( x, what???, "Check for \\r and \\n in jQuery()" ); */ /* // Disabled until we add this functionality in var pass = true; try { jQuery("
Testing
").appendTo(document.getElementById("iframe").contentDocument.body); } catch(e){ pass = false; } ok( pass, "jQuery('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/ var code = jQuery(""); equals( code.length, 1, "Correct number of elements generated for code" ); equals( code.parent().length, 0, "Make sure that the generated HTML has no parent." ); var img = jQuery(""); equals( img.length, 1, "Correct number of elements generated for img" ); equals( img.parent().length, 0, "Make sure that the generated HTML has no parent." ); var div = jQuery("

"); equals( div.length, 4, "Correct number of elements generated for div hr code b" ); equals( div.parent().length, 0, "Make sure that the generated HTML has no parent." ); equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" ); equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" ); var exec = false; var elem = jQuery("
", { width: 10, css: { paddingLeft:1, paddingRight:1 }, click: function(){ ok(exec, "Click executed."); }, text: "test", "class": "test2", id: "test3" }); equals( elem[0].style.width, '10px', 'jQuery() quick setter width'); equals( elem[0].style.paddingLeft, '1px', 'jQuery quick setter css'); equals( elem[0].style.paddingRight, '1px', 'jQuery quick setter css'); equals( elem[0].childNodes.length, 1, 'jQuery quick setter text'); equals( elem[0].firstChild.nodeValue, "test", 'jQuery quick setter text'); equals( elem[0].className, "test2", 'jQuery() quick setter class'); equals( elem[0].id, "test3", 'jQuery() quick setter id'); exec = true; elem.click(); }); test("selector state", function() { expect(31); var test; test = jQuery(undefined); equals( test.selector, "", "Empty jQuery Selector" ); equals( test.context, undefined, "Empty jQuery Context" ); test = jQuery(document); equals( test.selector, "", "Document Selector" ); equals( test.context, document, "Document Context" ); test = jQuery(document.body); equals( test.selector, "", "Body Selector" ); equals( test.context, document.body, "Body Context" ); test = jQuery("#main"); equals( test.selector, "#main", "#main Selector" ); equals( test.context, document, "#main Context" ); test = jQuery("#notfoundnono"); equals( test.selector, "#notfoundnono", "#notfoundnono Selector" ); equals( test.context, document, "#notfoundnono Context" ); test = jQuery("#main", document); equals( test.selector, "#main", "#main Selector" ); equals( test.context, document, "#main Context" ); test = jQuery("#main", document.body); equals( test.selector, "#main", "#main Selector" ); equals( test.context, document.body, "#main Context" ); // Test cloning test = jQuery(test); equals( test.selector, "#main", "#main Selector" ); equals( test.context, document.body, "#main Context" ); test = jQuery(document.body).find("#main"); equals( test.selector, "#main", "#main find Selector" ); equals( test.context, document.body, "#main find Context" ); test = jQuery("#main").filter("div"); equals( test.selector, "#main.filter(div)", "#main filter Selector" ); equals( test.context, document, "#main filter Context" ); test = jQuery("#main").not("div"); equals( test.selector, "#main.not(div)", "#main not Selector" ); equals( test.context, document, "#main not Context" ); test = jQuery("#main").filter("div").not("div"); equals( test.selector, "#main.filter(div).not(div)", "#main filter, not Selector" ); equals( test.context, document, "#main filter, not Context" ); test = jQuery("#main").filter("div").not("div").end(); equals( test.selector, "#main.filter(div)", "#main filter, not, end Selector" ); equals( test.context, document, "#main filter, not, end Context" ); test = jQuery("#main").parent("body"); equals( test.selector, "#main.parent(body)", "#main parent Selector" ); equals( test.context, document, "#main parent Context" ); test = jQuery("#main").eq(0); equals( test.selector, "#main.slice(0,1)", "#main eq Selector" ); equals( test.context, document, "#main eq Context" ); var d = "
"; equals( jQuery(d).appendTo(jQuery(d)).selector, jQuery(d).appendTo(d).selector, "manipulation methods make same selector for jQuery objects" ); }); if ( !isLocal ) { test("browser", function() { stop(); jQuery.get("data/ua.txt", function(data){ var uas = data.split("\n"); expect( (uas.length - 1) * 2 ); jQuery.each(uas, function(){ var parts = this.split("\t"); if ( parts[2] ) { var ua = jQuery.uaMatch( parts[2] ); equals( ua.browser, parts[0], "Checking browser for " + parts[2] ); equals( ua.version, parts[1], "Checking version string for " + parts[2] ); } }); start(); }); }); } test("noConflict", function() { expect(6); var $$ = jQuery; equals( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" ); equals( jQuery, $$, "Make sure jQuery wasn't touched." ); equals( $, original$, "Make sure $ was reverted." ); jQuery = $ = $$; equals( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" ); equals( jQuery, originaljQuery, "Make sure jQuery was reverted." ); equals( $, original$, "Make sure $ was reverted." ); jQuery = $$; }); test("trim", function() { expect(4); var nbsp = String.fromCharCode(160); equals( jQuery.trim("hello "), "hello", "trailing space" ); equals( jQuery.trim(" hello"), "hello", "leading space" ); equals( jQuery.trim(" hello "), "hello", "space on both sides" ); equals( jQuery.trim(" " + nbsp + "hello " + nbsp + " "), "hello", " " ); }); test("isPlainObject", function() { expect(14); stop(); // The use case that we want to match ok(jQuery.isPlainObject({}), "{}"); // Not objects shouldn't be matched ok(!jQuery.isPlainObject(""), "string"); ok(!jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number"); ok(!jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean"); ok(!jQuery.isPlainObject(null), "null"); ok(!jQuery.isPlainObject(undefined), "undefined"); // Arrays shouldn't be matched ok(!jQuery.isPlainObject([]), "array"); // Instantiated objects shouldn't be matched ok(!jQuery.isPlainObject(new Date), "new Date"); var fn = function(){}; // Functions shouldn't be matched ok(!jQuery.isPlainObject(fn), "fn"); // Again, instantiated objects shouldn't be matched ok(!jQuery.isPlainObject(new fn), "new fn (no methods)"); // Makes the function a little more realistic // (and harder to detect, incidentally) fn.prototype = {someMethod: function(){}}; // Again, instantiated objects shouldn't be matched ok(!jQuery.isPlainObject(new fn), "new fn"); // DOM Element ok(!jQuery.isPlainObject(document.createElement("div")), "DOM Element"); // Window ok(!jQuery.isPlainObject(window), "window"); var iframe = document.createElement("iframe"); document.body.appendChild(iframe); window.iframeDone = function(otherObject){ // Objects from other windows should be matched ok(jQuery.isPlainObject(new otherObject), "new otherObject"); document.body.removeChild( iframe ); start(); }; var doc = iframe.contentDocument || iframe.contentWindow.document; doc.open(); doc.write(""); doc.close(); }); test("isFunction", function() { expect(19); // Make sure that false values return false ok( !jQuery.isFunction(), "No Value" ); ok( !jQuery.isFunction( null ), "null Value" ); ok( !jQuery.isFunction( undefined ), "undefined Value" ); ok( !jQuery.isFunction( "" ), "Empty String Value" ); ok( !jQuery.isFunction( 0 ), "0 Value" ); // Check built-ins // Safari uses "(Internal Function)" ok( jQuery.isFunction(String), "String Function("+String+")" ); ok( jQuery.isFunction(Array), "Array Function("+Array+")" ); ok( jQuery.isFunction(Object), "Object Function("+Object+")" ); ok( jQuery.isFunction(Function), "Function Function("+Function+")" ); // When stringified, this could be misinterpreted var mystr = "function"; ok( !jQuery.isFunction(mystr), "Function String" ); // When stringified, this could be misinterpreted var myarr = [ "function" ]; ok( !jQuery.isFunction(myarr), "Function Array" ); // When stringified, this could be misinterpreted var myfunction = { "function": "test" }; ok( !jQuery.isFunction(myfunction), "Function Object" ); // Make sure normal functions still work var fn = function(){}; ok( jQuery.isFunction(fn), "Normal Function" ); var obj = document.createElement("object"); // Firefox says this is a function ok( !jQuery.isFunction(obj), "Object Element" ); // IE says this is an object // Since 1.3, this isn't supported (#2968) //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" ); var nodes = document.body.childNodes; // Safari says this is a function ok( !jQuery.isFunction(nodes), "childNodes Property" ); var first = document.body.firstChild; // Normal elements are reported ok everywhere ok( !jQuery.isFunction(first), "A normal DOM Element" ); var input = document.createElement("input"); input.type = "text"; document.body.appendChild( input ); // IE says this is an object // Since 1.3, this isn't supported (#2968) //ok( jQuery.isFunction(input.focus), "A default function property" ); document.body.removeChild( input ); var a = document.createElement("a"); a.href = "some-function"; document.body.appendChild( a ); // This serializes with the word 'function' in it ok( !jQuery.isFunction(a), "Anchor Element" ); document.body.removeChild( a ); // Recursive function calls have lengths and array-like properties function callme(callback){ function fn(response){ callback(response); } ok( jQuery.isFunction(fn), "Recursive Function Call" ); fn({ some: "data" }); }; callme(function(){ callme(function(){}); }); }); test("isXMLDoc - HTML", function() { expect(4); ok( !jQuery.isXMLDoc( document ), "HTML document" ); ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" ); ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" ); var iframe = document.createElement("iframe"); document.body.appendChild( iframe ); try { var body = jQuery(iframe).contents()[0]; ok( !jQuery.isXMLDoc( body ), "Iframe body element" ); } catch(e){ ok( false, "Iframe body element exception" ); } document.body.removeChild( iframe ); }); if ( !isLocal ) { test("isXMLDoc - XML", function() { expect(3); stop(); jQuery.get('data/dashboard.xml', function(xml) { ok( jQuery.isXMLDoc( xml ), "XML document" ); ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" ); ok( jQuery.isXMLDoc( jQuery("tab", xml)[0] ), "XML Tab Element" ); start(); }); }); } test("jQuery('html')", function() { expect(15); reset(); jQuery.foo = false; var s = jQuery("")[0]; ok( s, "Creating a script" ); ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" ); jQuery("body").append(""); ok( jQuery.foo, "Executing a scripts contents in the right context" ); // Test multi-line HTML var div = jQuery("
\r\nsome text\n

some p

\nmore text\r\n
")[0]; equals( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." ); equals( div.firstChild.nodeType, 3, "Text node." ); equals( div.lastChild.nodeType, 3, "Text node." ); equals( div.childNodes[1].nodeType, 1, "Paragraph." ); equals( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." ); reset(); ok( jQuery("")[0], "Creating a link" ); ok( !jQuery("