Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getOuterSizes returning NaN for virtual elements #711

Merged
merged 1 commit into from Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/popper/src/utils/getOuterSizes.js
Expand Up @@ -8,8 +8,8 @@
export default function getOuterSizes(element) {
const window = element.ownerDocument.defaultView;
const styles = window.getComputedStyle(element);
const x = parseFloat(styles.marginTop) + parseFloat(styles.marginBottom);
const y = parseFloat(styles.marginLeft) + parseFloat(styles.marginRight);
const x = parseFloat(styles.marginTop || 0) + parseFloat(styles.marginBottom || 0);
const y = parseFloat(styles.marginLeft || 0) + parseFloat(styles.marginRight || 0);
const result = {
width: element.offsetWidth + y,
height: element.offsetHeight + x,
Expand Down
47 changes: 47 additions & 0 deletions packages/popper/tests/unit/getOuterSizes.js
@@ -0,0 +1,47 @@
import chai from 'chai';
const { expect } = chai;
import getOuterSizes from '../../src/utils/getOuterSizes';

describe('utils/getOuterSizes', () => {
let node;

beforeEach(() => {
node = document.createElement('div');
});

describe('when the element is not attach on the DOM', () => {
it('should returns 0 width and height for empty elements', () => {
expect(getOuterSizes(node)).to.deep.equal({
width: 0,
height: 0,
});
});
});

describe('when the element is attach on the DOM', () => {
it('should returns width and height for elements without margins', () => {
node.style.width = '20px';
node.style.height = '20px';
document.body.appendChild(node);
node.style.position = 'relative';

expect(getOuterSizes(node)).to.deep.equal({
width: 20,
height: 20,
});
});

it('should returns width and height for elements with margins', () => {
node.style.width = '20px';
node.style.height = '20px';
node.style.margin = '10px';
document.body.appendChild(node);
node.style.position = 'relative';

expect(getOuterSizes(node)).to.deep.equal({
width: 40,
height: 40,
});
});
});
});