diff options
Diffstat (limited to 'test/unit/attributes.js')
-rw-r--r-- | test/unit/attributes.js | 133 |
1 files changed, 74 insertions, 59 deletions
diff --git a/test/unit/attributes.js b/test/unit/attributes.js index 335927b35..062dd504c 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -2,15 +2,15 @@ module( "attributes", { teardown: moduleTeardown }); -var bareObj = function( value ) { +function bareObj( value ) { return value; -}; +} -var functionReturningObj = function( value ) { - return (function() { +function functionReturningObj( value ) { + return function() { return value; - }); -}; + }; +} /* ======== local reference ======= @@ -52,6 +52,10 @@ test( "jQuery.propFix integrity test", function() { test( "attr(String)", function() { expect( 50 ); + var extras, body, $body, + select, optgroup, option, $img, styleElem, + $button, $form, $a; + equal( jQuery("#text1").attr("type"), "text", "Check for type attribute" ); equal( jQuery("#radio1").attr("type"), "radio", "Check for type attribute" ); equal( jQuery("#check1").attr("type"), "checkbox", "Check for type attribute" ); @@ -70,7 +74,7 @@ test( "attr(String)", function() { equal( jQuery("#foo").attr("height"), undefined, "Non existent height attribute should return undefined" ); // [7472] & [3113] (form contains an input with name="action" or name="id") - var extras = jQuery("<input id='id' name='id' /><input id='name' name='name' /><input id='target' name='target' />").appendTo("#testForm"); + extras = jQuery("<input id='id' name='id' /><input id='name' name='name' /><input id='target' name='target' />").appendTo("#testForm"); equal( jQuery("#form").attr("action","newformaction").attr("action"), "newformaction", "Check that action attribute was changed" ); equal( jQuery("#testForm").attr("target"), undefined, "Retrieving target does not equal the input with name=target" ); equal( jQuery("#testForm").attr("target", "newTarget").attr("target"), "newTarget", "Set target successfully on a form" ); @@ -100,7 +104,8 @@ test( "attr(String)", function() { equal( jQuery("#list-test").attr("list"), "datalist", "Check setting list attribute" ); // Related to [5574] and [5683] - var body = document.body, $body = jQuery( body ); + body = document.body; + $body = jQuery( body ); strictEqual( $body.attr("foo"), undefined, "Make sure that a non existent attribute returns undefined" ); @@ -112,28 +117,28 @@ test( "attr(String)", function() { body.removeAttribute("foo"); // Cleanup - var select = document.createElement("select"), - optgroup = document.createElement("optgroup"), - option = document.createElement("option"); + select = document.createElement("select"); + optgroup = document.createElement("optgroup"); + option = document.createElement("option"); optgroup.appendChild( option ); select.appendChild( optgroup ); equal( jQuery( option ).prop("selected"), true, "Make sure that a single option is selected, even when in an optgroup." ); - var $img = jQuery("<img style='display:none' width='215' height='53' src='data/1x1.jpg'/>").appendTo("body"); + $img = jQuery("<img style='display:none' width='215' height='53' src='data/1x1.jpg'/>").appendTo("body"); equal( $img.attr("width"), "215", "Retrieve width attribute an an element with display:none." ); equal( $img.attr("height"), "53", "Retrieve height attribute an an element with display:none." ); // Check for style support - var styleElem = jQuery("<div/>").appendTo("#qunit-fixture").css({ + styleElem = jQuery("<div/>").appendTo("#qunit-fixture").css({ background: "url(UPPERlower.gif)" }); ok( !!~styleElem.attr("style").indexOf("UPPERlower.gif"), "Check style attribute getter" ); ok( !!~styleElem.attr("style", "position:absolute;").attr("style").indexOf("absolute"), "Check style setter" ); // Check value on button element (#1954) - var $button = jQuery("<button>text</button>").insertAfter("#button"); + $button = jQuery("<button>text</button>").insertAfter("#button"); strictEqual( $button.attr("value"), undefined, "Absence of value attribute on a button" ); equal( $button.attr( "value", "foobar" ).attr("value"), "foobar", "Value attribute on a button does not return innerHTML" ); equal( $button.attr("value", "baz").html(), "text", "Setting the value attribute does not change innerHTML" ); @@ -142,10 +147,10 @@ test( "attr(String)", function() { equal( jQuery("#table").attr("test:attrib"), undefined, "Retrieving a non-existent attribute on a table with a colon does not throw an error." ); equal( jQuery("#table").attr( "test:attrib", "foobar" ).attr("test:attrib"), "foobar", "Setting an attribute on a table with a colon does not throw an error." ); - var $form = jQuery("<form class='something'></form>").appendTo("#qunit-fixture"); + $form = jQuery("<form class='something'></form>").appendTo("#qunit-fixture"); equal( $form.attr("class"), "something", "Retrieve the class attribute on a form." ); - var $a = jQuery("<a href='#' onclick='something()'>Click</a>").appendTo("#qunit-fixture"); + $a = jQuery("<a href='#' onclick='something()'>Click</a>").appendTo("#qunit-fixture"); equal( $a.attr("onclick"), "something()", "Retrieve ^on attribute without anonymous function wrapper." ); ok( jQuery("<div/>").attr("doesntexist") === undefined, "Make sure undefined is returned when no attribute is found." ); @@ -218,7 +223,7 @@ test( "attr(Hash)", function() { "foo": "baz", "zoo": "ping" }).each(function() { - if ( this.getAttribute("foo") != "baz" && this.getAttribute("zoo") != "ping" ) { + if ( this.getAttribute("foo") !== "baz" && this.getAttribute("zoo") !== "ping" ) { pass = false; } }); @@ -248,7 +253,11 @@ test( "attr(Hash)", function() { test( "attr(String, Object)", function() { expect( 71 ); - var div = jQuery("div").attr("foo", "bar"), + var $input, $text, $details, + attributeNode, commentNode, textNode, obj, + table, td, j, type, + check, thrown, button, $radio, $radios, $svg, + div = jQuery("div").attr("foo", "bar"), i = 0, fail = false; @@ -273,7 +282,7 @@ test( "attr(String, Object)", function() { jQuery("#name").attr( "name", null ); equal( jQuery("#name").attr("name"), undefined, "Remove name attribute" ); - var $input = jQuery( "<input>", { + $input = jQuery( "<input>", { name: "something", id: "specified" }); @@ -311,7 +320,7 @@ test( "attr(String, Object)", function() { $input = jQuery("#check2").attr( "checked", false ).attr( "checked", "checked" ); equal( $input.attr("checked"), "checked", "Set checked to 'checked' (verified by .attr)" ); - var $radios = jQuery("#checkedtest").find("input[type='radio']"); + $radios = jQuery("#checkedtest").find("input[type='radio']"); $radios.eq( 1 ).trigger("click"); equal( $radios.eq( 1 ).prop("checked"), true, "Second radio was checked when clicked" ); equal( $radios.eq( 0 ).attr("checked"), "checked", "First radio is still [checked]" ); @@ -329,7 +338,7 @@ test( "attr(String, Object)", function() { equal( $input[0].maxLength, 10, "Set maxlength (verified by native property)" ); // HTML5 boolean attributes - var $text = jQuery("#text1").attr({ + $text = jQuery("#text1").attr({ "autofocus": true, "required": true }); @@ -338,7 +347,7 @@ test( "attr(String, Object)", function() { equal( $text.attr("required"), "required", "Reading required attribute yields 'required'" ); equal( $text.attr( "required", false ).attr("required"), undefined, "Setting required attribute to false removes it" ); - var $details = jQuery("<details open></details>").appendTo("#qunit-fixture"); + $details = jQuery("<details open></details>").appendTo("#qunit-fixture"); equal( $details.attr("open"), "open", "open attribute presence indicates true" ); equal( $details.attr( "open", false ).attr("open"), undefined, "Setting open attribute to false removes it" ); @@ -354,10 +363,10 @@ test( "attr(String, Object)", function() { jQuery("#foo").attr("contenteditable", true); equal( jQuery("#foo").attr("contenteditable"), "true", "Enumerated attributes are set properly" ); - var attributeNode = document.createAttribute("irrelevant"), - commentNode = document.createComment("some comment"), - textNode = document.createTextNode("some text"), - obj = {}; + attributeNode = document.createAttribute("irrelevant"); + commentNode = document.createComment("some comment"); + textNode = document.createTextNode("some text"); + obj = {}; jQuery.each( [ commentNode, textNode, attributeNode ], function( i, elem ) { var $elem = jQuery( elem ); @@ -375,8 +384,8 @@ test( "attr(String, Object)", function() { elem.nonexisting = oldVal; }); - var table = jQuery("#table").append("<tr><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr>"), - td = table.find("td").eq(0); + table = jQuery("#table").append("<tr><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr>"); + td = table.find("td").eq(0); td.attr( "rowspan", "2" ); equal( td[ 0 ]["rowSpan"], 2, "Check rowspan is correctly set" ); td.attr( "colspan", "2" ); @@ -395,14 +404,14 @@ test( "attr(String, Object)", function() { equal( jQuery("#name").attr("someAttr"), "1", "Set attribute to the number 1" ); // using contents will get comments regular, text, and comment nodes - var j = jQuery("#nonnodes").contents(); + j = jQuery("#nonnodes").contents(); j.attr( "name", "attrvalue" ); equal( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" ); j.removeAttr("name"); // Type - var type = jQuery("#check2").attr("type"); + type = jQuery("#check2").attr("type"); try { jQuery("#check2").attr( "type", "hidden" ); ok( true, "No exception thrown on input type change" ); @@ -410,8 +419,8 @@ test( "attr(String, Object)", function() { ok( true, "Exception thrown on input type change: " + e ); } - var check = document.createElement("input"); - var thrown = true; + check = document.createElement("input"); + thrown = true; try { jQuery( check ).attr( "type", "checkbox" ); } catch( e ) { @@ -430,7 +439,7 @@ test( "attr(String, Object)", function() { ok( thrown, "Exception thrown when trying to change type property" ); equal( "checkbox", check.attr("type"), "Verify that you can change the type of an input element that isn't in the DOM" ); - var button = jQuery("#button"); + button = jQuery("#button"); try { button.attr( "type", "submit" ); ok( true, "No exception thrown on button type change" ); @@ -438,14 +447,14 @@ test( "attr(String, Object)", function() { ok( true, "Exception thrown on button type change: " + e ); } - var $radio = jQuery( "<input>", { + $radio = jQuery( "<input>", { "value": "sup", "type": "radio" }).appendTo("#testForm"); equal( $radio.val(), "sup", "Value is not reset when type is set after value on a radio" ); // Setting attributes on svg elements (bug #3116) - var $svg = jQuery( + $svg = jQuery( "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' version='1.1' baseProfile='full' width='200' height='200'>" + "<circle cx='200' cy='200' r='150' />" + @@ -463,8 +472,8 @@ test( "attr(String, Object)", function() { test( "attr(String, Object) - Loaded via XML document", function() { expect( 2 ); - var xml = createDashboardXML(); - var titles = []; + var xml = createDashboardXML(), + titles = []; jQuery( "tab", xml ).each(function() { titles.push( jQuery( this ).attr("title") ); }); @@ -607,7 +616,7 @@ test( "removeAttr(Multi String, variable space width)", function() { div.removeAttr( "id alt title rel " ); - jQuery.each( tests, function( key, val ) { + jQuery.each( tests, function( key ) { equal( div.attr( key ), undefined, "Attribute `" + key + "` was removed" ); }); }); @@ -643,7 +652,8 @@ test( "prop(String, Object)", function() { equal( jQuery("#table").prop("frameBorder"), 1, "Check setting and retrieving frameBorder" ); QUnit.reset(); - var body = document.body, + var select, optgroup, option, attributeNode, commentNode, textNode, obj, $form, + body = document.body, $body = jQuery( body ); ok( $body.prop("nextSibling") === null, "Make sure a null expando returns null" ); @@ -652,9 +662,9 @@ test( "prop(String, Object)", function() { body["foo"] = undefined; ok( $body.prop("foo") === undefined, "Make sure the expando is preferred over the dom attribute, even if undefined" ); - var select = document.createElement("select"), - optgroup = document.createElement("optgroup"), - option = document.createElement("option"); + select = document.createElement("select"); + optgroup = document.createElement("optgroup"); + option = document.createElement("option"); optgroup.appendChild( option ); select.appendChild( optgroup ); @@ -662,10 +672,10 @@ test( "prop(String, Object)", function() { equal( jQuery( option ).prop("selected"), true, "Make sure that a single option is selected, even when in an optgroup." ); equal( jQuery( document ).prop("nodeName"), "#document", "prop works correctly on document nodes (bug #7451)." ); - var attributeNode = document.createAttribute("irrelevant"), - commentNode = document.createComment("some comment"), - textNode = document.createTextNode("some text"), - obj = {}; + attributeNode = document.createAttribute("irrelevant"); + commentNode = document.createComment("some comment"); + textNode = document.createTextNode("some text"); + obj = {}; jQuery.each( [ document, attributeNode, commentNode, textNode, obj, "#firstp" ], function( i, ele ) { strictEqual( jQuery( ele ).prop("nonexisting"), undefined, "prop works correctly for non existing attributes (bug #7500)." ); }); @@ -678,7 +688,7 @@ test( "prop(String, Object)", function() { }); jQuery( document ).removeProp("nonexisting"); - var $form = jQuery("#form").prop( "enctype", "multipart/form-data" ); + $form = jQuery("#form").prop( "enctype", "multipart/form-data" ); equal( $form.prop("enctype"), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 #6743)" ); }); @@ -775,6 +785,8 @@ test( "removeProp(String)", function() { test( "val()", function() { expect( 21 + ( jQuery.fn.serialize ? 6 : 0 ) ); + var checks, $button; + document.getElementById("text1").value = "bla"; equal( jQuery("#text1").val(), "bla", "Check for modified value of input element" ); @@ -823,7 +835,7 @@ test( "val()", function() { ); if ( jQuery.fn.serialize ) { - var checks = jQuery("<input type='checkbox' name='test' value='1'/><input type='checkbox' name='test' value='2'/><input type='checkbox' name='test' value=''/><input type='checkbox' name='test'/>").appendTo("#form"); + checks = jQuery("<input type='checkbox' name='test' value='1'/><input type='checkbox' name='test' value='2'/><input type='checkbox' name='test' value=''/><input type='checkbox' name='test'/>").appendTo("#form"); deepEqual( checks.serialize(), "", "Get unchecked values." ); @@ -844,7 +856,7 @@ test( "val()", function() { checks.remove(); } - var $button = jQuery("<button value='foobar'>text</button>").insertAfter("#button"); + $button = jQuery("<button value='foobar'>text</button>").insertAfter("#button"); equal( $button.val(), "foobar", "Value retrieval on a button does not return innerHTML" ); equal( $button.val("baz").html(), "text", "Setting the value does not change innerHTML" ); @@ -907,7 +919,8 @@ var testVal = function( valueObj ) { jQuery("#text1").val( valueObj( null ) ); equal( document.getElementById("text1").value, "", "Check for modified (via val(null)) value of input element" ); - var $select1 = jQuery("#select1"); + var j, + $select1 = jQuery("#select1"); $select1.val( valueObj("3") ); equal( $select1.val(), "3", "Check for modified (via val(String)) value of select element" ); @@ -919,7 +932,7 @@ var testVal = function( valueObj ) { equal( $select1.val(), "4", "Should be possible to set the val() to a newly created option" ); // using contents will get comments regular, text, and comment nodes - var j = jQuery("#nonnodes").contents(); + j = jQuery("#nonnodes").contents(); j.val( valueObj( "asdf" ) ); equal( j.val(), "asdf", "Check node,textnode,comment with val()" ); j.removeAttr("value"); @@ -1019,10 +1032,11 @@ test( "val(select) after form.reset() (Bug #2551)", function() { var testAddClass = function( valueObj ) { expect( 9 ); - var div = jQuery("#qunit-fixture div"); + var pass, j, i, + div = jQuery("#qunit-fixture div"); div.addClass( valueObj("test") ); - var pass = true; - for ( var i = 0; i < div.length; i++ ) { + pass = true; + for ( i = 0; i < div.length; i++ ) { if ( !~div.get( i ).className.indexOf("test") ) { pass = false; } @@ -1030,7 +1044,7 @@ var testAddClass = function( valueObj ) { ok( pass, "Add Class" ); // using contents will get regular, text, and comment nodes - var j = jQuery("#nonnodes").contents(); + j = jQuery("#nonnodes").contents(); j.addClass( valueObj("asdf") ); ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" ); @@ -1071,7 +1085,8 @@ test( "addClass(Function)", function() { test( "addClass(Function) with incoming value", function() { expect( 52 ); - var div = jQuery("#qunit-fixture div"), + var pass, i, + div = jQuery("#qunit-fixture div"), old = div.map(function() { return jQuery(this).attr("class") || ""; }); @@ -1083,9 +1098,9 @@ test( "addClass(Function) with incoming value", function() { } }); - var pass = true; - for ( var i = 0; i < div.length; i++ ) { - if ( div.get(i).className.indexOf("test") == -1 ) { + pass = true; + for ( i = 0; i < div.length; i++ ) { + if ( div.get(i).className.indexOf("test") === -1 ) { pass = false; } } |