Skip to content

Commit bfbc0b1

Browse files
kborchersscottgonzalez
authored andcommitted
Position: Added a check for fraction support in element positions. Fixes #7255 - Position: Revisit solution for off-by-1 errors.
1 parent 757384b commit bfbc0b1

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

tests/unit/position/position_core.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -406,22 +406,22 @@ test("collision: flip, with margin", function() {
406406
}, { top: 0, left: 0 }, "right bottom");
407407
});
408408

409-
//test('bug #5280: consistent results (avoid fractional values)', function() {
410-
// var wrapper = $('#bug-5280'),
411-
// elem = wrapper.children(),
412-
// offset1 = elem.position({
413-
// my: 'center',
414-
// at: 'center',
415-
// of: wrapper,
416-
// collision: 'none'
417-
// }).offset(),
418-
// offset2 = elem.position({
419-
// my: 'center',
420-
// at: 'center',
421-
// of: wrapper,
422-
// collision: 'none'
423-
// }).offset();
424-
// same(offset1, offset2);
425-
//});
409+
test('bug #5280: consistent results (avoid fractional values)', function() {
410+
var wrapper = $('#bug-5280'),
411+
elem = wrapper.children(),
412+
offset1 = elem.position({
413+
my: 'center',
414+
at: 'center',
415+
of: wrapper,
416+
collision: 'none'
417+
}).offset(),
418+
offset2 = elem.position({
419+
my: 'center',
420+
at: 'center',
421+
of: wrapper,
422+
collision: 'none'
423+
}).offset();
424+
same(offset1, offset2);
425+
});
426426

427427
})(jQuery);

ui/jquery.ui.position.js

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ $.ui = $.ui || {};
1414
var horizontalPositions = /left|center|right/,
1515
verticalPositions = /top|center|bottom/,
1616
center = "center",
17+
support = {},
1718
_position = $.fn.position,
1819
_offset = $.fn.offset;
1920

@@ -121,9 +122,11 @@ $.fn.position = function( options ) {
121122
position.top -= elemHeight / 2;
122123
}
123124

124-
// prevent fractions (see #5280)
125-
position.left = Math.round( position.left );
126-
position.top = Math.round( position.top );
125+
// prevent fractions if jQuery version doesn't support them (see #5280)
126+
if ( !support.fractions ) {
127+
position.left = Math.round( position.left );
128+
position.top = Math.round( position.top );
129+
}
127130

128131
collisionPosition = {
129132
left: position.left - marginLeft,
@@ -249,4 +252,47 @@ if ( !$.offset.setOffset ) {
249252
};
250253
}
251254

255+
// fraction support test (older versions of jQuery don't support fractions)
256+
(function () {
257+
var body = document.getElementsByTagName( "body" )[ 0 ],
258+
div = document.createElement( "div" ),
259+
testElement, testElementParent, testElementStyle, offset, offsetTotal;
260+
261+
//Create a "fake body" for testing based on method used in jQuery.support
262+
testElement = document.createElement( body ? "div" : "body" );
263+
testElementStyle = {
264+
visibility: "hidden",
265+
width: 0,
266+
height: 0,
267+
border: 0,
268+
margin: 0,
269+
background: "none"
270+
};
271+
if ( body ) {
272+
jQuery.extend( testElementStyle, {
273+
position: "absolute",
274+
left: "-1000px",
275+
top: "-1000px"
276+
});
277+
}
278+
for ( var i in testElementStyle ) {
279+
testElement.style[ i ] = testElementStyle[ i ];
280+
}
281+
testElement.appendChild( div );
282+
testElementParent = body || document.documentElement;
283+
testElementParent.insertBefore( testElement, testElementParent.firstChild );
284+
285+
div.style.cssText = "position: absolute; left: 10.7432222px; top: 10.432325px; height: 30px; width: 201px;";
286+
287+
offset = $( div ).offset( function( _, offset ) {
288+
return offset;
289+
}).offset();
290+
291+
testElement.innerHTML = "";
292+
testElementParent.removeChild( testElement );
293+
294+
offsetTotal = offset.top + offset.left + ( body ? 2000 : 0 );
295+
support.fractions = offsetTotal > 21 && offsetTotal < 22;
296+
})();
297+
252298
}( jQuery ));

0 commit comments

Comments
 (0)