Skip to content

Commit

Permalink
Add a [workaround](jsdom/jsdom#135 (comment)) which allows the mostly…
Browse files Browse the repository at this point in the history
… correct behavior of `offsetHeight` and `offsetWidth` in jsdom.
  • Loading branch information
zach pratt committed Mar 18, 2015
1 parent 36a9b6c commit 1c76c00
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/helpers/jsdom.js
Expand Up @@ -2,13 +2,42 @@

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 = '<!DOCTYPE html><html><head><title></title></head><body></body></html>',
window = jsdom.jsdom(baseMarkup).defaultView;

global.window = window;
global.document = window.document;
global.navigator = window.navigator;
applyJsdomWorkaround(window);
}

setupDom();

0 comments on commit 1c76c00

Please sign in to comment.