Skip to content

Commit 68cf80f

Browse files
authored
Merge pull request #29 from kevinsawicki/guard-against-missing-parent
Guard against missing parent when replacing
2 parents 98dafb7 + a11e87b commit 68cf80f

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"devDependencies": {
66
"classlist": "2014.7.23",
77
"CustomElements": "0.2.4",
8-
"es5-basic-shim": "1.0.6",
8+
"es5-shim": "4.5.9",
99
"es6-promise": "1.0.0",
1010
"MutationObserver": "0.2.0",
1111
"qunit": "1.14.0",

include-fragment-element.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313

1414
function handleData(el, data) {
1515
return data.then(function(html) {
16-
el.insertAdjacentHTML('afterend', html);
17-
el.parentNode.removeChild(el);
16+
var parentNode = el.parentNode;
17+
if (parentNode) {
18+
el.insertAdjacentHTML('afterend', html);
19+
parentNode.removeChild(el);
20+
}
1821
}, function() {
1922
el.classList.add('is-error');
2023
});

test/test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<div id="qunit"></div>
1010
<div id="qunit-fixture"></div>
1111

12-
<script src="../bower_components/es5-basic-shim/src/shim.js"></script>
12+
<script src="../bower_components/es5-shim/es5-shim.js"></script>
1313
<script src="../bower_components/es6-promise/promise.js"></script>
1414
<script src="../bower_components/classlist/classList.js"></script>
1515
<script src="../bower_components/WeakMap/WeakMap.js"></script>

test/test.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,30 @@ asyncTest('replaces element on 200 status', 2, function() {
176176
});
177177
});
178178

179+
asyncTest('does not replace element if it has no parent', 2, function() {
180+
var div = document.createElement('div');
181+
div.innerHTML = '<include-fragment src="/hello">loading</include-fragment>';
182+
document.getElementById('qunit-fixture').appendChild(div);
183+
184+
var fragment = div.firstChild;
185+
fragment.remove();
186+
187+
window.addEventListener('unhandledrejection', function() {
188+
ok(false);
189+
});
190+
191+
fragment.addEventListener('load', function() {
192+
equal(document.querySelector('#replaced'), null);
193+
start();
194+
195+
div.appendChild(fragment);
196+
197+
setTimeout(function() {
198+
equal(document.querySelector('#replaced').textContent, 'hello');
199+
}, 10);
200+
});
201+
});
202+
179203
asyncTest('replaces with several new elements on 200 status', 3, function() {
180204
var div = document.createElement('div');
181205
div.innerHTML = '<include-fragment src="/one-two">loading</include-fragment>';
@@ -206,7 +230,7 @@ asyncTest('adds is-error class on 500 status', 1, function() {
206230
div.innerHTML = '<include-fragment src="/boom">loading</include-fragment>';
207231
document.getElementById('qunit-fixture').appendChild(div);
208232

209-
div.firstChild.addEventListener('error', function(event) {
233+
div.firstChild.addEventListener('error', function() {
210234
ok(document.querySelector('include-fragment').classList.contains('is-error'));
211235
start();
212236
});
@@ -217,7 +241,7 @@ asyncTest('adds is-error class on mising Content-Type', 1, function() {
217241
div.innerHTML = '<include-fragment src="/blank-type">loading</include-fragment>';
218242
document.getElementById('qunit-fixture').appendChild(div);
219243

220-
div.firstChild.addEventListener('error', function(event) {
244+
div.firstChild.addEventListener('error', function() {
221245
ok(document.querySelector('include-fragment').classList.contains('is-error'));
222246
start();
223247
});

0 commit comments

Comments
 (0)