@tanem/svg-injector 11.0.0 → 11.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.
@@ -18,7 +18,7 @@
18
18
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
19
  PERFORMANCE OF THIS SOFTWARE.
20
20
  ***************************************************************************** */
21
- /* global Reflect, Promise, SuppressedError, Symbol */
21
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
22
22
 
23
23
 
24
24
  function __spreadArray(to, from, pack) {
@@ -42,151 +42,241 @@
42
42
  return sourceSvg.cloneNode(true);
43
43
  };
44
44
 
45
+ var contentType = {};
46
+
45
47
  /*!
46
48
  * content-type
47
49
  * Copyright(c) 2015 Douglas Christopher Wilson
48
50
  * MIT Licensed
49
51
  */
50
52
 
51
- /**
52
- * RegExp to match *( ";" parameter ) in RFC 7231 sec 3.1.1.1
53
- *
54
- * parameter = token "=" ( token / quoted-string )
55
- * token = 1*tchar
56
- * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
57
- * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
58
- * / DIGIT / ALPHA
59
- * ; any VCHAR, except delimiters
60
- * quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
61
- * qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
62
- * obs-text = %x80-FF
63
- * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
64
- */
65
- var PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g; // eslint-disable-line no-control-regex
66
-
67
- /**
68
- * RegExp to match quoted-pair in RFC 7230 sec 3.2.6
69
- *
70
- * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
71
- * obs-text = %x80-FF
72
- */
73
- var QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g; // eslint-disable-line no-control-regex
74
-
75
- /**
76
- * RegExp to match type in RFC 7231 sec 3.1.1.1
77
- *
78
- * media-type = type "/" subtype
79
- * type = token
80
- * subtype = token
81
- */
82
- var TYPE_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
83
- var parse_1 = parse;
84
-
85
- /**
86
- * Parse media type to object.
87
- *
88
- * @param {string|object} string
89
- * @return {Object}
90
- * @public
91
- */
92
-
93
- function parse (string) {
94
- if (!string) {
95
- throw new TypeError('argument string is required')
96
- }
97
-
98
- // support req/res-like objects as argument
99
- var header = typeof string === 'object'
100
- ? getcontenttype(string)
101
- : string;
102
-
103
- if (typeof header !== 'string') {
104
- throw new TypeError('argument string is required to be a string')
105
- }
106
-
107
- var index = header.indexOf(';');
108
- var type = index !== -1
109
- ? header.slice(0, index).trim()
110
- : header.trim();
111
-
112
- if (!TYPE_REGEXP.test(type)) {
113
- throw new TypeError('invalid media type')
114
- }
115
-
116
- var obj = new ContentType(type.toLowerCase());
117
-
118
- // parse parameters
119
- if (index !== -1) {
120
- var key;
121
- var match;
122
- var value;
123
-
124
- PARAM_REGEXP.lastIndex = index;
125
-
126
- while ((match = PARAM_REGEXP.exec(header))) {
127
- if (match.index !== index) {
128
- throw new TypeError('invalid parameter format')
129
- }
130
-
131
- index += match[0].length;
132
- key = match[1].toLowerCase();
133
- value = match[2];
134
-
135
- if (value.charCodeAt(0) === 0x22 /* " */) {
136
- // remove quotes
137
- value = value.slice(1, -1);
138
-
139
- // remove escapes
140
- if (value.indexOf('\\') !== -1) {
141
- value = value.replace(QESC_REGEXP, '$1');
142
- }
143
- }
144
-
145
- obj.parameters[key] = value;
146
- }
147
-
148
- if (index !== header.length) {
149
- throw new TypeError('invalid parameter format')
150
- }
151
- }
152
-
153
- return obj
154
- }
155
-
156
- /**
157
- * Get content-type from req/res objects.
158
- *
159
- * @param {object}
160
- * @return {Object}
161
- * @private
162
- */
163
-
164
- function getcontenttype (obj) {
165
- var header;
166
-
167
- if (typeof obj.getHeader === 'function') {
168
- // res-like
169
- header = obj.getHeader('content-type');
170
- } else if (typeof obj.headers === 'object') {
171
- // req-like
172
- header = obj.headers && obj.headers['content-type'];
173
- }
174
-
175
- if (typeof header !== 'string') {
176
- throw new TypeError('content-type header is missing from object')
177
- }
178
-
179
- return header
53
+ var hasRequiredContentType;
54
+
55
+ function requireContentType () {
56
+ if (hasRequiredContentType) return contentType;
57
+ hasRequiredContentType = 1;
58
+
59
+ /**
60
+ * RegExp to match *( ";" parameter ) in RFC 7231 sec 3.1.1.1
61
+ *
62
+ * parameter = token "=" ( token / quoted-string )
63
+ * token = 1*tchar
64
+ * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
65
+ * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
66
+ * / DIGIT / ALPHA
67
+ * ; any VCHAR, except delimiters
68
+ * quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
69
+ * qdtext = HTAB / SP / %x21 / %x23-5B / %x5D-7E / obs-text
70
+ * obs-text = %x80-FF
71
+ * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
72
+ */
73
+ var PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g; // eslint-disable-line no-control-regex
74
+ var TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/; // eslint-disable-line no-control-regex
75
+ var TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
76
+
77
+ /**
78
+ * RegExp to match quoted-pair in RFC 7230 sec 3.2.6
79
+ *
80
+ * quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
81
+ * obs-text = %x80-FF
82
+ */
83
+ var QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g; // eslint-disable-line no-control-regex
84
+
85
+ /**
86
+ * RegExp to match chars that must be quoted-pair in RFC 7230 sec 3.2.6
87
+ */
88
+ var QUOTE_REGEXP = /([\\"])/g;
89
+
90
+ /**
91
+ * RegExp to match type in RFC 7231 sec 3.1.1.1
92
+ *
93
+ * media-type = type "/" subtype
94
+ * type = token
95
+ * subtype = token
96
+ */
97
+ var TYPE_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
98
+
99
+ /**
100
+ * Module exports.
101
+ * @public
102
+ */
103
+
104
+ contentType.format = format;
105
+ contentType.parse = parse;
106
+
107
+ /**
108
+ * Format object to media type.
109
+ *
110
+ * @param {object} obj
111
+ * @return {string}
112
+ * @public
113
+ */
114
+
115
+ function format (obj) {
116
+ if (!obj || typeof obj !== 'object') {
117
+ throw new TypeError('argument obj is required')
118
+ }
119
+
120
+ var parameters = obj.parameters;
121
+ var type = obj.type;
122
+
123
+ if (!type || !TYPE_REGEXP.test(type)) {
124
+ throw new TypeError('invalid type')
125
+ }
126
+
127
+ var string = type;
128
+
129
+ // append parameters
130
+ if (parameters && typeof parameters === 'object') {
131
+ var param;
132
+ var params = Object.keys(parameters).sort();
133
+
134
+ for (var i = 0; i < params.length; i++) {
135
+ param = params[i];
136
+
137
+ if (!TOKEN_REGEXP.test(param)) {
138
+ throw new TypeError('invalid parameter name')
139
+ }
140
+
141
+ string += '; ' + param + '=' + qstring(parameters[param]);
142
+ }
143
+ }
144
+
145
+ return string
146
+ }
147
+
148
+ /**
149
+ * Parse media type to object.
150
+ *
151
+ * @param {string|object} string
152
+ * @return {Object}
153
+ * @public
154
+ */
155
+
156
+ function parse (string) {
157
+ if (!string) {
158
+ throw new TypeError('argument string is required')
159
+ }
160
+
161
+ // support req/res-like objects as argument
162
+ var header = typeof string === 'object'
163
+ ? getcontenttype(string)
164
+ : string;
165
+
166
+ if (typeof header !== 'string') {
167
+ throw new TypeError('argument string is required to be a string')
168
+ }
169
+
170
+ var index = header.indexOf(';');
171
+ var type = index !== -1
172
+ ? header.slice(0, index).trim()
173
+ : header.trim();
174
+
175
+ if (!TYPE_REGEXP.test(type)) {
176
+ throw new TypeError('invalid media type')
177
+ }
178
+
179
+ var obj = new ContentType(type.toLowerCase());
180
+
181
+ // parse parameters
182
+ if (index !== -1) {
183
+ var key;
184
+ var match;
185
+ var value;
186
+
187
+ PARAM_REGEXP.lastIndex = index;
188
+
189
+ while ((match = PARAM_REGEXP.exec(header))) {
190
+ if (match.index !== index) {
191
+ throw new TypeError('invalid parameter format')
192
+ }
193
+
194
+ index += match[0].length;
195
+ key = match[1].toLowerCase();
196
+ value = match[2];
197
+
198
+ if (value.charCodeAt(0) === 0x22 /* " */) {
199
+ // remove quotes
200
+ value = value.slice(1, -1);
201
+
202
+ // remove escapes
203
+ if (value.indexOf('\\') !== -1) {
204
+ value = value.replace(QESC_REGEXP, '$1');
205
+ }
206
+ }
207
+
208
+ obj.parameters[key] = value;
209
+ }
210
+
211
+ if (index !== header.length) {
212
+ throw new TypeError('invalid parameter format')
213
+ }
214
+ }
215
+
216
+ return obj
217
+ }
218
+
219
+ /**
220
+ * Get content-type from req/res objects.
221
+ *
222
+ * @param {object}
223
+ * @return {Object}
224
+ * @private
225
+ */
226
+
227
+ function getcontenttype (obj) {
228
+ var header;
229
+
230
+ if (typeof obj.getHeader === 'function') {
231
+ // res-like
232
+ header = obj.getHeader('content-type');
233
+ } else if (typeof obj.headers === 'object') {
234
+ // req-like
235
+ header = obj.headers && obj.headers['content-type'];
236
+ }
237
+
238
+ if (typeof header !== 'string') {
239
+ throw new TypeError('content-type header is missing from object')
240
+ }
241
+
242
+ return header
243
+ }
244
+
245
+ /**
246
+ * Quote a string if necessary.
247
+ *
248
+ * @param {string} val
249
+ * @return {string}
250
+ * @private
251
+ */
252
+
253
+ function qstring (val) {
254
+ var str = String(val);
255
+
256
+ // no need to quote tokens
257
+ if (TOKEN_REGEXP.test(str)) {
258
+ return str
259
+ }
260
+
261
+ if (str.length > 0 && !TEXT_REGEXP.test(str)) {
262
+ throw new TypeError('invalid parameter value')
263
+ }
264
+
265
+ return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"'
266
+ }
267
+
268
+ /**
269
+ * Class to represent a content type.
270
+ * @private
271
+ */
272
+ function ContentType (type) {
273
+ this.parameters = Object.create(null);
274
+ this.type = type;
275
+ }
276
+ return contentType;
180
277
  }
181
278
 
182
- /**
183
- * Class to represent a content type.
184
- * @private
185
- */
186
- function ContentType (type) {
187
- this.parameters = Object.create(null);
188
- this.type = type;
189
- }
279
+ var contentTypeExports = requireContentType();
190
280
 
191
281
  var isLocal = function () {
192
282
  return window.location.protocol === 'file:';
@@ -201,7 +291,7 @@
201
291
  if (!contentType) {
202
292
  throw new Error('Content type not found');
203
293
  }
204
- var type = parse_1(contentType).type;
294
+ var type = contentTypeExports.parse(contentType).type;
205
295
  if (!(type === 'image/svg+xml' || type === 'text/plain')) {
206
296
  throw new Error("Invalid content type: ".concat(type));
207
297
  }
@@ -235,28 +325,36 @@
235
325
 
236
326
  var requestQueue = {};
237
327
  var queueRequest = function (url, callback) {
238
- requestQueue[url] = requestQueue[url] || [];
328
+ var _a;
329
+ (_a = requestQueue[url]) !== null && _a !== void 0 ? _a : requestQueue[url] = [];
239
330
  requestQueue[url].push(callback);
240
331
  };
241
332
  var processRequestQueue = function (url) {
333
+ var callbacks = requestQueue[url];
334
+ if (!callbacks) {
335
+ return;
336
+ }
242
337
  var _loop_1 = function (i, len) {
243
338
  setTimeout(function () {
244
339
  if (Array.isArray(requestQueue[url])) {
245
340
  var cacheValue = cache.get(url);
246
- var callback = requestQueue[url][i];
341
+ var callback = callbacks[i];
342
+ if (!callback) {
343
+ return;
344
+ }
247
345
  if (cacheValue instanceof SVGSVGElement) {
248
346
  callback(null, cloneSvg(cacheValue));
249
347
  }
250
348
  if (cacheValue instanceof Error) {
251
349
  callback(cacheValue);
252
350
  }
253
- if (i === requestQueue[url].length - 1) {
351
+ if (i === callbacks.length - 1) {
254
352
  delete requestQueue[url];
255
353
  }
256
354
  }
257
355
  }, 0);
258
356
  };
259
- for (var i = 0, len = requestQueue[url].length; i < len; i++) {
357
+ for (var i = 0, len = callbacks.length; i < len; i++) {
260
358
  _loop_1(i);
261
359
  }
262
360
  };
@@ -307,7 +405,8 @@
307
405
  var svgNamespace = 'http://www.w3.org/2000/svg';
308
406
  var xlinkNamespace = 'http://www.w3.org/1999/xlink';
309
407
  var injectElement = function (el, evalScripts, renumerateIRIElements, cacheRequests, httpRequestWithCredentials, beforeEach, callback) {
310
- var elUrl = el.getAttribute('data-src') || el.getAttribute('src');
408
+ var _a;
409
+ var elUrl = (_a = el.getAttribute('data-src')) !== null && _a !== void 0 ? _a : el.getAttribute('src');
311
410
  if (!elUrl) {
312
411
  callback(new Error('Invalid data-src or src attribute'));
313
412
  return;
@@ -321,6 +420,7 @@
321
420
  el.setAttribute('src', '');
322
421
  var loadSvg = cacheRequests ? loadSvgCached : loadSvgUncached;
323
422
  loadSvg(elUrl, httpRequestWithCredentials, function (error, svg) {
423
+ var _a, _b;
324
424
  if (!svg) {
325
425
  injectedElements.splice(injectedElements.indexOf(el), 1);
326
426
  el = null;
@@ -343,7 +443,7 @@
343
443
  if (elHeight) {
344
444
  svg.setAttribute('height', elHeight);
345
445
  }
346
- var mergedClasses = Array.from(new Set(__spreadArray(__spreadArray(__spreadArray([], (svg.getAttribute('class') || '').split(' '), true), ['injected-svg'], false), (el.getAttribute('class') || '').split(' '), true))).join(' ').trim();
446
+ var mergedClasses = Array.from(new Set(__spreadArray(__spreadArray(__spreadArray([], ((_a = svg.getAttribute('class')) !== null && _a !== void 0 ? _a : '').split(' '), true), ['injected-svg'], false), ((_b = el.getAttribute('class')) !== null && _b !== void 0 ? _b : '').split(' '), true))).join(' ').trim();
347
447
  svg.setAttribute('class', mergedClasses);
348
448
  var elStyle = el.getAttribute('style');
349
449
  if (elStyle) {
@@ -381,31 +481,34 @@
381
481
  properties_1 = iriElementsAndProperties_1[key];
382
482
  elements_1 = svg.querySelectorAll(element_1 + '[id]');
383
483
  var _loop_1 = function (a, elementsLen) {
384
- currentId_1 = elements_1[a].id;
484
+ var currentElement = elements_1[a];
485
+ currentId_1 = currentElement.id;
385
486
  newId_1 = currentId_1 + '-' + uniqueId();
386
487
  var referencingElements;
387
488
  Array.prototype.forEach.call(properties_1, function (property) {
388
489
  referencingElements = svg.querySelectorAll('[' + property + '*="' + currentId_1 + '"]');
389
490
  for (var b = 0, referencingElementLen = referencingElements.length; b < referencingElementLen; b++) {
390
- var attrValue = referencingElements[b].getAttribute(property);
491
+ var referencingElement = referencingElements[b];
492
+ var attrValue = referencingElement.getAttribute(property);
391
493
  if (attrValue && !attrValue.match(new RegExp('url\\("?#' + currentId_1 + '"?\\)'))) {
392
494
  continue;
393
495
  }
394
- referencingElements[b].setAttribute(property, 'url(#' + newId_1 + ')');
496
+ referencingElement.setAttribute(property, 'url(#' + newId_1 + ')');
395
497
  }
396
498
  });
397
499
  var allLinks = svg.querySelectorAll('[*|href]');
398
500
  var links = [];
399
501
  for (var c = 0, allLinksLen = allLinks.length; c < allLinksLen; c++) {
400
- var href = allLinks[c].getAttributeNS(xlinkNamespace, 'href');
401
- if (href && href.toString() === '#' + elements_1[a].id) {
402
- links.push(allLinks[c]);
502
+ var link = allLinks[c];
503
+ var href = link.getAttributeNS(xlinkNamespace, 'href');
504
+ if ((href === null || href === void 0 ? void 0 : href.toString()) === '#' + currentElement.id) {
505
+ links.push(link);
403
506
  }
404
507
  }
405
508
  for (var d = 0, linksLen = links.length; d < linksLen; d++) {
406
509
  links[d].setAttributeNS(xlinkNamespace, 'href', '#' + newId_1);
407
510
  }
408
- elements_1[a].id = newId_1;
511
+ currentElement.id = newId_1;
409
512
  };
410
513
  for (var a = 0, elementsLen = elements_1.length; a < elementsLen; a++) {
411
514
  _loop_1(a);
@@ -418,13 +521,14 @@
418
521
  var script;
419
522
  var scriptType;
420
523
  for (var i = 0, scriptsLen = scripts.length; i < scriptsLen; i++) {
421
- scriptType = scripts[i].getAttribute('type');
524
+ var scriptElement = scripts[i];
525
+ scriptType = scriptElement.getAttribute('type');
422
526
  if (!scriptType || scriptType === 'application/ecmascript' || scriptType === 'application/javascript' || scriptType === 'text/javascript') {
423
- script = scripts[i].innerText || scripts[i].textContent;
527
+ script = scriptElement.innerText || scriptElement.textContent;
424
528
  if (script) {
425
529
  scriptsToEval.push(script);
426
530
  }
427
- svg.removeChild(scripts[i]);
531
+ svg.removeChild(scriptElement);
428
532
  }
429
533
  }
430
534
  if (scriptsToEval.length > 0 && (evalScripts === 'always' || evalScripts === 'once' && !ranScripts[elUrl])) {
@@ -478,7 +582,11 @@
478
582
  if (elements && 'length' in elements) {
479
583
  var elementsLoaded_1 = 0;
480
584
  for (var i = 0, j = elements.length; i < j; i++) {
481
- injectElement(elements[i], evalScripts, renumerateIRIElements, cacheRequests, httpRequestWithCredentials, beforeEach, function (error, svg) {
585
+ var element = elements[i];
586
+ if (!element) {
587
+ continue;
588
+ }
589
+ injectElement(element, evalScripts, renumerateIRIElements, cacheRequests, httpRequestWithCredentials, beforeEach, function (error, svg) {
482
590
  afterEach(error, svg);
483
591
  if (elements && 'length' in elements && elements.length === ++elementsLoaded_1) {
484
592
  afterAll(elementsLoaded_1);