react-inlinesvg 2.2.2 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/index.js CHANGED
@@ -6,6 +6,8 @@ var __extends = (this && this.__extends) || (function () {
6
6
  return extendStatics(d, b);
7
7
  };
8
8
  return function (d, b) {
9
+ if (typeof b !== "function" && b !== null)
10
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
9
11
  extendStatics(d, b);
10
12
  function __() { this.constructor = d; }
11
13
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
@@ -38,102 +40,132 @@ var __read = (this && this.__read) || function (o, n) {
38
40
  }
39
41
  return ar;
40
42
  };
41
- var __spread = (this && this.__spread) || function () {
42
- for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
43
- return ar;
43
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
44
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
45
+ if (ar || !(i in from)) {
46
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
47
+ ar[i] = from[i];
48
+ }
49
+ }
50
+ return to.concat(ar || Array.prototype.slice.call(from));
44
51
  };
45
52
  import * as React from 'react';
46
53
  import convert from 'react-from-dom';
47
- import { STATUS, canUseDOM, isSupportedEnvironment, randomString, removeProperties, } from './helpers';
48
- var cacheStore = Object.create(null);
54
+ import { canUseDOM, isSupportedEnvironment, omit, randomString, STATUS } from './helpers';
55
+ export var cacheStore = Object.create(null);
49
56
  var InlineSVG = /** @class */ (function (_super) {
50
57
  __extends(InlineSVG, _super);
51
58
  function InlineSVG(props) {
52
59
  var _this = _super.call(this, props) || this;
53
- _this.isActive = false;
54
- _this.handleCacheQueue = function (content) {
55
- /* istanbul ignore else */
56
- if (typeof content === 'string') {
57
- _this.handleLoad(content);
58
- return;
59
- }
60
- _this.handleError(content);
61
- };
62
- _this.handleLoad = function (content) {
63
- /* istanbul ignore else */
64
- if (_this.isActive) {
65
- _this.setState({
66
- content: content,
67
- status: STATUS.LOADED,
68
- }, _this.getElement);
69
- }
70
- };
71
- _this.handleError = function (error) {
72
- var onError = _this.props.onError;
73
- var status = error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;
74
- /* istanbul ignore else */
75
- if (_this.isActive) {
76
- _this.setState({ status: status }, function () {
77
- /* istanbul ignore else */
78
- if (typeof onError === 'function') {
79
- onError(error);
80
- }
81
- });
60
+ Object.defineProperty(_this, "isInitialized", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: false
65
+ });
66
+ Object.defineProperty(_this, "isActive", {
67
+ enumerable: true,
68
+ configurable: true,
69
+ writable: true,
70
+ value: false
71
+ });
72
+ Object.defineProperty(_this, "hash", {
73
+ enumerable: true,
74
+ configurable: true,
75
+ writable: true,
76
+ value: void 0
77
+ });
78
+ Object.defineProperty(_this, "handleLoad", {
79
+ enumerable: true,
80
+ configurable: true,
81
+ writable: true,
82
+ value: function (content) {
83
+ /* istanbul ignore else */
84
+ if (_this.isActive) {
85
+ _this.setState({
86
+ content: content,
87
+ status: STATUS.LOADED,
88
+ }, _this.getElement);
89
+ }
82
90
  }
83
- };
84
- _this.request = function () {
85
- var _a = _this.props, cacheRequests = _a.cacheRequests, src = _a.src;
86
- try {
87
- if (cacheRequests) {
88
- cacheStore[src] = { content: '', status: STATUS.LOADING, queue: [] };
91
+ });
92
+ Object.defineProperty(_this, "handleError", {
93
+ enumerable: true,
94
+ configurable: true,
95
+ writable: true,
96
+ value: function (error) {
97
+ var onError = _this.props.onError;
98
+ var status = error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;
99
+ /* istanbul ignore else */
100
+ if (_this.isActive) {
101
+ _this.setState({ status: status }, function () {
102
+ /* istanbul ignore else */
103
+ if (typeof onError === 'function') {
104
+ onError(error);
105
+ }
106
+ });
89
107
  }
90
- return fetch(src)
91
- .then(function (response) {
92
- var contentType = response.headers.get('content-type');
93
- var _a = __read((contentType || '').split(/ ?; ?/), 1), fileType = _a[0];
94
- if (response.status > 299) {
95
- throw new Error('Not found');
96
- }
97
- if (!['image/svg+xml', 'text/plain'].some(function (d) { return fileType.indexOf(d) >= 0; })) {
98
- throw new Error("Content type isn't valid: " + fileType);
99
- }
100
- return response.text();
101
- })
102
- .then(function (content) {
103
- _this.handleLoad(content);
104
- /* istanbul ignore else */
108
+ }
109
+ });
110
+ Object.defineProperty(_this, "request", {
111
+ enumerable: true,
112
+ configurable: true,
113
+ writable: true,
114
+ value: function () {
115
+ var _a = _this.props, cacheRequests = _a.cacheRequests, fetchOptions = _a.fetchOptions, src = _a.src;
116
+ try {
105
117
  if (cacheRequests) {
106
- var cache = cacheStore[src];
118
+ cacheStore[src] = { content: '', status: STATUS.LOADING };
119
+ }
120
+ return fetch(src, fetchOptions)
121
+ .then(function (response) {
122
+ var contentType = response.headers.get('content-type');
123
+ var _a = __read((contentType || '').split(/ ?; ?/), 1), fileType = _a[0];
124
+ if (response.status > 299) {
125
+ throw new Error('Not found');
126
+ }
127
+ if (!['image/svg+xml', 'text/plain'].some(function (d) { return fileType.includes(d); })) {
128
+ throw new Error("Content type isn't valid: ".concat(fileType));
129
+ }
130
+ return response.text();
131
+ })
132
+ .then(function (content) {
133
+ var currentSrc = _this.props.src;
134
+ // the current src don't match the previous one, skipping...
135
+ if (src !== currentSrc) {
136
+ if (cacheStore[src].status === STATUS.LOADING) {
137
+ delete cacheStore[src];
138
+ }
139
+ return;
140
+ }
141
+ _this.handleLoad(content);
107
142
  /* istanbul ignore else */
108
- if (cache) {
109
- cache.content = content;
110
- cache.status = STATUS.LOADED;
111
- cache.queue = cache.queue.filter(function (cb) {
112
- cb(content);
113
- return false;
114
- });
143
+ if (cacheRequests) {
144
+ var cache = cacheStore[src];
145
+ /* istanbul ignore else */
146
+ if (cache) {
147
+ cache.content = content;
148
+ cache.status = STATUS.LOADED;
149
+ }
115
150
  }
116
- }
117
- })
118
- .catch(function (error) {
119
- _this.handleError(error);
120
- /* istanbul ignore else */
121
- if (cacheRequests) {
122
- var cache = cacheStore[src];
151
+ })
152
+ .catch(function (error) {
153
+ _this.handleError(error);
123
154
  /* istanbul ignore else */
124
- if (cache) {
125
- cache.queue.forEach(function (cb) {
126
- cb(error);
127
- });
128
- delete cacheStore[src];
155
+ if (cacheRequests) {
156
+ var cache = cacheStore[src];
157
+ /* istanbul ignore else */
158
+ if (cache) {
159
+ delete cacheStore[src];
160
+ }
129
161
  }
130
- }
131
- });
132
- }
133
- catch (error) {
134
- return _this.handleError(new Error(error.message));
162
+ });
163
+ }
164
+ catch (error) {
165
+ return _this.handleError(new Error(error.message));
166
+ }
135
167
  }
136
- };
168
+ });
137
169
  _this.state = {
138
170
  content: '',
139
171
  element: null,
@@ -143,202 +175,247 @@ var InlineSVG = /** @class */ (function (_super) {
143
175
  _this.hash = props.uniqueHash || randomString(8);
144
176
  return _this;
145
177
  }
146
- InlineSVG.prototype.componentDidMount = function () {
147
- this.isActive = true;
148
- if (!canUseDOM()) {
149
- return;
150
- }
151
- var status = this.state.status;
152
- var src = this.props.src;
153
- try {
154
- /* istanbul ignore else */
155
- if (status === STATUS.PENDING) {
156
- /* istanbul ignore else */
157
- if (!isSupportedEnvironment()) {
158
- throw new Error('Browser does not support SVG');
159
- }
178
+ Object.defineProperty(InlineSVG.prototype, "componentDidMount", {
179
+ enumerable: false,
180
+ configurable: true,
181
+ writable: true,
182
+ value: function () {
183
+ this.isActive = true;
184
+ if (!canUseDOM() || this.isInitialized) {
185
+ return;
186
+ }
187
+ var status = this.state.status;
188
+ var src = this.props.src;
189
+ try {
160
190
  /* istanbul ignore else */
161
- if (!src) {
162
- throw new Error('Missing src');
191
+ if (status === STATUS.PENDING) {
192
+ /* istanbul ignore else */
193
+ if (!isSupportedEnvironment()) {
194
+ throw new Error('Browser does not support SVG');
195
+ }
196
+ /* istanbul ignore else */
197
+ if (!src) {
198
+ throw new Error('Missing src');
199
+ }
200
+ this.load();
163
201
  }
164
- this.load();
165
202
  }
166
- }
167
- catch (error) {
168
- this.handleError(error);
169
- }
170
- };
171
- InlineSVG.prototype.componentDidUpdate = function (prevProps, prevState) {
172
- if (!canUseDOM()) {
173
- return;
174
- }
175
- var _a = this.state, hasCache = _a.hasCache, status = _a.status;
176
- var _b = this.props, onLoad = _b.onLoad, src = _b.src;
177
- if (prevState.status !== STATUS.READY && status === STATUS.READY) {
178
- /* istanbul ignore else */
179
- if (onLoad) {
180
- onLoad(src, hasCache);
203
+ catch (error) {
204
+ this.handleError(error);
181
205
  }
206
+ this.isInitialized = true;
182
207
  }
183
- if (prevProps.src !== src) {
184
- if (!src) {
185
- this.handleError(new Error('Missing src'));
208
+ });
209
+ Object.defineProperty(InlineSVG.prototype, "componentDidUpdate", {
210
+ enumerable: false,
211
+ configurable: true,
212
+ writable: true,
213
+ value: function (previousProps, previousState) {
214
+ if (!canUseDOM()) {
186
215
  return;
187
216
  }
188
- this.load();
189
- }
190
- };
191
- InlineSVG.prototype.componentWillUnmount = function () {
192
- this.isActive = false;
193
- };
194
- InlineSVG.prototype.processSVG = function () {
195
- var content = this.state.content;
196
- var preProcessor = this.props.preProcessor;
197
- if (preProcessor) {
198
- return preProcessor(content);
217
+ var _a = this.state, hasCache = _a.hasCache, status = _a.status;
218
+ var _b = this.props, onLoad = _b.onLoad, src = _b.src;
219
+ if (previousState.status !== STATUS.READY && status === STATUS.READY) {
220
+ /* istanbul ignore else */
221
+ if (onLoad) {
222
+ onLoad(src, hasCache);
223
+ }
224
+ }
225
+ if (previousProps.src !== src) {
226
+ if (!src) {
227
+ this.handleError(new Error('Missing src'));
228
+ return;
229
+ }
230
+ this.load();
231
+ }
199
232
  }
200
- return content;
201
- };
202
- InlineSVG.prototype.updateSVGAttributes = function (node) {
203
- var _this = this;
204
- var _a = this.props, _b = _a.baseURL, baseURL = _b === void 0 ? '' : _b, uniquifyIDs = _a.uniquifyIDs;
205
- var replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];
206
- var linkAttributes = ['href', 'xlink:href'];
207
- var isDataValue = function (name, value) {
208
- return linkAttributes.indexOf(name) >= 0 && (value ? value.indexOf('#') < 0 : false);
209
- };
210
- if (!uniquifyIDs) {
211
- return node;
233
+ });
234
+ Object.defineProperty(InlineSVG.prototype, "componentWillUnmount", {
235
+ enumerable: false,
236
+ configurable: true,
237
+ writable: true,
238
+ value: function () {
239
+ this.isActive = false;
212
240
  }
213
- __spread(node.children).map(function (d) {
214
- if (d.attributes && d.attributes.length) {
215
- var attributes_1 = Object.values(d.attributes).map(function (a) {
216
- var attr = a;
217
- var match = a.value.match(/url\((.*?)\)/);
218
- if (match && match[1]) {
219
- attr.value = a.value.replace(match[0], "url(" + baseURL + match[1] + "__" + _this.hash + ")");
241
+ });
242
+ Object.defineProperty(InlineSVG.prototype, "getNode", {
243
+ enumerable: false,
244
+ configurable: true,
245
+ writable: true,
246
+ value: function () {
247
+ var _a = this.props, description = _a.description, title = _a.title;
248
+ try {
249
+ var svgText = this.processSVG();
250
+ var node = convert(svgText, { nodeOnly: true });
251
+ if (!node || !(node instanceof SVGSVGElement)) {
252
+ throw new Error('Could not convert the src to a DOM Node');
253
+ }
254
+ var svg = this.updateSVGAttributes(node);
255
+ if (description) {
256
+ var originalDesc = svg.querySelector('desc');
257
+ if (originalDesc && originalDesc.parentNode) {
258
+ originalDesc.parentNode.removeChild(originalDesc);
220
259
  }
221
- return attr;
222
- });
223
- replaceableAttributes.forEach(function (r) {
224
- var attribute = attributes_1.find(function (a) { return a.name === r; });
225
- if (attribute && !isDataValue(r, attribute.value)) {
226
- attribute.value = attribute.value + "__" + _this.hash;
260
+ var descElement = document.createElement('desc');
261
+ descElement.innerHTML = description;
262
+ svg.prepend(descElement);
263
+ }
264
+ if (title) {
265
+ var originalTitle = svg.querySelector('title');
266
+ if (originalTitle && originalTitle.parentNode) {
267
+ originalTitle.parentNode.removeChild(originalTitle);
227
268
  }
228
- });
229
- }
230
- if (d.children.length) {
231
- return _this.updateSVGAttributes(d);
232
- }
233
- return d;
234
- });
235
- return node;
236
- };
237
- InlineSVG.prototype.getNode = function () {
238
- var _a = this.props, description = _a.description, title = _a.title;
239
- try {
240
- var svgText = this.processSVG();
241
- var node = convert(svgText, { nodeOnly: true });
242
- if (!node || !(node instanceof SVGSVGElement)) {
243
- throw new Error('Could not convert the src to a DOM Node');
244
- }
245
- var svg = this.updateSVGAttributes(node);
246
- if (description) {
247
- var originalDesc = svg.querySelector('desc');
248
- if (originalDesc && originalDesc.parentNode) {
249
- originalDesc.parentNode.removeChild(originalDesc);
269
+ var titleElement = document.createElement('title');
270
+ titleElement.innerHTML = title;
271
+ svg.prepend(titleElement);
250
272
  }
251
- var descElement = document.createElement('desc');
252
- descElement.innerHTML = description;
253
- svg.prepend(descElement);
273
+ return svg;
254
274
  }
255
- if (title) {
256
- var originalTitle = svg.querySelector('title');
257
- if (originalTitle && originalTitle.parentNode) {
258
- originalTitle.parentNode.removeChild(originalTitle);
259
- }
260
- var titleElement = document.createElement('title');
261
- titleElement.innerHTML = title;
262
- svg.prepend(titleElement);
275
+ catch (error) {
276
+ return this.handleError(error);
263
277
  }
264
- return svg;
265
- }
266
- catch (error) {
267
- return this.handleError(error);
268
278
  }
269
- };
270
- InlineSVG.prototype.getElement = function () {
271
- try {
272
- var node = this.getNode();
273
- var element = convert(node);
274
- if (!element || !React.isValidElement(element)) {
275
- throw new Error('Could not convert the src to a React element');
279
+ });
280
+ Object.defineProperty(InlineSVG.prototype, "getElement", {
281
+ enumerable: false,
282
+ configurable: true,
283
+ writable: true,
284
+ value: function () {
285
+ try {
286
+ var node = this.getNode();
287
+ var element = convert(node);
288
+ if (!element || !React.isValidElement(element)) {
289
+ throw new Error('Could not convert the src to a React element');
290
+ }
291
+ this.setState({
292
+ element: element,
293
+ status: STATUS.READY,
294
+ });
295
+ }
296
+ catch (error) {
297
+ this.handleError(new Error(error.message));
276
298
  }
277
- this.setState({
278
- element: element,
279
- status: STATUS.READY,
280
- });
281
- }
282
- catch (error) {
283
- this.handleError(new Error(error.message));
284
299
  }
285
- };
286
- InlineSVG.prototype.load = function () {
287
- var _this = this;
288
- /* istanbul ignore else */
289
- if (this.isActive) {
290
- this.setState({
291
- content: '',
292
- element: null,
293
- status: STATUS.LOADING,
294
- }, function () {
295
- var _a = _this.props, cacheRequests = _a.cacheRequests, src = _a.src;
296
- var cache = cacheRequests && cacheStore[src];
297
- if (cache) {
298
- /* istanbul ignore else */
299
- if (cache.status === STATUS.LOADING) {
300
- cache.queue.push(_this.handleCacheQueue);
301
- }
302
- else if (cache.status === STATUS.LOADED) {
300
+ });
301
+ Object.defineProperty(InlineSVG.prototype, "load", {
302
+ enumerable: false,
303
+ configurable: true,
304
+ writable: true,
305
+ value: function () {
306
+ var _this = this;
307
+ /* istanbul ignore else */
308
+ if (this.isActive) {
309
+ this.setState({
310
+ content: '',
311
+ element: null,
312
+ status: STATUS.LOADING,
313
+ }, function () {
314
+ var _a = _this.props, cacheRequests = _a.cacheRequests, src = _a.src;
315
+ var cache = cacheRequests && cacheStore[src];
316
+ if (cache && cache.status === STATUS.LOADED) {
303
317
  _this.handleLoad(cache.content);
318
+ return;
304
319
  }
305
- return;
306
- }
307
- var dataURI = src.match(/data:image\/svg[^,]*?(;base64)?,(.*)/);
308
- var inlineSrc;
309
- if (dataURI) {
310
- inlineSrc = dataURI[1] ? atob(dataURI[2]) : decodeURIComponent(dataURI[2]);
311
- }
312
- else if (src.indexOf('<svg') >= 0) {
313
- inlineSrc = src;
320
+ var dataURI = src.match(/data:image\/svg[^,]*?(;base64)?,(.*)/);
321
+ var inlineSrc;
322
+ if (dataURI) {
323
+ inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);
324
+ }
325
+ else if (src.includes('<svg')) {
326
+ inlineSrc = src;
327
+ }
328
+ if (inlineSrc) {
329
+ _this.handleLoad(inlineSrc);
330
+ return;
331
+ }
332
+ _this.request();
333
+ });
334
+ }
335
+ }
336
+ });
337
+ Object.defineProperty(InlineSVG.prototype, "updateSVGAttributes", {
338
+ enumerable: false,
339
+ configurable: true,
340
+ writable: true,
341
+ value: function (node) {
342
+ var _this = this;
343
+ var _a = this.props, _b = _a.baseURL, baseURL = _b === void 0 ? '' : _b, uniquifyIDs = _a.uniquifyIDs;
344
+ var replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];
345
+ var linkAttributes = ['href', 'xlink:href'];
346
+ var isDataValue = function (name, value) {
347
+ return linkAttributes.includes(name) && (value ? !value.includes('#') : false);
348
+ };
349
+ if (!uniquifyIDs) {
350
+ return node;
351
+ }
352
+ __spreadArray([], __read(node.children), false).map(function (d) {
353
+ if (d.attributes && d.attributes.length) {
354
+ var attributes_1 = Object.values(d.attributes).map(function (a) {
355
+ var attribute = a;
356
+ var match = a.value.match(/url\((.*?)\)/);
357
+ if (match && match[1]) {
358
+ attribute.value = a.value.replace(match[0], "url(".concat(baseURL).concat(match[1], "__").concat(_this.hash, ")"));
359
+ }
360
+ return attribute;
361
+ });
362
+ replaceableAttributes.forEach(function (r) {
363
+ var attribute = attributes_1.find(function (a) { return a.name === r; });
364
+ if (attribute && !isDataValue(r, attribute.value)) {
365
+ attribute.value = "".concat(attribute.value, "__").concat(_this.hash);
366
+ }
367
+ });
314
368
  }
315
- if (inlineSrc) {
316
- _this.handleLoad(inlineSrc);
317
- return;
369
+ if (d.children.length) {
370
+ return _this.updateSVGAttributes(d);
318
371
  }
319
- _this.request();
372
+ return d;
320
373
  });
374
+ return node;
321
375
  }
322
- };
323
- InlineSVG.prototype.render = function () {
324
- var _a = this.state, element = _a.element, status = _a.status;
325
- var _b = this.props, _c = _b.children, children = _c === void 0 ? null : _c, innerRef = _b.innerRef, _d = _b.loader, loader = _d === void 0 ? null : _d;
326
- var elementProps = removeProperties(this.props, 'baseURL', 'cacheRequests', 'children', 'description', 'innerRef', 'loader', 'onError', 'onLoad', 'preProcessor', 'src', 'title', 'uniqueHash', 'uniquifyIDs');
327
- if (!canUseDOM()) {
328
- return loader;
376
+ });
377
+ Object.defineProperty(InlineSVG.prototype, "processSVG", {
378
+ enumerable: false,
379
+ configurable: true,
380
+ writable: true,
381
+ value: function () {
382
+ var content = this.state.content;
383
+ var preProcessor = this.props.preProcessor;
384
+ if (preProcessor) {
385
+ return preProcessor(content);
386
+ }
387
+ return content;
329
388
  }
330
- if (element) {
331
- return React.cloneElement(element, __assign({ ref: innerRef }, elementProps));
389
+ });
390
+ Object.defineProperty(InlineSVG.prototype, "render", {
391
+ enumerable: false,
392
+ configurable: true,
393
+ writable: true,
394
+ value: function () {
395
+ var _a = this.state, element = _a.element, status = _a.status;
396
+ var _b = this.props, _c = _b.children, children = _c === void 0 ? null : _c, innerRef = _b.innerRef, _d = _b.loader, loader = _d === void 0 ? null : _d;
397
+ var elementProps = omit(this.props, 'baseURL', 'cacheRequests', 'children', 'description', 'fetchOptions', 'innerRef', 'loader', 'onError', 'onLoad', 'preProcessor', 'src', 'title', 'uniqueHash', 'uniquifyIDs');
398
+ if (!canUseDOM()) {
399
+ return loader;
400
+ }
401
+ if (element) {
402
+ return React.cloneElement(element, __assign({ ref: innerRef }, elementProps));
403
+ }
404
+ if ([STATUS.UNSUPPORTED, STATUS.FAILED].includes(status)) {
405
+ return children;
406
+ }
407
+ return loader;
332
408
  }
333
- if ([STATUS.UNSUPPORTED, STATUS.FAILED].indexOf(status) > -1) {
334
- return children;
409
+ });
410
+ Object.defineProperty(InlineSVG, "defaultProps", {
411
+ enumerable: true,
412
+ configurable: true,
413
+ writable: true,
414
+ value: {
415
+ cacheRequests: true,
416
+ uniquifyIDs: false,
335
417
  }
336
- return loader;
337
- };
338
- InlineSVG.defaultProps = {
339
- cacheRequests: true,
340
- uniquifyIDs: false,
341
- };
418
+ });
342
419
  return InlineSVG;
343
420
  }(React.PureComponent));
344
421
  export default InlineSVG;