parseXML.ts 692 B

12345678910111213141516171819202122
  1. import { isString } from '../core/util';
  2. /**
  3. * For big svg string, this method might be time consuming.
  4. */
  5. export function parseXML(svg: Document | string | SVGElement): SVGElement {
  6. if (isString(svg)) {
  7. const parser = new DOMParser();
  8. svg = parser.parseFromString(svg, 'text/xml');
  9. }
  10. let svgNode: Node = svg;
  11. // Document node. If using $.get, doc node may be input.
  12. if (svgNode.nodeType === 9) {
  13. svgNode = svgNode.firstChild;
  14. }
  15. // nodeName of <!DOCTYPE svg> is also 'svg'.
  16. while (svgNode.nodeName.toLowerCase() !== 'svg' || svgNode.nodeType !== 1) {
  17. svgNode = svgNode.nextSibling;
  18. }
  19. return svgNode as SVGElement;
  20. }