Skip to content

Commit 0b9f034

Browse files
committed
No ticket. Abstract conditional hook definition.
1 parent 4ded9be commit 0b9f034

3 files changed

Lines changed: 52 additions & 40 deletions

File tree

src/css.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var
1414
curCSS = require( "./css/curCSS" ),
1515
support = require( "./css/support" ),
1616
defaultDisplay = require( "./css/defaultDisplay" ),
17+
addGetHookIf = require( "./css/addGetHookIf" ),
1718
data_priv = require( "./data/var/data_priv" ),
1819

1920
// swappable if display is none or starts with table except "table", "table-cell", or "table-caption"
@@ -369,28 +370,16 @@ jQuery.each([ "height", "width" ], function( i, name ) {
369370
});
370371

371372
// Support: Android 2.3
372-
jQuery.cssHooks.marginRight = {
373-
get: function( elem, computed ) {
374-
if ( support.reliableMarginRight() ) {
375-
// Hook not needed, remove it.
376-
// Since there are no other hooks for marginRight, remove the whole object.
377-
delete jQuery.cssHooks.marginRight;
378-
return;
373+
addGetHookIf( jQuery.cssHooks.marginRight, support.reliableMarginRight,
374+
function ( elem, computed ) {
375+
if ( computed ) {
376+
// WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
377+
// Work around by temporarily setting element display to inline-block
378+
return jQuery.swap( elem, { "display": "inline-block" },
379+
curCSS, [ elem, "marginRight" ] );
379380
}
380-
381-
jQuery.cssHooks.marginRight.get = function( elem, computed ) {
382-
if ( computed ) {
383-
// Support: Android 2.3
384-
// WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
385-
// Work around by temporarily setting element display to inline-block
386-
return jQuery.swap( elem, { "display": "inline-block" },
387-
curCSS, [ elem, "marginRight" ] );
388-
}
389-
};
390-
391-
return jQuery.cssHooks.marginRight.get( elem, computed );
392381
}
393-
};
382+
);
394383

395384
// These hooks are used by animate to expand properties
396385
jQuery.each({

src/css/addGetHookIf.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
define( function() {
2+
3+
function addGetHookIf( hookVar, conditionFn, hookFn ) {
4+
// Define the hook, we'll check on the first run if it's really needed.
5+
hookVar = {
6+
get: function() {
7+
var condition = conditionFn();
8+
9+
if ( condition == null ) {
10+
// The test was not ready at this point; screw the hook this time
11+
// but check again when needed next time.
12+
return;
13+
}
14+
15+
if ( condition ) {
16+
// Hook not needed (or it's not possible to use it due to missing dependency),
17+
// remove it.
18+
// Since there are no other hooks for marginRight, remove the whole object.
19+
delete hookVar.get;
20+
return;
21+
}
22+
23+
// Hook needed; redefine it so that the support test is not executed again.
24+
hookVar.get = hookFn;
25+
26+
return hookVar.get.apply( hookVar, arguments );
27+
}
28+
};
29+
}
30+
31+
return addGetHookIf;
32+
33+
});

src/offset.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var
66
access = require( "./core/access" ),
77
rnumnonpx = require( "./css/var/rnumnonpx" ),
88
curCSS = require( "./css/curCSS" ),
9+
addGetHookIf = require( "./css/addGetHookIf" ),
910
support = require( "./css/support" ),
1011
docElem = window.document.documentElement;
1112

@@ -186,28 +187,17 @@ jQuery.each( {scrollLeft: "pageXOffset", scrollTop: "pageYOffset"}, function( me
186187
// getComputedStyle returns percent when specified for top/left/bottom/right
187188
// rather than make the css module depend on the offset module, we just check for it here
188189
jQuery.each( [ "top", "left" ], function( i, prop ) {
189-
jQuery.cssHooks[ prop ] = {
190-
get: function( elem, computed ) {
191-
if ( support.pixelPosition() ) {
192-
// Hook not needed, remove it.
193-
// Since there are no other hooks for prop, remove the whole object.
194-
delete jQuery.cssHooks[ prop ];
195-
return;
190+
addGetHookIf( jQuery.cssHooks[ prop ], support.pixelPosition,
191+
function ( elem, computed ) {
192+
if ( computed ) {
193+
computed = curCSS( elem, prop );
194+
// if curCSS returns percentage, fallback to offset
195+
return rnumnonpx.test( computed ) ?
196+
jQuery( elem ).position()[ prop ] + "px" :
197+
computed;
196198
}
197-
198-
jQuery.cssHooks[ prop ].get = function ( i, prop ) {
199-
if ( computed ) {
200-
computed = curCSS( elem, prop );
201-
// if curCSS returns percentage, fallback to offset
202-
return rnumnonpx.test( computed ) ?
203-
jQuery( elem ).position()[ prop ] + "px" :
204-
computed;
205-
}
206-
};
207-
208-
return jQuery.cssHooks[ prop ].get( i, prop );
209199
}
210-
};
200+
);
211201
});
212202

213203
return jQuery;

0 commit comments

Comments
 (0)