forked from Modernizr/Modernizr
/
unit.js
483 lines (321 loc) · 13.7 KB
/
unit.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
test("globals set up",2, function() {
ok(window.Modernizr, 'global modernizr object created');
var remainingGlobals = getMembers(window);
// thx opera for globalizing IDs...
for (var len = remainingGlobals.length; len-- ;){
if (document.getElementById(remainingGlobals[len])){
remainingGlobals.splice(len,1);
}
}
var count = remainingGlobals.length - window.__globals.length
// jQuery and $ are included before the first globals are grabbed, so not included here.
- (window.TEST ? 1 : 0)
- (window.backuphtml ? 1 : 0)
- (window.cbfunc ? 1 : 0)
// couldnt tell why you why gecko has this exposed so peculiarly..
- ($.inArray('onhashchange',remainingGlobals) > -1 && $.inArray('onhashchange',remainingGlobals) == -1 &&
'onhashchange' in window && !window.onhashchange ? 1 : 0);
ok( ! (count > 1) , 'no more than one global object created');
/* * /
var arr = [];
var x = $.each(remainingGlobals,function(k,v){
if ( $.inArray(v,window.__globals) === -1) arr.push(v);
});
alert(remainingGlobals.length + ', ' + window.__globals.length + ' : ('+arr.length+') '+ arr.join(', '));
/* */
});
test("document.documentElement is valid and correct",1, function() {
equals(document.documentElement,document.getElementsByTagName('html')[0]);
});
test("no-js class is gone.", function() {
equals(document.documentElement.className.indexOf('no-js') , -1,
'no-js is gone.');
ok(/\bjs /.test(document.documentElement.className),
'html.js class is present')
if (document.querySelector){
ok(document.querySelector('html.js') == document.documentElement,
"document.querySelector('html.js') matches.");
}
});
test('html classes are looking good',function(){
var classes = TEST.trim(document.documentElement.className).split(/\s+/);
var modprops = getMembers(Modernizr).length,
newprops = modprops;
// decrement for the properties that are private
for (var i = -1, len = TEST.privates.length; ++i < len;){
if (Modernizr[TEST.privates[i]] != undefined) newprops--;
}
// decrement for the non-boolean objects
for (var i = -1, len = TEST.inputs.length; ++i < len;){
if (Modernizr[TEST.inputs[i]] != undefined) newprops--;
}
equals(classes.length,newprops,'equal number of classes and global object props');
for (var i = 0, len = classes.length, aclass; i <len; i++){
aclass = classes[i];
if (aclass === 'js') continue;
if (aclass.indexOf('no-') === 0){
aclass = aclass.replace('no-','');
equals(Modernizr[aclass], false,
aclass + ' is correctly false in the classes and object')
} else {
equals(Modernizr[aclass], true,
aclass + ' is correctly true in the classes and object')
}
}
for (var i = 0, len = classes.length, aclass; i <len; i++){
equals(classes[i],classes[i].toLowerCase(),'all classes are lowerCase.');
}
equals(/[^\s]no-/.test(document.documentElement.className),false,
'whitespace between all classes.');
})
test('Modernizr properties are looking good',function(){
var count = 0,
nobool = TEST.API.concat(TEST.inputs)
.concat(TEST.audvid).concat(TEST.privates);
for (var prop in window.Modernizr){
if (window.Modernizr.hasOwnProperty(prop)){
if (TEST.inArray(prop,nobool) >= 0) continue;
ok(Modernizr[prop] === true || Modernizr[prop] === false,
'Modernizr.'+prop+' is a straight up boolean');
equals(prop,prop.toLowerCase(),'all properties are lowerCase.')
}
}
})
test('Modernizr.addTest()',9,function(){
Modernizr.addTest('testtrue',function(){
return true;
});
Modernizr.addTest('testtruthy',function(){
return 100;
});
Modernizr.addTest('testfalse',function(){
return false;
});
Modernizr.addTest('testfalsy',function(){
return undefined;
});
ok(document.documentElement.className.indexOf(' testtrue') >= 0,'positive class added');
equals(Modernizr.testtrue,true,'positive prop added');
ok(document.documentElement.className.indexOf(' testtruthy') >= 0,'positive class added');
equals(Modernizr.testtruthy,true,'positive prop added');
ok(document.documentElement.className.indexOf(' no-testfalse') >= 0,'negative class added');
equals(Modernizr.testfalse,false,'negative prop added');
ok(document.documentElement.className.indexOf(' no-testfalsy') >= 0,'negative class added');
equals(Modernizr.testfalsy,false,'negative prop added');
Modernizr.addTest('camelCase',function(){
return true;
});
ok(document.documentElement.className.indexOf(' camelCase') === -1,
'camelCase test name toLowerCase()\'d');
})
test('Modernizr.audio and Modernizr.video',function(){
for (var i = -1, len = TEST.audvid.length; ++i < len;){
var prop = TEST.audvid[i];
if (Modernizr[prop].toString() == 'true'){
ok(Modernizr[prop], 'Modernizr.'+prop+' is truthy.');
equals(Modernizr[prop] == true,true, 'Modernizr.'+prop+' is == true')
equals(typeof Modernizr[prop] === 'object',true,'Moderizr.'+prop+' is truly an object');
equals(Modernizr[prop] !== true,true, 'Modernizr.'+prop+' is !== true')
} else {
equals(Modernizr[prop] != true,true, 'Modernizr.'+prop+' is != true')
}
}
})
asyncTest('async @font-face test',4,function(){
// we do this to verify our callback indeed will run.
start();
ok(Modernizr._fontfaceready,'passing a method to Modernizr._fontfaceready')
stop();
Modernizr._fontfaceready(function(bool){
ok(bool === true || bool === false,'passed argument is a boolean');
equals(bool,Modernizr.fontface,'Modernizr prop matches passed arg');
var expectedclass = (Modernizr.fontface ? '' : 'no-') + 'fontface';
ok(document.documentElement.className.indexOf(' '+expectedclass) >= 0,
'correct class added to documentElement');
start();
});
})
test('Modernizr results match expected values',function(){
// i'm bringing over a few tests from inside Modernizr.js
equals(!!document.createElement('canvas').getContext,Modernizr.canvas,'canvas test consistent');
equals(!!window.Worker,Modernizr.webworkers,'web workers test consistent')
});
/**
* We're going to test the current browser results against the www.findmebyip.com data tables
* These tables are originaly sourced from Modernizr results but get enough eyeballs to perhaps
* trust them more.
* However, as testing has shown, there are many inconsistencies in their results.
* Regardless, having *a* baseline to test feature detection results against is worthwhile,
* even if its flawed.
* In the future, I'd like to test against the whencaniuse.com tables.
*/
$(function fmbip() {
var isDataAdded = false;
var testbed = document.getElementById('testbed');
// return 0-based column number, considering colspans and such
$.fn.getColNum = function () {
var spans = this.eq(0).prevAll().map(function () {
return $(this).attr('colspan') || 1
}).get().join('+');
return eval('(' + spans + ')');
};
$.fn.grabColNum = function (n, wid) {
var elem = $(this).is('td,th') ? $(this).siblings().andSelf().eq(0) : $(this).find('td,th').eq(0);
wid = wid || 0;
//console.log('inside grabcol',elem,wid, elem.nextAll().andSelf().slice(n,n+1 + wid))
return elem.nextAll().andSelf().slice(n, n + 1 + wid);
};
// takes a given table cell and removes the entire column associated with it
$.fn.removeColumn = function(){
return this.each(function () {
var span = $(this).attr('colspan');
var skip = $(this).getColNum();
var rows = $(this).closest('table').find('thead,tbody').find('tr');
rows.each(function () {
var counter = span; // 4
var elem = $(this).children().eq(skip);
(function loop() {
var elemspan = $(elem).attr('colspan') || 1;
elem = elem.next();
elem.prev().remove();
if (counter - elemspan == 0) return;
else {
counter--;
loop();
}
})()
})
})
};
// determine the modernizr property we're testing.
// also we'll handle video/audio/input here
function getModernizrProperty(testname) {
var splitz = testname.split(':');
splitz[0] = dict[splitz[0]] || splitz[0];
var modresult = Modernizr[splitz[0]];
if (splitz[1] !== undefined) {
if (Modernizr.inputtypes[splitz[1]] !== undefined) {
modresult = Modernizr.inputtypes;
}
modresult = modresult[splitz[1]];
}
return modresult;
}
// hacky translation lookup when replace(/\s+/g, '').toLowerCase() isnt good enough
var dict = {
'postmessage': 'crosswindowmessaging',
'cssfontface': 'fontface',
'multiplebackgrounds': 'multiplebgs',
'webdatabase': 'websqldatabase',
'workers': 'webworkers',
'form': 'input'
}
function testAgainstTables() {
if (testAgainstTables.isRun) return;
testAgainstTables.isRun = true;
// remove css3 selectors
$('#css3-selectors').next().andSelf().remove();
// remove the mac columns.. kinda useless to us.
$('thead th:contains(MAC)').removeColumn();
test('Results match fmbip data', function () {
// find the matching browser..
$('thead').each(function () {
var elem = $(this).find('th:contains(' + sniff.browser.toUpperCase() + ')'),
pos = elem.getColNum(),
wid = elem.attr('colspan'),
// ver is an array of the browser versions of this matching browser.
ver = elem.closest('tr').next().grabColNum(pos, wid - 1).map(function () {
return $.trim($(this).text());
}).get(),
rows = $(this).closest('table').find('tbody tr');
rows.each(function () {
var thistest = $(this).find('td,th').eq(0).text().replace(/\s+/g, '').toLowerCase();
thistest = dict[test] || thistest;
var modresult = getModernizrProperty(thistest),
that = this;
// insert a loop here to loop over the versions.
$.each(ver, function (k, v) {
// add one on each iteration
var start = $(that).grabColNum(pos + k),
bool = $.trim(start.text()) == 'Y',
thisver = v;
// chrome 6 should succeed on everything chrome 5 did
if (sniff.version > thisver && modresult !== undefined) {
if (bool === true) {
ok( !! modresult === true, thistest + ' matches expected result for ' + sniff.browser + ' on ' + sniff.version + ' vs ' + thisver);
if (( !! modresult === true) !== bool) start.addClass('wrong');
}
}
// ie6 should fail on everything ie7 does.
if (sniff.version < thisver && modresult !== undefined) {
if (bool === false) {
ok( !! modresult === false, thistest + ' matches expected result for ' + sniff.browser + ' on ' + sniff.version + ' vs ' + thisver)
if (( !! modresult !== true) !== false) start.addClass('wrong');
}
}
// if ff3.5 is being tested. it should match every result in the table here.
if (parseFloat(sniff.version) == thisver && modresult !== undefined) {
ok( !! modresult === bool, thistest + ' matches expected result for ' + sniff.browser + ' on ' + sniff.version + ' vs ' + thisver);
if (( !! modresult === bool) !== true) start.addClass('wrong');
}
}); // version each loop
}) // eo rows each
}) // eo thead each
}); // test!
} // eo testAgainstTables();
// thx jquery
function uaMatch(ua) {
ua = ua.toLowerCase();
var match = /(webkit)[ \/]([\w.]+)/.exec(ua) || /(opera)(?:.*version)?[ \/]([\w.]+)/.exec(ua) || /(msie) ([\w.]+)/.exec(ua) || !/compatible/.test(ua) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec(ua) || [];
return {
browser: match[1] || "",
version: match[2] || "0"
};
}
// e.g. sniff.browser == 'webkit' && sniff.version == '534.0'
var sniff = uaMatch(navigator.userAgent);
// browser version
// this gnarly code is exactly the reason why Modernizr exists.
if (sniff.browser == 'webkit') {
var chrome = navigator.userAgent.match(/Chrome\/(.*?)(\s|$)/);
var safari = navigator.userAgent.match(/Safari\/(.*?)(\s|$)/);
if (chrome) {
sniff = {
browser: 'chrome',
version: chrome[1]
}
} else if (safari) {
sniff = {
browser: 'safari',
version: safari[1]
}
}
} else if (sniff.browser == 'mozilla') {
var firefox = navigator.userAgent.match(/(?:firefox|minefield)\/(.*?)(?:\s|$)/i);
sniff.browser = 'firefox';
sniff.version = firefox[1];
}
// fire it off
$('#testbed button').click(function() {
$(this).hide().next().show();
var script = document.createElement('script');
script.onerror = function () {
isDataAdded = true;
testbed.innerHTML += backuphtml;
testAgainstTables();
}
script.src = 'http://jsonpify.com/api?url=http://www.findmebyip.com/litmus/&format=string&jsonp=cbfunc';
document.body.appendChild(script);
// opera is weak with script onerror
setTimeout(function(){
if ( isDataAdded) return;
script.onerror();
},1200);
});
window.cbfunc = function (data) {
isDataAdded = true;
var html = data.replace(/\s+|\n/g, ' ').match(/(<h2 id="css3-properties.*)<div class="data-notes/)[1];
testbed.innerHTML += html;
testAgainstTables();
};
})