// ======================================================================== // CoreQuery Tests // ======================================================================== /* This test file incorporates most of the changes that come bundled with the jQuery-core suite of tests, modified to fit CoreQuery's reduced API. You should be able to update most of these tests with updated versons of the same tests in jQuery. */ htmlbody('\ \ \
\
\
\ \ \ ') ; module("CQ 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( SC.CoreQuery, "SC.CoreQuery" ); ok( SC.$, "SC.$" ); }); test("SC.CoreQuery()", function() { expect(8); var main = SC.$("#main"); same( SC.$("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with SC.$ object as context" ); var code = SC.$(""); equals( code.length, 1, "Correct number of elements generated for code" ); var img = SC.$(""); equals( img.length, 1, "Correct number of elements generated for img" ); var div = SC.$("

"); equals( div.length, 4, "Correct number of elements generated for div hr code b" ); // can actually yield more than one, when iframes are included, the window is an array as well equals( SC.$(window).length, 1, "Correct number of elements generated for window" ); equals( SC.$(document).length, 1, "Correct number of elements generated for document" ); equals( SC.$([1,2,3]).get(1), 2, "Test passing an array to the factory" ); equals( SC.$(document.body).get(0), SC.$('body').get(0), "Test passing an html node to the factory" ); }); test("SC.$('html')", function() { expect(3); reset(); ok( SC.$("")[0], "Creating a link" ); reset(); var j = SC.$("hi there "); ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" ); ok( !SC.$("")[0].selected, "Make sure that options are auto-selected #2050" ); }); test("SC.$('html', context)", function() { expect(1); var $div = SC.$("
"); var $span = SC.$("", $div); equals($span.length, 1, "Verify a span created with a div context works, #1763"); }); test("length", function() { expect(1); equals( SC.$("p").length, 6, "Get Number of Elements Found" ); }); test("size()", function() { expect(1); equals( SC.$("p").size(), 6, "Get Number of Elements Found" ); }); test("get()", function() { expect(1); same( SC.$("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" ); }); test("get(Number)", function() { expect(1); equals( SC.$("p").get(0), document.getElementById("firstp"), "Get A Single Element" ); }); test("add(String|Element|Array|undefined)", function() { expect(12); same( SC.$("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" ); same( SC.$("#sndp").add( SC.$("#en")[0] ).add( SC.$("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" ); ok( SC.$([]).add(SC.$("#form")[0].elements).length >= 13, "Check elements from array" ); var x = SC.$([]).add(SC.$("

xxx

")).add(SC.$("

xxx

")); equals( x[0].id, "x1", "Check on-the-fly element1" ); equals( x[1].id, "x2", "Check on-the-fly element2" ); var x = SC.$([]).add("

xxx

").add("

xxx

"); equals( x[0].id, "x1", "Check on-the-fly element1" ); equals( x[1].id, "x2", "Check on-the-fly element2" ); var notDefined; equals( SC.$([]).add(notDefined).length, 0, "Check that undefined adds nothing" ); // Added after #2811 equals( SC.$([]).add([window,document,document.body,document]).length, 3, "Pass an array" ); equals( SC.$(document).add(document).length, 1, "Check duplicated elements" ); equals( SC.$(window).add(window).length, 1, "Check duplicated elements using the window" ); ok( SC.$([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" ); }); test("each(Function)", function() { expect(1); var div = SC.$("div"); div.each(function(){this.foo = 'zoo';}); var pass = true; for ( var i = 0; i < div.size(); i++ ) { if ( div.get(i).foo != "zoo" ) pass = false; } ok( pass, "Execute a function, Relative" ); }); test("index(Object)", function() { expect(10); var elements = SC.$([window, document]), inputElements = SC.$('#radio1,#radio2,#check1,#check2'); equals( elements.index(window), 0, "Check for index of elements" ); equals( elements.index(document), 1, "Check for index of elements" ); equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" ); equals( inputElements.index(document.getElementById('radio2')), 1, "Check for index of elements" ); equals( inputElements.index(document.getElementById('check1')), 2, "Check for index of elements" ); equals( inputElements.index(document.getElementById('check2')), 3, "Check for index of elements" ); equals( inputElements.index(window), -1, "Check for not found index" ); equals( inputElements.index(document), -1, "Check for not found index" ); // enabled since [5500] equals( elements.index( elements ), 0, "Pass in a SC.$ object" ); equals( elements.index( elements.eq(1) ), 1, "Pass in a SC.$ object" ); }); test("attr(String)", function() { expect(26); equals( SC.$('#text1').attr('value'), "Test", 'Check for value attribute' ); equals( SC.$('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' ); equals( SC.$('#text1').attr('type'), "text", 'Check for type attribute' ); equals( SC.$('#radio1').attr('type'), "radio", 'Check for type attribute' ); equals( SC.$('#check1').attr('type'), "checkbox", 'Check for type attribute' ); equals( SC.$('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' ); equals( SC.$('#google').attr('title'), "Google!", 'Check for title attribute' ); equals( SC.$('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' ); equals( SC.$('#en').attr('lang'), "en", 'Check for lang attribute' ); equals( SC.$('#simon').attr('class'), "blog link", 'Check for class attribute' ); equals( SC.$('#name').attr('name'), "name", 'Check for name attribute' ); equals( SC.$('#text1').attr('name'), "action", 'Check for name attribute' ); ok( SC.$('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' ); equals( SC.$('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' ); equals( SC.$('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' ); equals( SC.$('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' ); equals( SC.$('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' ); equals( SC.$('#foo').attr('nodeName'), 'DIV', 'Check for nodeName attribute' ); equals( SC.$('#foo').attr('tagName'), 'DIV', 'Check for tagName attribute' ); SC.$('#main').append(SC.$('').attr('href', '#5')); // using innerHTML in IE causes href attribute to be serialized to the full path equals( SC.$('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' ); // Related to [5574] and [5683] var body = document.body, $body = SC.$(body); ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' ); // ignore the Firebug console if present... var nextSibling = $body.attr('nextSibling') ; ok( (($body.attr('nextSibling') === null) || (nextSibling.id == '_firebugConsole')), 'Make sure a null expando returns null' ); $body.attr('foo', 'baz'); equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' ); body.foo = 'bar'; equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' ); $body.attr('foo','cool'); equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' ); body.foo = undefined; ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' ); body.removeAttribute('foo'); // Cleanup }); test("attr(String, Function)", function() { expect(2); equals( SC.$('#text1').attr('value', function() { return this.id; })[0].value, "text1", "Set value from id" ); equals( SC.$('#text1').attr('title', function(i) { return i; }).attr('title'), "0", "Set value with an index"); }); test("attr(Hash)", function() { expect(1); var pass = true; SC.$("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){ if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false; }); ok( pass, "Set Multiple Attributes" ); }); test("attr(String, Object)", function() { expect(17); var div = SC.$("div").attr("foo", "bar"); fail = false; for ( var i = 0; i < div.size(); i++ ) { if ( div.get(i).getAttribute('foo') != "bar" ){ fail = i; break; } } equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" ); ok( SC.$("#foo").attr({"width": null}), "Try to set an attribute to nothing" ); SC.$("#name").attr('name', 'something'); equals( SC.$("#name").attr('name'), 'something', 'Set name attribute' ); SC.$("#check2").attr('checked', true); equals( document.getElementById('check2').checked, true, 'Set checked attribute' ); SC.$("#check2").attr('checked', false); equals( document.getElementById('check2').checked, false, 'Set checked attribute' ); SC.$("#text1").attr('readonly', true); equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' ); SC.$("#text1").attr('readonly', false); equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' ); SC.$("#name").attr('maxlength', '5'); equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' ); SC.$("#name").attr('maxLength', '10'); equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' ); // for #1070 SC.$("#name").attr('someAttr', '0'); equals( SC.$("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' ); SC.$("#name").attr('someAttr', 0); equals( SC.$("#name").attr('someAttr'), 0, 'Set attribute to the number 0' ); SC.$("#name").attr('someAttr', 1); equals( SC.$("#name").attr('someAttr'), 1, 'Set attribute to the number 1' ); // using contents will get comments regular, text, and comment nodes var j = SC.$("#nonnodes").contents(); j.attr("name", "attrvalue"); equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" ); j.removeAttr("name"); reset(); var type = SC.$("#check2").attr('type'); var thrown = false; try { SC.$("#check2").attr('type','hidden'); } catch(e) { thrown = true; } ok( thrown, "Exception thrown when trying to change type property" ); equals( type, SC.$("#check2").attr('type'), "Verify that you can't change the type of an input element" ); var check = document.createElement("input"); var thrown = true; try { SC.$(check).attr('type','checkbox'); } catch(e) { thrown = false; } ok( thrown, "Exception thrown when trying to change type property" ); equals( "checkbox", SC.$(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" ); }); test("css(String|Hash)", function() { expect(19); equals( SC.$('#main').css("display"), 'none', 'Check for css property "display"'); ok( SC.$('#foo').isVisible(), 'Modifying CSS display: Assert element is visible'); SC.$('#foo').css({display: 'none'}); ok( !SC.$('#foo').isVisible(), 'Modified CSS display: Assert element is hidden'); SC.$('#foo').css({display: 'block'}); ok( SC.$('#foo').isVisible(), 'Modified CSS display: Assert element is visible'); SC.$('#floatTest').css({styleFloat: 'right'}); equals( SC.$('#floatTest').css('styleFloat'), 'right', 'Modified CSS float using "styleFloat": Assert float is right'); SC.$('#floatTest').css({cssFloat: 'left'}); equals( SC.$('#floatTest').css('cssFloat'), 'left', 'Modified CSS float using "cssFloat": Assert float is left'); SC.$('#floatTest').css({'float': 'right'}); equals( SC.$('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right'); SC.$('#floatTest').css({'font-size': '30px'}); equals( SC.$('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px'); SC.$.each("0,0.25,0.5,0.75,1".split(','), function(i, n) { SC.$('#foo').css({opacity: n}); equals( SC.$('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" ); SC.$('#foo').css({opacity: parseFloat(n)}); equals( SC.$('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" ); }); SC.$('#foo').css({opacity: ''}); equals( SC.$('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" ); }); test("css(String, Object)", function() { expect(21); ok( SC.$('#foo').isVisible(), 'Modifying CSS display: Assert element is visible'); SC.$('#foo').css('display', 'none'); ok( !SC.$('#foo').isVisible(), 'Modified CSS display: Assert element is hidden'); SC.$('#foo').css('display', 'block'); ok( SC.$('#foo').isVisible(), 'Modified CSS display: Assert element is visible'); SC.$('#floatTest').css('styleFloat', 'left'); equals( SC.$('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left'); SC.$('#floatTest').css('cssFloat', 'right'); equals( SC.$('#floatTest').css('cssFloat'), 'right', 'Modified CSS float using "cssFloat": Assert float is right'); SC.$('#floatTest').css('float', 'left'); equals( SC.$('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left'); SC.$('#floatTest').css('font-size', '20px'); equals( SC.$('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px'); SC.$.each("0,0.25,0.5,0.75,1".split(','), function(i, n) { SC.$('#foo').css('opacity', n); equals( SC.$('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" ); SC.$('#foo').css('opacity', parseFloat(n)); equals( SC.$('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" ); }); SC.$('#foo').css('opacity', ''); equals( SC.$('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" ); // for #1438, IE throws JS error when filter exists but doesn't have opacity in it if (SC.browser.msie) { SC.$('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');"); } equals( SC.$('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" ); // using contents will get comments regular, text, and comment nodes var j = SC.$("#nonnodes").contents(); j.css("padding-left", "1px"); equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" ); // opera sometimes doesn't update 'display' correctly, see #2037 SC.$("#t2037")[0].innerHTML = SC.$("#t2037")[0].innerHTML ; equals( SC.$("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" ); }); test("SC.$.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () { expect(4); var $checkedtest = SC.$("#checkedtest"); // IE6 was clearing "checked" in SC.$.css(elem, "height"); SC.$.css($checkedtest[0], "height"); var input = SC.$("input.radio", $checkedtest); ok( !! SC.$(input[0]).attr("checked"), "Check first radio still checked." ); ok( ! SC.$(input[input.length]).attr("checked"), "Check last radio still NOT checked." ); var input = SC.$("input.checkbox", $checkedtest); ok( !! SC.$(input[0]).attr("checked"), "Check first checkbox still checked." ); ok( ! SC.$(input[input.length]).attr("checked"), "Check last checkbox still NOT checked." ); }); test("width()", function() { expect(9); var $div = SC.$("#nothiddendiv"); $div.width(30); equals($div.width(), 30, "Test set to 30 correctly"); $div.width(-1); // handle negative numbers by ignoring #1599 equals($div.width(), 30, "Test negative width ignored"); $div.css("padding", "20px"); equals($div.width(), 30, "Test padding specified with pixels"); $div.css("border", "2px solid #fff"); equals($div.width(), 30, "Test border specified with pixels"); // IE is off by one on this. We don't really care at this point since ems // is not really central to most SC apps. $div.css("padding", "2em"); var e = SC.browser.msie==8 ? 29 : 30; equals($div.width(), e, "Test padding specified with ems"); $div.css("border", "1em solid #fff"); equals($div.width(), 30, "Test border specified with ems"); $div.css("padding", "2%"); equals($div.width(), 30, "Test padding specified with percent"); $div.hide(); equals($div.width(), 30, "Test hidden div"); $div.css({ display: "", border: "", padding: "" }); SC.$("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" }); equals(SC.$("#nothiddendivchild").width(), 20, "Test child width with border and padding"); SC.$("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" }); }); test("height()", function() { expect(8); var $div = SC.$("#nothiddendiv"); $div.height(30); equals($div.height(), 30, "Test set to 30 correctly"); $div.height(-1); // handle negative numbers by ignoring #1599 equals($div.height(), 30, "Test negative height ignored"); $div.css("padding", "20px"); equals($div.height(), 30, "Test padding specified with pixels"); $div.css("border", "2px solid #fff"); equals($div.height(), 30, "Test border specified with pixels"); // IE is off by one on this. We don't really care at this point since ems // is not really central to most SC apps. $div.css("padding", "2em"); var e = SC.browser.msie==8 ? 29 : 30; equals($div.height(), e, "Test padding specified with ems"); $div.css("border", "1em solid #fff"); equals($div.height(), 30, "Test border specified with ems"); $div.css("padding", "2%"); equals($div.height(), 30, "Test padding specified with percent"); $div.hide(); equals($div.height(), 30, "Test hidden div"); $div.css({ display: "", border: "", padding: "", height: "1px" }); }); test("text()", function() { expect(1); var expected = "This link has class=\"blog\": Simon Willison's Weblog"; equals( SC.$('#sap').text(), expected, 'Check for merged text of more then one element.' ); }); test("append(String|Element|Array<Element>|SC.$)", function() { expect(21); var defaultText = 'Try them out:' ; var result = SC.$('#first').append('buga'); equals( result.text(), defaultText + 'buga', 'Check if text appending works' ); var cq = SC.$('#select3').append('').find('option'); equals(SC.$(cq[cq.length-1]).attr('value'), 'appendTest', 'Appending html options to select element'); reset(); var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:"; SC.$('#sap').append(document.getElementById('first')); equals(SC.$('#sap').text(), expected, "Check for appending of element" ); reset(); expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; SC.$('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]); equals(SC.$('#sap').text(), expected, "Check for appending of array of elements" ); reset(); expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; SC.$('#sap').append(SC.$("#first, #yahoo")); equals( expected, SC.$('#sap').text(), "Check for appending of SC.$ object" ); reset(); SC.$("#sap").append( 5 ); ok( SC.$("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" ); reset(); SC.$("#sap").append( " text with spaces " ); ok( SC.$("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" ); reset(); ok( SC.$("#sap").append([]), "Check for appending an empty array." ); ok( SC.$("#sap").append(""), "Check for appending an empty string." ); ok( SC.$("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." ); reset(); SC.$("#sap").append(document.getElementById('form')); equals( SC.$("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910 reset(); var pass = true; try { SC.$( SC.$("#iframe")[0].contentWindow.document.body ).append("
test
"); } catch(e) { pass = false; } ok( pass, "Test for appending a DOM node to the contents of an IFrame" ); reset(); SC.$('
').appendTo('#form').append('test'); t( 'Append legend', '#legend', ['legend'] ); reset(); SC.$('#select1').append(''); equals( SC.$('#select1 option').last().text(), "Test", "Appending