react-svg
Advanced tools
Comparing version 1.1.5 to 2.0.0
@@ -1,13 +0,13 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import ReactSVG from '../lib'; | ||
import ReactSVG from '../src' | ||
ReactDOM.render( | ||
<ReactSVG | ||
path={'atomic.svg'} | ||
className={'example'} | ||
callback={(svg) => console.log(svg)} | ||
path="atomic.svg" | ||
callback={svg => console.log(svg)} | ||
className="example" | ||
/>, | ||
document.querySelector('.Root') | ||
); | ||
) |
681
lib/index.js
@@ -1,630 +0,93 @@ | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
module.exports = factory(require("react")); | ||
else if(typeof define === 'function' && define.amd) | ||
define(["react"], factory); | ||
else { | ||
var a = typeof exports === 'object' ? factory(require("react")) : factory(root["React"]); | ||
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i]; | ||
} | ||
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
'use strict'; | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) | ||
/******/ return installedModules[moduleId].exports; | ||
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; }; }(); | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ exports: {}, | ||
/******/ id: moduleId, | ||
/******/ loaded: false | ||
/******/ }; | ||
var _react = require('react'); | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
var _react2 = _interopRequireDefault(_react); | ||
/******/ // Flag the module as loaded | ||
/******/ module.loaded = true; | ||
var _svgInjector = require('svg-injector'); | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
var _svgInjector2 = _interopRequireDefault(_svgInjector); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
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; } | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
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; } | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
var ReactSVG = function (_PureComponent) { | ||
_inherits(ReactSVG, _PureComponent); | ||
'use strict'; | ||
function ReactSVG() { | ||
_classCallCheck(this, ReactSVG); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
return _possibleConstructorReturn(this, (ReactSVG.__proto__ || Object.getPrototypeOf(ReactSVG)).apply(this, arguments)); | ||
} | ||
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; }; }(); | ||
_createClass(ReactSVG, [{ | ||
key: 'injectSVG', | ||
value: function injectSVG() { | ||
var _props = this.props, | ||
evalScripts = _props.evalScripts, | ||
each = _props.callback; | ||
var _react = __webpack_require__(1); | ||
var _react2 = _interopRequireDefault(_react); | ||
if (this._img) { | ||
(0, _svgInjector2.default)(this._img, { | ||
evalScripts: evalScripts, | ||
each: each | ||
}); | ||
} | ||
} | ||
}, { | ||
key: 'componentDidMount', | ||
value: function componentDidMount() { | ||
this.injectSVG(); | ||
} | ||
}, { | ||
key: 'componentDidUpdate', | ||
value: function componentDidUpdate() { | ||
this.injectSVG(); | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render() { | ||
var _this2 = this; | ||
var _svgInjector = __webpack_require__(2); | ||
var _props2 = this.props, | ||
className = _props2.className, | ||
path = _props2.path; | ||
var _svgInjector2 = _interopRequireDefault(_svgInjector); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
return _react2.default.createElement('img', { | ||
ref: function ref(img) { | ||
return _this2._img = img; | ||
}, | ||
className: className, | ||
'data-src': path | ||
}); | ||
} | ||
}]); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
return ReactSVG; | ||
}(_react.PureComponent); | ||
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 ReactSVG = function (_Component) { | ||
_inherits(ReactSVG, _Component); | ||
function ReactSVG() { | ||
_classCallCheck(this, ReactSVG); | ||
return _possibleConstructorReturn(this, (ReactSVG.__proto__ || Object.getPrototypeOf(ReactSVG)).apply(this, arguments)); | ||
} | ||
_createClass(ReactSVG, [{ | ||
key: 'render', | ||
value: function render() { | ||
var _this2 = this; | ||
var _props = this.props; | ||
var className = _props.className; | ||
var path = _props.path; | ||
var fallbackPath = _props.fallbackPath; | ||
return _react2.default.createElement('img', { | ||
className: className, | ||
'data-src': path, | ||
'data-fallback': fallbackPath, | ||
ref: function ref(img) { | ||
return _this2._img = img; | ||
} | ||
}); | ||
} | ||
}, { | ||
key: 'componentDidMount', | ||
value: function componentDidMount() { | ||
this.updateSVG(); | ||
} | ||
}, { | ||
key: 'componentDidUpdate', | ||
value: function componentDidUpdate() { | ||
this.updateSVG(); | ||
} | ||
}, { | ||
key: 'updateSVG', | ||
value: function updateSVG() { | ||
var _props2 = this.props; | ||
var evalScripts = _props2.evalScripts; | ||
var each = _props2.callback; | ||
(0, _svgInjector2.default)(this._img, { | ||
evalScripts: evalScripts, | ||
each: each | ||
}); | ||
} | ||
}]); | ||
return ReactSVG; | ||
}(_react.Component); | ||
ReactSVG.defaultProps = { | ||
evalScripts: 'never', | ||
callback: function callback() {} | ||
}; | ||
ReactSVG.propTypes = { | ||
path: _react.PropTypes.string.isRequired, | ||
className: _react.PropTypes.string, | ||
evalScripts: _react.PropTypes.oneOf(['always', 'once', 'never']), | ||
fallbackPath: _react.PropTypes.string, | ||
callback: _react.PropTypes.func | ||
}; | ||
exports.default = ReactSVG; | ||
/***/ }, | ||
/* 1 */ | ||
/***/ function(module, exports) { | ||
module.exports = __WEBPACK_EXTERNAL_MODULE_1__; | ||
/***/ }, | ||
/* 2 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
var __WEBPACK_AMD_DEFINE_RESULT__;/** | ||
* SVGInjector v1.1.3 - Fast, caching, dynamic inline SVG DOM injection library | ||
* https://github.com/iconic/SVGInjector | ||
* | ||
* Copyright (c) 2014-2015 Waybury <hello@waybury.com> | ||
* @license MIT | ||
*/ | ||
(function (window, document) { | ||
'use strict'; | ||
// Environment | ||
var isLocal = window.location.protocol === 'file:'; | ||
var hasSvgSupport = document.implementation.hasFeature('http://www.w3.org/TR/SVG11/feature#BasicStructure', '1.1'); | ||
function uniqueClasses(list) { | ||
list = list.split(' '); | ||
var hash = {}; | ||
var i = list.length; | ||
var out = []; | ||
while (i--) { | ||
if (!hash.hasOwnProperty(list[i])) { | ||
hash[list[i]] = 1; | ||
out.unshift(list[i]); | ||
} | ||
} | ||
return out.join(' '); | ||
} | ||
/** | ||
* cache (or polyfill for <= IE8) Array.forEach() | ||
* source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach | ||
*/ | ||
var forEach = Array.prototype.forEach || function (fn, scope) { | ||
if (this === void 0 || this === null || typeof fn !== 'function') { | ||
throw new TypeError(); | ||
} | ||
/* jshint bitwise: false */ | ||
var i, len = this.length >>> 0; | ||
/* jshint bitwise: true */ | ||
for (i = 0; i < len; ++i) { | ||
if (i in this) { | ||
fn.call(scope, this[i], i, this); | ||
} | ||
} | ||
}; | ||
// SVG Cache | ||
var svgCache = {}; | ||
var injectCount = 0; | ||
var injectedElements = []; | ||
// Request Queue | ||
var requestQueue = []; | ||
// Script running status | ||
var ranScripts = {}; | ||
var cloneSvg = function (sourceSvg) { | ||
return sourceSvg.cloneNode(true); | ||
}; | ||
var queueRequest = function (url, callback) { | ||
requestQueue[url] = requestQueue[url] || []; | ||
requestQueue[url].push(callback); | ||
}; | ||
var processRequestQueue = function (url) { | ||
for (var i = 0, len = requestQueue[url].length; i < len; i++) { | ||
// Make these calls async so we avoid blocking the page/renderer | ||
/* jshint loopfunc: true */ | ||
(function (index) { | ||
setTimeout(function () { | ||
requestQueue[url][index](cloneSvg(svgCache[url])); | ||
}, 0); | ||
})(i); | ||
/* jshint loopfunc: false */ | ||
} | ||
}; | ||
var loadSvg = function (url, callback) { | ||
if (svgCache[url] !== undefined) { | ||
if (svgCache[url] instanceof SVGSVGElement) { | ||
// We already have it in cache, so use it | ||
callback(cloneSvg(svgCache[url])); | ||
} | ||
else { | ||
// We don't have it in cache yet, but we are loading it, so queue this request | ||
queueRequest(url, callback); | ||
} | ||
} | ||
else { | ||
if (!window.XMLHttpRequest) { | ||
callback('Browser does not support XMLHttpRequest'); | ||
return false; | ||
} | ||
// Seed the cache to indicate we are loading this URL already | ||
svgCache[url] = {}; | ||
queueRequest(url, callback); | ||
var httpRequest = new XMLHttpRequest(); | ||
httpRequest.onreadystatechange = function () { | ||
// readyState 4 = complete | ||
if (httpRequest.readyState === 4) { | ||
// Handle status | ||
if (httpRequest.status === 404 || httpRequest.responseXML === null) { | ||
callback('Unable to load SVG file: ' + url); | ||
if (isLocal) callback('Note: SVG injection ajax calls do not work locally without adjusting security setting in your browser. Or consider using a local webserver.'); | ||
callback(); | ||
return false; | ||
} | ||
// 200 success from server, or 0 when using file:// protocol locally | ||
if (httpRequest.status === 200 || (isLocal && httpRequest.status === 0)) { | ||
/* globals Document */ | ||
if (httpRequest.responseXML instanceof Document) { | ||
// Cache it | ||
svgCache[url] = httpRequest.responseXML.documentElement; | ||
} | ||
/* globals -Document */ | ||
// IE9 doesn't create a responseXML Document object from loaded SVG, | ||
// and throws a "DOM Exception: HIERARCHY_REQUEST_ERR (3)" error when injected. | ||
// | ||
// So, we'll just create our own manually via the DOMParser using | ||
// the the raw XML responseText. | ||
// | ||
// :NOTE: IE8 and older doesn't have DOMParser, but they can't do SVG either, so... | ||
else if (DOMParser && (DOMParser instanceof Function)) { | ||
var xmlDoc; | ||
try { | ||
var parser = new DOMParser(); | ||
xmlDoc = parser.parseFromString(httpRequest.responseText, 'text/xml'); | ||
} | ||
catch (e) { | ||
xmlDoc = undefined; | ||
} | ||
if (!xmlDoc || xmlDoc.getElementsByTagName('parsererror').length) { | ||
callback('Unable to parse SVG file: ' + url); | ||
return false; | ||
} | ||
else { | ||
// Cache it | ||
svgCache[url] = xmlDoc.documentElement; | ||
} | ||
} | ||
// We've loaded a new asset, so process any requests waiting for it | ||
processRequestQueue(url); | ||
} | ||
else { | ||
callback('There was a problem injecting the SVG: ' + httpRequest.status + ' ' + httpRequest.statusText); | ||
return false; | ||
} | ||
} | ||
}; | ||
httpRequest.open('GET', url); | ||
// Treat and parse the response as XML, even if the | ||
// server sends us a different mimetype | ||
if (httpRequest.overrideMimeType) httpRequest.overrideMimeType('text/xml'); | ||
httpRequest.send(); | ||
} | ||
}; | ||
// Inject a single element | ||
var injectElement = function (el, evalScripts, pngFallback, callback) { | ||
// Grab the src or data-src attribute | ||
var imgUrl = el.getAttribute('data-src') || el.getAttribute('src'); | ||
// We can only inject SVG | ||
if (!(/\.svg/i).test(imgUrl)) { | ||
callback('Attempted to inject a file with a non-svg extension: ' + imgUrl); | ||
return; | ||
} | ||
// If we don't have SVG support try to fall back to a png, | ||
// either defined per-element via data-fallback or data-png, | ||
// or globally via the pngFallback directory setting | ||
if (!hasSvgSupport) { | ||
var perElementFallback = el.getAttribute('data-fallback') || el.getAttribute('data-png'); | ||
// Per-element specific PNG fallback defined, so use that | ||
if (perElementFallback) { | ||
el.setAttribute('src', perElementFallback); | ||
callback(null); | ||
} | ||
// Global PNG fallback directoriy defined, use the same-named PNG | ||
else if (pngFallback) { | ||
el.setAttribute('src', pngFallback + '/' + imgUrl.split('/').pop().replace('.svg', '.png')); | ||
callback(null); | ||
} | ||
// um... | ||
else { | ||
callback('This browser does not support SVG and no PNG fallback was defined.'); | ||
} | ||
return; | ||
} | ||
// Make sure we aren't already in the process of injecting this element to | ||
// avoid a race condition if multiple injections for the same element are run. | ||
// :NOTE: Using indexOf() only _after_ we check for SVG support and bail, | ||
// so no need for IE8 indexOf() polyfill | ||
if (injectedElements.indexOf(el) !== -1) { | ||
return; | ||
} | ||
// Remember the request to inject this element, in case other injection | ||
// calls are also trying to replace this element before we finish | ||
injectedElements.push(el); | ||
// Try to avoid loading the orginal image src if possible. | ||
el.setAttribute('src', ''); | ||
// Load it up | ||
loadSvg(imgUrl, function (svg) { | ||
if (typeof svg === 'undefined' || typeof svg === 'string') { | ||
callback(svg); | ||
return false; | ||
} | ||
var imgId = el.getAttribute('id'); | ||
if (imgId) { | ||
svg.setAttribute('id', imgId); | ||
} | ||
var imgTitle = el.getAttribute('title'); | ||
if (imgTitle) { | ||
svg.setAttribute('title', imgTitle); | ||
} | ||
// Concat the SVG classes + 'injected-svg' + the img classes | ||
var classMerge = [].concat(svg.getAttribute('class') || [], 'injected-svg', el.getAttribute('class') || []).join(' '); | ||
svg.setAttribute('class', uniqueClasses(classMerge)); | ||
var imgStyle = el.getAttribute('style'); | ||
if (imgStyle) { | ||
svg.setAttribute('style', imgStyle); | ||
} | ||
// Copy all the data elements to the svg | ||
var imgData = [].filter.call(el.attributes, function (at) { | ||
return (/^data-\w[\w\-]*$/).test(at.name); | ||
}); | ||
forEach.call(imgData, function (dataAttr) { | ||
if (dataAttr.name && dataAttr.value) { | ||
svg.setAttribute(dataAttr.name, dataAttr.value); | ||
} | ||
}); | ||
// Make sure any internally referenced clipPath ids and their | ||
// clip-path references are unique. | ||
// | ||
// This addresses the issue of having multiple instances of the | ||
// same SVG on a page and only the first clipPath id is referenced. | ||
// | ||
// Browsers often shortcut the SVG Spec and don't use clipPaths | ||
// contained in parent elements that are hidden, so if you hide the first | ||
// SVG instance on the page, then all other instances lose their clipping. | ||
// Reference: https://bugzilla.mozilla.org/show_bug.cgi?id=376027 | ||
// Handle all defs elements that have iri capable attributes as defined by w3c: http://www.w3.org/TR/SVG/linking.html#processingIRI | ||
// Mapping IRI addressable elements to the properties that can reference them: | ||
var iriElementsAndProperties = { | ||
'clipPath': ['clip-path'], | ||
'color-profile': ['color-profile'], | ||
'cursor': ['cursor'], | ||
'filter': ['filter'], | ||
'linearGradient': ['fill', 'stroke'], | ||
'marker': ['marker', 'marker-start', 'marker-mid', 'marker-end'], | ||
'mask': ['mask'], | ||
'pattern': ['fill', 'stroke'], | ||
'radialGradient': ['fill', 'stroke'] | ||
}; | ||
var element, elementDefs, properties, currentId, newId; | ||
Object.keys(iriElementsAndProperties).forEach(function (key) { | ||
element = key; | ||
properties = iriElementsAndProperties[key]; | ||
elementDefs = svg.querySelectorAll('defs ' + element + '[id]'); | ||
for (var i = 0, elementsLen = elementDefs.length; i < elementsLen; i++) { | ||
currentId = elementDefs[i].id; | ||
newId = currentId + '-' + injectCount; | ||
// All of the properties that can reference this element type | ||
var referencingElements; | ||
forEach.call(properties, function (property) { | ||
// :NOTE: using a substring match attr selector here to deal with IE "adding extra quotes in url() attrs" | ||
referencingElements = svg.querySelectorAll('[' + property + '*="' + currentId + '"]'); | ||
for (var j = 0, referencingElementLen = referencingElements.length; j < referencingElementLen; j++) { | ||
referencingElements[j].setAttribute(property, 'url(#' + newId + ')'); | ||
} | ||
}); | ||
elementDefs[i].id = newId; | ||
} | ||
}); | ||
// Remove any unwanted/invalid namespaces that might have been added by SVG editing tools | ||
svg.removeAttribute('xmlns:a'); | ||
// Post page load injected SVGs don't automatically have their script | ||
// elements run, so we'll need to make that happen, if requested | ||
// Find then prune the scripts | ||
var scripts = svg.querySelectorAll('script'); | ||
var scriptsToEval = []; | ||
var script, scriptType; | ||
for (var k = 0, scriptsLen = scripts.length; k < scriptsLen; k++) { | ||
scriptType = scripts[k].getAttribute('type'); | ||
// Only process javascript types. | ||
// SVG defaults to 'application/ecmascript' for unset types | ||
if (!scriptType || scriptType === 'application/ecmascript' || scriptType === 'application/javascript') { | ||
// innerText for IE, textContent for other browsers | ||
script = scripts[k].innerText || scripts[k].textContent; | ||
// Stash | ||
scriptsToEval.push(script); | ||
// Tidy up and remove the script element since we don't need it anymore | ||
svg.removeChild(scripts[k]); | ||
} | ||
} | ||
// Run/Eval the scripts if needed | ||
if (scriptsToEval.length > 0 && (evalScripts === 'always' || (evalScripts === 'once' && !ranScripts[imgUrl]))) { | ||
for (var l = 0, scriptsToEvalLen = scriptsToEval.length; l < scriptsToEvalLen; l++) { | ||
// :NOTE: Yup, this is a form of eval, but it is being used to eval code | ||
// the caller has explictely asked to be loaded, and the code is in a caller | ||
// defined SVG file... not raw user input. | ||
// | ||
// Also, the code is evaluated in a closure and not in the global scope. | ||
// If you need to put something in global scope, use 'window' | ||
new Function(scriptsToEval[l])(window); // jshint ignore:line | ||
} | ||
// Remember we already ran scripts for this svg | ||
ranScripts[imgUrl] = true; | ||
} | ||
// :WORKAROUND: | ||
// IE doesn't evaluate <style> tags in SVGs that are dynamically added to the page. | ||
// This trick will trigger IE to read and use any existing SVG <style> tags. | ||
// | ||
// Reference: https://github.com/iconic/SVGInjector/issues/23 | ||
var styleTags = svg.querySelectorAll('style'); | ||
forEach.call(styleTags, function (styleTag) { | ||
styleTag.textContent += ''; | ||
}); | ||
// Replace the image with the svg | ||
el.parentNode.replaceChild(svg, el); | ||
// Now that we no longer need it, drop references | ||
// to the original element so it can be GC'd | ||
delete injectedElements[injectedElements.indexOf(el)]; | ||
el = null; | ||
// Increment the injected count | ||
injectCount++; | ||
callback(svg); | ||
}); | ||
}; | ||
/** | ||
* SVGInjector | ||
* | ||
* Replace the given elements with their full inline SVG DOM elements. | ||
* | ||
* :NOTE: We are using get/setAttribute with SVG because the SVG DOM spec differs from HTML DOM and | ||
* can return other unexpected object types when trying to directly access svg properties. | ||
* ex: "className" returns a SVGAnimatedString with the class value found in the "baseVal" property, | ||
* instead of simple string like with HTML Elements. | ||
* | ||
* @param {mixes} Array of or single DOM element | ||
* @param {object} options | ||
* @param {function} callback | ||
* @return {object} Instance of SVGInjector | ||
*/ | ||
var SVGInjector = function (elements, options, done) { | ||
// Options & defaults | ||
options = options || {}; | ||
// Should we run the scripts blocks found in the SVG | ||
// 'always' - Run them every time | ||
// 'once' - Only run scripts once for each SVG | ||
// [false|'never'] - Ignore scripts | ||
var evalScripts = options.evalScripts || 'always'; | ||
// Location of fallback pngs, if desired | ||
var pngFallback = options.pngFallback || false; | ||
// Callback to run during each SVG injection, returning the SVG injected | ||
var eachCallback = options.each; | ||
// Do the injection... | ||
if (elements.length !== undefined) { | ||
var elementsLoaded = 0; | ||
forEach.call(elements, function (element) { | ||
injectElement(element, evalScripts, pngFallback, function (svg) { | ||
if (eachCallback && typeof eachCallback === 'function') eachCallback(svg); | ||
if (done && elements.length === ++elementsLoaded) done(elementsLoaded); | ||
}); | ||
}); | ||
} | ||
else { | ||
if (elements) { | ||
injectElement(elements, evalScripts, pngFallback, function (svg) { | ||
if (eachCallback && typeof eachCallback === 'function') eachCallback(svg); | ||
if (done) done(1); | ||
elements = null; | ||
}); | ||
} | ||
else { | ||
if (done) done(0); | ||
} | ||
} | ||
}; | ||
/* global module, exports: true, define */ | ||
// Node.js or CommonJS | ||
if (typeof module === 'object' && typeof module.exports === 'object') { | ||
module.exports = exports = SVGInjector; | ||
} | ||
// AMD support | ||
else if (true) { | ||
!(__WEBPACK_AMD_DEFINE_RESULT__ = function () { | ||
return SVGInjector; | ||
}.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); | ||
} | ||
// Otherwise, attach to window as global | ||
else if (typeof window === 'object') { | ||
window.SVGInjector = SVGInjector; | ||
} | ||
/* global -module, -exports, -define */ | ||
}(window, document)); | ||
/***/ } | ||
/******/ ]) | ||
}); | ||
; | ||
ReactSVG.defaultProps = { | ||
callback: function callback() {}, | ||
className: '', | ||
evalScripts: 'once' | ||
}; | ||
ReactSVG.propTypes = { | ||
callback: _react.PropTypes.func, | ||
className: _react.PropTypes.string, | ||
evalScripts: _react.PropTypes.oneOf(['always', 'once', 'never']), | ||
path: _react.PropTypes.string.isRequired | ||
}; | ||
exports.default = ReactSVG; |
{ | ||
"name": "react-svg", | ||
"version": "1.1.5", | ||
"version": "2.0.0", | ||
"description": "A react.js svg component for loading svg files into the dom.", | ||
@@ -8,6 +8,6 @@ "main": "lib/index.js", | ||
"clean": "rimraf dist lib", | ||
"lint": "eslint bin example src test", | ||
"lint": "eslint .", | ||
"test": "npm run lint && babel-node ./bin/test.js", | ||
"start": "npm run build:lib && babel-node ./bin/start.js", | ||
"build:lib": "babel-node ./bin/build.js lib", | ||
"start": "babel-node ./bin/start.js", | ||
"build:lib": "babel src -d lib", | ||
"build:umd": "babel-node ./bin/build.js umd", | ||
@@ -17,3 +17,3 @@ "build:umd:min": "babel-node ./bin/build.js umd:min", | ||
"preversion": "npm test", | ||
"version": "npm run build && git add -A dist", | ||
"version": "npm run build:lib", | ||
"postversion": "git push && git push --tags && npm publish", | ||
@@ -45,5 +45,5 @@ "release": "npm version -m 'Release v%s'" | ||
"babel-cli": "^6.16.0", | ||
"babel-eslint": "^7.0.0", | ||
"babel-clibel-eslint": "^7.0.0", | ||
"babel-loader": "^6.2.5", | ||
"babel-preset-es2015": "^6.16.0", | ||
"babel-preset-es2015": "^6.18.0", | ||
"babel-preset-react": "^6.16.0", | ||
@@ -53,2 +53,3 @@ "babel-preset-stage-1": "^6.16.0", | ||
"eslint": "^3.8.0", | ||
"eslint-config-rackt": "^1.1.1", | ||
"eslint-plugin-react": "^6.4.1", | ||
@@ -55,0 +56,0 @@ "karma": "^1.3.0", |
@@ -11,14 +11,14 @@ # react-svg | ||
```js | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import ReactSVG from 'react-svg'; | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import ReactSVG from 'react-svg' | ||
ReactDOM.render( | ||
<ReactSVG | ||
path={'atomic.svg'} | ||
className={'example'} | ||
callback={(svg) => console.log(svg)} | ||
path="atomic.svg" | ||
callback={svg => console.log(svg)} | ||
className="example" | ||
/>, | ||
document.querySelector('.Root') | ||
); | ||
) | ||
``` | ||
@@ -33,6 +33,5 @@ | ||
- `path` - Path to the SVG. | ||
- `className` - *Optional* Class name to be added to the SVG. | ||
- `callback` - *Optional* Function to call after the SVG is injected. Receives the newly injected SVG DOM element as a parameter. Defaults to `null`. | ||
- `className` - *Optional* Class name to be added to the SVG. Defaults to `''`. | ||
- `evalScripts` - *Optional* Run any script blocks found in the SVG (`always`, `once`, or `never`). Defaults to `never`. | ||
- `fallbackPath` - *Optional* Path to the fallback PNG. | ||
- `callback` - *Optional* Function to call after the SVG is injected. Receives the newly injected SVG DOM element as a parameter. Defaults to `null`. | ||
@@ -43,7 +42,6 @@ __Example__ | ||
<ReactSVG | ||
path={'atomic.svg'} | ||
className={'example'} | ||
evalScript={'always'} | ||
fallbackPath={'atomic.png'} | ||
path="atomic.svg" | ||
callback={(svg) => console.log(svg)} | ||
className="example" | ||
evalScript="always" | ||
/> | ||
@@ -60,6 +58,11 @@ ``` | ||
There are also UMD builds available in the `dist` directory. If you use these, make sure you have already included React as a dependency. | ||
There are also UMD builds available via [unpkg](https://unpkg.com/): | ||
- https://unpkg.com/react-svg/dist/react-svg.js | ||
- https://unpkg.com/react-svg/dist/react-svg.min.js | ||
If you use these, make sure you have already included React as a dependency. | ||
## License | ||
MIT |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
0
65
1
11560
23
8
89