UNPKG

convert-svg-react

Version:

Converts SVG XML code to be used as ReactJS component.

60 lines (50 loc) 2.21 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _react = _interopRequireDefault(require("react")); var CreateSVGfromHTML = function CreateSVGfromHTML(nodeArray) { // console.log('nodeArray:', nodeArray) return Array.prototype.map.call(nodeArray, function (node, i) { var attributes = node.attributes, localName = node.localName, childNodes = node.childNodes, nodeValue = node.nodeValue; var attributeObj = {}; if (attributes) { Array.from(attributes).forEach(function (attribute) { if (attribute.name === "style") { var styleAttributes = attribute.nodeValue.split(";"); console.log('styleAttributes:', styleAttributes); var styleObj = {}; styleAttributes.forEach(function (attribute) { var _attribute$split = attribute.split(":"), _attribute$split2 = (0, _slicedToArray2["default"])(_attribute$split, 2), key = _attribute$split2[0], value = _attribute$split2[1]; styleObj[key] = value; }); attributeObj[attribute.name] = styleObj; } else if (attribute.name === 'classname') { // const className = attribute.name.replace(/n/, 'N') // console.log('className:', className) attributeObj[attribute.name.replace(/n/, 'N')] = attribute.nodeValue; } else { attributeObj[attribute.name] = attribute.nodeValue; // Adds inline style to svg if (localName === 'svg') attributeObj.style = { width: '5rem', height: '5rem', margin: '10px' }; } }); } attributeObj.key = i; return localName ? /*#__PURE__*/_react["default"].createElement(localName, attributeObj, childNodes && Array.isArray(Array.from(childNodes)) ? CreateSVGfromHTML(Array.from(childNodes)) : []) : nodeValue; }); }; var _default = CreateSVGfromHTML; exports["default"] = _default;