react-inlinesvg 3.0.0 → 3.0.2

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/lib/index.js CHANGED
@@ -74,83 +74,117 @@ var InlineSVG = /** @class */ (function (_super) {
74
74
  __extends(InlineSVG, _super);
75
75
  function InlineSVG(props) {
76
76
  var _this = _super.call(this, props) || this;
77
- _this.isActive = false;
78
- _this.handleLoad = function (content) {
79
- /* istanbul ignore else */
80
- if (_this.isActive) {
81
- _this.setState({
82
- content: content,
83
- status: helpers_1.STATUS.LOADED,
84
- }, _this.getElement);
85
- }
86
- };
87
- _this.handleError = function (error) {
88
- var onError = _this.props.onError;
89
- var status = error.message === 'Browser does not support SVG' ? helpers_1.STATUS.UNSUPPORTED : helpers_1.STATUS.FAILED;
90
- /* istanbul ignore else */
91
- if (_this.isActive) {
92
- _this.setState({ status: status }, function () {
93
- /* istanbul ignore else */
94
- if (typeof onError === 'function') {
95
- onError(error);
96
- }
97
- });
77
+ Object.defineProperty(_this, "isInitialized", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: false
82
+ });
83
+ Object.defineProperty(_this, "isActive", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: false
88
+ });
89
+ Object.defineProperty(_this, "hash", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: void 0
94
+ });
95
+ Object.defineProperty(_this, "handleLoad", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: function (content, hasCache) {
100
+ if (hasCache === void 0) { hasCache = false; }
101
+ /* istanbul ignore else */
102
+ if (_this.isActive) {
103
+ _this.setState({
104
+ content: content,
105
+ hasCache: hasCache,
106
+ status: helpers_1.STATUS.LOADED,
107
+ }, _this.getElement);
108
+ }
98
109
  }
99
- };
100
- _this.request = function () {
101
- var _a = _this.props, cacheRequests = _a.cacheRequests, fetchOptions = _a.fetchOptions, src = _a.src;
102
- try {
103
- if (cacheRequests) {
104
- exports.cacheStore[src] = { content: '', status: helpers_1.STATUS.LOADING };
110
+ });
111
+ Object.defineProperty(_this, "handleError", {
112
+ enumerable: true,
113
+ configurable: true,
114
+ writable: true,
115
+ value: function (error) {
116
+ var onError = _this.props.onError;
117
+ var status = error.message === 'Browser does not support SVG' ? helpers_1.STATUS.UNSUPPORTED : helpers_1.STATUS.FAILED;
118
+ /* istanbul ignore else */
119
+ if (_this.isActive) {
120
+ _this.setState({ status: status }, function () {
121
+ /* istanbul ignore else */
122
+ if (typeof onError === 'function') {
123
+ onError(error);
124
+ }
125
+ });
105
126
  }
106
- return fetch(src, fetchOptions)
107
- .then(function (response) {
108
- var contentType = response.headers.get('content-type');
109
- var _a = __read((contentType || '').split(/ ?; ?/), 1), fileType = _a[0];
110
- if (response.status > 299) {
111
- throw new Error('Not found');
112
- }
113
- if (!['image/svg+xml', 'text/plain'].some(function (d) { return fileType.includes(d); })) {
114
- throw new Error("Content type isn't valid: ".concat(fileType));
127
+ }
128
+ });
129
+ Object.defineProperty(_this, "request", {
130
+ enumerable: true,
131
+ configurable: true,
132
+ writable: true,
133
+ value: function () {
134
+ var _a = _this.props, cacheRequests = _a.cacheRequests, fetchOptions = _a.fetchOptions, src = _a.src;
135
+ try {
136
+ if (cacheRequests) {
137
+ exports.cacheStore[src] = { content: '', status: helpers_1.STATUS.LOADING };
115
138
  }
116
- return response.text();
117
- })
118
- .then(function (content) {
119
- var currentSrc = _this.props.src;
120
- // the current src don't match the previous one, skipping...
121
- if (src !== currentSrc) {
122
- if (exports.cacheStore[src].status === helpers_1.STATUS.LOADING) {
123
- delete exports.cacheStore[src];
139
+ return fetch(src, fetchOptions)
140
+ .then(function (response) {
141
+ var contentType = response.headers.get('content-type');
142
+ var _a = __read((contentType || '').split(/ ?; ?/), 1), fileType = _a[0];
143
+ if (response.status > 299) {
144
+ throw new Error('Not found');
124
145
  }
125
- return;
126
- }
127
- _this.handleLoad(content);
128
- /* istanbul ignore else */
129
- if (cacheRequests) {
130
- var cache = exports.cacheStore[src];
146
+ if (!['image/svg+xml', 'text/plain'].some(function (d) { return fileType.includes(d); })) {
147
+ throw new Error("Content type isn't valid: ".concat(fileType));
148
+ }
149
+ return response.text();
150
+ })
151
+ .then(function (content) {
152
+ var currentSrc = _this.props.src;
153
+ // the current src don't match the previous one, skipping...
154
+ if (src !== currentSrc) {
155
+ if (exports.cacheStore[src].status === helpers_1.STATUS.LOADING) {
156
+ delete exports.cacheStore[src];
157
+ }
158
+ return;
159
+ }
160
+ _this.handleLoad(content);
131
161
  /* istanbul ignore else */
132
- if (cache) {
133
- cache.content = content;
134
- cache.status = helpers_1.STATUS.LOADED;
162
+ if (cacheRequests) {
163
+ var cache = exports.cacheStore[src];
164
+ /* istanbul ignore else */
165
+ if (cache) {
166
+ cache.content = content;
167
+ cache.status = helpers_1.STATUS.LOADED;
168
+ }
135
169
  }
136
- }
137
- })
138
- .catch(function (error) {
139
- _this.handleError(error);
140
- /* istanbul ignore else */
141
- if (cacheRequests) {
142
- var cache = exports.cacheStore[src];
170
+ })
171
+ .catch(function (error) {
172
+ _this.handleError(error);
143
173
  /* istanbul ignore else */
144
- if (cache) {
145
- delete exports.cacheStore[src];
174
+ if (cacheRequests) {
175
+ var cache = exports.cacheStore[src];
176
+ /* istanbul ignore else */
177
+ if (cache) {
178
+ delete exports.cacheStore[src];
179
+ }
146
180
  }
147
- }
148
- });
149
- }
150
- catch (error) {
151
- return _this.handleError(new Error(error.message));
181
+ });
182
+ }
183
+ catch (error) {
184
+ return _this.handleError(new Error(error.message));
185
+ }
152
186
  }
153
- };
187
+ });
154
188
  _this.state = {
155
189
  content: '',
156
190
  element: null,
@@ -160,196 +194,250 @@ var InlineSVG = /** @class */ (function (_super) {
160
194
  _this.hash = props.uniqueHash || (0, helpers_1.randomString)(8);
161
195
  return _this;
162
196
  }
163
- InlineSVG.prototype.componentDidMount = function () {
164
- this.isActive = true;
165
- if (!(0, helpers_1.canUseDOM)()) {
166
- return;
167
- }
168
- var status = this.state.status;
169
- var src = this.props.src;
170
- try {
171
- /* istanbul ignore else */
172
- if (status === helpers_1.STATUS.PENDING) {
197
+ Object.defineProperty(InlineSVG.prototype, "componentDidMount", {
198
+ enumerable: false,
199
+ configurable: true,
200
+ writable: true,
201
+ value: function () {
202
+ this.isActive = true;
203
+ if (!(0, helpers_1.canUseDOM)() || this.isInitialized) {
204
+ return;
205
+ }
206
+ var status = this.state.status;
207
+ var src = this.props.src;
208
+ try {
173
209
  /* istanbul ignore else */
174
- if (!(0, helpers_1.isSupportedEnvironment)()) {
175
- throw new Error('Browser does not support SVG');
210
+ if (status === helpers_1.STATUS.PENDING) {
211
+ /* istanbul ignore else */
212
+ if (!(0, helpers_1.isSupportedEnvironment)()) {
213
+ throw new Error('Browser does not support SVG');
214
+ }
215
+ /* istanbul ignore else */
216
+ if (!src) {
217
+ throw new Error('Missing src');
218
+ }
219
+ this.load();
176
220
  }
221
+ }
222
+ catch (error) {
223
+ this.handleError(error);
224
+ }
225
+ this.isInitialized = true;
226
+ }
227
+ });
228
+ Object.defineProperty(InlineSVG.prototype, "componentDidUpdate", {
229
+ enumerable: false,
230
+ configurable: true,
231
+ writable: true,
232
+ value: function (previousProps, previousState) {
233
+ if (!(0, helpers_1.canUseDOM)()) {
234
+ return;
235
+ }
236
+ var _a = this.state, hasCache = _a.hasCache, status = _a.status;
237
+ var _b = this.props, onLoad = _b.onLoad, src = _b.src;
238
+ if (previousState.status !== helpers_1.STATUS.READY && status === helpers_1.STATUS.READY) {
177
239
  /* istanbul ignore else */
240
+ if (onLoad) {
241
+ onLoad(src, hasCache);
242
+ }
243
+ }
244
+ if (previousProps.src !== src) {
178
245
  if (!src) {
179
- throw new Error('Missing src');
246
+ this.handleError(new Error('Missing src'));
247
+ return;
180
248
  }
181
249
  this.load();
182
250
  }
183
251
  }
184
- catch (error) {
185
- this.handleError(error);
252
+ });
253
+ Object.defineProperty(InlineSVG.prototype, "componentWillUnmount", {
254
+ enumerable: false,
255
+ configurable: true,
256
+ writable: true,
257
+ value: function () {
258
+ this.isActive = false;
186
259
  }
187
- };
188
- InlineSVG.prototype.componentDidUpdate = function (previousProps, previousState) {
189
- if (!(0, helpers_1.canUseDOM)()) {
190
- return;
191
- }
192
- var _a = this.state, hasCache = _a.hasCache, status = _a.status;
193
- var _b = this.props, onLoad = _b.onLoad, src = _b.src;
194
- if (previousState.status !== helpers_1.STATUS.READY && status === helpers_1.STATUS.READY) {
195
- /* istanbul ignore else */
196
- if (onLoad) {
197
- onLoad(src, hasCache);
260
+ });
261
+ Object.defineProperty(InlineSVG.prototype, "getNode", {
262
+ enumerable: false,
263
+ configurable: true,
264
+ writable: true,
265
+ value: function () {
266
+ var _a = this.props, description = _a.description, title = _a.title;
267
+ try {
268
+ var svgText = this.processSVG();
269
+ var node = (0, react_from_dom_1.default)(svgText, { nodeOnly: true });
270
+ if (!node || !(node instanceof SVGSVGElement)) {
271
+ throw new Error('Could not convert the src to a DOM Node');
272
+ }
273
+ var svg = this.updateSVGAttributes(node);
274
+ if (description) {
275
+ var originalDesc = svg.querySelector('desc');
276
+ if (originalDesc && originalDesc.parentNode) {
277
+ originalDesc.parentNode.removeChild(originalDesc);
278
+ }
279
+ var descElement = document.createElementNS('http://www.w3.org/2000/svg', 'desc');
280
+ descElement.innerHTML = description;
281
+ svg.prepend(descElement);
282
+ }
283
+ if (typeof title !== 'undefined') {
284
+ var originalTitle = svg.querySelector('title');
285
+ if (originalTitle && originalTitle.parentNode) {
286
+ originalTitle.parentNode.removeChild(originalTitle);
287
+ }
288
+ if (title) {
289
+ var titleElement = document.createElementNS('http://www.w3.org/2000/svg', 'title');
290
+ titleElement.innerHTML = title;
291
+ svg.prepend(titleElement);
292
+ }
293
+ }
294
+ return svg;
198
295
  }
199
- }
200
- if (previousProps.src !== src) {
201
- if (!src) {
202
- this.handleError(new Error('Missing src'));
203
- return;
296
+ catch (error) {
297
+ return this.handleError(error);
204
298
  }
205
- this.load();
206
299
  }
207
- };
208
- InlineSVG.prototype.componentWillUnmount = function () {
209
- this.isActive = false;
210
- };
211
- InlineSVG.prototype.getNode = function () {
212
- var _a = this.props, description = _a.description, title = _a.title;
213
- try {
214
- var svgText = this.processSVG();
215
- var node = (0, react_from_dom_1.default)(svgText, { nodeOnly: true });
216
- if (!node || !(node instanceof SVGSVGElement)) {
217
- throw new Error('Could not convert the src to a DOM Node');
218
- }
219
- var svg = this.updateSVGAttributes(node);
220
- if (description) {
221
- var originalDesc = svg.querySelector('desc');
222
- if (originalDesc && originalDesc.parentNode) {
223
- originalDesc.parentNode.removeChild(originalDesc);
300
+ });
301
+ Object.defineProperty(InlineSVG.prototype, "getElement", {
302
+ enumerable: false,
303
+ configurable: true,
304
+ writable: true,
305
+ value: function () {
306
+ try {
307
+ var node = this.getNode();
308
+ var element = (0, react_from_dom_1.default)(node);
309
+ if (!element || !React.isValidElement(element)) {
310
+ throw new Error('Could not convert the src to a React element');
224
311
  }
225
- var descElement = document.createElement('desc');
226
- descElement.innerHTML = description;
227
- svg.prepend(descElement);
312
+ this.setState({
313
+ element: element,
314
+ status: helpers_1.STATUS.READY,
315
+ });
228
316
  }
229
- if (title) {
230
- var originalTitle = svg.querySelector('title');
231
- if (originalTitle && originalTitle.parentNode) {
232
- originalTitle.parentNode.removeChild(originalTitle);
233
- }
234
- var titleElement = document.createElement('title');
235
- titleElement.innerHTML = title;
236
- svg.prepend(titleElement);
317
+ catch (error) {
318
+ this.handleError(new Error(error.message));
237
319
  }
238
- return svg;
239
- }
240
- catch (error) {
241
- return this.handleError(error);
242
320
  }
243
- };
244
- InlineSVG.prototype.getElement = function () {
245
- try {
246
- var node = this.getNode();
247
- var element = (0, react_from_dom_1.default)(node);
248
- if (!element || !React.isValidElement(element)) {
249
- throw new Error('Could not convert the src to a React element');
321
+ });
322
+ Object.defineProperty(InlineSVG.prototype, "load", {
323
+ enumerable: false,
324
+ configurable: true,
325
+ writable: true,
326
+ value: function () {
327
+ var _this = this;
328
+ /* istanbul ignore else */
329
+ if (this.isActive) {
330
+ this.setState({
331
+ content: '',
332
+ element: null,
333
+ hasCache: false,
334
+ status: helpers_1.STATUS.LOADING,
335
+ }, function () {
336
+ var _a = _this.props, cacheRequests = _a.cacheRequests, src = _a.src;
337
+ var cache = cacheRequests && exports.cacheStore[src];
338
+ if (cache && cache.status === helpers_1.STATUS.LOADED) {
339
+ _this.handleLoad(cache.content, true);
340
+ return;
341
+ }
342
+ var dataURI = src.match(/data:image\/svg[^,]*?(;base64)?,(.*)/);
343
+ var inlineSrc;
344
+ if (dataURI) {
345
+ inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);
346
+ }
347
+ else if (src.includes('<svg')) {
348
+ inlineSrc = src;
349
+ }
350
+ if (inlineSrc) {
351
+ _this.handleLoad(inlineSrc);
352
+ return;
353
+ }
354
+ _this.request();
355
+ });
250
356
  }
251
- this.setState({
252
- element: element,
253
- status: helpers_1.STATUS.READY,
254
- });
255
- }
256
- catch (error) {
257
- this.handleError(new Error(error.message));
258
357
  }
259
- };
260
- InlineSVG.prototype.load = function () {
261
- var _this = this;
262
- /* istanbul ignore else */
263
- if (this.isActive) {
264
- this.setState({
265
- content: '',
266
- element: null,
267
- status: helpers_1.STATUS.LOADING,
268
- }, function () {
269
- var _a = _this.props, cacheRequests = _a.cacheRequests, src = _a.src;
270
- var cache = cacheRequests && exports.cacheStore[src];
271
- if (cache && cache.status === helpers_1.STATUS.LOADED) {
272
- _this.handleLoad(cache.content);
273
- return;
274
- }
275
- var dataURI = src.match(/data:image\/svg[^,]*?(;base64)?,(.*)/);
276
- var inlineSrc;
277
- if (dataURI) {
278
- inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);
279
- }
280
- else if (src.includes('<svg')) {
281
- inlineSrc = src;
358
+ });
359
+ Object.defineProperty(InlineSVG.prototype, "updateSVGAttributes", {
360
+ enumerable: false,
361
+ configurable: true,
362
+ writable: true,
363
+ value: function (node) {
364
+ var _this = this;
365
+ var _a = this.props, _b = _a.baseURL, baseURL = _b === void 0 ? '' : _b, uniquifyIDs = _a.uniquifyIDs;
366
+ var replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];
367
+ var linkAttributes = ['href', 'xlink:href'];
368
+ var isDataValue = function (name, value) {
369
+ return linkAttributes.includes(name) && (value ? !value.includes('#') : false);
370
+ };
371
+ if (!uniquifyIDs) {
372
+ return node;
373
+ }
374
+ __spreadArray([], __read(node.children), false).map(function (d) {
375
+ if (d.attributes && d.attributes.length) {
376
+ var attributes_1 = Object.values(d.attributes).map(function (a) {
377
+ var attribute = a;
378
+ var match = a.value.match(/url\((.*?)\)/);
379
+ if (match && match[1]) {
380
+ attribute.value = a.value.replace(match[0], "url(".concat(baseURL).concat(match[1], "__").concat(_this.hash, ")"));
381
+ }
382
+ return attribute;
383
+ });
384
+ replaceableAttributes.forEach(function (r) {
385
+ var attribute = attributes_1.find(function (a) { return a.name === r; });
386
+ if (attribute && !isDataValue(r, attribute.value)) {
387
+ attribute.value = "".concat(attribute.value, "__").concat(_this.hash);
388
+ }
389
+ });
282
390
  }
283
- if (inlineSrc) {
284
- _this.handleLoad(inlineSrc);
285
- return;
391
+ if (d.children.length) {
392
+ return _this.updateSVGAttributes(d);
286
393
  }
287
- _this.request();
394
+ return d;
288
395
  });
289
- }
290
- };
291
- InlineSVG.prototype.updateSVGAttributes = function (node) {
292
- var _this = this;
293
- var _a = this.props, _b = _a.baseURL, baseURL = _b === void 0 ? '' : _b, uniquifyIDs = _a.uniquifyIDs;
294
- var replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];
295
- var linkAttributes = ['href', 'xlink:href'];
296
- var isDataValue = function (name, value) {
297
- return linkAttributes.includes(name) && (value ? !value.includes('#') : false);
298
- };
299
- if (!uniquifyIDs) {
300
396
  return node;
301
397
  }
302
- __spreadArray([], __read(node.children), false).map(function (d) {
303
- if (d.attributes && d.attributes.length) {
304
- var attributes_1 = Object.values(d.attributes).map(function (a) {
305
- var attribute = a;
306
- var match = a.value.match(/url\((.*?)\)/);
307
- if (match && match[1]) {
308
- attribute.value = a.value.replace(match[0], "url(".concat(baseURL).concat(match[1], "__").concat(_this.hash, ")"));
309
- }
310
- return attribute;
311
- });
312
- replaceableAttributes.forEach(function (r) {
313
- var attribute = attributes_1.find(function (a) { return a.name === r; });
314
- if (attribute && !isDataValue(r, attribute.value)) {
315
- attribute.value = "".concat(attribute.value, "__").concat(_this.hash);
316
- }
317
- });
398
+ });
399
+ Object.defineProperty(InlineSVG.prototype, "processSVG", {
400
+ enumerable: false,
401
+ configurable: true,
402
+ writable: true,
403
+ value: function () {
404
+ var content = this.state.content;
405
+ var preProcessor = this.props.preProcessor;
406
+ if (preProcessor) {
407
+ return preProcessor(content);
318
408
  }
319
- if (d.children.length) {
320
- return _this.updateSVGAttributes(d);
321
- }
322
- return d;
323
- });
324
- return node;
325
- };
326
- InlineSVG.prototype.processSVG = function () {
327
- var content = this.state.content;
328
- var preProcessor = this.props.preProcessor;
329
- if (preProcessor) {
330
- return preProcessor(content);
409
+ return content;
331
410
  }
332
- return content;
333
- };
334
- InlineSVG.prototype.render = function () {
335
- var _a = this.state, element = _a.element, status = _a.status;
336
- 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;
337
- var elementProps = (0, helpers_1.omit)(this.props, 'baseURL', 'cacheRequests', 'children', 'description', 'fetchOptions', 'innerRef', 'loader', 'onError', 'onLoad', 'preProcessor', 'src', 'title', 'uniqueHash', 'uniquifyIDs');
338
- if (!(0, helpers_1.canUseDOM)()) {
411
+ });
412
+ Object.defineProperty(InlineSVG.prototype, "render", {
413
+ enumerable: false,
414
+ configurable: true,
415
+ writable: true,
416
+ value: function () {
417
+ var _a = this.state, element = _a.element, status = _a.status;
418
+ 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;
419
+ var elementProps = (0, helpers_1.omit)(this.props, 'baseURL', 'cacheRequests', 'children', 'description', 'fetchOptions', 'innerRef', 'loader', 'onError', 'onLoad', 'preProcessor', 'src', 'title', 'uniqueHash', 'uniquifyIDs');
420
+ if (!(0, helpers_1.canUseDOM)()) {
421
+ return loader;
422
+ }
423
+ if (element) {
424
+ return React.cloneElement(element, __assign({ ref: innerRef }, elementProps));
425
+ }
426
+ if ([helpers_1.STATUS.UNSUPPORTED, helpers_1.STATUS.FAILED].includes(status)) {
427
+ return children;
428
+ }
339
429
  return loader;
340
430
  }
341
- if (element) {
342
- return React.cloneElement(element, __assign({ ref: innerRef }, elementProps));
431
+ });
432
+ Object.defineProperty(InlineSVG, "defaultProps", {
433
+ enumerable: true,
434
+ configurable: true,
435
+ writable: true,
436
+ value: {
437
+ cacheRequests: true,
438
+ uniquifyIDs: false,
343
439
  }
344
- if ([helpers_1.STATUS.UNSUPPORTED, helpers_1.STATUS.FAILED].includes(status)) {
345
- return children;
346
- }
347
- return loader;
348
- };
349
- InlineSVG.defaultProps = {
350
- cacheRequests: true,
351
- uniquifyIDs: false,
352
- };
440
+ });
353
441
  return InlineSVG;
354
442
  }(React.PureComponent));
355
443
  exports.default = InlineSVG;