diff options
author | Richard Gibson <richard.gibson@gmail.com> | 2012-12-08 16:28:10 -0500 |
---|---|---|
committer | Richard Gibson <richard.gibson@gmail.com> | 2012-12-14 10:37:20 -0500 |
commit | 2c40fdd4a852fe6ee16feaa3bb6d7d49c7a02606 (patch) | |
tree | b38e8ebf6f22bed3bb326f0398abce7c9a96905c /src | |
parent | 7d61c5238e57cf252faa2d999465bd69afc6c038 (diff) | |
download | jquery-2c40fdd4a852fe6ee16feaa3bb6d7d49c7a02606.tar.gz jquery-2c40fdd4a852fe6ee16feaa3bb6d7d49c7a02606.zip |
Fix #12600: don't use value property in place of value attribute. Close gh-1063.
Diffstat (limited to 'src')
-rw-r--r-- | src/attributes.js | 82 | ||||
-rw-r--r-- | src/support.js | 28 |
2 files changed, 53 insertions, 57 deletions
diff --git a/src/attributes.js b/src/attributes.js index 688282e09..9d603e419 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -1,4 +1,4 @@ -var nodeHook, boolHook, fixSpecified, +var nodeHook, boolHook, rclass = /[\t\r\n]/g, rreturn = /\r/g, rfocusable = /^(?:input|select|textarea|button|object)$/i, @@ -376,25 +376,6 @@ jQuery.extend({ return value; } } - }, - // Use the value property for back compat - // Use the nodeHook for button elements in IE6/7 (#1954) - value: { - get: function( elem, name ) { - if ( nodeHook && jQuery.nodeName( elem, "button" ) ) { - return nodeHook.get( elem, name ); - } - return name in elem ? - elem.value : - null; - }, - set: function( elem, value, name ) { - if ( nodeHook && jQuery.nodeName( elem, "button" ) ) { - return nodeHook.set( elem, value, name ); - } - // Does not return so that setAttribute is also used - elem.value = value; - } } }, @@ -496,22 +477,39 @@ boolHook = { } }; -// IE6/7 do not support getting/setting some attributes with get/setAttribute -if ( !getSetAttribute ) { +// fix oldIE value attroperty +if ( !getSetAttribute || !jQuery.support.valueAttribute ) { + jQuery.attrHooks.value = { + get: function( elem, name ) { + var ret = elem.getAttributeNode( name ); + return jQuery.nodeName( elem, "input" ) ? - fixSpecified = { - name: true, - id: true, - coords: true + // Ignore the value *property* by using defaultValue + elem.defaultValue : + + ret && ret.specified ? ret.value : undefined; + }, + set: function( elem, value, name ) { + if ( jQuery.nodeName( elem, "input" ) ) { + // Does not return so that setAttribute is also used + elem.defaultValue = value; + } else { + // Use nodeHook if defined (#1954); otherwise setAttribute is fine + return nodeHook && nodeHook.set( elem, value, name ); + } + } }; +} + +// IE6/7 do not support getting/setting some attributes with get/setAttribute +if ( !getSetAttribute ) { // Use this for any attribute in IE6/7 // This fixes almost every IE6/7 issue nodeHook = jQuery.valHooks.button = { get: function( elem, name ) { - var ret; - ret = elem.getAttributeNode( name ); - return ret && ( fixSpecified[ name ] ? ret.value !== "" : ret.specified ) ? + var ret = elem.getAttributeNode( name ); + return ret && ( name === "id" || name === "name" || name === "coords" ? ret.value !== "" : ret.specified ) ? ret.value : undefined; }, @@ -519,8 +517,9 @@ if ( !getSetAttribute ) { // Set the existing or create a new attribute node var ret = elem.getAttributeNode( name ); if ( !ret ) { - ret = elem.ownerDocument.createAttribute( name ); - elem.setAttributeNode( ret ); + elem.setAttributeNode( + (ret = elem.ownerDocument.createAttribute( name )) + ); } ret.value = value += ""; @@ -534,6 +533,15 @@ if ( !getSetAttribute ) { } }; + // Set contenteditable to false on removals(#10429) + // Setting to empty string throws an error as an invalid value + jQuery.attrHooks.contenteditable = { + get: nodeHook.get, + set: function( elem, value, name ) { + nodeHook.set( elem, value === "" ? false : value, name ); + } + }; + // Set width and height to auto instead of 0 on empty string( Bug #8150 ) // This is for removals jQuery.each([ "width", "height" ], function( i, name ) { @@ -546,18 +554,6 @@ if ( !getSetAttribute ) { } }); }); - - // Set contenteditable to false on removals(#10429) - // Setting to empty string throws an error as an invalid value - jQuery.attrHooks.contenteditable = { - get: nodeHook.get, - set: function( elem, value, name ) { - if ( value === "" ) { - value = "false"; - } - nodeHook.set( elem, value, name ); - } - }; } diff --git a/src/support.js b/src/support.js index 85b6899dc..5c4f192db 100644 --- a/src/support.js +++ b/src/support.js @@ -31,8 +31,11 @@ jQuery.support = (function() { a.style.cssText = "top:1px;float:left;opacity:.5"; support = { + // Test setAttribute on camelCase class. If it works, we need attrFixes when doing get/setAttribute (ie6/7) + getSetAttribute: div.className !== "t", + // IE strips leading whitespace when .innerHTML is used - leadingWhitespace: ( div.firstChild.nodeType === 3 ), + leadingWhitespace: div.firstChild.nodeType === 3, // Make sure that tbody elements aren't automatically inserted // IE will insert them into empty tables @@ -48,7 +51,7 @@ jQuery.support = (function() { // Make sure that URLs aren't manipulated // (IE normalizes it by default) - hrefNormalized: ( a.getAttribute("href") === "/a" ), + hrefNormalized: a.getAttribute("href") === "/a", // Make sure that element opacity exists // (IE uses filter instead) @@ -59,18 +62,13 @@ jQuery.support = (function() { // (IE uses styleFloat instead of cssFloat) cssFloat: !!a.style.cssFloat, - // Make sure that if no value is specified for a checkbox - // that it defaults to "on". - // (WebKit defaults to "" instead) - checkOn: ( input.value === "on" ), + // Check the default checkbox/radio value ("" on WebKit; "on" elsewhere) + checkOn: !!input.value, // Make sure that a selected-by-default option has a working selected property. // (WebKit defaults to false instead of true, IE too, if it's in an optgroup) optSelected: opt.selected, - // Test setAttribute on camelCase class. If it works, we need attrFixes when doing get/setAttribute (ie6/7) - getSetAttribute: div.className !== "t", - // Tests for enctype support on a form (#6743) enctype: !!document.createElement("form").enctype, @@ -79,7 +77,7 @@ jQuery.support = (function() { html5Clone: document.createElement("nav").cloneNode( true ).outerHTML !== "<:nav></:nav>", // jQuery.support.boxModel DEPRECATED in 1.8 since we don't support Quirks Mode - boxModel: ( document.compatMode === "CSS1Compat" ), + boxModel: document.compatMode === "CSS1Compat", // Will be defined later submitBubbles: true, @@ -121,16 +119,18 @@ jQuery.support = (function() { div.detachEvent( "onclick", clickFn ); } - // Check if a radio maintains its value - // after being appended to the DOM + // Check if we can trust getAttribute("value") input = document.createElement("input"); + input.setAttribute( "value", "" ); + support.valueAttribute = input.getAttribute( "value" ) === ""; + + // Check if an input maintains its value after becoming a radio input.value = "t"; input.setAttribute( "type", "radio" ); support.radioValue = input.value === "t"; - input.setAttribute( "checked", "checked" ); - // #11217 - WebKit loses check when the name is after the checked attribute + input.setAttribute( "checked", "checked" ); input.setAttribute( "name", "t" ); div.appendChild( input ); |