Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: cb1d7fbb4ac6a7f3f76bb88e2182d3cb741fa62f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import mapsheets from './assets/images/mapsheets.svg';

const doc = new DOMParser().parseFromString(mapsheets, 'image/svg+xml');
const refs = [];

document.querySelectorAll('use[href*=".svg"').forEach(el => {
  const fragId = el.getAttributeNS(null, 'href').split('.svg').pop();
  const frag = doc.querySelector(fragId);
  frag.querySelectorAll('use').forEach(el => refs.push(el.getAttributeNS(null, 'href')));
  if (el.style.transform) frag.style.transform = el.style.transform;
  el.replaceWith(frag);
});

const refsQuery = [...new Set([...refs])].join(', ');
const refNodes = doc.querySelectorAll(refsQuery);
const defs = document.querySelector('defs');

refNodes.forEach(n => defs.appendChild(n));