summaryrefslogtreecommitdiff
path: root/source/bower_components/modernizr/test/js/unit-caniuse.js
blob: 78bda30031a01cc85c504914c5704410af2f97a9 (plain)
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


var myscript = document.createElement('script'),
    ref = document.getElementsByTagName('script')[0];

myscript.src = 'http://caniuse.com/jsonp.php?callback=caniusecb';

setTimeout(function(){
  ref.parentNode.insertBefore(myscript, ref);
}, 100);

// mapping Modernizr terms over to caniuse terms
var map = {
  audio : 'audio',
  borderimage : 'border-image',
  borderradius : 'border-radius',
  canvas : 'canvas',
  canvastext : 'canvas-text',
  cssanimations : 'css-animation',
  boxshadow : 'css-boxshadow',
  cssgradients : 'css-gradients',
  opacity : 'css-opacity',
  cssreflections : 'css-reflections',
  textshadow : 'css-textshadow',
  csstransitions : 'css-transitions',
  hsla : 'css3-colors',
  rgba : 'css3-colors',
  draganddrop : 'dragndrop',
  flexbox : 'flexbox',
  fontface : 'fontface',
  geolocation : 'geolocation',
  hashchange : 'hashchange',
  history : 'history',
  indexeddb : 'indexeddb',
  multiplebgs : 'multibackgrounds',
  csscolumns : 'multicolumn',
  localstorage : 'namevalue-storage',
  applicationcache : 'offline-apps',
  websqldatabase : 'sql-storage',
  svg : 'svg',
  touch : 'touch',
  csstransforms : 'transforms2d',
  csstransforms3d : 'transforms3d',
  video: 'video',
  webgl: 'webgl',
  websockets : 'websockets',
  webworkers : 'webworkers',
  postmessage : 'x-doc-messaging'
};

window.caniusecb = function(scriptdata) {

  window.doo = scriptdata;

  // quit if JSONSelect didn't make it.
  if (!window.JSONSelect) return;

  var testdata     = scriptdata.data,

      // parse the current UA with uaparser
      ua           = uaparse(navigator.userAgent),

      // match the UA from uaparser into the browser used by caniuse
      browserKey   = JSONSelect.match('.agents .browser', scriptdata).indexOf(ua.family),
      currBrowser  = Object.keys(scriptdata.agents)[browserKey];

  // So Phantom doesn't kill the caniuse.com matching exit out as it's useless anyway within PhantomJS
  if(navigator.userAgent.indexOf("PhantomJS") != -1) {
    return;
  }

  // translate 'y' 'n' or 'a' into a boolean that Modernizr uses
  function bool(ciuresult){
    if (ciuresult == 'y' || ciuresult == 'a') return true;
    // 'p' is for polyfill
    if (ciuresult == 'n' || ciuresult == 'p') return false;
    throw 'unknown return value from can i use';
  }

  function testify(o){

    var ciubool = bool(o.ciuresult);

    // caniuse says audio/video are yes/no, Modernizr has more detail which we'll dumb down.
    if (~TEST.audvid.indexOf(o.feature))
      o.result = !!o.result;

    // if caniuse gave us a 'partial', lets let it pass with a note.
    if (o.ciuresult == 'a'){
      return ok(true,
        o.browser + o.version + ': Caniuse reported partial support for ' + o.ciufeature +
        '. So.. Modernizr\'s ' + o.result + ' is good enough...'
      );
    }


    // change the *documented* false positives
    if ((o.feature == 'textshadow' && o.browser == 'firefox' && o.version == 3)
        && ciubool == false
    ) {
      ciubool = o.fp = true;
    }

    // where we actually do most our assertions
    equal(o.result, ciubool,
      o.browser + o.version + ': Caniuse result for ' + o.ciufeature +
      ' matches Modernizr\'s ' + (o.fp ? '*false positive*' : 'result') + ' for ' + o.feature
    );
  }


  module('caniuse.com data matches', {
      setup:function() {
      },
      teardown:function() {
      }
  });


  test("we match caniuse data", function() {

    for (var feature in Modernizr){

      var ciufeatname = map[feature];

      if (ciufeatname === undefined) continue;

      var ciufeatdata = testdata[ciufeatname];

      if (ciufeatdata === undefined) throw 'unknown key of caniusedata';

      // get results for this feature for all versions of this browser
      var browserResults = ciufeatdata.stats[currBrowser];

      // let's get our versions in order..
      var minorver   = ua.minor &&                                  // caniuse doesn't use two digit minors
                       ua.minor.toString().replace(/(\d)\d$/,'$1'), // but opera does.

          majorminor = (ua.major + '.' + minorver)
                          // opera gets grouped in some cases by caniuse
                          .replace(/(9\.(6|5))/ , ua.family == 'opera' ? '9.5-9.6'   : "$1")
                          .replace(/(10\.(0|1))/, ua.family == 'opera' ? '10.0-10.1' : "$1"),

          mmResult   = browserResults[majorminor],
          mResult    = browserResults[ua.major];


      // check it against the major.minor: eg. FF 3.6
      if (mmResult && mmResult != 'u'){ // 'y' 'n' or 'a'

        // data ends w/ ` x` if its still prefixed in the imp
        mmResult = mmResult.replace(' x','');

        // match it against our data.
        testify({ feature     : feature
                , ciufeature  : ciufeatname
                , result      : Modernizr[feature]
                , ciuresult   : mmResult
                , browser     : currBrowser
                , version     : majorminor
        });

        continue; // don't check the major version
      }

      // check it against just the major version: eg. FF 3
      if (mResult){

        // unknown support from caniuse... He would probably like to know our data, though!
        if (mResult == 'u') continue;

        // data ends w/ ` x` if its still prefixed in the imp
        mResult = mResult.replace(' x','');

        testify({ feature     : feature
                , ciufeature  : ciufeatname
                , result      : Modernizr[feature]
                , ciuresult   : mResult
                , browser     : currBrowser
                , version     : ua.major
        });


      }

    } // for in loop

  }); // eo test()


}; // eo caniusecallback()