Skip to content

Commit 047ec83

Browse files
committed
CSS: Add jQuery.swap and warnings
Fixes #100 Closes #103
1 parent d9edf5a commit 047ec83

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = function(grunt) {
2222
"src/migrate.js",
2323
"src/attributes.js",
2424
"src/core.js",
25+
"src/css.js",
2526
"src/ajax.js",
2627
"src/data.js",
2728
"src/manipulation.js",

src/css.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
var internalSwapCall = false;
3+
4+
// If this version of jQuery has .swap(), don't false-alarm on internal uses
5+
if ( jQuery.swap ) {
6+
jQuery.each( [ "height", "width", "reliableMarginRight" ], function( _, name ) {
7+
var oldHook = jQuery.cssHooks[ name ] && jQuery.cssHooks[ name ].get;
8+
9+
if ( oldHook ) {
10+
jQuery.cssHooks[ name ].get = function() {
11+
var ret;
12+
13+
internalSwapCall = true;
14+
ret = oldHook.apply( this, arguments );
15+
internalSwapCall = false;
16+
return ret;
17+
};
18+
}
19+
});
20+
}
21+
22+
jQuery.swap = function( elem, options, callback, args ) {
23+
var ret, name,
24+
old = {};
25+
26+
if ( !internalSwapCall ) {
27+
migrateWarn( "jQuery.swap() is undocumented and deprecated" );
28+
}
29+
30+
// Remember the old values, and insert the new ones
31+
for ( name in options ) {
32+
old[ name ] = elem.style[ name ];
33+
elem.style[ name ] = options[ name ];
34+
}
35+
36+
ret = callback.apply( elem, args || [] );
37+
38+
// Revert the old values
39+
for ( name in options ) {
40+
elem.style[ name ] = old[ name ];
41+
}
42+
43+
return ret;
44+
};

test/css.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
module("css");
3+
4+
test( "jQuery.swap()", function( assert ) {
5+
assert.expect( 6 );
6+
7+
var div = document.createElement( "div" );
8+
div.style.borderWidth = "4px";
9+
10+
expectWarning( "External swap() call", function() {
11+
jQuery.swap( div, { borderRightWidth: "5px" }, function( arg ) {
12+
13+
assert.equal( this.style.borderRightWidth, "5px", "style was changed" );
14+
assert.equal( arg, 42, "arg was passed" );
15+
16+
}, [ 42 ] );
17+
});
18+
assert.equal( div.style.borderRightWidth, "4px", "style was restored" );
19+
20+
expectNoWarning( "Internal swap() call", function() {
21+
var $fp = jQuery( "#firstp" ).width( "10em" ),
22+
width = $fp.width();
23+
24+
assert.equal( $fp.hide().width(), width, "correct width" );
25+
});
26+
27+
});

test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<!-- Unit test files -->
3535
<script src="migrate.js"></script>
3636
<script src="core.js"></script>
37+
<script src="css.js"></script>
3738
<script src="data.js"></script>
3839
<script src="attributes.js"></script>
3940
<script src="manipulation.js"></script>

warnings.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,8 @@ $(document).ajaxStart(function(){ $("#status").text("Ajax started"); });
178178

179179
**Solution**: Replace any use of `.size()` with `.length`.
180180

181+
### JQMIGRATE: jQuery.swap() is undocumented and deprecated
182+
183+
**Cause**: The `jQuery.swap()` method temporarily exchanges a set of CSS properties. It was never documented as part of jQuery's public API and should not be used because it can cause performance problems due to forced layout.
184+
185+
**Solution**: Rework the code to avoid calling `jQuery.swap()`, or explicitly set and restore the properties you need to change.

0 commit comments

Comments
 (0)