forked from jonathantneal/svg4everybody
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg4everybody.ie8.js
100 lines (78 loc) · 2.28 KB
/
svg4everybody.ie8.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
/*! svg4everybody v1.0.0 | github.com/jonathantneal/svg4everybody */
(function (document, uses, requestAnimationFrame, CACHE, LTEIE8, IE9TO11) {
function embed(svg, g) {
if (g) {
var
viewBox = g.getAttribute('viewBox'),
preserveAspectRatio = g.getAttribute('preserveAspectRatio'),
fragment = document.createDocumentFragment(),
clone = g.cloneNode(true);
if (viewBox) {
svg.setAttribute('viewBox', viewBox);
}
if (preserveAspectRatio) {
svg.setAttribute('preserveAspectRatio', preserveAspectRatio);
}
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])) {
if (LTEIE8) {
var
img = new Image();
img.src = use.getAttribute('xlink:href').replace('#', '.').replace(/^\./, '') + '.png';
use.parentNode.replaceChild(img, use);
} else {
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 (LTEIE8 || IE9TO11) {
onframe();
}
})(
document,
document.getElementsByTagName('use'),
window.requestAnimationFrame || window.setTimeout,
{},
/MSIE\s[1-8]\b/.test(navigator.userAgent),
/Trident\/[567]\b/.test(navigator.userAgent) || (navigator.userAgent.match(/AppleWebKit\/(\d+)/) || [])[1] < 537,
document.createElement('svg'),
document.createElement('use')
);