This repository was archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_template.js
More file actions
137 lines (118 loc) · 2.74 KB
/
index_template.js
File metadata and controls
137 lines (118 loc) · 2.74 KB
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
(function (global, define) {
"use strict";
define(function() {
var doc = global.document;
var html = doc.documentElement;
var body, fakeBody, div, span;
var overflow;
var style = "­<style>{{css}}</style>";
var formats = ["woff2", "woff", "ttf", "svg"];
var isComplete = false;
var isRunning = false;
var callbacks = [];
var support = {};
return function (callback, returnOption) {
callbacks.push({callback: callback, option: returnOption || null});
if (isComplete) {
handleCallbacks();
}
else {
if (isRunning) {
return;
}
detect();
}
};
function formatResult (option) {
var result;
if (!option) {
result = support;
}
else if (typeof option == "string") {
result = support[option];
}
else {
result = [];
var i = 0;
var l = option.length;
while (i < l) {
(function (format) {
if (support[format]) {
result.push(format);
}
}(option[i++]));
}
result = 0 in result && result[0] || null;
}
return result;
}
function handleCallbacks () {
var data;
while (callbacks.length) {
data = callbacks.shift();
data.callback(formatResult(data.option));
}
}
function detect () {
isRunning = true;
body = doc.body,
fakeBody = body || doc.createElement("body"),
div = doc.createElement("div"),
span = doc.createElement("span"),
span.innerHTML = ".";
(body ? div : fakeBody).innerHTML += style;
fakeBody.appendChild(div);
if (!body) {
fakeBody.style.background = "";
fakeBody.style.overflow = "hidden";
overflow = html.style.overflow;
html.style.overflow = "hidden";
html.appendChild(fakeBody);
}
var i = 0;
var l = formats.length;
var countdown = l;
var clones = [];
var detected;
var start = new Date();
var timeout = 2000;
while (i < l) {
(function (format, clone) {
clone.className = "test-" + format;
clones.push(clone);
div.appendChild(clone);
}(formats[i++], span.cloneNode(true)));
}
(function check() {
var i = 0;
while (i < l) {
if (clones[i].offsetWidth == 10) {
detected = true;
support[formats[i]] = true;
}
i++;
}
if (detected) {
isComplete = true;
cleanup();
}
else if (new Date() - start > timeout) {
cleanup();
}
else {
setTimeout(check, 100);
}
}());
}
function cleanup() {
if (!body) {
fakeBody.parentNode.removeChild(fakeBody);
html.style.overflow = overflow;
} else {
div.parentNode.removeChild(div);
}
handleCallbacks();
isRunning = false;
}
});
}(window, typeof define == "function" && define.amd ? define : function(factory) { window.fontSupport = factory() } ));