Skip to content

Commit

Permalink
Fixes #11938: Adding array getter method to jQuery.fn.css
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesherov committed Dec 11, 2012
1 parent d0d0fa2 commit 4b5b6cf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core.js
Expand Up @@ -798,7 +798,7 @@ jQuery.extend({
bulk = key == null; bulk = key == null;


// Sets many values // Sets many values
if ( key && typeof key === "object" ) { if ( jQuery.type( key ) === "object" ) {
chainable = true; chainable = true;
for ( i in key ) { for ( i in key ) {
jQuery.access( elems, fn, i, key[i], true, emptyGet, raw ); jQuery.access( elems, fn, i, key[i], true, emptyGet, raw );
Expand Down
15 changes: 15 additions & 0 deletions src/css.js
Expand Up @@ -98,6 +98,21 @@ function showHide( elements, show ) {
jQuery.fn.extend({ jQuery.fn.extend({
css: function( name, value ) { css: function( name, value ) {
return jQuery.access( this, function( elem, name, value ) { return jQuery.access( this, function( elem, name, value ) {
var styles, len,
map = {},
i = 0;

if ( jQuery.isArray( name ) ) {
styles = getStyles( elem );
len = name.length;

for ( ; i < len; i++ ) {

This comment has been minimized.

Copy link
@gibson042

gibson042 Dec 11, 2012

-3 bytes by dropping len (var i, styles, map = {};) and changing this to i = name.length; while ( i-- ) {

This comment has been minimized.

Copy link
@mikesherov

mikesherov Dec 11, 2012

Author Owner

I thought of this. And I know there is no true ordering of object keys according to spec, but I'd like to leverage the fact that in implementation they are ordered properly and maintain that order.

map[ name[ i ] ] = jQuery.css( elem, name[ i ], false, styles );
}

return map;
}

return value !== undefined ? return value !== undefined ?
jQuery.style( elem, name, value ) : jQuery.style( elem, name, value ) :
jQuery.css( elem, name ); jQuery.css( elem, name );
Expand Down
16 changes: 16 additions & 0 deletions test/unit/css.js
Expand Up @@ -257,6 +257,22 @@ test("css(String, Object)", function() {
ok( success, "Setting RGBA values does not throw Error" ); ok( success, "Setting RGBA values does not throw Error" );
}); });


test( "css(Array)", function() {
expect( 2 );

var expectedMany = {
"overflow": "visible",
"width": "16px"
},
expectedSingle = {
"width": "16px"
},
elem = jQuery("<div></div>").appendTo("#qunit-fixture");

deepEqual( elem.css( expectedMany ).css([ "overflow", "width" ]), expectedMany, "Getting multiple element array" );
deepEqual( elem.css( expectedSingle ).css([ "width" ]), expectedSingle, "Getting single element array" );
});

if ( !jQuery.support.opacity ) { if ( !jQuery.support.opacity ) {
test("css(String, Object) for MSIE", function() { test("css(String, Object) for MSIE", function() {
expect( 5 ); expect( 5 );
Expand Down

0 comments on commit 4b5b6cf

Please sign in to comment.