Skip to content

Commit

Permalink
Fix possible error with dom.remove() if no parent node
Browse files Browse the repository at this point in the history
  • Loading branch information
samclarke committed Nov 20, 2017
1 parent 5536804 commit 60e7028
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ export function closest(node, selector) {
* @param {!HTMLElement} node
*/
export function remove(node) {
node.parentNode.removeChild(node);
if (node.parentNode) {
node.parentNode.removeChild(node);
}
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ QUnit.test('css()', function (assert) {
1,
'Get computed value'
);

assert.equal(
dom.css(window, 'width'),
null,
'Get computed value of window'
);
});

QUnit.test('data()', function (assert) {
Expand Down Expand Up @@ -294,6 +300,19 @@ QUnit.test('is()', function (assert) {
assert.notOk(dom.is(div, '.testing'));
});

QUnit.test('remove()', function (assert) {
var parent = document.createElement('div');
var child = document.createElement('div');

parent.appendChild(child);
dom.remove(child);

assert.ok(!child.parentNode);

// Make sure doesn't throw if has no parent
dom.remove(child);
});

QUnit.test('contains()', function (assert) {
var parent = document.createElement('div');
var child = document.createElement('div');
Expand Down

0 comments on commit 60e7028

Please sign in to comment.