@tanem/svg-injector 10.1.34 → 10.1.35
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/dist/svg-injector.cjs.development.js +20 -77
- package/dist/svg-injector.cjs.development.js.map +1 -1
- package/dist/svg-injector.cjs.production.js.map +1 -1
- package/dist/svg-injector.esm.js +20 -77
- package/dist/svg-injector.esm.js.map +1 -1
- package/dist/svg-injector.umd.development.js +20 -77
- package/dist/svg-injector.umd.development.js.map +1 -1
- package/dist/svg-injector.umd.production.js.map +1 -1
- package/package.json +10 -10
|
@@ -17,28 +17,22 @@ var isLocal = function isLocal() {
|
|
|
17
17
|
|
|
18
18
|
var makeAjaxRequest = function makeAjaxRequest(url, httpRequestWithCredentials, callback) {
|
|
19
19
|
var httpRequest = new XMLHttpRequest();
|
|
20
|
-
|
|
21
20
|
httpRequest.onreadystatechange = function () {
|
|
22
21
|
try {
|
|
23
22
|
if (!/\.svg/i.test(url) && httpRequest.readyState === 2) {
|
|
24
23
|
var contentType$1 = httpRequest.getResponseHeader('Content-Type');
|
|
25
|
-
|
|
26
24
|
if (!contentType$1) {
|
|
27
25
|
throw new Error('Content type not found');
|
|
28
26
|
}
|
|
29
|
-
|
|
30
27
|
var type = contentType.parse(contentType$1).type;
|
|
31
|
-
|
|
32
28
|
if (!(type === 'image/svg+xml' || type === 'text/plain')) {
|
|
33
29
|
throw new Error("Invalid content type: ".concat(type));
|
|
34
30
|
}
|
|
35
31
|
}
|
|
36
|
-
|
|
37
32
|
if (httpRequest.readyState === 4) {
|
|
38
33
|
if (httpRequest.status === 404 || httpRequest.responseXML === null) {
|
|
39
34
|
throw new Error(isLocal() ? 'Note: SVG injection ajax calls do not work locally without ' + 'adjusting security settings in your browser. Or consider ' + 'using a local webserver.' : 'Unable to load SVG file: ' + url);
|
|
40
35
|
}
|
|
41
|
-
|
|
42
36
|
if (httpRequest.status === 200 || isLocal() && httpRequest.status === 0) {
|
|
43
37
|
callback(null, httpRequest);
|
|
44
38
|
} else {
|
|
@@ -47,7 +41,6 @@ var makeAjaxRequest = function makeAjaxRequest(url, httpRequestWithCredentials,
|
|
|
47
41
|
}
|
|
48
42
|
} catch (error) {
|
|
49
43
|
httpRequest.abort();
|
|
50
|
-
|
|
51
44
|
if (error instanceof Error) {
|
|
52
45
|
callback(error, httpRequest);
|
|
53
46
|
} else {
|
|
@@ -55,14 +48,11 @@ var makeAjaxRequest = function makeAjaxRequest(url, httpRequestWithCredentials,
|
|
|
55
48
|
}
|
|
56
49
|
}
|
|
57
50
|
};
|
|
58
|
-
|
|
59
51
|
httpRequest.open('GET', url);
|
|
60
52
|
httpRequest.withCredentials = httpRequestWithCredentials;
|
|
61
|
-
|
|
62
53
|
if (httpRequest.overrideMimeType) {
|
|
63
54
|
httpRequest.overrideMimeType('text/xml');
|
|
64
55
|
}
|
|
65
|
-
|
|
66
56
|
httpRequest.send();
|
|
67
57
|
};
|
|
68
58
|
|
|
@@ -77,22 +67,18 @@ var processRequestQueue = function processRequestQueue(url) {
|
|
|
77
67
|
if (Array.isArray(requestQueue[url])) {
|
|
78
68
|
var cacheValue = cache.get(url);
|
|
79
69
|
var callback = requestQueue[url][i];
|
|
80
|
-
|
|
81
70
|
if (cacheValue instanceof SVGSVGElement) {
|
|
82
71
|
callback(null, cloneSvg(cacheValue));
|
|
83
72
|
}
|
|
84
|
-
|
|
85
73
|
if (cacheValue instanceof Error) {
|
|
86
74
|
callback(cacheValue);
|
|
87
75
|
}
|
|
88
|
-
|
|
89
76
|
if (i === requestQueue[url].length - 1) {
|
|
90
77
|
delete requestQueue[url];
|
|
91
78
|
}
|
|
92
79
|
}
|
|
93
80
|
}, 0);
|
|
94
81
|
};
|
|
95
|
-
|
|
96
82
|
for (var i = 0, len = requestQueue[url].length; i < len; i++) {
|
|
97
83
|
_loop_1(i);
|
|
98
84
|
}
|
|
@@ -101,18 +87,15 @@ var processRequestQueue = function processRequestQueue(url) {
|
|
|
101
87
|
var loadSvgCached = function loadSvgCached(url, httpRequestWithCredentials, callback) {
|
|
102
88
|
if (cache.has(url)) {
|
|
103
89
|
var cacheValue = cache.get(url);
|
|
104
|
-
|
|
105
90
|
if (cacheValue === undefined) {
|
|
106
91
|
queueRequest(url, callback);
|
|
107
92
|
return;
|
|
108
93
|
}
|
|
109
|
-
|
|
110
94
|
if (cacheValue instanceof SVGSVGElement) {
|
|
111
95
|
callback(null, cloneSvg(cacheValue));
|
|
112
96
|
return;
|
|
113
97
|
}
|
|
114
98
|
}
|
|
115
|
-
|
|
116
99
|
cache.set(url, undefined);
|
|
117
100
|
queueRequest(url, callback);
|
|
118
101
|
makeAjaxRequest(url, httpRequestWithCredentials, function (error, httpRequest) {
|
|
@@ -121,7 +104,6 @@ var loadSvgCached = function loadSvgCached(url, httpRequestWithCredentials, call
|
|
|
121
104
|
} else if (httpRequest.responseXML instanceof Document && httpRequest.responseXML.documentElement && httpRequest.responseXML.documentElement instanceof SVGSVGElement) {
|
|
122
105
|
cache.set(url, httpRequest.responseXML.documentElement);
|
|
123
106
|
}
|
|
124
|
-
|
|
125
107
|
processRequestQueue(url);
|
|
126
108
|
});
|
|
127
109
|
};
|
|
@@ -137,7 +119,6 @@ var loadSvgUncached = function loadSvgUncached(url, httpRequestWithCredentials,
|
|
|
137
119
|
};
|
|
138
120
|
|
|
139
121
|
var idCounter = 0;
|
|
140
|
-
|
|
141
122
|
var uniqueId = function uniqueId() {
|
|
142
123
|
return ++idCounter;
|
|
143
124
|
};
|
|
@@ -146,21 +127,17 @@ var injectedElements = [];
|
|
|
146
127
|
var ranScripts = {};
|
|
147
128
|
var svgNamespace = 'http://www.w3.org/2000/svg';
|
|
148
129
|
var xlinkNamespace = 'http://www.w3.org/1999/xlink';
|
|
149
|
-
|
|
150
130
|
var injectElement = function injectElement(el, evalScripts, renumerateIRIElements, cacheRequests, httpRequestWithCredentials, beforeEach, callback) {
|
|
151
131
|
var elUrl = el.getAttribute('data-src') || el.getAttribute('src');
|
|
152
|
-
|
|
153
132
|
if (!elUrl) {
|
|
154
133
|
callback(new Error('Invalid data-src or src attribute'));
|
|
155
134
|
return;
|
|
156
135
|
}
|
|
157
|
-
|
|
158
136
|
if (injectedElements.indexOf(el) !== -1) {
|
|
159
137
|
injectedElements.splice(injectedElements.indexOf(el), 1);
|
|
160
138
|
el = null;
|
|
161
139
|
return;
|
|
162
140
|
}
|
|
163
|
-
|
|
164
141
|
injectedElements.push(el);
|
|
165
142
|
el.setAttribute('src', '');
|
|
166
143
|
var loadSvg = cacheRequests ? loadSvgCached : loadSvgUncached;
|
|
@@ -171,39 +148,28 @@ var injectElement = function injectElement(el, evalScripts, renumerateIRIElement
|
|
|
171
148
|
callback(error);
|
|
172
149
|
return;
|
|
173
150
|
}
|
|
174
|
-
|
|
175
151
|
var elId = el.getAttribute('id');
|
|
176
|
-
|
|
177
152
|
if (elId) {
|
|
178
153
|
svg.setAttribute('id', elId);
|
|
179
154
|
}
|
|
180
|
-
|
|
181
155
|
var elTitle = el.getAttribute('title');
|
|
182
|
-
|
|
183
156
|
if (elTitle) {
|
|
184
157
|
svg.setAttribute('title', elTitle);
|
|
185
158
|
}
|
|
186
|
-
|
|
187
159
|
var elWidth = el.getAttribute('width');
|
|
188
|
-
|
|
189
160
|
if (elWidth) {
|
|
190
161
|
svg.setAttribute('width', elWidth);
|
|
191
162
|
}
|
|
192
|
-
|
|
193
163
|
var elHeight = el.getAttribute('height');
|
|
194
|
-
|
|
195
164
|
if (elHeight) {
|
|
196
165
|
svg.setAttribute('height', elHeight);
|
|
197
166
|
}
|
|
198
|
-
|
|
199
167
|
var mergedClasses = Array.from(new Set(tslib.__spreadArray(tslib.__spreadArray(tslib.__spreadArray([], (svg.getAttribute('class') || '').split(' '), true), ['injected-svg'], false), (el.getAttribute('class') || '').split(' '), true))).join(' ').trim();
|
|
200
168
|
svg.setAttribute('class', mergedClasses);
|
|
201
169
|
var elStyle = el.getAttribute('style');
|
|
202
|
-
|
|
203
170
|
if (elStyle) {
|
|
204
171
|
svg.setAttribute('style', elStyle);
|
|
205
172
|
}
|
|
206
|
-
|
|
207
173
|
svg.setAttribute('data-src', elUrl);
|
|
208
174
|
var elData = [].filter.call(el.attributes, function (at) {
|
|
209
175
|
return /^data-\w[\w-]*$/.test(at.name);
|
|
@@ -213,7 +179,6 @@ var injectElement = function injectElement(el, evalScripts, renumerateIRIElement
|
|
|
213
179
|
svg.setAttribute(dataAttr.name, dataAttr.value);
|
|
214
180
|
}
|
|
215
181
|
});
|
|
216
|
-
|
|
217
182
|
if (renumerateIRIElements) {
|
|
218
183
|
var iriElementsAndProperties_1 = {
|
|
219
184
|
clipPath: ['clip-path'],
|
|
@@ -236,76 +201,59 @@ var injectElement = function injectElement(el, evalScripts, renumerateIRIElement
|
|
|
236
201
|
element_1 = key;
|
|
237
202
|
properties_1 = iriElementsAndProperties_1[key];
|
|
238
203
|
elements_1 = svg.querySelectorAll(element_1 + '[id]');
|
|
239
|
-
|
|
240
204
|
var _loop_1 = function _loop_1(a, elementsLen) {
|
|
241
205
|
currentId_1 = elements_1[a].id;
|
|
242
206
|
newId_1 = currentId_1 + '-' + uniqueId();
|
|
243
207
|
var referencingElements;
|
|
244
208
|
Array.prototype.forEach.call(properties_1, function (property) {
|
|
245
209
|
referencingElements = svg.querySelectorAll('[' + property + '*="' + currentId_1 + '"]');
|
|
246
|
-
|
|
247
210
|
for (var b = 0, referencingElementLen = referencingElements.length; b < referencingElementLen; b++) {
|
|
248
211
|
var attrValue = referencingElements[b].getAttribute(property);
|
|
249
|
-
|
|
250
212
|
if (attrValue && !attrValue.match(new RegExp('url\\("?#' + currentId_1 + '"?\\)'))) {
|
|
251
213
|
continue;
|
|
252
214
|
}
|
|
253
|
-
|
|
254
215
|
referencingElements[b].setAttribute(property, 'url(#' + newId_1 + ')');
|
|
255
216
|
}
|
|
256
217
|
});
|
|
257
218
|
var allLinks = svg.querySelectorAll('[*|href]');
|
|
258
219
|
var links = [];
|
|
259
|
-
|
|
260
220
|
for (var c = 0, allLinksLen = allLinks.length; c < allLinksLen; c++) {
|
|
261
221
|
var href = allLinks[c].getAttributeNS(xlinkNamespace, 'href');
|
|
262
|
-
|
|
263
222
|
if (href && href.toString() === '#' + elements_1[a].id) {
|
|
264
223
|
links.push(allLinks[c]);
|
|
265
224
|
}
|
|
266
225
|
}
|
|
267
|
-
|
|
268
226
|
for (var d = 0, linksLen = links.length; d < linksLen; d++) {
|
|
269
227
|
links[d].setAttributeNS(xlinkNamespace, 'href', '#' + newId_1);
|
|
270
228
|
}
|
|
271
|
-
|
|
272
229
|
elements_1[a].id = newId_1;
|
|
273
230
|
};
|
|
274
|
-
|
|
275
231
|
for (var a = 0, elementsLen = elements_1.length; a < elementsLen; a++) {
|
|
276
232
|
_loop_1(a);
|
|
277
233
|
}
|
|
278
234
|
});
|
|
279
235
|
}
|
|
280
|
-
|
|
281
236
|
svg.removeAttribute('xmlns:a');
|
|
282
237
|
var scripts = svg.querySelectorAll('script');
|
|
283
238
|
var scriptsToEval = [];
|
|
284
239
|
var script;
|
|
285
240
|
var scriptType;
|
|
286
|
-
|
|
287
241
|
for (var i = 0, scriptsLen = scripts.length; i < scriptsLen; i++) {
|
|
288
242
|
scriptType = scripts[i].getAttribute('type');
|
|
289
|
-
|
|
290
243
|
if (!scriptType || scriptType === 'application/ecmascript' || scriptType === 'application/javascript' || scriptType === 'text/javascript') {
|
|
291
244
|
script = scripts[i].innerText || scripts[i].textContent;
|
|
292
|
-
|
|
293
245
|
if (script) {
|
|
294
246
|
scriptsToEval.push(script);
|
|
295
247
|
}
|
|
296
|
-
|
|
297
248
|
svg.removeChild(scripts[i]);
|
|
298
249
|
}
|
|
299
250
|
}
|
|
300
|
-
|
|
301
251
|
if (scriptsToEval.length > 0 && (evalScripts === 'always' || evalScripts === 'once' && !ranScripts[elUrl])) {
|
|
302
252
|
for (var l = 0, scriptsToEvalLen = scriptsToEval.length; l < scriptsToEvalLen; l++) {
|
|
303
253
|
new Function(scriptsToEval[l])(window);
|
|
304
254
|
}
|
|
305
|
-
|
|
306
255
|
ranScripts[elUrl] = true;
|
|
307
256
|
}
|
|
308
|
-
|
|
309
257
|
var styleTags = svg.querySelectorAll('style');
|
|
310
258
|
Array.prototype.forEach.call(styleTags, function (styleTag) {
|
|
311
259
|
styleTag.textContent += '';
|
|
@@ -313,14 +261,12 @@ var injectElement = function injectElement(el, evalScripts, renumerateIRIElement
|
|
|
313
261
|
svg.setAttribute('xmlns', svgNamespace);
|
|
314
262
|
svg.setAttribute('xmlns:xlink', xlinkNamespace);
|
|
315
263
|
beforeEach(svg);
|
|
316
|
-
|
|
317
264
|
if (!el.parentNode) {
|
|
318
265
|
injectedElements.splice(injectedElements.indexOf(el), 1);
|
|
319
266
|
el = null;
|
|
320
267
|
callback(new Error('Parent node is null'));
|
|
321
268
|
return;
|
|
322
269
|
}
|
|
323
|
-
|
|
324
270
|
el.parentNode.replaceChild(svg, el);
|
|
325
271
|
injectedElements.splice(injectedElements.indexOf(el), 1);
|
|
326
272
|
el = null;
|
|
@@ -330,34 +276,31 @@ var injectElement = function injectElement(el, evalScripts, renumerateIRIElement
|
|
|
330
276
|
|
|
331
277
|
var SVGInjector = function SVGInjector(elements, _a) {
|
|
332
278
|
var _b = _a === void 0 ? {} : _a,
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
279
|
+
_c = _b.afterAll,
|
|
280
|
+
afterAll = _c === void 0 ? function () {
|
|
281
|
+
return undefined;
|
|
282
|
+
} : _c,
|
|
283
|
+
_d = _b.afterEach,
|
|
284
|
+
afterEach = _d === void 0 ? function () {
|
|
285
|
+
return undefined;
|
|
286
|
+
} : _d,
|
|
287
|
+
_e = _b.beforeEach,
|
|
288
|
+
beforeEach = _e === void 0 ? function () {
|
|
289
|
+
return undefined;
|
|
290
|
+
} : _e,
|
|
291
|
+
_f = _b.cacheRequests,
|
|
292
|
+
cacheRequests = _f === void 0 ? true : _f,
|
|
293
|
+
_g = _b.evalScripts,
|
|
294
|
+
evalScripts = _g === void 0 ? 'never' : _g,
|
|
295
|
+
_h = _b.httpRequestWithCredentials,
|
|
296
|
+
httpRequestWithCredentials = _h === void 0 ? false : _h,
|
|
297
|
+
_j = _b.renumerateIRIElements,
|
|
298
|
+
renumerateIRIElements = _j === void 0 ? true : _j;
|
|
354
299
|
if (elements && 'length' in elements) {
|
|
355
300
|
var elementsLoaded_1 = 0;
|
|
356
|
-
|
|
357
301
|
for (var i = 0, j = elements.length; i < j; i++) {
|
|
358
302
|
injectElement(elements[i], evalScripts, renumerateIRIElements, cacheRequests, httpRequestWithCredentials, beforeEach, function (error, svg) {
|
|
359
303
|
afterEach(error, svg);
|
|
360
|
-
|
|
361
304
|
if (elements && 'length' in elements && elements.length === ++elementsLoaded_1) {
|
|
362
305
|
afterAll(elementsLoaded_1);
|
|
363
306
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"svg-injector.cjs.development.js","sources":["../src/cache.ts","../src/clone-svg.ts","../src/is-local.ts","../src/make-ajax-request.ts","../src/request-queue.ts","../src/load-svg-cached.ts","../src/load-svg-uncached.ts","../src/unique-id.ts","../src/inject-element.ts","../src/svg-injector.ts"],"sourcesContent":["const cache = new Map<string, SVGSVGElement | Error | undefined>()\n\nexport default cache\n","const cloneSvg = (sourceSvg: SVGSVGElement) =>\n sourceSvg.cloneNode(true) as SVGSVGElement\n\nexport default cloneSvg\n","const isLocal = () => window.location.protocol === 'file:'\n\nexport default isLocal\n","import { parse as parseContentType } from 'content-type'\nimport isLocal from './is-local'\n\nconst makeAjaxRequest = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: (error: Error | null, httpRequest: XMLHttpRequest) => void\n) => {\n const httpRequest = new XMLHttpRequest()\n\n httpRequest.onreadystatechange = () => {\n try {\n if (!/\\.svg/i.test(url) && httpRequest.readyState === 2) {\n const contentType = httpRequest.getResponseHeader('Content-Type')\n if (!contentType) {\n throw new Error('Content type not found')\n }\n\n const { type } = parseContentType(contentType)\n if (!(type === 'image/svg+xml' || type === 'text/plain')) {\n throw new Error(`Invalid content type: ${type}`)\n }\n }\n\n if (httpRequest.readyState === 4) {\n if (httpRequest.status === 404 || httpRequest.responseXML === null) {\n throw new Error(\n isLocal()\n ? 'Note: SVG injection ajax calls do not work locally without ' +\n 'adjusting security settings in your browser. Or consider ' +\n 'using a local webserver.'\n : 'Unable to load SVG file: ' + url\n )\n }\n\n if (\n httpRequest.status === 200 ||\n (isLocal() && httpRequest.status === 0)\n ) {\n callback(null, httpRequest)\n } else {\n throw new Error(\n 'There was a problem injecting the SVG: ' +\n httpRequest.status +\n ' ' +\n httpRequest.statusText\n )\n }\n }\n } catch (error) {\n httpRequest.abort()\n if (error instanceof Error) {\n callback(error, httpRequest)\n } else {\n throw error\n }\n }\n }\n\n httpRequest.open('GET', url)\n\n httpRequest.withCredentials = httpRequestWithCredentials\n\n /* istanbul ignore else */\n if (httpRequest.overrideMimeType) {\n httpRequest.overrideMimeType('text/xml')\n }\n\n httpRequest.send()\n}\n\nexport default makeAjaxRequest\n","import cache from './cache'\nimport cloneSvg from './clone-svg'\nimport { Errback } from './types'\n\nlet requestQueue: { [key: string]: Errback[] } = {}\n\nexport const clear = () => {\n requestQueue = {}\n}\n\nexport const queueRequest = (url: string, callback: Errback) => {\n requestQueue[url] = requestQueue[url] || []\n requestQueue[url].push(callback)\n}\n\nexport const processRequestQueue = (url: string) => {\n for (let i = 0, len = requestQueue[url].length; i < len; i++) {\n // Make these calls async so we avoid blocking the page/renderer.\n setTimeout(() => {\n /* istanbul ignore else */\n if (Array.isArray(requestQueue[url])) {\n const cacheValue = cache.get(url)\n const callback = requestQueue[url][i]\n\n /* istanbul ignore else */\n if (cacheValue instanceof SVGSVGElement) {\n callback(null, cloneSvg(cacheValue))\n }\n\n /* istanbul ignore else */\n if (cacheValue instanceof Error) {\n callback(cacheValue)\n }\n\n /* istanbul ignore else */\n if (i === requestQueue[url].length - 1) {\n delete requestQueue[url]\n }\n }\n }, 0)\n }\n}\n","import cache from './cache'\nimport cloneSvg from './clone-svg'\nimport makeAjaxRequest from './make-ajax-request'\nimport { processRequestQueue, queueRequest } from './request-queue'\nimport { Errback } from './types'\n\nconst loadSvgCached = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: Errback\n) => {\n if (cache.has(url)) {\n const cacheValue = cache.get(url)\n\n if (cacheValue === undefined) {\n queueRequest(url, callback)\n return\n }\n\n /* istanbul ignore else */\n if (cacheValue instanceof SVGSVGElement) {\n callback(null, cloneSvg(cacheValue))\n return\n }\n\n // Errors are always refetched.\n }\n\n // Seed the cache to indicate we are loading this URL.\n cache.set(url, undefined)\n queueRequest(url, callback)\n\n makeAjaxRequest(url, httpRequestWithCredentials, (error, httpRequest) => {\n /* istanbul ignore else */\n if (error) {\n cache.set(url, error)\n } else if (\n httpRequest.responseXML instanceof Document &&\n httpRequest.responseXML.documentElement &&\n httpRequest.responseXML.documentElement instanceof SVGSVGElement\n ) {\n cache.set(url, httpRequest.responseXML.documentElement)\n }\n processRequestQueue(url)\n })\n}\n\nexport default loadSvgCached\n","import makeAjaxRequest from './make-ajax-request'\nimport { Errback } from './types'\n\nconst loadSvgUncached = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: Errback\n) => {\n makeAjaxRequest(url, httpRequestWithCredentials, (error, httpRequest) => {\n /* istanbul ignore else */\n if (error) {\n callback(error)\n } else if (\n httpRequest.responseXML instanceof Document &&\n httpRequest.responseXML.documentElement &&\n httpRequest.responseXML.documentElement instanceof SVGSVGElement\n ) {\n callback(null, httpRequest.responseXML.documentElement)\n }\n })\n}\n\nexport default loadSvgUncached\n","let idCounter = 0\nconst uniqueId = () => ++idCounter\nexport default uniqueId\n","import loadSvgCached from './load-svg-cached'\nimport loadSvgUncached from './load-svg-uncached'\nimport { BeforeEach, Errback, EvalScripts } from './types'\nimport uniqueId from './unique-id'\n\ntype ElementType = Element | HTMLElement | null\n\nconst injectedElements: ElementType[] = []\nconst ranScripts: { [key: string]: boolean } = {}\nconst svgNamespace = 'http://www.w3.org/2000/svg'\nconst xlinkNamespace = 'http://www.w3.org/1999/xlink'\n\nconst injectElement = (\n el: NonNullable<ElementType>,\n evalScripts: EvalScripts,\n renumerateIRIElements: boolean,\n cacheRequests: boolean,\n httpRequestWithCredentials: boolean,\n beforeEach: BeforeEach,\n callback: Errback\n) => {\n const elUrl = el.getAttribute('data-src') || el.getAttribute('src')\n\n /* istanbul ignore else */\n if (!elUrl) {\n callback(new Error('Invalid data-src or src attribute'))\n return\n }\n\n // Make sure we aren't already in the process of injecting this element to\n // avoid a race condition if multiple injections for the same element are run.\n // :NOTE: Using indexOf() only _after_ we check for SVG support and bail, so\n // no need for IE8 indexOf() polyfill.\n /* istanbul ignore else */\n if (injectedElements.indexOf(el) !== -1) {\n // TODO: Extract.\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n return\n }\n\n // Remember the request to inject this element, in case other injection calls\n // are also trying to replace this element before we finish.\n injectedElements.push(el)\n\n // Try to avoid loading the orginal image src if possible.\n el.setAttribute('src', '')\n\n const loadSvg = cacheRequests ? loadSvgCached : loadSvgUncached\n\n loadSvg(elUrl, httpRequestWithCredentials, (error, svg) => {\n /* istanbul ignore else */\n if (!svg) {\n // TODO: Extract.\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n callback(error)\n return\n }\n\n const elId = el.getAttribute('id')\n /* istanbul ignore else */\n if (elId) {\n svg.setAttribute('id', elId)\n }\n\n const elTitle = el.getAttribute('title')\n /* istanbul ignore else */\n if (elTitle) {\n svg.setAttribute('title', elTitle)\n }\n\n const elWidth = el.getAttribute('width')\n /* istanbul ignore else */\n if (elWidth) {\n svg.setAttribute('width', elWidth)\n }\n\n const elHeight = el.getAttribute('height')\n /* istanbul ignore else */\n if (elHeight) {\n svg.setAttribute('height', elHeight)\n }\n\n const mergedClasses = Array.from(\n new Set([\n ...(svg.getAttribute('class') || '').split(' '),\n 'injected-svg',\n ...(el.getAttribute('class') || '').split(' '),\n ])\n )\n .join(' ')\n .trim()\n svg.setAttribute('class', mergedClasses)\n\n const elStyle = el.getAttribute('style')\n /* istanbul ignore else */\n if (elStyle) {\n svg.setAttribute('style', elStyle)\n }\n\n svg.setAttribute('data-src', elUrl)\n\n // Copy all the data elements to the svg.\n const elData = [].filter.call(el.attributes, (at: Attr) => {\n return /^data-\\w[\\w-]*$/.test(at.name)\n })\n\n Array.prototype.forEach.call(elData, (dataAttr: Attr) => {\n /* istanbul ignore else */\n if (dataAttr.name && dataAttr.value) {\n svg.setAttribute(dataAttr.name, dataAttr.value)\n }\n })\n\n /* istanbul ignore else */\n if (renumerateIRIElements) {\n // Make sure any internally referenced clipPath ids and their clip-path\n // references are unique.\n //\n // This addresses the issue of having multiple instances of the same SVG\n // on a page and only the first clipPath id is referenced.\n //\n // Browsers often shortcut the SVG Spec and don't use clipPaths contained\n // in parent elements that are hidden, so if you hide the first SVG\n // instance on the page, then all other instances lose their clipping.\n // Reference: https://bugzilla.mozilla.org/show_bug.cgi?id=376027\n\n // Handle all defs elements that have iri capable attributes as defined by\n // w3c: http://www.w3.org/TR/SVG/linking.html#processingIRI. Mapping IRI\n // addressable elements to the properties that can reference them.\n const iriElementsAndProperties: { [key: string]: string[] } = {\n clipPath: ['clip-path'],\n 'color-profile': ['color-profile'],\n cursor: ['cursor'],\n filter: ['filter'],\n linearGradient: ['fill', 'stroke'],\n marker: ['marker', 'marker-start', 'marker-mid', 'marker-end'],\n mask: ['mask'],\n path: [],\n pattern: ['fill', 'stroke'],\n radialGradient: ['fill', 'stroke'],\n }\n\n let element\n let elements\n let properties\n let currentId: string\n let newId: string\n\n Object.keys(iriElementsAndProperties).forEach((key) => {\n element = key\n properties = iriElementsAndProperties[key]\n\n elements = svg.querySelectorAll(element + '[id]')\n for (let a = 0, elementsLen = elements.length; a < elementsLen; a++) {\n currentId = elements[a].id\n newId = currentId + '-' + uniqueId()\n\n // All of the properties that can reference this element type.\n let referencingElements\n Array.prototype.forEach.call(properties, (property: string) => {\n // :NOTE: using a substring match attr selector here to deal with IE\n // \"adding extra quotes in url() attrs\".\n referencingElements = svg.querySelectorAll(\n '[' + property + '*=\"' + currentId + '\"]'\n )\n for (\n let b = 0, referencingElementLen = referencingElements.length;\n b < referencingElementLen;\n b++\n ) {\n const attrValue: string | null =\n referencingElements[b].getAttribute(property)\n if (\n attrValue &&\n !attrValue.match(new RegExp('url\\\\(\"?#' + currentId + '\"?\\\\)'))\n ) {\n continue\n }\n referencingElements[b].setAttribute(\n property,\n 'url(#' + newId + ')'\n )\n }\n })\n\n const allLinks = svg.querySelectorAll('[*|href]')\n const links = []\n for (let c = 0, allLinksLen = allLinks.length; c < allLinksLen; c++) {\n const href = allLinks[c].getAttributeNS(xlinkNamespace, 'href')\n /* istanbul ignore else */\n if (href && href.toString() === '#' + elements[a].id) {\n links.push(allLinks[c])\n }\n }\n for (let d = 0, linksLen = links.length; d < linksLen; d++) {\n links[d].setAttributeNS(xlinkNamespace, 'href', '#' + newId)\n }\n\n elements[a].id = newId\n }\n })\n }\n\n // Remove any unwanted/invalid namespaces that might have been added by SVG\n // editing tools.\n svg.removeAttribute('xmlns:a')\n\n // Post page load injected SVGs don't automatically have their script\n // elements run, so we'll need to make that happen, if requested.\n\n // Find then prune the scripts.\n const scripts = svg.querySelectorAll('script')\n const scriptsToEval: string[] = []\n let script\n let scriptType\n\n for (let i = 0, scriptsLen = scripts.length; i < scriptsLen; i++) {\n scriptType = scripts[i].getAttribute('type')\n\n // Only process javascript types. SVG defaults to 'application/ecmascript'\n // for unset types.\n /* istanbul ignore else */\n if (\n !scriptType ||\n scriptType === 'application/ecmascript' ||\n scriptType === 'application/javascript' ||\n scriptType === 'text/javascript'\n ) {\n // innerText for IE, textContent for other browsers.\n script = scripts[i].innerText || scripts[i].textContent\n\n // Stash.\n /* istanbul ignore else */\n if (script) {\n scriptsToEval.push(script)\n }\n\n // Tidy up and remove the script element since we don't need it anymore.\n svg.removeChild(scripts[i])\n }\n }\n\n // Run/Eval the scripts if needed.\n /* istanbul ignore else */\n if (\n scriptsToEval.length > 0 &&\n (evalScripts === 'always' ||\n (evalScripts === 'once' && !ranScripts[elUrl]))\n ) {\n for (\n let l = 0, scriptsToEvalLen = scriptsToEval.length;\n l < scriptsToEvalLen;\n l++\n ) {\n // :NOTE: Yup, this is a form of eval, but it is being used to eval code\n // the caller has explictely asked to be loaded, and the code is in a\n // caller defined SVG file... not raw user input.\n //\n // Also, the code is evaluated in a closure and not in the global scope.\n // If you need to put something in global scope, use 'window'.\n new Function(scriptsToEval[l])(window)\n }\n\n // Remember we already ran scripts for this svg.\n ranScripts[elUrl] = true\n }\n\n // :WORKAROUND: IE doesn't evaluate <style> tags in SVGs that are\n // dynamically added to the page. This trick will trigger IE to read and use\n // any existing SVG <style> tags.\n //\n // Reference: https://github.com/iconic/SVGInjector/issues/23.\n const styleTags = svg.querySelectorAll('style')\n Array.prototype.forEach.call(styleTags, (styleTag: HTMLStyleElement) => {\n styleTag.textContent += ''\n })\n\n svg.setAttribute('xmlns', svgNamespace)\n svg.setAttribute('xmlns:xlink', xlinkNamespace)\n\n beforeEach(svg)\n\n if (!el.parentNode) {\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n callback(new Error('Parent node is null'))\n return\n }\n\n // Replace the image with the svg.\n el.parentNode.replaceChild(svg, el)\n\n // Now that we no longer need it, drop references to the original element so\n // it can be GC'd.\n // TODO: Extract\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n\n callback(null, svg)\n })\n}\n\nexport default injectElement\n","import injectElement from './inject-element'\nimport { AfterAll, BeforeEach, Errback, EvalScripts } from './types'\n\ntype Elements = HTMLCollectionOf<Element> | NodeListOf<Element> | Element | null\n\ninterface OptionalArgs {\n afterAll?: AfterAll\n afterEach?: Errback\n beforeEach?: BeforeEach\n cacheRequests?: boolean\n evalScripts?: EvalScripts\n httpRequestWithCredentials?: boolean\n renumerateIRIElements?: boolean\n}\n\nconst SVGInjector = (\n elements: Elements,\n {\n afterAll = () => undefined,\n afterEach = () => undefined,\n beforeEach = () => undefined,\n cacheRequests = true,\n evalScripts = 'never',\n httpRequestWithCredentials = false,\n renumerateIRIElements = true,\n }: OptionalArgs = {}\n) => {\n if (elements && 'length' in elements) {\n let elementsLoaded = 0\n for (let i = 0, j = elements.length; i < j; i++) {\n injectElement(\n elements[i],\n evalScripts,\n renumerateIRIElements,\n cacheRequests,\n httpRequestWithCredentials,\n beforeEach,\n (error, svg) => {\n afterEach(error, svg)\n if (\n elements &&\n 'length' in elements &&\n elements.length === ++elementsLoaded\n ) {\n afterAll(elementsLoaded)\n }\n }\n )\n }\n } else if (elements) {\n injectElement(\n elements,\n evalScripts,\n renumerateIRIElements,\n cacheRequests,\n httpRequestWithCredentials,\n beforeEach,\n (error, svg) => {\n afterEach(error, svg)\n afterAll(1)\n elements = null\n }\n )\n } else {\n afterAll(0)\n }\n}\n\nexport default SVGInjector\n"],"names":["cache","Map","cloneSvg","sourceSvg","cloneNode","isLocal","location","protocol","makeAjaxRequest","callback","httpRequest","onreadystatechange","test","url","readyState","contentType","Error","parseContentType","type","status","responseXML","statusText","error","abort","requestQueue","queueRequest","push","len","setTimeout","cacheValue","get","i","SVGSVGElement","length","httpRequestWithCredentials","has","undefined","set","loadSvgUncached","Document","idCounter","uniqueId","injectedElements","ranScripts","svgNamespace","xlinkNamespace","el","renumerateIRIElements","beforeEach","elUrl","getAttribute","indexOf","splice","cacheRequests","loadSvgCached","loadSvg","svg","elId","setAttribute","elTitle","elWidth","elHeight","mergedClasses","Array","from","split","join","trim","elStyle","elData","filter","call","attributes","at","name","prototype","forEach","dataAttr","value","iriElementsAndProperties_1","clipPath","linearGradient","marker","pattern","element_1","elements_1","properties_1","currentId_1","newId_1","Object","keys","key","querySelectorAll","a","elementsLen","id","property","referencingElements","SVGInjector","elements","_a","_b","_c","afterAll","_d","_e","_f","_g","evalScripts","_h","_j"],"mappings":";;;;;;;AAAA,IAAWA,KAAA,GAAO,IAAAC,GAAA,EAAlB;;ACAA,IAAMC,QAAW,GAAA,SAAXA,QAAW,CAAAC,SAAA,EAAyB;EACxC,OAAA,SAAA,CAASC,SAAT,CAAmB,IAAnB,CAAA,CAAA;AAA0C,CAD5C;;ACAA,IAAaC,OAAA,GAAS,SAATA,OAAS,GAAA;AAAA,EAAA,OAAA,MAAA,CAAAC,QAAA,CAAMC,QAAN,KAAgB,OAAhB,CAAA;AAAwB,CAA9C;;ACQE,IAAAC,eAAiB,GAAO,SAAxBA,eAAwB,IAAA,4BAAA,EAIpBC,QAJoB,EAIqC;iBAEnD;;EAEHC,WAAA,CAAAC,kBAAA,GAAA,YAAA;IAEO,IAAA;oBACJC,KAAEC,QAASH,WAAA,CAAAI,UAAA,KAAwC,GAAA;AACrD,QAAA,IAAAC,aAAA,gDAAA,CAAA;;QACD,IAAA,CAAAA,aAAA,EAAA;AACF,UAAA,MAAA,IAAAC,KAAA,CAAA,wBAAA,CAAA,CAAA;AAED,SAAA;;AAEI,QAAA,IAAA,IAAA,GAAAC,iBACE,CAAAF,aAAA,CADF,CACWG,IADX,CAAA;;QAEI,IAAA,EAAAA,IAAA,KAAA,eAAA,IAA+DA,IAAA,KAAA,YAA/D,CAAA,EAA+D;kDACFA,CAAAA,MAAAA,CAAAA;;AAE7D,OAAA;;AAIN,MAAA,IAAAR,WACa,CAAAI,UADb,KACoB,CADpB,EACoB;uBACjB,CAAAK,WAAa,OAAAT,WAAkB,CAAAU,WAAlB,KACd,MAAA;AACA,UAAA,MAAA,IAAAJ,KAAA,CACDX,OAAA,EAAM,GAAA,6DAAA,8DAAA,GAGD,0BAHL,8BAIQQ,GAAAA,GALP,CAAA,CAAA;AAQD,SAAA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AAAME,MAAAA,EAAAA,GAAAA,EAAAA,CAAAA,0BAAAA;MAAA3E,0BAAA,GAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,GAAA2E,EAAAA;;2BACM,GAAA,EAAA,KAAA,KAAA,CAAA,GAAA,IAAA,GAAAC,EAAAA,CAAAA;;AAIf,EAAA,IAAAX,QAAA,YAA0BA,IAAAA,QAA1B,EAA0B;;;;;;;;;;;;GAA1B;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"svg-injector.cjs.development.js","sources":["../src/cache.ts","../src/clone-svg.ts","../src/is-local.ts","../src/make-ajax-request.ts","../src/request-queue.ts","../src/load-svg-cached.ts","../src/load-svg-uncached.ts","../src/unique-id.ts","../src/inject-element.ts","../src/svg-injector.ts"],"sourcesContent":["const cache = new Map<string, SVGSVGElement | Error | undefined>()\n\nexport default cache\n","const cloneSvg = (sourceSvg: SVGSVGElement) =>\n sourceSvg.cloneNode(true) as SVGSVGElement\n\nexport default cloneSvg\n","const isLocal = () => window.location.protocol === 'file:'\n\nexport default isLocal\n","import { parse as parseContentType } from 'content-type'\nimport isLocal from './is-local'\n\nconst makeAjaxRequest = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: (error: Error | null, httpRequest: XMLHttpRequest) => void\n) => {\n const httpRequest = new XMLHttpRequest()\n\n httpRequest.onreadystatechange = () => {\n try {\n if (!/\\.svg/i.test(url) && httpRequest.readyState === 2) {\n const contentType = httpRequest.getResponseHeader('Content-Type')\n if (!contentType) {\n throw new Error('Content type not found')\n }\n\n const { type } = parseContentType(contentType)\n if (!(type === 'image/svg+xml' || type === 'text/plain')) {\n throw new Error(`Invalid content type: ${type}`)\n }\n }\n\n if (httpRequest.readyState === 4) {\n if (httpRequest.status === 404 || httpRequest.responseXML === null) {\n throw new Error(\n isLocal()\n ? 'Note: SVG injection ajax calls do not work locally without ' +\n 'adjusting security settings in your browser. Or consider ' +\n 'using a local webserver.'\n : 'Unable to load SVG file: ' + url\n )\n }\n\n if (\n httpRequest.status === 200 ||\n (isLocal() && httpRequest.status === 0)\n ) {\n callback(null, httpRequest)\n } else {\n throw new Error(\n 'There was a problem injecting the SVG: ' +\n httpRequest.status +\n ' ' +\n httpRequest.statusText\n )\n }\n }\n } catch (error) {\n httpRequest.abort()\n if (error instanceof Error) {\n callback(error, httpRequest)\n } else {\n throw error\n }\n }\n }\n\n httpRequest.open('GET', url)\n\n httpRequest.withCredentials = httpRequestWithCredentials\n\n /* istanbul ignore else */\n if (httpRequest.overrideMimeType) {\n httpRequest.overrideMimeType('text/xml')\n }\n\n httpRequest.send()\n}\n\nexport default makeAjaxRequest\n","import cache from './cache'\nimport cloneSvg from './clone-svg'\nimport { Errback } from './types'\n\nlet requestQueue: { [key: string]: Errback[] } = {}\n\nexport const clear = () => {\n requestQueue = {}\n}\n\nexport const queueRequest = (url: string, callback: Errback) => {\n requestQueue[url] = requestQueue[url] || []\n requestQueue[url].push(callback)\n}\n\nexport const processRequestQueue = (url: string) => {\n for (let i = 0, len = requestQueue[url].length; i < len; i++) {\n // Make these calls async so we avoid blocking the page/renderer.\n setTimeout(() => {\n /* istanbul ignore else */\n if (Array.isArray(requestQueue[url])) {\n const cacheValue = cache.get(url)\n const callback = requestQueue[url][i]\n\n /* istanbul ignore else */\n if (cacheValue instanceof SVGSVGElement) {\n callback(null, cloneSvg(cacheValue))\n }\n\n /* istanbul ignore else */\n if (cacheValue instanceof Error) {\n callback(cacheValue)\n }\n\n /* istanbul ignore else */\n if (i === requestQueue[url].length - 1) {\n delete requestQueue[url]\n }\n }\n }, 0)\n }\n}\n","import cache from './cache'\nimport cloneSvg from './clone-svg'\nimport makeAjaxRequest from './make-ajax-request'\nimport { processRequestQueue, queueRequest } from './request-queue'\nimport { Errback } from './types'\n\nconst loadSvgCached = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: Errback\n) => {\n if (cache.has(url)) {\n const cacheValue = cache.get(url)\n\n if (cacheValue === undefined) {\n queueRequest(url, callback)\n return\n }\n\n /* istanbul ignore else */\n if (cacheValue instanceof SVGSVGElement) {\n callback(null, cloneSvg(cacheValue))\n return\n }\n\n // Errors are always refetched.\n }\n\n // Seed the cache to indicate we are loading this URL.\n cache.set(url, undefined)\n queueRequest(url, callback)\n\n makeAjaxRequest(url, httpRequestWithCredentials, (error, httpRequest) => {\n /* istanbul ignore else */\n if (error) {\n cache.set(url, error)\n } else if (\n httpRequest.responseXML instanceof Document &&\n httpRequest.responseXML.documentElement &&\n httpRequest.responseXML.documentElement instanceof SVGSVGElement\n ) {\n cache.set(url, httpRequest.responseXML.documentElement)\n }\n processRequestQueue(url)\n })\n}\n\nexport default loadSvgCached\n","import makeAjaxRequest from './make-ajax-request'\nimport { Errback } from './types'\n\nconst loadSvgUncached = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: Errback\n) => {\n makeAjaxRequest(url, httpRequestWithCredentials, (error, httpRequest) => {\n /* istanbul ignore else */\n if (error) {\n callback(error)\n } else if (\n httpRequest.responseXML instanceof Document &&\n httpRequest.responseXML.documentElement &&\n httpRequest.responseXML.documentElement instanceof SVGSVGElement\n ) {\n callback(null, httpRequest.responseXML.documentElement)\n }\n })\n}\n\nexport default loadSvgUncached\n","let idCounter = 0\nconst uniqueId = () => ++idCounter\nexport default uniqueId\n","import loadSvgCached from './load-svg-cached'\nimport loadSvgUncached from './load-svg-uncached'\nimport { BeforeEach, Errback, EvalScripts } from './types'\nimport uniqueId from './unique-id'\n\ntype ElementType = Element | HTMLElement | null\n\nconst injectedElements: ElementType[] = []\nconst ranScripts: { [key: string]: boolean } = {}\nconst svgNamespace = 'http://www.w3.org/2000/svg'\nconst xlinkNamespace = 'http://www.w3.org/1999/xlink'\n\nconst injectElement = (\n el: NonNullable<ElementType>,\n evalScripts: EvalScripts,\n renumerateIRIElements: boolean,\n cacheRequests: boolean,\n httpRequestWithCredentials: boolean,\n beforeEach: BeforeEach,\n callback: Errback\n) => {\n const elUrl = el.getAttribute('data-src') || el.getAttribute('src')\n\n /* istanbul ignore else */\n if (!elUrl) {\n callback(new Error('Invalid data-src or src attribute'))\n return\n }\n\n // Make sure we aren't already in the process of injecting this element to\n // avoid a race condition if multiple injections for the same element are run.\n // :NOTE: Using indexOf() only _after_ we check for SVG support and bail, so\n // no need for IE8 indexOf() polyfill.\n /* istanbul ignore else */\n if (injectedElements.indexOf(el) !== -1) {\n // TODO: Extract.\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n return\n }\n\n // Remember the request to inject this element, in case other injection calls\n // are also trying to replace this element before we finish.\n injectedElements.push(el)\n\n // Try to avoid loading the orginal image src if possible.\n el.setAttribute('src', '')\n\n const loadSvg = cacheRequests ? loadSvgCached : loadSvgUncached\n\n loadSvg(elUrl, httpRequestWithCredentials, (error, svg) => {\n /* istanbul ignore else */\n if (!svg) {\n // TODO: Extract.\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n callback(error)\n return\n }\n\n const elId = el.getAttribute('id')\n /* istanbul ignore else */\n if (elId) {\n svg.setAttribute('id', elId)\n }\n\n const elTitle = el.getAttribute('title')\n /* istanbul ignore else */\n if (elTitle) {\n svg.setAttribute('title', elTitle)\n }\n\n const elWidth = el.getAttribute('width')\n /* istanbul ignore else */\n if (elWidth) {\n svg.setAttribute('width', elWidth)\n }\n\n const elHeight = el.getAttribute('height')\n /* istanbul ignore else */\n if (elHeight) {\n svg.setAttribute('height', elHeight)\n }\n\n const mergedClasses = Array.from(\n new Set([\n ...(svg.getAttribute('class') || '').split(' '),\n 'injected-svg',\n ...(el.getAttribute('class') || '').split(' '),\n ])\n )\n .join(' ')\n .trim()\n svg.setAttribute('class', mergedClasses)\n\n const elStyle = el.getAttribute('style')\n /* istanbul ignore else */\n if (elStyle) {\n svg.setAttribute('style', elStyle)\n }\n\n svg.setAttribute('data-src', elUrl)\n\n // Copy all the data elements to the svg.\n const elData = [].filter.call(el.attributes, (at: Attr) => {\n return /^data-\\w[\\w-]*$/.test(at.name)\n })\n\n Array.prototype.forEach.call(elData, (dataAttr: Attr) => {\n /* istanbul ignore else */\n if (dataAttr.name && dataAttr.value) {\n svg.setAttribute(dataAttr.name, dataAttr.value)\n }\n })\n\n /* istanbul ignore else */\n if (renumerateIRIElements) {\n // Make sure any internally referenced clipPath ids and their clip-path\n // references are unique.\n //\n // This addresses the issue of having multiple instances of the same SVG\n // on a page and only the first clipPath id is referenced.\n //\n // Browsers often shortcut the SVG Spec and don't use clipPaths contained\n // in parent elements that are hidden, so if you hide the first SVG\n // instance on the page, then all other instances lose their clipping.\n // Reference: https://bugzilla.mozilla.org/show_bug.cgi?id=376027\n\n // Handle all defs elements that have iri capable attributes as defined by\n // w3c: http://www.w3.org/TR/SVG/linking.html#processingIRI. Mapping IRI\n // addressable elements to the properties that can reference them.\n const iriElementsAndProperties: { [key: string]: string[] } = {\n clipPath: ['clip-path'],\n 'color-profile': ['color-profile'],\n cursor: ['cursor'],\n filter: ['filter'],\n linearGradient: ['fill', 'stroke'],\n marker: ['marker', 'marker-start', 'marker-mid', 'marker-end'],\n mask: ['mask'],\n path: [],\n pattern: ['fill', 'stroke'],\n radialGradient: ['fill', 'stroke'],\n }\n\n let element\n let elements\n let properties\n let currentId: string\n let newId: string\n\n Object.keys(iriElementsAndProperties).forEach((key) => {\n element = key\n properties = iriElementsAndProperties[key]\n\n elements = svg.querySelectorAll(element + '[id]')\n for (let a = 0, elementsLen = elements.length; a < elementsLen; a++) {\n currentId = elements[a].id\n newId = currentId + '-' + uniqueId()\n\n // All of the properties that can reference this element type.\n let referencingElements\n Array.prototype.forEach.call(properties, (property: string) => {\n // :NOTE: using a substring match attr selector here to deal with IE\n // \"adding extra quotes in url() attrs\".\n referencingElements = svg.querySelectorAll(\n '[' + property + '*=\"' + currentId + '\"]'\n )\n for (\n let b = 0, referencingElementLen = referencingElements.length;\n b < referencingElementLen;\n b++\n ) {\n const attrValue: string | null =\n referencingElements[b].getAttribute(property)\n if (\n attrValue &&\n !attrValue.match(new RegExp('url\\\\(\"?#' + currentId + '\"?\\\\)'))\n ) {\n continue\n }\n referencingElements[b].setAttribute(\n property,\n 'url(#' + newId + ')'\n )\n }\n })\n\n const allLinks = svg.querySelectorAll('[*|href]')\n const links = []\n for (let c = 0, allLinksLen = allLinks.length; c < allLinksLen; c++) {\n const href = allLinks[c].getAttributeNS(xlinkNamespace, 'href')\n /* istanbul ignore else */\n if (href && href.toString() === '#' + elements[a].id) {\n links.push(allLinks[c])\n }\n }\n for (let d = 0, linksLen = links.length; d < linksLen; d++) {\n links[d].setAttributeNS(xlinkNamespace, 'href', '#' + newId)\n }\n\n elements[a].id = newId\n }\n })\n }\n\n // Remove any unwanted/invalid namespaces that might have been added by SVG\n // editing tools.\n svg.removeAttribute('xmlns:a')\n\n // Post page load injected SVGs don't automatically have their script\n // elements run, so we'll need to make that happen, if requested.\n\n // Find then prune the scripts.\n const scripts = svg.querySelectorAll('script')\n const scriptsToEval: string[] = []\n let script\n let scriptType\n\n for (let i = 0, scriptsLen = scripts.length; i < scriptsLen; i++) {\n scriptType = scripts[i].getAttribute('type')\n\n // Only process javascript types. SVG defaults to 'application/ecmascript'\n // for unset types.\n /* istanbul ignore else */\n if (\n !scriptType ||\n scriptType === 'application/ecmascript' ||\n scriptType === 'application/javascript' ||\n scriptType === 'text/javascript'\n ) {\n // innerText for IE, textContent for other browsers.\n script = scripts[i].innerText || scripts[i].textContent\n\n // Stash.\n /* istanbul ignore else */\n if (script) {\n scriptsToEval.push(script)\n }\n\n // Tidy up and remove the script element since we don't need it anymore.\n svg.removeChild(scripts[i])\n }\n }\n\n // Run/Eval the scripts if needed.\n /* istanbul ignore else */\n if (\n scriptsToEval.length > 0 &&\n (evalScripts === 'always' ||\n (evalScripts === 'once' && !ranScripts[elUrl]))\n ) {\n for (\n let l = 0, scriptsToEvalLen = scriptsToEval.length;\n l < scriptsToEvalLen;\n l++\n ) {\n // :NOTE: Yup, this is a form of eval, but it is being used to eval code\n // the caller has explictely asked to be loaded, and the code is in a\n // caller defined SVG file... not raw user input.\n //\n // Also, the code is evaluated in a closure and not in the global scope.\n // If you need to put something in global scope, use 'window'.\n new Function(scriptsToEval[l])(window)\n }\n\n // Remember we already ran scripts for this svg.\n ranScripts[elUrl] = true\n }\n\n // :WORKAROUND: IE doesn't evaluate <style> tags in SVGs that are\n // dynamically added to the page. This trick will trigger IE to read and use\n // any existing SVG <style> tags.\n //\n // Reference: https://github.com/iconic/SVGInjector/issues/23.\n const styleTags = svg.querySelectorAll('style')\n Array.prototype.forEach.call(styleTags, (styleTag: HTMLStyleElement) => {\n styleTag.textContent += ''\n })\n\n svg.setAttribute('xmlns', svgNamespace)\n svg.setAttribute('xmlns:xlink', xlinkNamespace)\n\n beforeEach(svg)\n\n if (!el.parentNode) {\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n callback(new Error('Parent node is null'))\n return\n }\n\n // Replace the image with the svg.\n el.parentNode.replaceChild(svg, el)\n\n // Now that we no longer need it, drop references to the original element so\n // it can be GC'd.\n // TODO: Extract\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n\n callback(null, svg)\n })\n}\n\nexport default injectElement\n","import injectElement from './inject-element'\nimport { AfterAll, BeforeEach, Errback, EvalScripts } from './types'\n\ntype Elements = HTMLCollectionOf<Element> | NodeListOf<Element> | Element | null\n\ninterface OptionalArgs {\n afterAll?: AfterAll\n afterEach?: Errback\n beforeEach?: BeforeEach\n cacheRequests?: boolean\n evalScripts?: EvalScripts\n httpRequestWithCredentials?: boolean\n renumerateIRIElements?: boolean\n}\n\nconst SVGInjector = (\n elements: Elements,\n {\n afterAll = () => undefined,\n afterEach = () => undefined,\n beforeEach = () => undefined,\n cacheRequests = true,\n evalScripts = 'never',\n httpRequestWithCredentials = false,\n renumerateIRIElements = true,\n }: OptionalArgs = {}\n) => {\n if (elements && 'length' in elements) {\n let elementsLoaded = 0\n for (let i = 0, j = elements.length; i < j; i++) {\n injectElement(\n elements[i],\n evalScripts,\n renumerateIRIElements,\n cacheRequests,\n httpRequestWithCredentials,\n beforeEach,\n (error, svg) => {\n afterEach(error, svg)\n if (\n elements &&\n 'length' in elements &&\n elements.length === ++elementsLoaded\n ) {\n afterAll(elementsLoaded)\n }\n }\n )\n }\n } else if (elements) {\n injectElement(\n elements,\n evalScripts,\n renumerateIRIElements,\n cacheRequests,\n httpRequestWithCredentials,\n beforeEach,\n (error, svg) => {\n afterEach(error, svg)\n afterAll(1)\n elements = null\n }\n )\n } else {\n afterAll(0)\n }\n}\n\nexport default SVGInjector\n"],"names":["cache","Map","cloneSvg","sourceSvg","cloneNode","isLocal","location","protocol","makeAjaxRequest","callback","httpRequest","onreadystatechange","test","url","readyState","contentType","Error","parseContentType","type","status","responseXML","statusText","error","abort","requestQueue","queueRequest","push","len","setTimeout","cacheValue","get","i","SVGSVGElement","length","httpRequestWithCredentials","has","undefined","set","loadSvgUncached","Document","idCounter","uniqueId","injectedElements","ranScripts","svgNamespace","xlinkNamespace","el","renumerateIRIElements","beforeEach","elUrl","getAttribute","indexOf","splice","cacheRequests","loadSvgCached","loadSvg","svg","elId","setAttribute","elTitle","elWidth","elHeight","mergedClasses","Array","from","split","join","trim","elStyle","elData","filter","call","attributes","at","name","prototype","forEach","dataAttr","value","iriElementsAndProperties_1","clipPath","linearGradient","marker","pattern","element_1","elements_1","properties_1","currentId_1","newId_1","Object","keys","key","querySelectorAll","a","elementsLen","id","property","referencingElements","SVGInjector","elements","_a","_b","_c","afterAll","_f","_g","evalScripts","_h"],"mappings":";;;;;;;AAAA,IAAWA,KAAA,GAAO,IAAAC,GAAgD,EAAA;;ACAlE,IAAMC,QAAW,GAAA,SAAXA,QAAW,CAAAC,SAAyB,EAAA;AACxC,EAAA,OAAA,SAAA,CAASC,SAAC,CAAS,IAAA,CAAuB,CAAA;AAA1C,CAA0C;;ACD5C,IAAaC,OAAA,GAAS,SAATA,OAAA,GAAS;AAAA,EAAA,OAAA,MAAA,CAAAC,QAAM,CAAAC,QAAS,KAAC,OAAQ,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A,CAAMC,UACLC,EAeD,EAAA;AAfCC,EAAAA,IAAAA,EAAAA,GAAAA,EAAAA,KAAAA,KAAAA,CAAAA,GAeD,EAAA,GAAA,EAAA;IAPKC,EAAiB,GAAA,EAAA,CAAA,QAAA;AAAjBC,IAAAA,QAAA,GAAA,EAAA,KAAA,KAAA,CAAA,GAAA,YAAA;MAAS,OAAQ,SAAA,CAAA;KAAA,GAAA,EAAA;MACN,GAAA,EAAA,CAAA,SAAA;aAAH,GAAA,EAAA,KAAA,KAAA,CAAA,GAAA,YAAA;MAAE,OAAC,SAAA,CAAA;KAAA,GAAA,EAAA;MACI,GAAA,EAAA,CAAA,UAAA;cAAP,GAAA,EAAA,KAAA,KAAA,CAAA,GAAA,YAAA;MAAG,OAAI,SAAA,CAAA;KAAA,GAAA,EAAA;IACjBC,EACD,GAAA,EAAA,CAAA,aAAA;AADCnD,IAAAA,aACD,mBAAA,IAAA,GAAA,EAAA;IACFoD,EAAA,GAAA,EAAA,CAAA,WAAA;AAAAC,IAAAA,WAAA,mBAAA,OAAA,GAAA,EAAA;IAAMC,EAAA,GAAA,EAAA,CAAA,0BAAA;AAAAzE,IAAAA,0BAAA,mBAAA,KAAA,GAAA,EAAA;MACM,GAAA,EAAA,CAAA,qBAAA;yBAAA,mBAAA,IAAA,GAAA,EAAA,CAAA;AAIf,EAAA,IAAAiE,QAAA,YAA0B,IAAAA,QAAA,EAAA;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"svg-injector.cjs.production.js","sources":["../src/cache.ts","../src/clone-svg.ts","../src/is-local.ts","../src/make-ajax-request.ts","../src/request-queue.ts","../src/load-svg-cached.ts","../src/load-svg-uncached.ts","../src/unique-id.ts","../src/inject-element.ts","../src/svg-injector.ts"],"sourcesContent":["const cache = new Map<string, SVGSVGElement | Error | undefined>()\n\nexport default cache\n","const cloneSvg = (sourceSvg: SVGSVGElement) =>\n sourceSvg.cloneNode(true) as SVGSVGElement\n\nexport default cloneSvg\n","const isLocal = () => window.location.protocol === 'file:'\n\nexport default isLocal\n","import { parse as parseContentType } from 'content-type'\nimport isLocal from './is-local'\n\nconst makeAjaxRequest = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: (error: Error | null, httpRequest: XMLHttpRequest) => void\n) => {\n const httpRequest = new XMLHttpRequest()\n\n httpRequest.onreadystatechange = () => {\n try {\n if (!/\\.svg/i.test(url) && httpRequest.readyState === 2) {\n const contentType = httpRequest.getResponseHeader('Content-Type')\n if (!contentType) {\n throw new Error('Content type not found')\n }\n\n const { type } = parseContentType(contentType)\n if (!(type === 'image/svg+xml' || type === 'text/plain')) {\n throw new Error(`Invalid content type: ${type}`)\n }\n }\n\n if (httpRequest.readyState === 4) {\n if (httpRequest.status === 404 || httpRequest.responseXML === null) {\n throw new Error(\n isLocal()\n ? 'Note: SVG injection ajax calls do not work locally without ' +\n 'adjusting security settings in your browser. Or consider ' +\n 'using a local webserver.'\n : 'Unable to load SVG file: ' + url\n )\n }\n\n if (\n httpRequest.status === 200 ||\n (isLocal() && httpRequest.status === 0)\n ) {\n callback(null, httpRequest)\n } else {\n throw new Error(\n 'There was a problem injecting the SVG: ' +\n httpRequest.status +\n ' ' +\n httpRequest.statusText\n )\n }\n }\n } catch (error) {\n httpRequest.abort()\n if (error instanceof Error) {\n callback(error, httpRequest)\n } else {\n throw error\n }\n }\n }\n\n httpRequest.open('GET', url)\n\n httpRequest.withCredentials = httpRequestWithCredentials\n\n /* istanbul ignore else */\n if (httpRequest.overrideMimeType) {\n httpRequest.overrideMimeType('text/xml')\n }\n\n httpRequest.send()\n}\n\nexport default makeAjaxRequest\n","import cache from './cache'\nimport cloneSvg from './clone-svg'\nimport { Errback } from './types'\n\nlet requestQueue: { [key: string]: Errback[] } = {}\n\nexport const clear = () => {\n requestQueue = {}\n}\n\nexport const queueRequest = (url: string, callback: Errback) => {\n requestQueue[url] = requestQueue[url] || []\n requestQueue[url].push(callback)\n}\n\nexport const processRequestQueue = (url: string) => {\n for (let i = 0, len = requestQueue[url].length; i < len; i++) {\n // Make these calls async so we avoid blocking the page/renderer.\n setTimeout(() => {\n /* istanbul ignore else */\n if (Array.isArray(requestQueue[url])) {\n const cacheValue = cache.get(url)\n const callback = requestQueue[url][i]\n\n /* istanbul ignore else */\n if (cacheValue instanceof SVGSVGElement) {\n callback(null, cloneSvg(cacheValue))\n }\n\n /* istanbul ignore else */\n if (cacheValue instanceof Error) {\n callback(cacheValue)\n }\n\n /* istanbul ignore else */\n if (i === requestQueue[url].length - 1) {\n delete requestQueue[url]\n }\n }\n }, 0)\n }\n}\n","import cache from './cache'\nimport cloneSvg from './clone-svg'\nimport makeAjaxRequest from './make-ajax-request'\nimport { processRequestQueue, queueRequest } from './request-queue'\nimport { Errback } from './types'\n\nconst loadSvgCached = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: Errback\n) => {\n if (cache.has(url)) {\n const cacheValue = cache.get(url)\n\n if (cacheValue === undefined) {\n queueRequest(url, callback)\n return\n }\n\n /* istanbul ignore else */\n if (cacheValue instanceof SVGSVGElement) {\n callback(null, cloneSvg(cacheValue))\n return\n }\n\n // Errors are always refetched.\n }\n\n // Seed the cache to indicate we are loading this URL.\n cache.set(url, undefined)\n queueRequest(url, callback)\n\n makeAjaxRequest(url, httpRequestWithCredentials, (error, httpRequest) => {\n /* istanbul ignore else */\n if (error) {\n cache.set(url, error)\n } else if (\n httpRequest.responseXML instanceof Document &&\n httpRequest.responseXML.documentElement &&\n httpRequest.responseXML.documentElement instanceof SVGSVGElement\n ) {\n cache.set(url, httpRequest.responseXML.documentElement)\n }\n processRequestQueue(url)\n })\n}\n\nexport default loadSvgCached\n","import makeAjaxRequest from './make-ajax-request'\nimport { Errback } from './types'\n\nconst loadSvgUncached = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: Errback\n) => {\n makeAjaxRequest(url, httpRequestWithCredentials, (error, httpRequest) => {\n /* istanbul ignore else */\n if (error) {\n callback(error)\n } else if (\n httpRequest.responseXML instanceof Document &&\n httpRequest.responseXML.documentElement &&\n httpRequest.responseXML.documentElement instanceof SVGSVGElement\n ) {\n callback(null, httpRequest.responseXML.documentElement)\n }\n })\n}\n\nexport default loadSvgUncached\n","let idCounter = 0\nconst uniqueId = () => ++idCounter\nexport default uniqueId\n","import loadSvgCached from './load-svg-cached'\nimport loadSvgUncached from './load-svg-uncached'\nimport { BeforeEach, Errback, EvalScripts } from './types'\nimport uniqueId from './unique-id'\n\ntype ElementType = Element | HTMLElement | null\n\nconst injectedElements: ElementType[] = []\nconst ranScripts: { [key: string]: boolean } = {}\nconst svgNamespace = 'http://www.w3.org/2000/svg'\nconst xlinkNamespace = 'http://www.w3.org/1999/xlink'\n\nconst injectElement = (\n el: NonNullable<ElementType>,\n evalScripts: EvalScripts,\n renumerateIRIElements: boolean,\n cacheRequests: boolean,\n httpRequestWithCredentials: boolean,\n beforeEach: BeforeEach,\n callback: Errback\n) => {\n const elUrl = el.getAttribute('data-src') || el.getAttribute('src')\n\n /* istanbul ignore else */\n if (!elUrl) {\n callback(new Error('Invalid data-src or src attribute'))\n return\n }\n\n // Make sure we aren't already in the process of injecting this element to\n // avoid a race condition if multiple injections for the same element are run.\n // :NOTE: Using indexOf() only _after_ we check for SVG support and bail, so\n // no need for IE8 indexOf() polyfill.\n /* istanbul ignore else */\n if (injectedElements.indexOf(el) !== -1) {\n // TODO: Extract.\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n return\n }\n\n // Remember the request to inject this element, in case other injection calls\n // are also trying to replace this element before we finish.\n injectedElements.push(el)\n\n // Try to avoid loading the orginal image src if possible.\n el.setAttribute('src', '')\n\n const loadSvg = cacheRequests ? loadSvgCached : loadSvgUncached\n\n loadSvg(elUrl, httpRequestWithCredentials, (error, svg) => {\n /* istanbul ignore else */\n if (!svg) {\n // TODO: Extract.\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n callback(error)\n return\n }\n\n const elId = el.getAttribute('id')\n /* istanbul ignore else */\n if (elId) {\n svg.setAttribute('id', elId)\n }\n\n const elTitle = el.getAttribute('title')\n /* istanbul ignore else */\n if (elTitle) {\n svg.setAttribute('title', elTitle)\n }\n\n const elWidth = el.getAttribute('width')\n /* istanbul ignore else */\n if (elWidth) {\n svg.setAttribute('width', elWidth)\n }\n\n const elHeight = el.getAttribute('height')\n /* istanbul ignore else */\n if (elHeight) {\n svg.setAttribute('height', elHeight)\n }\n\n const mergedClasses = Array.from(\n new Set([\n ...(svg.getAttribute('class') || '').split(' '),\n 'injected-svg',\n ...(el.getAttribute('class') || '').split(' '),\n ])\n )\n .join(' ')\n .trim()\n svg.setAttribute('class', mergedClasses)\n\n const elStyle = el.getAttribute('style')\n /* istanbul ignore else */\n if (elStyle) {\n svg.setAttribute('style', elStyle)\n }\n\n svg.setAttribute('data-src', elUrl)\n\n // Copy all the data elements to the svg.\n const elData = [].filter.call(el.attributes, (at: Attr) => {\n return /^data-\\w[\\w-]*$/.test(at.name)\n })\n\n Array.prototype.forEach.call(elData, (dataAttr: Attr) => {\n /* istanbul ignore else */\n if (dataAttr.name && dataAttr.value) {\n svg.setAttribute(dataAttr.name, dataAttr.value)\n }\n })\n\n /* istanbul ignore else */\n if (renumerateIRIElements) {\n // Make sure any internally referenced clipPath ids and their clip-path\n // references are unique.\n //\n // This addresses the issue of having multiple instances of the same SVG\n // on a page and only the first clipPath id is referenced.\n //\n // Browsers often shortcut the SVG Spec and don't use clipPaths contained\n // in parent elements that are hidden, so if you hide the first SVG\n // instance on the page, then all other instances lose their clipping.\n // Reference: https://bugzilla.mozilla.org/show_bug.cgi?id=376027\n\n // Handle all defs elements that have iri capable attributes as defined by\n // w3c: http://www.w3.org/TR/SVG/linking.html#processingIRI. Mapping IRI\n // addressable elements to the properties that can reference them.\n const iriElementsAndProperties: { [key: string]: string[] } = {\n clipPath: ['clip-path'],\n 'color-profile': ['color-profile'],\n cursor: ['cursor'],\n filter: ['filter'],\n linearGradient: ['fill', 'stroke'],\n marker: ['marker', 'marker-start', 'marker-mid', 'marker-end'],\n mask: ['mask'],\n path: [],\n pattern: ['fill', 'stroke'],\n radialGradient: ['fill', 'stroke'],\n }\n\n let element\n let elements\n let properties\n let currentId: string\n let newId: string\n\n Object.keys(iriElementsAndProperties).forEach((key) => {\n element = key\n properties = iriElementsAndProperties[key]\n\n elements = svg.querySelectorAll(element + '[id]')\n for (let a = 0, elementsLen = elements.length; a < elementsLen; a++) {\n currentId = elements[a].id\n newId = currentId + '-' + uniqueId()\n\n // All of the properties that can reference this element type.\n let referencingElements\n Array.prototype.forEach.call(properties, (property: string) => {\n // :NOTE: using a substring match attr selector here to deal with IE\n // \"adding extra quotes in url() attrs\".\n referencingElements = svg.querySelectorAll(\n '[' + property + '*=\"' + currentId + '\"]'\n )\n for (\n let b = 0, referencingElementLen = referencingElements.length;\n b < referencingElementLen;\n b++\n ) {\n const attrValue: string | null =\n referencingElements[b].getAttribute(property)\n if (\n attrValue &&\n !attrValue.match(new RegExp('url\\\\(\"?#' + currentId + '\"?\\\\)'))\n ) {\n continue\n }\n referencingElements[b].setAttribute(\n property,\n 'url(#' + newId + ')'\n )\n }\n })\n\n const allLinks = svg.querySelectorAll('[*|href]')\n const links = []\n for (let c = 0, allLinksLen = allLinks.length; c < allLinksLen; c++) {\n const href = allLinks[c].getAttributeNS(xlinkNamespace, 'href')\n /* istanbul ignore else */\n if (href && href.toString() === '#' + elements[a].id) {\n links.push(allLinks[c])\n }\n }\n for (let d = 0, linksLen = links.length; d < linksLen; d++) {\n links[d].setAttributeNS(xlinkNamespace, 'href', '#' + newId)\n }\n\n elements[a].id = newId\n }\n })\n }\n\n // Remove any unwanted/invalid namespaces that might have been added by SVG\n // editing tools.\n svg.removeAttribute('xmlns:a')\n\n // Post page load injected SVGs don't automatically have their script\n // elements run, so we'll need to make that happen, if requested.\n\n // Find then prune the scripts.\n const scripts = svg.querySelectorAll('script')\n const scriptsToEval: string[] = []\n let script\n let scriptType\n\n for (let i = 0, scriptsLen = scripts.length; i < scriptsLen; i++) {\n scriptType = scripts[i].getAttribute('type')\n\n // Only process javascript types. SVG defaults to 'application/ecmascript'\n // for unset types.\n /* istanbul ignore else */\n if (\n !scriptType ||\n scriptType === 'application/ecmascript' ||\n scriptType === 'application/javascript' ||\n scriptType === 'text/javascript'\n ) {\n // innerText for IE, textContent for other browsers.\n script = scripts[i].innerText || scripts[i].textContent\n\n // Stash.\n /* istanbul ignore else */\n if (script) {\n scriptsToEval.push(script)\n }\n\n // Tidy up and remove the script element since we don't need it anymore.\n svg.removeChild(scripts[i])\n }\n }\n\n // Run/Eval the scripts if needed.\n /* istanbul ignore else */\n if (\n scriptsToEval.length > 0 &&\n (evalScripts === 'always' ||\n (evalScripts === 'once' && !ranScripts[elUrl]))\n ) {\n for (\n let l = 0, scriptsToEvalLen = scriptsToEval.length;\n l < scriptsToEvalLen;\n l++\n ) {\n // :NOTE: Yup, this is a form of eval, but it is being used to eval code\n // the caller has explictely asked to be loaded, and the code is in a\n // caller defined SVG file... not raw user input.\n //\n // Also, the code is evaluated in a closure and not in the global scope.\n // If you need to put something in global scope, use 'window'.\n new Function(scriptsToEval[l])(window)\n }\n\n // Remember we already ran scripts for this svg.\n ranScripts[elUrl] = true\n }\n\n // :WORKAROUND: IE doesn't evaluate <style> tags in SVGs that are\n // dynamically added to the page. This trick will trigger IE to read and use\n // any existing SVG <style> tags.\n //\n // Reference: https://github.com/iconic/SVGInjector/issues/23.\n const styleTags = svg.querySelectorAll('style')\n Array.prototype.forEach.call(styleTags, (styleTag: HTMLStyleElement) => {\n styleTag.textContent += ''\n })\n\n svg.setAttribute('xmlns', svgNamespace)\n svg.setAttribute('xmlns:xlink', xlinkNamespace)\n\n beforeEach(svg)\n\n if (!el.parentNode) {\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n callback(new Error('Parent node is null'))\n return\n }\n\n // Replace the image with the svg.\n el.parentNode.replaceChild(svg, el)\n\n // Now that we no longer need it, drop references to the original element so\n // it can be GC'd.\n // TODO: Extract\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n\n callback(null, svg)\n })\n}\n\nexport default injectElement\n","import injectElement from './inject-element'\nimport { AfterAll, BeforeEach, Errback, EvalScripts } from './types'\n\ntype Elements = HTMLCollectionOf<Element> | NodeListOf<Element> | Element | null\n\ninterface OptionalArgs {\n afterAll?: AfterAll\n afterEach?: Errback\n beforeEach?: BeforeEach\n cacheRequests?: boolean\n evalScripts?: EvalScripts\n httpRequestWithCredentials?: boolean\n renumerateIRIElements?: boolean\n}\n\nconst SVGInjector = (\n elements: Elements,\n {\n afterAll = () => undefined,\n afterEach = () => undefined,\n beforeEach = () => undefined,\n cacheRequests = true,\n evalScripts = 'never',\n httpRequestWithCredentials = false,\n renumerateIRIElements = true,\n }: OptionalArgs = {}\n) => {\n if (elements && 'length' in elements) {\n let elementsLoaded = 0\n for (let i = 0, j = elements.length; i < j; i++) {\n injectElement(\n elements[i],\n evalScripts,\n renumerateIRIElements,\n cacheRequests,\n httpRequestWithCredentials,\n beforeEach,\n (error, svg) => {\n afterEach(error, svg)\n if (\n elements &&\n 'length' in elements &&\n elements.length === ++elementsLoaded\n ) {\n afterAll(elementsLoaded)\n }\n }\n )\n }\n } else if (elements) {\n injectElement(\n elements,\n evalScripts,\n renumerateIRIElements,\n cacheRequests,\n httpRequestWithCredentials,\n beforeEach,\n (error, svg) => {\n afterEach(error, svg)\n afterAll(1)\n elements = null\n }\n )\n } else {\n afterAll(0)\n }\n}\n\nexport default SVGInjector\n"],"names":["cache","Map","cloneSvg","sourceSvg","cloneNode","isLocal","window","location","protocol","makeAjaxRequest","callback","httpRequest","onreadystatechange","test","url","readyState","contentType","Error","type","parseContentType","status","responseXML","statusText","error","abort","requestQueue","queueRequest","push","len","setTimeout","cacheValue","get","i","SVGSVGElement","length","httpRequestWithCredentials","has","undefined","set","loadSvgUncached","Document","idCounter","uniqueId","injectedElements","ranScripts","svgNamespace","xlinkNamespace","el","renumerateIRIElements","beforeEach","elUrl","getAttribute","indexOf","splice","cacheRequests","loadSvgCached","svg","elId","setAttribute","elTitle","elWidth","elHeight","mergedClasses","Array","from","split","join","trim","elStyle","elData","filter","call","attributes","at","name","prototype","forEach","dataAttr","value","elements_1","properties_1","currentId_1","newId_1","iriElementsAndProperties_1","clipPath","linearGradient","marker","pattern","Object","keys","key","a","elementsLen","id","property","referencingElements","querySelectorAll","SVGInjector","elements","_a","_b","_c","afterAll","_d","_e","_f","_g","evalScripts","_h","_j"],"mappings":"mIAAWA,MAAO,IAAAC,ICAZC,SAAW,SAAAC,GACf,OAAAA,EAASC,WAAU,EAAuB,ECD/BC,QAAS,WAAA,MAAgB,UAAhBC,OAAAC,SAAMC,QAAkB,ECQ5CC,gBAAwB,aAIpBC,4BAIGC,EAAAC,mBAAA,WAEO,iBACJC,KAAEC,IAAiD,IAAxCH,EAAAI,WAAwC,CACrD,IAAAC,sCACD,IAAAA,EACF,MAAA,IAAAC,MAAA,0BAIG,IAAAC,EAAAC,YAAAA,MACEH,GAASE,KACP,GAAA,kBAAAA,GAA+D,eAAAA,2CACFA,OAAAA,GAE7D,CAIN,GACoB,IADpBP,EACaI,WAAO,IACJ,QAAbK,QACD,OADcT,EAAkBU,YAEhC,MAAA,IAAAJ,MACDZ,UAAM,2KAIES,GAKZ,KAAe,MAAdH,EAAcS,mBACK,IAAPT,EAAOS,QAGZ,MAAA,IAAAH,MACL,0CACDN,EAAAS,OACF,IACFT,EAAAW,YALKZ,EAAA,KAASC,EAYX,EACF,MAAAY,GAGS,GAFVZ,EAAAa,UAEUD,aAAON,eACnBP,EAAAa,EAAAZ,sGC9DCc,aAAiB,CAAA,EAQaC,aAAA,SAAAZ,KACrBW,aAAAX,GAAAW,aAAUX,IAAA,GAEjBW,aAAAX,GAAWa,KAAAjB,wBAIsB,SAAAI,wBAGzBc,GAEHC,YAAA,4BAISJ,aAAWX,IAAC,CACrB,IAAAgB,EAAA9B,MAAA+B,IAAAjB,K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,EAAAA,EAAAA,cAAAnD,OACD,IAAAmD,GAAAA,EACFC,EAAAA,EAAAA,YAAAC,OAAA,IAAAD,EAAA,QAAAA,EAAME,EAAAA,EAAAA,2BAAAzE,OAAA,IAAAyE,GAAAA,mCACM,IAAAC,GAAAA,EAIf,GAAAX,cAA0BA"}
|
|
1
|
+
{"version":3,"file":"svg-injector.cjs.production.js","sources":["../src/cache.ts","../src/clone-svg.ts","../src/is-local.ts","../src/make-ajax-request.ts","../src/request-queue.ts","../src/load-svg-cached.ts","../src/load-svg-uncached.ts","../src/unique-id.ts","../src/inject-element.ts","../src/svg-injector.ts"],"sourcesContent":["const cache = new Map<string, SVGSVGElement | Error | undefined>()\n\nexport default cache\n","const cloneSvg = (sourceSvg: SVGSVGElement) =>\n sourceSvg.cloneNode(true) as SVGSVGElement\n\nexport default cloneSvg\n","const isLocal = () => window.location.protocol === 'file:'\n\nexport default isLocal\n","import { parse as parseContentType } from 'content-type'\nimport isLocal from './is-local'\n\nconst makeAjaxRequest = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: (error: Error | null, httpRequest: XMLHttpRequest) => void\n) => {\n const httpRequest = new XMLHttpRequest()\n\n httpRequest.onreadystatechange = () => {\n try {\n if (!/\\.svg/i.test(url) && httpRequest.readyState === 2) {\n const contentType = httpRequest.getResponseHeader('Content-Type')\n if (!contentType) {\n throw new Error('Content type not found')\n }\n\n const { type } = parseContentType(contentType)\n if (!(type === 'image/svg+xml' || type === 'text/plain')) {\n throw new Error(`Invalid content type: ${type}`)\n }\n }\n\n if (httpRequest.readyState === 4) {\n if (httpRequest.status === 404 || httpRequest.responseXML === null) {\n throw new Error(\n isLocal()\n ? 'Note: SVG injection ajax calls do not work locally without ' +\n 'adjusting security settings in your browser. Or consider ' +\n 'using a local webserver.'\n : 'Unable to load SVG file: ' + url\n )\n }\n\n if (\n httpRequest.status === 200 ||\n (isLocal() && httpRequest.status === 0)\n ) {\n callback(null, httpRequest)\n } else {\n throw new Error(\n 'There was a problem injecting the SVG: ' +\n httpRequest.status +\n ' ' +\n httpRequest.statusText\n )\n }\n }\n } catch (error) {\n httpRequest.abort()\n if (error instanceof Error) {\n callback(error, httpRequest)\n } else {\n throw error\n }\n }\n }\n\n httpRequest.open('GET', url)\n\n httpRequest.withCredentials = httpRequestWithCredentials\n\n /* istanbul ignore else */\n if (httpRequest.overrideMimeType) {\n httpRequest.overrideMimeType('text/xml')\n }\n\n httpRequest.send()\n}\n\nexport default makeAjaxRequest\n","import cache from './cache'\nimport cloneSvg from './clone-svg'\nimport { Errback } from './types'\n\nlet requestQueue: { [key: string]: Errback[] } = {}\n\nexport const clear = () => {\n requestQueue = {}\n}\n\nexport const queueRequest = (url: string, callback: Errback) => {\n requestQueue[url] = requestQueue[url] || []\n requestQueue[url].push(callback)\n}\n\nexport const processRequestQueue = (url: string) => {\n for (let i = 0, len = requestQueue[url].length; i < len; i++) {\n // Make these calls async so we avoid blocking the page/renderer.\n setTimeout(() => {\n /* istanbul ignore else */\n if (Array.isArray(requestQueue[url])) {\n const cacheValue = cache.get(url)\n const callback = requestQueue[url][i]\n\n /* istanbul ignore else */\n if (cacheValue instanceof SVGSVGElement) {\n callback(null, cloneSvg(cacheValue))\n }\n\n /* istanbul ignore else */\n if (cacheValue instanceof Error) {\n callback(cacheValue)\n }\n\n /* istanbul ignore else */\n if (i === requestQueue[url].length - 1) {\n delete requestQueue[url]\n }\n }\n }, 0)\n }\n}\n","import cache from './cache'\nimport cloneSvg from './clone-svg'\nimport makeAjaxRequest from './make-ajax-request'\nimport { processRequestQueue, queueRequest } from './request-queue'\nimport { Errback } from './types'\n\nconst loadSvgCached = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: Errback\n) => {\n if (cache.has(url)) {\n const cacheValue = cache.get(url)\n\n if (cacheValue === undefined) {\n queueRequest(url, callback)\n return\n }\n\n /* istanbul ignore else */\n if (cacheValue instanceof SVGSVGElement) {\n callback(null, cloneSvg(cacheValue))\n return\n }\n\n // Errors are always refetched.\n }\n\n // Seed the cache to indicate we are loading this URL.\n cache.set(url, undefined)\n queueRequest(url, callback)\n\n makeAjaxRequest(url, httpRequestWithCredentials, (error, httpRequest) => {\n /* istanbul ignore else */\n if (error) {\n cache.set(url, error)\n } else if (\n httpRequest.responseXML instanceof Document &&\n httpRequest.responseXML.documentElement &&\n httpRequest.responseXML.documentElement instanceof SVGSVGElement\n ) {\n cache.set(url, httpRequest.responseXML.documentElement)\n }\n processRequestQueue(url)\n })\n}\n\nexport default loadSvgCached\n","import makeAjaxRequest from './make-ajax-request'\nimport { Errback } from './types'\n\nconst loadSvgUncached = (\n url: string,\n httpRequestWithCredentials: boolean,\n callback: Errback\n) => {\n makeAjaxRequest(url, httpRequestWithCredentials, (error, httpRequest) => {\n /* istanbul ignore else */\n if (error) {\n callback(error)\n } else if (\n httpRequest.responseXML instanceof Document &&\n httpRequest.responseXML.documentElement &&\n httpRequest.responseXML.documentElement instanceof SVGSVGElement\n ) {\n callback(null, httpRequest.responseXML.documentElement)\n }\n })\n}\n\nexport default loadSvgUncached\n","let idCounter = 0\nconst uniqueId = () => ++idCounter\nexport default uniqueId\n","import loadSvgCached from './load-svg-cached'\nimport loadSvgUncached from './load-svg-uncached'\nimport { BeforeEach, Errback, EvalScripts } from './types'\nimport uniqueId from './unique-id'\n\ntype ElementType = Element | HTMLElement | null\n\nconst injectedElements: ElementType[] = []\nconst ranScripts: { [key: string]: boolean } = {}\nconst svgNamespace = 'http://www.w3.org/2000/svg'\nconst xlinkNamespace = 'http://www.w3.org/1999/xlink'\n\nconst injectElement = (\n el: NonNullable<ElementType>,\n evalScripts: EvalScripts,\n renumerateIRIElements: boolean,\n cacheRequests: boolean,\n httpRequestWithCredentials: boolean,\n beforeEach: BeforeEach,\n callback: Errback\n) => {\n const elUrl = el.getAttribute('data-src') || el.getAttribute('src')\n\n /* istanbul ignore else */\n if (!elUrl) {\n callback(new Error('Invalid data-src or src attribute'))\n return\n }\n\n // Make sure we aren't already in the process of injecting this element to\n // avoid a race condition if multiple injections for the same element are run.\n // :NOTE: Using indexOf() only _after_ we check for SVG support and bail, so\n // no need for IE8 indexOf() polyfill.\n /* istanbul ignore else */\n if (injectedElements.indexOf(el) !== -1) {\n // TODO: Extract.\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n return\n }\n\n // Remember the request to inject this element, in case other injection calls\n // are also trying to replace this element before we finish.\n injectedElements.push(el)\n\n // Try to avoid loading the orginal image src if possible.\n el.setAttribute('src', '')\n\n const loadSvg = cacheRequests ? loadSvgCached : loadSvgUncached\n\n loadSvg(elUrl, httpRequestWithCredentials, (error, svg) => {\n /* istanbul ignore else */\n if (!svg) {\n // TODO: Extract.\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n callback(error)\n return\n }\n\n const elId = el.getAttribute('id')\n /* istanbul ignore else */\n if (elId) {\n svg.setAttribute('id', elId)\n }\n\n const elTitle = el.getAttribute('title')\n /* istanbul ignore else */\n if (elTitle) {\n svg.setAttribute('title', elTitle)\n }\n\n const elWidth = el.getAttribute('width')\n /* istanbul ignore else */\n if (elWidth) {\n svg.setAttribute('width', elWidth)\n }\n\n const elHeight = el.getAttribute('height')\n /* istanbul ignore else */\n if (elHeight) {\n svg.setAttribute('height', elHeight)\n }\n\n const mergedClasses = Array.from(\n new Set([\n ...(svg.getAttribute('class') || '').split(' '),\n 'injected-svg',\n ...(el.getAttribute('class') || '').split(' '),\n ])\n )\n .join(' ')\n .trim()\n svg.setAttribute('class', mergedClasses)\n\n const elStyle = el.getAttribute('style')\n /* istanbul ignore else */\n if (elStyle) {\n svg.setAttribute('style', elStyle)\n }\n\n svg.setAttribute('data-src', elUrl)\n\n // Copy all the data elements to the svg.\n const elData = [].filter.call(el.attributes, (at: Attr) => {\n return /^data-\\w[\\w-]*$/.test(at.name)\n })\n\n Array.prototype.forEach.call(elData, (dataAttr: Attr) => {\n /* istanbul ignore else */\n if (dataAttr.name && dataAttr.value) {\n svg.setAttribute(dataAttr.name, dataAttr.value)\n }\n })\n\n /* istanbul ignore else */\n if (renumerateIRIElements) {\n // Make sure any internally referenced clipPath ids and their clip-path\n // references are unique.\n //\n // This addresses the issue of having multiple instances of the same SVG\n // on a page and only the first clipPath id is referenced.\n //\n // Browsers often shortcut the SVG Spec and don't use clipPaths contained\n // in parent elements that are hidden, so if you hide the first SVG\n // instance on the page, then all other instances lose their clipping.\n // Reference: https://bugzilla.mozilla.org/show_bug.cgi?id=376027\n\n // Handle all defs elements that have iri capable attributes as defined by\n // w3c: http://www.w3.org/TR/SVG/linking.html#processingIRI. Mapping IRI\n // addressable elements to the properties that can reference them.\n const iriElementsAndProperties: { [key: string]: string[] } = {\n clipPath: ['clip-path'],\n 'color-profile': ['color-profile'],\n cursor: ['cursor'],\n filter: ['filter'],\n linearGradient: ['fill', 'stroke'],\n marker: ['marker', 'marker-start', 'marker-mid', 'marker-end'],\n mask: ['mask'],\n path: [],\n pattern: ['fill', 'stroke'],\n radialGradient: ['fill', 'stroke'],\n }\n\n let element\n let elements\n let properties\n let currentId: string\n let newId: string\n\n Object.keys(iriElementsAndProperties).forEach((key) => {\n element = key\n properties = iriElementsAndProperties[key]\n\n elements = svg.querySelectorAll(element + '[id]')\n for (let a = 0, elementsLen = elements.length; a < elementsLen; a++) {\n currentId = elements[a].id\n newId = currentId + '-' + uniqueId()\n\n // All of the properties that can reference this element type.\n let referencingElements\n Array.prototype.forEach.call(properties, (property: string) => {\n // :NOTE: using a substring match attr selector here to deal with IE\n // \"adding extra quotes in url() attrs\".\n referencingElements = svg.querySelectorAll(\n '[' + property + '*=\"' + currentId + '\"]'\n )\n for (\n let b = 0, referencingElementLen = referencingElements.length;\n b < referencingElementLen;\n b++\n ) {\n const attrValue: string | null =\n referencingElements[b].getAttribute(property)\n if (\n attrValue &&\n !attrValue.match(new RegExp('url\\\\(\"?#' + currentId + '\"?\\\\)'))\n ) {\n continue\n }\n referencingElements[b].setAttribute(\n property,\n 'url(#' + newId + ')'\n )\n }\n })\n\n const allLinks = svg.querySelectorAll('[*|href]')\n const links = []\n for (let c = 0, allLinksLen = allLinks.length; c < allLinksLen; c++) {\n const href = allLinks[c].getAttributeNS(xlinkNamespace, 'href')\n /* istanbul ignore else */\n if (href && href.toString() === '#' + elements[a].id) {\n links.push(allLinks[c])\n }\n }\n for (let d = 0, linksLen = links.length; d < linksLen; d++) {\n links[d].setAttributeNS(xlinkNamespace, 'href', '#' + newId)\n }\n\n elements[a].id = newId\n }\n })\n }\n\n // Remove any unwanted/invalid namespaces that might have been added by SVG\n // editing tools.\n svg.removeAttribute('xmlns:a')\n\n // Post page load injected SVGs don't automatically have their script\n // elements run, so we'll need to make that happen, if requested.\n\n // Find then prune the scripts.\n const scripts = svg.querySelectorAll('script')\n const scriptsToEval: string[] = []\n let script\n let scriptType\n\n for (let i = 0, scriptsLen = scripts.length; i < scriptsLen; i++) {\n scriptType = scripts[i].getAttribute('type')\n\n // Only process javascript types. SVG defaults to 'application/ecmascript'\n // for unset types.\n /* istanbul ignore else */\n if (\n !scriptType ||\n scriptType === 'application/ecmascript' ||\n scriptType === 'application/javascript' ||\n scriptType === 'text/javascript'\n ) {\n // innerText for IE, textContent for other browsers.\n script = scripts[i].innerText || scripts[i].textContent\n\n // Stash.\n /* istanbul ignore else */\n if (script) {\n scriptsToEval.push(script)\n }\n\n // Tidy up and remove the script element since we don't need it anymore.\n svg.removeChild(scripts[i])\n }\n }\n\n // Run/Eval the scripts if needed.\n /* istanbul ignore else */\n if (\n scriptsToEval.length > 0 &&\n (evalScripts === 'always' ||\n (evalScripts === 'once' && !ranScripts[elUrl]))\n ) {\n for (\n let l = 0, scriptsToEvalLen = scriptsToEval.length;\n l < scriptsToEvalLen;\n l++\n ) {\n // :NOTE: Yup, this is a form of eval, but it is being used to eval code\n // the caller has explictely asked to be loaded, and the code is in a\n // caller defined SVG file... not raw user input.\n //\n // Also, the code is evaluated in a closure and not in the global scope.\n // If you need to put something in global scope, use 'window'.\n new Function(scriptsToEval[l])(window)\n }\n\n // Remember we already ran scripts for this svg.\n ranScripts[elUrl] = true\n }\n\n // :WORKAROUND: IE doesn't evaluate <style> tags in SVGs that are\n // dynamically added to the page. This trick will trigger IE to read and use\n // any existing SVG <style> tags.\n //\n // Reference: https://github.com/iconic/SVGInjector/issues/23.\n const styleTags = svg.querySelectorAll('style')\n Array.prototype.forEach.call(styleTags, (styleTag: HTMLStyleElement) => {\n styleTag.textContent += ''\n })\n\n svg.setAttribute('xmlns', svgNamespace)\n svg.setAttribute('xmlns:xlink', xlinkNamespace)\n\n beforeEach(svg)\n\n if (!el.parentNode) {\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n callback(new Error('Parent node is null'))\n return\n }\n\n // Replace the image with the svg.\n el.parentNode.replaceChild(svg, el)\n\n // Now that we no longer need it, drop references to the original element so\n // it can be GC'd.\n // TODO: Extract\n injectedElements.splice(injectedElements.indexOf(el), 1)\n ;(el as ElementType) = null\n\n callback(null, svg)\n })\n}\n\nexport default injectElement\n","import injectElement from './inject-element'\nimport { AfterAll, BeforeEach, Errback, EvalScripts } from './types'\n\ntype Elements = HTMLCollectionOf<Element> | NodeListOf<Element> | Element | null\n\ninterface OptionalArgs {\n afterAll?: AfterAll\n afterEach?: Errback\n beforeEach?: BeforeEach\n cacheRequests?: boolean\n evalScripts?: EvalScripts\n httpRequestWithCredentials?: boolean\n renumerateIRIElements?: boolean\n}\n\nconst SVGInjector = (\n elements: Elements,\n {\n afterAll = () => undefined,\n afterEach = () => undefined,\n beforeEach = () => undefined,\n cacheRequests = true,\n evalScripts = 'never',\n httpRequestWithCredentials = false,\n renumerateIRIElements = true,\n }: OptionalArgs = {}\n) => {\n if (elements && 'length' in elements) {\n let elementsLoaded = 0\n for (let i = 0, j = elements.length; i < j; i++) {\n injectElement(\n elements[i],\n evalScripts,\n renumerateIRIElements,\n cacheRequests,\n httpRequestWithCredentials,\n beforeEach,\n (error, svg) => {\n afterEach(error, svg)\n if (\n elements &&\n 'length' in elements &&\n elements.length === ++elementsLoaded\n ) {\n afterAll(elementsLoaded)\n }\n }\n )\n }\n } else if (elements) {\n injectElement(\n elements,\n evalScripts,\n renumerateIRIElements,\n cacheRequests,\n httpRequestWithCredentials,\n beforeEach,\n (error, svg) => {\n afterEach(error, svg)\n afterAll(1)\n elements = null\n }\n )\n } else {\n afterAll(0)\n }\n}\n\nexport default SVGInjector\n"],"names":["cache","Map","cloneSvg","sourceSvg","cloneNode","isLocal","window","location","protocol","makeAjaxRequest","callback","httpRequest","onreadystatechange","test","url","readyState","contentType","Error","type","parseContentType","status","responseXML","statusText","error","abort","requestQueue","queueRequest","push","len","setTimeout","cacheValue","get","i","SVGSVGElement","length","httpRequestWithCredentials","has","undefined","set","loadSvgUncached","Document","idCounter","uniqueId","injectedElements","ranScripts","svgNamespace","xlinkNamespace","el","renumerateIRIElements","beforeEach","elUrl","getAttribute","indexOf","splice","cacheRequests","loadSvgCached","svg","elId","setAttribute","elTitle","elWidth","elHeight","mergedClasses","Array","from","split","join","trim","elStyle","elData","filter","call","attributes","at","name","prototype","forEach","dataAttr","value","elements_1","properties_1","currentId_1","newId_1","iriElementsAndProperties_1","clipPath","linearGradient","marker","pattern","Object","keys","key","a","elementsLen","id","property","referencingElements","querySelectorAll","SVGInjector","elements","_a","_b","_c","afterAll","afterEach","_d","_e","_f","_g","evalScripts","_h","_j"],"mappings":"mIAAWA,MAAO,IAAAC,ICAZC,SAAW,SAAAC,GACf,OAAAA,EAASC,WAAU,EAAnB,ECDWC,QAAS,WAAA,MAAgB,UAAhBC,OAAAC,SAAMC,QAAN,ECQpBC,gBAAwB,aAIpBC,4BAIGC,EAAAC,mBAAA,WAEO,iBACJC,KAAEC,IAAiD,IAAxCH,EAAAI,WAAwC,CACrD,IAAAC,sCACD,IAAAA,EACF,MAAA,IAAAC,MAAA,0BAIG,IAAAC,EAAAC,YAAAA,MACEH,GADFE,KAEI,GAAA,kBAAAA,GAA+D,eAAAA,2CACFA,OAAAA,IAMnE,GACoB,IADpBP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