UNPKG

svg-inline-react

Version:
199 lines (152 loc) 7.75 kB
/******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { module.exports = __webpack_require__(1); /***/ }, /* 1 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = __webpack_require__(!(function webpackMissingModule() { var e = new Error("Cannot find module \"react\""); e.code = 'MODULE_NOT_FOUND'; throw e; }())); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var DOMParser = typeof window !== 'undefined' && window.DOMParser; var process = process || {}; process.env = process.env || {}; var parserAvailable = typeof DOMParser !== 'undefined' && DOMParser.prototype != null && DOMParser.prototype.parseFromString != null; function isParsable(src) { // kinda naive but meh, ain't gonna use full-blown parser for this return parserAvailable && typeof src === 'string' && src.trim().substr(0, 4) === '<svg'; } // parse SVG string using `DOMParser` function parseFromSVGString(src) { var parser = new DOMParser(); return parser.parseFromString(src, "image/svg+xml"); } // Transform DOM prop/attr names applicable to `<svg>` element but react-limited function switchSVGAttrToReactProp(propName) { switch (propName) { case 'class': return 'className'; default: return propName; } } var InlineSVG = function (_React$Component) { _inherits(InlineSVG, _React$Component); function InlineSVG(props) { _classCallCheck(this, InlineSVG); var _this = _possibleConstructorReturn(this, (InlineSVG.__proto__ || Object.getPrototypeOf(InlineSVG)).call(this, props)); _this._extractSVGProps = _this._extractSVGProps.bind(_this); return _this; } // Serialize `Attr` objects in `NamedNodeMap` _createClass(InlineSVG, [{ key: '_serializeAttrs', value: function _serializeAttrs(map) { var ret = {}; var prop = void 0; for (var i = 0; i < map.length; i++) { prop = switchSVGAttrToReactProp(map[i].name); ret[prop] = map[i].value; } return ret; } // get <svg /> element props }, { key: '_extractSVGProps', value: function _extractSVGProps(src) { var map = parseFromSVGString(src).documentElement.attributes; return map.length > 0 ? this._serializeAttrs(map) : null; } // get content inside <svg> element. }, { key: '_stripSVG', value: function _stripSVG(src) { return parseFromSVGString(src).documentElement.innerHTML; } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(_ref) { var children = _ref.children; if ("production" !== process.env.NODE_ENV && children != null) { console.info('<InlineSVG />: `children` prop will be ignored.'); } } }, { key: 'render', value: function render() { var Element = void 0, __html = void 0, svgProps = void 0; var _props = this.props, element = _props.element, raw = _props.raw, src = _props.src, otherProps = _objectWithoutProperties(_props, ['element', 'raw', 'src']); if (raw === true && isParsable(src)) { Element = 'svg'; svgProps = this._extractSVGProps(src); __html = this._stripSVG(src); } __html = __html || src; Element = Element || element; svgProps = svgProps || {}; return _react2.default.createElement(Element, _extends({}, svgProps, otherProps, { src: null, children: null, dangerouslySetInnerHTML: { __html: __html } })); } }]); return InlineSVG; }(_react2.default.Component); InlineSVG.defaultProps = { element: 'i', raw: false, src: '' }; InlineSVG.propTypes = { src: _react2.default.PropTypes.string.isRequired, element: _react2.default.PropTypes.string, raw: _react2.default.PropTypes.bool }; exports.default = InlineSVG; /***/ } /******/ ]);