From 1c76c00981ad478afe9e55c6aa4ea78187c9cbc8 Mon Sep 17 00:00:00 2001 From: zach pratt Date: Wed, 18 Mar 2015 14:19:33 -0500 Subject: [PATCH] Add a [workaround](https://github.com/tmpvar/jsdom/issues/135#issuecomment-68191941) which allows the mostly correct behavior of `offsetHeight` and `offsetWidth` in jsdom. --- test/helpers/jsdom.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/helpers/jsdom.js b/test/helpers/jsdom.js index 8925255..ec428e2 100644 --- a/test/helpers/jsdom.js +++ b/test/helpers/jsdom.js @@ -2,6 +2,34 @@ var jsdom = require('jsdom'); +/** + * Borrowed from: https://github.com/tmpvar/jsdom/issues/135#issuecomment-68191941 + */ +function applyJsdomWorkaround(window) { + Object.defineProperties(window.HTMLElement.prototype, { + offsetLeft: { + get: function () { + return parseFloat(window.getComputedStyle(this).marginLeft) || 0; + } + }, + offsetTop: { + get: function () { + return parseFloat(window.getComputedStyle(this).marginTop) || 0; + } + }, + offsetHeight: { + get: function () { + return parseFloat(window.getComputedStyle(this).height) || 0; + } + }, + offsetWidth: { + get: function () { + return parseFloat(window.getComputedStyle(this).width) || 0; + } + } + }); +} + function setupDom() { var baseMarkup = '', window = jsdom.jsdom(baseMarkup).defaultView; @@ -9,6 +37,7 @@ function setupDom() { global.window = window; global.document = window.document; global.navigator = window.navigator; + applyJsdomWorkaround(window); } setupDom();