From 97144424ccb79e251d6df354384fa3e4d5362949 Mon Sep 17 00:00:00 2001 From: timmywil Date: Wed, 4 May 2011 00:31:01 -0400 Subject: [PATCH] Update boolean check to avoid crashes, add all name fixes to propFix (properties are case-sensitive in all browsers), add tests for prop --- src/attributes.js | 36 +++++++----- test/unit/attributes.js | 125 ++++++++++++++++++++++++---------------- 2 files changed, 95 insertions(+), 66 deletions(-) diff --git a/src/attributes.js b/src/attributes.js index 9db69abcc..da63a3334 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -337,9 +337,12 @@ jQuery.extend({ return hooks.get( elem, name ); } else { - var boolProp = elem[ jQuery.propFix[ name ] || name ]; + var boolProp; - if ( typeof boolProp === "boolean" ) { + // Align boolean attributes with corresponding properties + // Do not check the property if the name contains characters + // valid for attributes, but not for properties + if ( !rinvalidChar.test( name ) && typeof (boolProp = elem[ jQuery.propFix[ name ] || name ]) === "boolean" ) { return boolProp ? name.toLowerCase() : undefined; @@ -354,7 +357,7 @@ jQuery.extend({ } } }, - + removeAttr: function( elem, name ) { if ( elem.nodeType === 1 ) { name = jQuery.attrFix[ name ] || name; @@ -404,7 +407,17 @@ jQuery.extend({ }, propFix: { - readonly: "readOnly" + tabindex: "tabIndex", + readonly: "readOnly", + "for": "htmlFor", + "class": "className", + maxlength: "maxLength", + cellspacing: "cellSpacing", + cellpadding: "cellPadding", + rowspan: "rowSpan", + colspan: "colSpan", + usemap: "useMap", + frameborder: "frameBorder" }, prop: function( elem, name, value ) { @@ -446,18 +459,9 @@ jQuery.extend({ // IE6/7 do not support getting/setting some attributes with get/setAttribute if ( !jQuery.support.getSetAttribute ) { - jQuery.attrFix = jQuery.extend( jQuery.attrFix, { - readonly: "readOnly", - "for": "htmlFor", - "class": "className", - maxlength: "maxLength", - cellspacing: "cellSpacing", - cellpadding: "cellPadding", - rowspan: "rowSpan", - colspan: "colSpan", - usemap: "useMap", - frameborder: "frameBorder" - }); + + // propFix is more comprehensive and contains all fixes + jQuery.attrFix = jQuery.propFix; // Use this for any attribute on a form in IE6/7 formHook = jQuery.attrHooks.name = jQuery.attrHooks.value = jQuery.valHooks.button = { diff --git a/test/unit/attributes.js b/test/unit/attributes.js index f93cb2a6a..b20a14fcb 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -4,16 +4,14 @@ var bareObj = function(value) { return value; }; var functionReturningObj = function(value) { return (function() { return value; }); }; -test("jQuery.attrFix integrity test", function() { - expect(1); +test("jQuery.attrFix/jQuery.propFix integrity test", function() { + expect(2); // This must be maintained and equal jQuery.attrFix when appropriate // Ensure that accidental or erroneous property // overwrites don't occur // This is simply for better code coverage and future proofing. - var propsShouldBe; - if ( !jQuery.support.getSetAttribute ) { - propsShouldBe = { + var props = { tabindex: "tabIndex", readonly: "readOnly", "for": "htmlFor", @@ -25,54 +23,19 @@ test("jQuery.attrFix integrity test", function() { colspan: "colSpan", usemap: "useMap", frameborder: "frameBorder" - }; + }, + propsShouldBe; + + if ( !jQuery.support.getSetAttribute ) { + propsShouldBe = props; } else { propsShouldBe = { tabindex: "tabIndex" }; } - same(propsShouldBe, jQuery.attrFix, "jQuery.attrFix passes integrity check"); -}); - -test("prop(String, Object)", function() { - expect(19); - equals( jQuery("#text1").prop("value"), "Test", "Check for value attribute" ); - equals( jQuery("#text1").prop("value", "Test2").prop("defaultValue"), "Test", "Check for defaultValue attribute" ); - equals( jQuery("#select2").prop("selectedIndex"), 3, "Check for selectedIndex attribute" ); - equals( jQuery("#foo").prop("nodeName").toUpperCase(), "DIV", "Check for nodeName attribute" ); - equals( jQuery("#foo").prop("tagName").toUpperCase(), "DIV", "Check for tagName attribute" ); - equals( jQuery("