UNPKG

svg-inline-react

Version:
54 lines (47 loc) 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertReactSVGDOMProperty = convertReactSVGDOMProperty; exports.startsWith = startsWith; exports.serializeAttrs = serializeAttrs; exports.getSVGFromSource = getSVGFromSource; exports.extractSVGProps = extractSVGProps; // Transform DOM prop/attr names applicable to `<svg>` element but react-limited function convertReactSVGDOMProperty(str) { return str.replace(/[-|:]([a-z])/g, function (g) { return g[1].toUpperCase(); }); } function startsWith(str, substring) { return str.indexOf(substring) === 0; } var DataPropPrefix = 'data-'; // Serialize `Attr` objects in `NamedNodeMap` function serializeAttrs(map) { var ret = {}; for (var prop, i = 0; i < map.length; i++) { var key = map[i].name; if (key == "class") { prop = "className"; } else if (!startsWith(key, DataPropPrefix)) { prop = convertReactSVGDOMProperty(key); } else { prop = key; } ret[prop] = map[i].value; } return ret; } function getSVGFromSource(src) { var svgContainer = document.createElement('div'); svgContainer.innerHTML = src; var svg = svgContainer.firstElementChild; svg.remove ? svg.remove() : svgContainer.removeChild(svg); // deref from parent element return svg; } // get <svg /> element props function extractSVGProps(src) { var map = getSVGFromSource(src).attributes; return map.length > 0 ? serializeAttrs(map) : null; }