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