Skip to content

Instantly share code, notes, and snippets.

@EWhite613
Created May 19, 2017 15:04
Show Gist options
  • Save EWhite613/454c20fce836e48b20000c57cdc5de50 to your computer and use it in GitHub Desktop.
Save EWhite613/454c20fce836e48b20000c57cdc5de50 to your computer and use it in GitHub Desktop.
Convert SVG4Everybody (svg `use` tags) to inline
function convertSVG4Everbody () {
var items = Array.from(document.querySelectorAll('use'))
items.map(function (use) {
var svgParent = use.parentNode
var url = use.href.baseVal
var id = /#\S*/.exec(url)[0].replace('#', '')
console.log('url: ', url)
var xml = httpGetXML(url)
var symbol = xml.getElementById(id)
// Grab all of the original's attributes, and pass them to the replacement
svgParent.innerHTML = symbol.innerHTML
for (var i = 0, l = symbol.attributes.length; i < l; ++i) {
var nodeName = symbol.attributes.item(i).nodeName
var nodeValue = symbol.attributes.item(i).nodeValue
if (nodeName !== 'id' && nodeName !== 'class') {
svgParent.setAttribute(nodeName, nodeValue)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment