-
Notifications
You must be signed in to change notification settings - Fork 21
/
modernizr.js
292 lines (289 loc) · 10.6 KB
/
modernizr.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
/* Modernizr 2.6.2 (Custom Build) | MIT & BSD
* Build: http://modernizr.com/download/#-fontface-canvas-canvastext-audio-localstorage-sessionstorage-teststyles-network_xhr2
*/
window.Modernizr = (function(window, document, undefined) {
var version = '2.6.2',
Modernizr = {},
docElement = document.documentElement,
mod = 'modernizr',
modElem = document.createElement(mod),
mStyle = modElem.style,
inputElem,
toString = {}.toString,
omPrefixes = 'Webkit Moz O ms MS',
cssomPrefixes = omPrefixes.split(' '),
domPrefixes = omPrefixes.toLowerCase().split(' '),
tests = {},
inputs = {},
attrs = {},
classes = [],
slice = classes.slice,
featureName,
injectElementWithStyles = function(rule, callback, nodes, testnames) {
var style, ret, node, docOverflow,
div = document.createElement('div'),
body = document.body,
fakeBody = body || document.createElement('body');
if(parseInt(nodes, 10)) {
while(nodes--) {
node = document.createElement('div');
node.id = testnames ? testnames[nodes] : mod + (nodes + 1);
div.appendChild(node);
}
}
style = ['­','<style id="s', mod, '">', rule, '</style>'].join('');
div.id = mod;
(body ? div : fakeBody).innerHTML += style;
fakeBody.appendChild(div);
if(!body) {
fakeBody.style.background = '';
fakeBody.style.overflow = 'hidden';
docOverflow = docElement.style.overflow;
docElement.style.overflow = 'hidden';
docElement.appendChild(fakeBody);
}
ret = callback(div, rule);
if(!body) {
fakeBody.parentNode.removeChild(fakeBody);
docElement.style.overflow = docOverflow;
} else {
div.parentNode.removeChild(div);
}
return !!ret;
},
_hasOwnProperty = ({}).hasOwnProperty,
hasOwnProp; // End of var declaration
if (!is(_hasOwnProperty, 'undefined') && !is(_hasOwnProperty.call, 'undefined')) {
hasOwnProp = function(object, property) {
return _hasOwnProperty.call(object, property);
};
} else {
hasOwnProp = function(object, property) {
return ((property in object) && is(object.constructor.prototype[property], 'undefined'));
};
}
if(!Function.prototype.bind) {
Function.prototype.bind = function bind(that) {
var target = this;
if(typeof target != "function") {
throw new TypeError();
}
var args = slice.call(arguments, 1),
bound = function() {
if(this instanceof bound) {
var F = function(){};
F.prototype = target.prototype;
var self = new F();
var result = target.apply(self, args.concat(slice.call(arguments)));
if (Object(result) === result) {
return result;
}
return self;
} else {
return target.apply(that, args.concat(slice.call(arguments)));
}
};
return bound;
};
}
function setCss(str) {
mStyle.cssText = str;
}
function setCssAll(str1, str2) {
return setCss(prefixes.join(str1 + ';') + (str2 || ''));
}
function is(obj, type) {
return typeof obj === type;
}
function contains(str, substr) {
return !!~('' + str).indexOf(substr);
}
function testProps(props, prefixed) {
for(var i in props) {
var prop = props[i];
if(!contains(prop, "-") && mStyle[prop] !== undefined) {
return prefixed == 'pfx' ? prop : true;
}
}
return false;
}
function testDOMProps(props, obj, elem) {
for(var i in props) {
var item = obj[props[i]];
if(item !== undefined) {
if (elem === false) return props[i];
if (is(item, 'function')){
return item.bind(elem || obj);
}
return item;
}
}
return false;
}
function testPropsAll(prop, prefixed, elem) {
var ucProp = prop.charAt(0).toUpperCase() + prop.slice(1),
props = (prop + ' ' + cssomPrefixes.join(ucProp + ' ') + ucProp).split(' ');
if(is(prefixed, "string") || is(prefixed, "undefined")) {
return testProps(props, prefixed);
} else {
props = (prop + ' ' + (domPrefixes.concat(cssomPrefixes)).join(ucProp + ' ') + ucProp).split(' ');
return testDOMProps(props, prefixed, elem);
}
}
function prefixed(prop, obj, elem){
if(!obj) {
return testPropsAll(prop, 'pfx');
} else {
return testPropsAll(prop, obj, elem);
}
};
// Official tests
tests['canvas'] = function() {
var elem = document.createElement('canvas');
return !!(elem.getContext && elem.getContext('2d'));
};
tests['canvastext'] = function() {
return !!(Modernizr['canvas'] && is(document.createElement('canvas').getContext('2d').fillText, 'function'));
};
tests['fontface'] = function() {
var bool;
injectElementWithStyles('@font-face {font-family:"font";src:url("https://")}', function(node, rule) {
var style = document.getElementById('smodernizr'),
sheet = style.sheet || style.styleSheet,
cssText = sheet ? (sheet.cssRules && sheet.cssRules[0] ? sheet.cssRules[0].cssText : sheet.cssText || '') : '';
bool = /src/i.test(cssText) && cssText.indexOf(rule.split(' ')[0]) === 0;
});
return bool;
};
tests['audio'] = function() {
var elem = document.createElement('audio'),
bool = false;
try {
if(bool = !!elem.canPlayType) {
bool = new Boolean(bool);
bool.ogg = elem.canPlayType('audio/ogg; codecs="vorbis"').replace(/^no$/,'');
bool.mp3 = elem.canPlayType('audio/mpeg;').replace(/^no$/,'');
bool.wav = elem.canPlayType('audio/wav; codecs="1"').replace(/^no$/,'');
bool.m4a = (elem.canPlayType('audio/x-m4a;') || elem.canPlayType('audio/aac;')).replace(/^no$/,'');
}
} catch(e) { }
return bool;
};
tests['localstorage'] = function() {
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
} catch(e) {
return false;
}
};
tests['sessionstorage'] = function() {
try {
sessionStorage.setItem(mod, mod);
sessionStorage.removeItem(mod);
return true;
} catch(e) {
return false;
}
};
tests['xhr2'] = function() {
return 'FormData' in window; // means onprogress should work, not that responseType = blob will
};
tests['json'] = function() {
return !!window.JSON && !!JSON.parse;
};
tests['filereader'] = function() {
return !!(window.File && window.FileList && window.FileReader);
}
// Extra tests
tests['xmlserializer'] = function() {
return 'XMLSerializer' in window;
}
tests['overridemimetype'] = function() {
return !!((new XMLHttpRequest()).overrideMimeType);
}
tests['vbarray'] = function() {
return 'VBArray' in window;
}
tests['blob'] = function () {
var bool = false;
try {
if(bool = 'Blob' in window) { // This is probably the wrong test
bool = new Boolean(bool);
bool.slice = !!(prefixed("slice", Blob.prototype, false));
bool.builder = !!(prefixed("BlobBuilder", window, false));
bool.url = !!(prefixed("URL", window, false));
try {
var URLCreator = window[prefixed("URL", window, false)];
var u = URLCreator.createObjectURL(new Blob([0,0]),{autoRevoke: false});
bool.revoke = true;
URLCreator.revokeObjectURL(u);
} catch(e) {
bool.revoke = false;
}
try {
bool.creator = !!new Blob();
} catch(e) {
bool.creator = false;
}
}
} catch(e) {}
return bool;
};
tests['arraybuffer'] = function() {
var bool = false;
try {
if(bool = 'ArrayBuffer' in window) {
bool = new Boolean(bool);
bool.dataview = !!(prefixed("Uint8Array",window,false));
}
} catch(e) {}
return bool;
}
// Parse tests
for(var feature in tests) {
if (hasOwnProp(tests, feature)) {
featureName = feature.toLowerCase();
Modernizr[featureName] = tests[feature]();
classes.push((Modernizr[featureName] ? '' : 'no-') + featureName);
}
}
Modernizr.addTest = function (feature, test) {
if(typeof feature == 'object') {
for(var key in feature) {
if(hasOwnProp(feature, key)) {
Modernizr.addTest(key, feature[key]);
}
}
} else {
feature = feature.toLowerCase();
if(Modernizr[feature] !== undefined) {
return Modernizr;
}
test = typeof test == 'function' ? test() : test;
if(typeof enableClasses !== "undefined" && enableClasses) {
docElement.className += ' ' + (test ? '' : 'no-') + feature;
}
Modernizr[feature] = test;
}
return Modernizr;
};
setCss('');
modElem = inputElem = null;
Modernizr._version = version;
Modernizr.testStyles = injectElementWithStyles;
Modernizr._domPrefixes = domPrefixes;
Modernizr._cssomPrefixes = cssomPrefixes;
Modernizr.testProp = function(prop){ return testProps([prop]); };
Modernizr.testAllProps = testPropsAll;
Modernizr.prefixed = prefixed;
return Modernizr;
})(this, this.document);
// Async tests
(function(){
var datauri = new Image();
datauri.onerror = function() { Modernizr.addTest('datauri', function () { return false; }); };
datauri.onload = function() { Modernizr.addTest('datauri', function () { return (datauri.width == 1 && datauri.height == 1); }); };
datauri.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
})();