1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 20x 19x 1x 2x 45x 2x 1x 1x 1x 16x 16x 4x 3x 3x 4x 1x 1x 1x 1x 1x 4x 12x 12x 7x 7x 7x 7x 4x 20x 20x 20x 20x 4x 16x 20x 3x 3x 17x 16x 16x 67x 67x 2x 67x 20x 20x 19x 1x 47x 6x 41x 67x 20x 47x 67x | import React from 'react'; import PropTypes from 'prop-types'; import httpplease from 'httpplease'; import ieXDomain from 'httpplease/plugins/oldiexdomain'; import { configurationError, isSupportedEnvironment, randomString, uniquifySVGIDs, unsupportedBrowserError, } from './utils'; const http = httpplease.use(ieXDomain); const Status = { PENDING: 'pending', LOADING: 'loading', LOADED: 'loaded', FAILED: 'failed', UNSUPPORTED: 'unsupported' }; const getRequestsByUrl = {}; const loadedIcons = {}; export default class InlineSVG extends React.PureComponent { constructor(props) { super(props); this.state = { status: Status.PENDING }; this.isActive = false; } static propTypes = { cacheGetRequests: PropTypes.bool, children: PropTypes.node, className: PropTypes.string, onError: PropTypes.func, onLoad: PropTypes.func, preloader: PropTypes.node, src: PropTypes.string.isRequired, style: PropTypes.object, supportTest: PropTypes.func, uniqueHash: PropTypes.string, uniquifyIDs: PropTypes.bool, wrapper: PropTypes.func }; static defaultProps = { cacheGetRequests: false, onLoad: () => {}, supportTest: isSupportedEnvironment, uniquifyIDs: true, uniqueHash: randomString(), wrapper: React.createFactory('span'), }; componentWillMount() { this.isActive = true; } componentDidMount() { /* istanbul ignore else */ if (this.state.status === Status.PENDING) { if (this.props.supportTest()) { if (this.props.src) { this.startLoad(); } else { this.fail(configurationError('Missing source')); } } else { this.fail(unsupportedBrowserError()); } } } componentDidUpdate(prevProps) { if (prevProps.src !== this.props.src) { if (this.props.src) { this.startLoad(); } else { this.fail(configurationError('Missing source')); } } } componentWillUnmount() { this.isActive = false; } getFile(callback) { const { cacheGetRequests, src } = this.props; if (cacheGetRequests) { if (loadedIcons[src]) { const [err, res] = loadedIcons[src]; setTimeout(() => callback(err, res, true), 0); } if (!getRequestsByUrl[src]) { getRequestsByUrl[src] = []; http.get(src, (err, res) => { getRequestsByUrl[src].forEach(cb => { loadedIcons[src] = [err, res]; cb(err, res); }); }); } getRequestsByUrl[src].push(callback); } else { http.get(src, (err, res) => { callback(err, res); }); } } fail(error) { const status = error.isUnsupportedBrowserError ? Status.UNSUPPORTED : Status.FAILED; /* istanbul ignore else */ if (this.isActive) { this.setState({ status }, () => { if (typeof this.props.onError === 'function') { this.props.onError(error); } }); } } startLoad() { /* istanbul ignore else */ if (this.isActive) { this.setState({ status: Status.LOADING }, this.load); } } load() { const match = this.props.src.match(/data:image\/svg[^,]*?(;base64)?,(.*)/); if (match) { return this.handleLoad(null, { text: match[1] ? atob(match[2]) : decodeURIComponent(match[2]) }); } return this.getFile(this.handleLoad); } handleLoad = (err, res, isCached = false) => { if (err) { this.fail(err); return; } if (this.isActive) { this.setState({ loadedText: res.text, status: Status.LOADED }, () => { this.props.onLoad(this.props.src, isCached); }); } }; getClassName() { let className = `isvg ${this.state.status}`; if (this.props.className) { className += ` ${this.props.className}`; } return className; } processSVG(svgText) { const { uniquifyIDs, uniqueHash } = this.props; if (uniquifyIDs) { return uniquifySVGIDs(svgText, uniqueHash); } return svgText; } renderContents() { switch (this.state.status) { case Status.UNSUPPORTED: case Status.FAILED: return this.props.children; default: return this.props.preloader; } } render() { let content; let html; if (this.state.loadedText) { html = { __html: this.processSVG(this.state.loadedText) }; } else { content = this.renderContents(); } return this.props.wrapper({ style: this.props.style, className: this.getClassName(), dangerouslySetInnerHTML: html, }, content); } } |