|
111 | 111 | *
|
112 | 112 | * @private
|
113 | 113 | * @param {Object} options The options object to simulate environment objects.
|
114 |
| - * @returns {Object} The modified string. |
| 114 | + * @returns {Object} The simulated platform object. |
115 | 115 | */
|
116 | 116 | var getPlatform = (function() {
|
117 |
| - var code, |
118 |
| - result, |
119 |
| - xhr; |
120 |
| - |
121 |
| - // for browsers |
122 |
| - if (root.document && !root.phantom) { |
123 |
| - if (isHostType(root, 'ActiveXObject')) { |
124 |
| - xhr = new ActiveXObject('Microsoft.XMLHTTP'); |
125 |
| - } else if (isHostType(root, 'XMLHttpRequest')) { |
126 |
| - xhr = new XMLHttpRequest; |
127 |
| - } |
128 |
| - each(document.getElementsByTagName('script'), function(element) { |
129 |
| - var src = element.src; |
130 |
| - if (/platform\.js$/.test(src)) { |
131 |
| - xhr.open('get', src + '?t=' + (+new Date), false); |
132 |
| - xhr.send(null); |
133 |
| - code = xhr.responseText; |
| 117 | + // provides a cache for platform objects |
| 118 | + var cache = {}; |
| 119 | + var getPlatform = (function() { |
| 120 | + var code, |
| 121 | + result, |
| 122 | + xhr; |
| 123 | + |
| 124 | + // for browsers |
| 125 | + if (root.document && !root.phantom) { |
| 126 | + if (isHostType(root, 'ActiveXObject')) { |
| 127 | + xhr = new ActiveXObject('Microsoft.XMLHTTP'); |
| 128 | + } else if (isHostType(root, 'XMLHttpRequest')) { |
| 129 | + xhr = new XMLHttpRequest; |
134 | 130 | }
|
135 |
| - }); |
136 |
| - } |
137 |
| - // for Narwhal and Rhino |
138 |
| - else if (typeof readFile == 'function') { |
139 |
| - code = readFile('../platform.js'); |
140 |
| - } |
141 |
| - // for Node.js, PhantomJS, and RingoJS |
142 |
| - else if (typeof require == 'function') { |
143 |
| - code = (require('fs').readFileSync || require('fs').read)('../platform.js'); |
144 |
| - } |
145 |
| - return Function('options', |
146 |
| - ('return ' + |
147 |
| - /\(function[\s\S]+?(?=if\s*\(typeof define)/.exec(code)[0] + |
148 |
| - ' return parse()}.call(this))') |
149 |
| - .replace('/internal|\\n/i.test(toString.toString())', '!me.likeChrome') |
150 |
| - .replace(/\broot\s*=[^\n]+?(;\n)/, 'root=options$1') |
151 |
| - .replace(/\boldRoot\s*=[^\n]+?(;\n)/, 'oldRoot=options$1') |
152 |
| - .replace(/\bvar thisBinding\s*=[^\n]+?(;\n)/, '') |
153 |
| - .replace(/\bfreeGlobal\s*=(?:.|\n)+?(;\n)\s*if[^}]+\}/, 'freeGlobal=options.global$1') |
154 |
| - .replace(/\buserAgent\s*=[^\n]+?(;\n)/, 'userAgent=me.ua$1') |
155 |
| - .replace(/\b(?:thisBinding|root)\b/g, 'me') |
156 |
| - .replace(/([^.])\bsystem\b/g, '$1me.system') |
157 |
| - .replace(/\bgetClassOf\(opera\)/g, 'opera&&opera["[[Class]]"]') |
158 |
| - .replace(/\b(?:Environment|Java|RuntimeObject|ScriptBridgingProxyObject)\b/g, 'Object') |
159 |
| - .replace(/\bnav\.appMinorVersion/g, 'me.appMinorVersion') |
160 |
| - .replace(/\bnav\.cpuClass/g, 'me.cpuClass') |
161 |
| - .replace(/\bnav\.platform/g, 'me.platform') |
162 |
| - .replace(/\bexports\b/g, 'me.exports') |
163 |
| - .replace(/\bexternal/g, 'me.external') |
164 |
| - .replace(/\bprocess\b/g, 'me.process') |
165 |
| - .replace(/\brequire\b/g, 'me.require') |
166 |
| - .replace(/\bdoc\.documentMode/g, 'me.mode')); |
| 131 | + each(document.getElementsByTagName('script'), function(element) { |
| 132 | + var src = element.src; |
| 133 | + if (/platform\.js$/.test(src)) { |
| 134 | + xhr.open('get', src + '?t=' + (+new Date), false); |
| 135 | + xhr.send(null); |
| 136 | + code = xhr.responseText; |
| 137 | + } |
| 138 | + }); |
| 139 | + } |
| 140 | + // for Narwhal and Rhino |
| 141 | + else if (typeof readFile == 'function') { |
| 142 | + code = readFile('../platform.js'); |
| 143 | + } |
| 144 | + // for Node.js, PhantomJS, and RingoJS |
| 145 | + else if (typeof require == 'function') { |
| 146 | + code = (require('fs').readFileSync || require('fs').read)('../platform.js'); |
| 147 | + } |
| 148 | + return Function('options', |
| 149 | + ('return ' + |
| 150 | + /\(function[\s\S]+?(?=if\s*\(typeof define)/.exec(code)[0] + |
| 151 | + ' return parse()}.call(this))') |
| 152 | + .replace('/internal|\\n/i.test(toString.toString())', '!me.likeChrome') |
| 153 | + .replace(/\broot\s*=[^\n]+?(;\n)/, 'root=options$1') |
| 154 | + .replace(/\boldRoot\s*=[^\n]+?(;\n)/, 'oldRoot=options$1') |
| 155 | + .replace(/\bvar thisBinding\s*=[^\n]+?(;\n)/, '') |
| 156 | + .replace(/\bfreeGlobal\s*=(?:.|\n)+?(;\n)\s*if[^}]+\}/, 'freeGlobal=options.global$1') |
| 157 | + .replace(/\buserAgent\s*=[^\n]+?(;\n)/, 'userAgent=me.ua$1') |
| 158 | + .replace(/\b(?:thisBinding|root)\b/g, 'me') |
| 159 | + .replace(/([^.])\bsystem\b/g, '$1me.system') |
| 160 | + .replace(/\bgetClassOf\(opera\)/g, 'opera&&opera["[[Class]]"]') |
| 161 | + .replace(/\b(?:Environment|Java|RuntimeObject|ScriptBridgingProxyObject)\b/g, 'Object') |
| 162 | + .replace(/\bnav\.appMinorVersion/g, 'me.appMinorVersion') |
| 163 | + .replace(/\bnav\.cpuClass/g, 'me.cpuClass') |
| 164 | + .replace(/\bnav\.platform/g, 'me.platform') |
| 165 | + .replace(/\bexports\b/g, 'me.exports') |
| 166 | + .replace(/\bexternal/g, 'me.external') |
| 167 | + .replace(/\bprocess\b/g, 'me.process') |
| 168 | + .replace(/\brequire\b/g, 'me.require') |
| 169 | + .replace(/\bdoc\.documentMode/g, 'me.mode')); |
| 170 | + }()); |
| 171 | + return function(name, options) { |
| 172 | + return cache[name] || (cache[name] = getPlatform(options)); |
| 173 | + }; |
167 | 174 | }());
|
168 | 175 |
|
| 176 | + |
169 | 177 | /*--------------------------------------------------------------------------*/
|
170 | 178 |
|
171 | 179 | /**
|
|
1883 | 1891 | each(['description', 'layout', 'manufacturer', 'name', 'os', 'prerelease', 'product', 'version'], function(name) {
|
1884 | 1892 | test('has the correct `platform.' + name + '` property', function() {
|
1885 | 1893 | forOwn(Tests, function(value, key) {
|
1886 |
| - var platform = getPlatform(value); |
| 1894 | + var platform = getPlatform(key, value); |
1887 | 1895 | value = name == 'description' ? key : value[name];
|
1888 | 1896 | value = value ? interpolate(value, { 'alpha': '\u03b1', 'beta': '\u03b2', ' ': ' ' }) : null;
|
1889 | 1897 | equal(platform && String(platform[name]), String(value), String(platform));
|
|
1892 | 1900 | });
|
1893 | 1901 |
|
1894 | 1902 | test('has correct null values', function() {
|
1895 |
| - forOwn(Tests, function(value) { |
1896 |
| - forOwn(getPlatform(value), function(value, key) { |
| 1903 | + forOwn(Tests, function(value, key) { |
| 1904 | + forOwn(getPlatform(key, value), function(value, key) { |
1897 | 1905 | !value && strictEqual(value, null, 'platform.' + key);
|
1898 | 1906 | });
|
1899 | 1907 | });
|
1900 | 1908 | });
|
1901 | 1909 |
|
1902 | 1910 | test('handles no user agent', function() {
|
1903 |
| - forOwn(getPlatform({}), function(value, key) { |
| 1911 | + forOwn(getPlatform('', {}), function(value, key) { |
1904 | 1912 | if (typeof value != 'function') {
|
1905 | 1913 | equal(String(value), 'null', 'platform.' + key);
|
1906 | 1914 | }
|
|
0 commit comments