-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsvg-loader.js
410 lines (340 loc) · 12.9 KB
/
svg-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"use strict";
const { get, set, del, entries } = require("idb-keyval");
const cssScope = require("./lib/scope-css");
const cssUrlFixer = require("./lib/css-url-fixer");
const counter = require("./lib/counter");
const isCacheAvailable = async (url) => {
let item;
try {
item = await get(`loader_${url}`);
} catch (e) {}
if (!item) {
try {
item = localStorage.getItem(`loader_${url}`);
} catch(e) {}
}
if (!item) {
return;
}
item = JSON.parse(item);
if (Date.now() < item.expiry) {
return item.data;
} else {
del(`loader_${url}`);
return;
}
};
const setCache = async (url, data, cacheOpt) => {
const cacheExp = parseInt(cacheOpt, 10);
const dataToSet = JSON.stringify({
data,
expiry: Date.now() + (Number.isNaN(cacheExp) ? 60 * 60 * 1000 * 24 * 30 : cacheExp * 1000)
});
try {
await set(`loader_${url}`, dataToSet);
} catch (e) {
try {
localStorage.setItem(`loader_${url}`, dataToSet)
} catch (e) {
console.warn("Failed to set cache: ", e)
}
};
};
const DOM_EVENTS = [];
const getAllEventNames = () => {
if (DOM_EVENTS.length) {
return DOM_EVENTS;
}
for (const prop in document.body) {
if (prop.startsWith("on")) {
DOM_EVENTS.push(prop);
}
}
// SVG <animate> events
DOM_EVENTS.push('onbegin', 'onend', 'onrepeat');
// Some non-standard events, just in case the browser is handling them
DOM_EVENTS.push('onfocusin', 'onfocusout', 'onbounce', 'onfinish', 'onshow');
return DOM_EVENTS;
};
const attributesSet = {};
const renderBody = (elem, options, body) => {
const { enableJs, disableUniqueIds, disableCssScoping, spriteIconId } = options;
const isSpriteIcon = !!spriteIconId;
const parser = new DOMParser();
const doc = parser.parseFromString(body, "text/html");
const fragment = isSpriteIcon ? doc.getElementById(spriteIconId) : doc.querySelector("svg");
const eventNames = getAllEventNames();
// When svg-loader is loading in the same element, it's
// important to keep track of original properties.
const elemAttributesSet = attributesSet[elem.getAttribute("data-id")] || new Set();
const elemUniqueId = elem.getAttribute("data-id") || `svg-loader_${counter.incr()}`;
const idMap = {};
if (!disableUniqueIds) {
// Append a unique suffix for every ID so elements don't conflict.
Array.from(fragment.querySelectorAll("[id]")).forEach((elem) => {
const id = elem.getAttribute("id");
const newId = `${id}_${counter.incr()}`;
elem.setAttribute("id", newId);
idMap[id] = newId;
});
}
Array.from(fragment.querySelectorAll("*")).concat(fragment).forEach((el) => {
// Unless explicitly set, remove JS code (default)
if (el.tagName === "script") {
el.remove();
if (!enableJs) {
return;
} else {
const scriptEl = document.createElement("script");
scriptEl.appendChild(el.childNodes[0]);
elem.appendChild(scriptEl)
}
}
const attributesToRemove = []
for (let i = 0; i < el.attributes.length; i++) {
const {
name,
value
} = el.attributes[i];
const newValue = cssUrlFixer(idMap, value, name);
if (value !== newValue) {
el.setAttribute(name, newValue);
}
// Remove event functions: onmouseover, onclick ... unless specifically enabled
if (eventNames.includes(name.toLowerCase()) && !enableJs) {
attributesToRemove.push(name);
continue;
}
// Remove "javascript:..." unless specifically enabled
if (["href", "xlink:href", "values"].includes(name) && value.startsWith("javascript") && !enableJs) {
attributesToRemove.push(name);
}
}
attributesToRemove.forEach((attr) => el.removeAttribute(attr))
// .first -> [data-id="svg_loader_341xx"] .first
// Makes sure that class names don't conflict with each other.
if (el.tagName === "style" && !disableCssScoping) {
let newValue = cssScope(el.innerHTML, `[data-id="${elemUniqueId}"]`, idMap);
newValue = cssUrlFixer(idMap, newValue);
if (newValue !== el.innerHTML)
el.innerHTML = newValue;
}
});
// For a sprite we want to include the whole DOM of sprite element
elem.innerHTML = spriteIconId ? fragment.outerHTML : fragment.innerHTML;
// This code block basically merges attributes of the original SVG
// the SVG element where it is called from. For eg,
//
// Let's say the original SVG is this:
//
// a.svg = <svg viewBox='..' ...></svg>
//
// and it is used as with svg-loader as <svg data-src="./a.svg" width="32"></svg>
// this will create a combined element <svg data-src="./a.svg" width="32" viewBox='..' ...></svg>
//
// For sprite icons, we don't need this as we are including the whole outerHTML.
if (!isSpriteIcon) {
for (let i = 0; i < fragment.attributes.length; i++) {
const {
name,
value
} = fragment.attributes[i];
// Don't override the attributes already defined, but override the ones that
// were in the original element
if (!elem.getAttribute(name) || elemAttributesSet.has(name)) {
elemAttributesSet.add(name);
elem.setAttribute(name, value);
}
}
}
attributesSet[elemUniqueId] = elemAttributesSet;
elem.setAttribute("data-id", elemUniqueId);
const event = new CustomEvent('iconload', {
bubbles: true
});
elem.dispatchEvent(event);
if (elem.getAttribute('oniconload')) {
// Handling (and executing) event attribute for our event (oniconload)
// isn't straightforward. Because a) the code is a raw string b) there's
// no way to specify the context for execution. So, `this` in the attribute
// will point to `window` instead of the element itself.
//
// Here we are recycling a rarely used GlobalEventHandler 'onauxclick'
// and offloading the execution to the browser. This is a hack, but because
// the event doesn't bubble, it shouldn't affect anything else in the code.
elem.setAttribute('onauxclick', elem.getAttribute('oniconload'));
const event = new CustomEvent('auxclick', {
bubbles: false,
view: window
});
elem.dispatchEvent(event);
elem.removeAttribute('onauxclick');
}
};
const requestsInProgress = {};
const memoryCache = {};
const renderIcon = async (elem) => {
const url = new URL(elem.getAttribute("data-src"), globalThis.location);
const src = url.toString().replace(url.hash, "");
const spriteIconId = url.hash.replace("#", "");
const cacheOpt = elem.getAttribute("data-cache");
const enableJs = elem.getAttribute("data-js") === "enabled";
const disableUniqueIds = elem.getAttribute("data-unique-ids") === "disabled";
const disableCssScoping = elem.getAttribute("data-css-scoping") === "disabled";
const lsCache = await isCacheAvailable(src);
const isCachingEnabled = cacheOpt !== "disabled";
const renderBodyCb = renderBody.bind(self, elem, { enableJs, disableUniqueIds, disableCssScoping, spriteIconId });
// Memory cache optimizes same icon requested multiple
// times on the page
if (memoryCache[src] || (isCachingEnabled && lsCache)) {
const cache = memoryCache[src] || lsCache;
renderBodyCb(cache);
} else {
// If the same icon is being requested to rendered
// avoid firing multiple XHRs
if (requestsInProgress[src]) {
setTimeout(() => renderIcon(elem), 20);
return;
}
requestsInProgress[src] = true;
fetch(src)
.then((response) => {
if (!response.ok) {
throw Error(`Request for '${src}' returned ${response.status} (${response.statusText})`);
}
return response.text();
})
.then((body) => {
const bodyLower = body.toLowerCase().trim();
if (!(bodyLower.startsWith("<svg") || bodyLower.startsWith("<?xml") || bodyLower.startsWith("<!doctype") )) {
throw Error(`Resource '${src}' returned an invalid SVG file`);
}
if (isCachingEnabled) {
setCache(src, body, cacheOpt);
}
memoryCache[src] = body;
renderBodyCb(body);
})
.catch((e) => {
console.error(e);
const event = new CustomEvent('iconloaderror', {
bubbles: true,
detail: e.toString(),
});
elem.dispatchEvent(event);
if(elem.getAttribute('oniconloaderror')){
// the oniconloaderror inline function will have access to an `error` argument
const loadErrorFunction = Function("error", elem.getAttribute('oniconloaderror'));
loadErrorFunction(e);
}
})
.finally(() => {
delete requestsInProgress[src];
});
}
};
let intObserver;
if (globalThis.IntersectionObserver) {
intObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
renderIcon(entry.target);
// Unobserve as soon as soon the icon is rendered
intObserver.unobserve(entry.target);
}
});
}, {
// Keep high root margin because intersection observer
// can be slow to react
rootMargin: "1200px"
}
);
}
const handled = [];
function renderAllSVGs() {
Array.from(document.querySelectorAll("svg[data-src]:not([data-id])"))
.forEach((element) => {
if (handled.indexOf(element) !== -1) {
return;
}
handled.push(element);
if (element.getAttribute("data-loading") === "lazy") {
intObserver.observe(element);
} else {
renderIcon(element);
}
});
}
let observerAdded = false;
const addObservers = () => {
if (observerAdded) {
return;
}
observerAdded = true;
const observer = new MutationObserver((mutationRecords) => {
const shouldTriggerRender = mutationRecords.some(
(record) => Array.from(record.addedNodes).some(
(elem) => elem.nodeType === Node.ELEMENT_NODE
&& ((elem.getAttribute("data-src") && !elem.getAttribute("data-id")) // Check if the element needs to be rendered
|| elem.querySelector("svg[data-src]:not([data-id])")) // Check if any of the element's children need to be rendered
)
);
// If any node is added, render all new nodes because the nodes that have already
// been rendered won't be rendered again.
if (shouldTriggerRender) {
renderAllSVGs();
}
// If data-src is changed, re-render
mutationRecords.forEach((record) => {
if (record.type === "attributes") {
renderIcon(record.target);
}
});
});
observer.observe(
document.documentElement,
{
attributeFilter: ["data-src"],
attributes: true,
childList: true,
subtree: true
}
);
};
if (globalThis.addEventListener) {
// Start rendering SVGs as soon as possible
const intervalCheck = setInterval(() => {
renderAllSVGs();
}, 100);
function init() {
clearInterval(intervalCheck);
renderAllSVGs();
addObservers();
}
if (document.readyState === 'interactive') {
init();
} else {
globalThis.addEventListener("DOMContentLoaded", () => {
init();
});
}
}
globalThis.SVGLoader = {}
globalThis.SVGLoader.destroyCache = async () => {
// Handle error, "mutation operation was attempted on a database"
// with try-catch
try {
const entriesCache = await entries();
for (const entry of entriesCache) {
if (entry[0].startsWith('loader_')) {
await del(entry[0]);
}
}
} catch(e) {}
Object.keys(localStorage).forEach((key) => {
if (key.startsWith('loader_')) {
localStorage.removeItem(key);
}
});
}