Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions svg-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@
return;
}

// If we don't have a parentNode we can not replace the node inline
if (!el.parentNode) {
callback('Attempted to process an image that is not in the DOM. SVG to load:' + imgUrl);
return;
}

// Remember the request to inject this element, in case other injection
// calls are also trying to replace this element before we finish
injectedElements.push(el);
Expand Down
21 changes: 21 additions & 0 deletions tests/race-condition/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@
<img id="thumb-up-two" class="inject-me-twice thumb-icon" data-src="assets/svg/thumb-up.svg" title="I like it!" alt="thumb up">
<img id="thumb-up-three" class="inject-me-three-times thumb-icon" data-src="assets/svg/thumb-up.svg" title="I like it!" alt="thumb up">

<img id="thumb-up-errors" class="thumb-icon" data-src="assets/svg/thumb-up.svg" style="fill:plum;" title="I like it!" alt="js errors">


<script src="js/svg-injector.min.js"></script>
<script>

window.onerror = function(e){
// visually identify catch js errors
document.getElementById('thumb-up-errors').setAttribute('style', 'fill:maroon');
};

SVGInjector(document.getElementById('thumb-up-errors'));


// Stress Test...

// Call the same injection call lots of time to try and trigger a race condition
Expand Down Expand Up @@ -94,6 +104,17 @@
console.log('[Three] We injected ' + totalSVGsInjected + ' SVG(s)!');
});


(function(){

// Attempt to run SVG injection on an orphaned element
var orphan = document.createElement('img');
orphan.setAttribute('data-src', 'assets/svg/thumb-up.svg');

// Make sure we don't throw a parentNode is null error
SVGInjector(orphan);
})();

</script>
</body>
</html>