Skip to content

Commit

Permalink
.dim() & height() & width() on document element
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Oct 27, 2012
1 parent 23028df commit d4b483d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
1 change: 0 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
, "undef": true
, "sub": true
, "white": false
, "indent": 2
, "asi": true
, "laxbreak": true
, "eqnull": true
Expand Down
11 changes: 8 additions & 3 deletions src/bonzo.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,8 @@
, dim: function () {
if (!this.length) return { height: 0, width: 0 }
var el = this[0]
, orig = !el.offsetWidth && !el.offsetHeight ?
, de = el.nodeType == 9 && el.documentElement // document
, orig = !de && !!el.style && !el.offsetWidth && !el.offsetHeight ?
// el isn't visible, can't be measured properly, so fix that
function (t) {
var s = {
Expand All @@ -832,8 +833,12 @@
})
return s
}(this) : null
, width = el.offsetWidth
, height = el.offsetHeight
, width = de
? Math.max(el.body.scrollWidth, el.body.offsetWidth, de.scrollWidth, de.offsetWidth, de.clientWidth)
: el.offsetWidth
, height = de
? Math.max(el.body.scrollHeight, el.body.offsetHeight, de.scrollWidth, de.offsetWidth, de.clientHeight)
: el.offsetHeight

orig && this.first().css(orig)
return {
Expand Down
5 changes: 5 additions & 0 deletions tests/bridge-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ sink('Ender bridge', function (test, ok, before, after, assert) {
$el.remove()
})

test('height & width of `document`', 2, function () {
ok(ender(document).height() > 0)
ok(ender(document).width() > 0)
})

test('no-arg parents()', 5, function () {
try {
var parents = ender('#parent-test').parents()
Expand Down
5 changes: 4 additions & 1 deletion tests/layout-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ sink('Layout', function (test, ok) {
ok(!nullSet.height, 'no offset().height')
})

test('dimensions', 5, function () {
test('dimensions', 7, function () {
var $el = $(dom.create('<div/>')).css({
position: 'absolute'
, left: '50px'
Expand All @@ -73,6 +73,9 @@ sink('Layout', function (test, ok) {
ok($hidden.dim().height > 0 && $hidden.dim().height < 100, 'hidden element dim().height is reasonable non-zero')
ok($hidden.dim().width > 0 && $hidden.dim().width < 10000, 'hidden element dim().width is reasonable non-zero')
ok($hidden[0].style.display == 'none', 'hidden element is still hidden after dim() call')

ok($(document).dim().height > 0, 'document has a height > 0: ' + $(document).dim().height)
ok($(document).dim().width > 0, 'document has a width > 0: ' + $(document).dim().width)
})

test('viewport width & height', 2, function () {
Expand Down

0 comments on commit d4b483d

Please sign in to comment.