Skip to content

Instantly share code, notes, and snippets.

@treetrum
Last active April 19, 2018 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treetrum/6914cb21d49cb8be4684e06cd1b35e28 to your computer and use it in GitHub Desktop.
Save treetrum/6914cb21d49cb8be4684e06cd1b35e28 to your computer and use it in GitHub Desktop.
Automatically inline SVGs when using img tags.
'use strict';(function(){var a=new DOMParser,b=document.querySelectorAll('img[data-svg-inline]');b.forEach(function(c){var d=c.src,e=c.parentNode;fetch(d).then(function(f){return f.text()}).then(function(f){var g=a.parseFromString(f,'image/svg+xml'),h=g.documentElement;e.replaceChild(h,c)})})})();
(() => {
let parser = new DOMParser();
let svgTags = document.querySelectorAll('img[data-svg-inline]');
svgTags.forEach(svgTag => {
let imageURL = svgTag.src;
let imageParent = svgTag.parentNode;
fetch(imageURL)
.then(response => response.text())
.then(svgData => {
var svgDocument = parser.parseFromString(svgData, 'image/svg+xml');
let svgElement = svgDocument.documentElement;
imageParent.replaceChild(svgElement, svgTag);
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment