Skip to content

Commit

Permalink
Use element.getBoundingClient() to implement getOffsetWidth(); this a…
Browse files Browse the repository at this point in the history
…lso affects Metrics, since it uses getOffsetWidth().

element.getBoundingClient originated on IE, and is implemented by
every browser we are compatible with.
  • Loading branch information
Andrea Campi committed Aug 27, 2012
1 parent 41107ce commit 8c0308c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/dimensions.js
Expand Up @@ -128,7 +128,7 @@ goog.scope(function() {
* @return {!number} Value in pixels.
*/
dimensions.getOffsetWidth = function(el) {
return el && el.offsetWidth || 0;
return el && el.getBoundingClientRect()['width'] || 0;
};

/**
Expand Down
21 changes: 21 additions & 0 deletions test/dimensions.js
Expand Up @@ -128,6 +128,27 @@ $(function() {
equals(d.outerH, 20);
});

test('offsetWidth with subpixels', function() {
var e = $('<div></div>').addClass('testonly').appendTo('body').css({
fontSize: '13px',
lineHeight: '13px'
}),
c1 = $('<div></div>').appendTo(e).css({
"margin-left": '15em',
width: '21.5em'
}),
c2 = $('<div></div>').appendTo(e).css({
"margin-left": '37.5em',
width: '21.5em'
}),
d1 = new treesaver.dimensions.Metrics(c1[0]),
d2 = new treesaver.dimensions.Metrics(c2[0]);

equals(d1.outerW, d2.outerW);
equals(d1.outerW, treesaver.capabilities.SUPPORTS_SUBPIXELS ? 279.5 : 279);
equals(d2.outerW, treesaver.capabilities.SUPPORTS_SUBPIXELS ? 279.5 : 279);
});

test('lineHeight', function() {
var e = $('<div></div>').addClass('testonly').appendTo('body').css({
fontSize: '14px',
Expand Down

0 comments on commit 8c0308c

Please sign in to comment.