summaryrefslogtreecommitdiff
path: root/source/bower_components/modernizr/test/js/lib/detect-global.js
blob: 48b4ac244cb5918d44425f0c7844f220d11abfa6 (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
// https://github.com/kangax/detect-global

// tweaked to run without a UI.

(function () {
    function getPropertyDescriptors(object) {
      var props = { };
      for (var prop in object) {
        
        // nerfing for firefox who goes crazy over some objects like sessionStorage
        try {
          
          props[prop] = {
            type:  typeof object[prop],
            value: object[prop]
          };
          
        } catch(e){
          props[prop] = {}; 
        }
      }
      return props;
    }
    
    function getCleanWindow() {
      var elIframe = document.createElement('iframe');
      elIframe.style.display = 'none';
      
      var ref = document.getElementsByTagName('script')[0];
      ref.parentNode.insertBefore(elIframe, ref);
      
      elIframe.src = 'about:blank';
      return elIframe.contentWindow;
    }
    
    function appendControl(el, name) {
      var elCheckbox = document.createElement('input');
      elCheckbox.type = 'checkbox';
      elCheckbox.checked = true;
      elCheckbox.id = '__' + name;
      
      var elLabel = document.createElement('label');
      elLabel.htmlFor = '__' + name;
      elLabel.innerHTML = 'Exclude ' + name + ' properties?';
      elLabel.style.marginLeft = '0.5em';
      
      var elWrapper = document.createElement('p');
      elWrapper.style.marginBottom = '0.5em';
      
      elWrapper.appendChild(elCheckbox);
      elWrapper.appendChild(elLabel);

      el.appendChild(elWrapper);
    }
    
    function appendAnalyze(el) {
      var elAnalyze = document.createElement('button');
      elAnalyze.id = '__analyze';
      elAnalyze.innerHTML = 'Analyze';
      elAnalyze.style.marginTop = '1em';
      el.appendChild(elAnalyze);
    }
    
    function appendCancel(el) {
      var elCancel = document.createElement('a');
      elCancel.href = '#';
      elCancel.innerHTML = 'Cancel';
      elCancel.style.cssText = 'color:#eee;margin-left:0.5em;';
      elCancel.onclick = function() {
        el.parentNode.removeChild(el);
        return false; 
      };
      el.appendChild(elCancel);
    }
    
    function initConfigPopup() {
      var el = document.createElement('div');
      
      el.style.cssText =  'position:fixed; left:10px; top:10px; width:300px; background:rgba(50,50,50,0.9);' +
                          '-moz-border-radius:10px; padding:1em; color: #eee; text-align: left;' +
                          'font-family: "Helvetica Neue", Verdana, Arial, sans serif; z-index: 99999;';
      
      for (var prop in propSets) {
        appendControl(el, prop);
      }
      
      appendAnalyze(el);
      appendCancel(el);
      
      var ref = document.getElementsByTagName('script')[0];
      ref.parentNode.insertBefore(el, ref);
    }
    
    function getPropsCount(object) {
      var count = 0;
      for (var prop in object) {
        count++;
      }
      return count;
    }
    
    function shouldDeleteProperty(propToCheck) {
      for (var prop in propSets) {
        var elCheckbox = document.getElementById('__' + prop);
        var isPropInSet = propSets[prop].indexOf(propToCheck) > -1;
        if (isPropInSet && (elCheckbox ? elCheckbox.checked : true) ) {
          return true;
        }
      }
    }
    
    function analyze() {
      var global = (function(){ return this; })(),
          globalProps = getPropertyDescriptors(global),
          cleanWindow = getCleanWindow();
          
      for (var prop in cleanWindow) {
        if (globalProps[prop]) {
          delete globalProps[prop];
        }
      }
      for (var prop in globalProps) {
        if (shouldDeleteProperty(prop)) {
          delete globalProps[prop];
        }
      }
      
      window.__globalsCount = getPropsCount(globalProps);
      window.__globals      = globalProps;
      
      window.console && console.log('Total number of global properties: ' + __globalsCount);
      window.console && console.dir(__globals);
    }
    
    var propSets = {
      'Prototype':        '$$ $A $F $H $R $break $continue $w Abstract Ajax Class Enumerable Element Field Form ' +
                          'Hash Insertion ObjectRange PeriodicalExecuter Position Prototype Selector Template Toggle Try'.split(' '),
                        
      'Scriptaculous':    'Autocompleter Builder Control Draggable Draggables Droppables Effect Sortable SortableObserver Sound Scriptaculous'.split(' '),
      'Firebug':          'loadFirebugConsole console _getFirebugConsoleElement _FirebugConsole _FirebugCommandLine _firebug'.split(' '),
      'Mozilla':          'Components XPCNativeWrapper XPCSafeJSObjectWrapper getInterface netscape GetWeakReference GeckoActiveXObject'.split(' '),
      'GoogleAnalytics':  'gaJsHost gaGlobal _gat _gaq pageTracker'.split(' '),
      'lazyGlobals':      'onhashchange'.split(' ')
    };
    
    // initConfigPopup(); // disable because we're going UI-less.
    
    var analyzeElem = document.getElementById('__analyze');
    analyzeElem && (analyzeElem.onclick = analyze);
    
    analyze(); // and assign total added globals to window.__globalsCount
    
})();