forked from jonathantneal/svg4everybody
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg4everybody.js
84 lines (64 loc) · 1.65 KB
/
svg4everybody.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
/*! svg4everybody v1.0.0 | github.com/jonathantneal/svg4everybody */
(function (document, uses, requestAnimationFrame, CACHE, IE9TO11) {
function embed(svg, g) {
if (g) {
var
viewBox = g.getAttribute('viewBox'),
fragment = document.createDocumentFragment(),
clone = g.cloneNode(true);
if (viewBox) {
svg.setAttribute('viewBox', viewBox);
}
while (clone.childNodes.length) {
fragment.appendChild(clone.childNodes[0]);
}
svg.appendChild(fragment);
}
}
function onload() {
var xhr = this, x = document.createElement('x'), s = xhr.s;
x.innerHTML = xhr.responseText;
xhr.onload = function () {
s.splice(0).map(function (array) {
embed(array[0], x.querySelector('#' + array[1].replace(/(\W)/g, '\\$1')));
});
};
xhr.onload();
}
function onframe() {
var use;
while ((use = uses[0])) {
var
svg = use.parentNode,
url = use.getAttribute('xlink:href').split('#'),
url_root = url[0],
url_hash = url[1];
svg.removeChild(use);
if (url_root.length) {
var xhr = CACHE[url_root] = CACHE[url_root] || new XMLHttpRequest();
if (!xhr.s) {
xhr.s = [];
xhr.open('GET', url_root);
xhr.onload = onload;
xhr.send();
}
xhr.s.push([svg, url_hash]);
if (xhr.readyState === 4) {
xhr.onload();
}
} else {
embed(svg, document.getElementById(url_hash));
}
}
requestAnimationFrame(onframe);
}
if (IE9TO11) {
onframe();
}
})(
document,
document.getElementsByTagName('use'),
window.requestAnimationFrame || window.setTimeout,
{},
/Trident\/[567]\b/.test(navigator.userAgent) || (navigator.userAgent.match(/AppleWebKit\/(\d+)/) || [])[1] < 537
);