Merge "Keep headers from jumping when expire interface is shown"
[lhc/web/wiklou.git] / resources / src / startup.js
1 /**
2 * This file is where we decide whether to initialise the Grade A run-time.
3 *
4 * - Beware: This file MUST parse without errors on even the most ancient of browsers!
5 * - Beware: Do not call mwNow before the isCompatible() check.
6 */
7
8 /* global mw, $VARS, $CODE */
9
10 var mwPerformance = ( window.performance && performance.mark ) ? performance : {
11 mark: function () {}
12 },
13 // Define now() here to ensure valid comparison with mediaWikiLoadEnd (T153819).
14 mwNow = ( function () {
15 var perf = window.performance,
16 navStart = perf && perf.timing && perf.timing.navigationStart;
17 return navStart && typeof perf.now === 'function' ?
18 function () { return navStart + perf.now(); } :
19 function () { return Date.now(); };
20 }() ),
21 // eslint-disable-next-line no-unused-vars
22 mediaWikiLoadStart;
23
24 /**
25 * See <https://www.mediawiki.org/wiki/Compatibility#Browsers>
26 *
27 * Capabilities required for modern run-time:
28 * - ECMAScript 5
29 * - DOM Level 4 & Selectors API Level 1
30 * - HTML5 & Web Storage
31 * - DOM Level 2 Events
32 *
33 * Browsers we support in our modern run-time (Grade A):
34 * - Chrome 13+
35 * - IE 10+
36 * - Firefox 4+
37 * - Safari 5+
38 * - Opera 12.10+
39 * - Mobile Safari 5.1+ (iOS 5+)
40 * - Android 4.1+
41 *
42 * Browsers we support in our no-javascript run-time (Grade C):
43 * - Chrome 1+
44 * - IE 6+
45 * - Firefox 3+
46 * - Safari 3+
47 * - Opera 10+
48 * - Mobile Safari 5.0+ (iOS 4+)
49 * - Android 2.0+
50 * - WebOS < 1.5
51 * - PlayStation
52 * - Symbian-based browsers
53 * - NetFront-based browser
54 * - Opera Mini
55 * - Nokia's Ovi Browser
56 * - MeeGo's browser
57 * - Google Glass
58 * - UC Mini (speed mode on)
59 *
60 * Other browsers that pass the check are considered Grade X.
61 *
62 * @param {string} [str] User agent, defaults to navigator.userAgent
63 * @return {boolean} User agent is compatible with MediaWiki JS
64 */
65 function isCompatible( str ) {
66 var ua = str || navigator.userAgent;
67 return !!(
68 // http://caniuse.com/#feat=es5
69 // http://caniuse.com/#feat=use-strict
70 // http://caniuse.com/#feat=json / https://phabricator.wikimedia.org/T141344#2784065
71 ( function () {
72 'use strict';
73 return !this && !!Function.prototype.bind && !!window.JSON;
74 }() ) &&
75
76 // http://caniuse.com/#feat=queryselector
77 'querySelector' in document &&
78
79 // http://caniuse.com/#feat=namevalue-storage
80 // https://developer.blackberry.com/html5/apis/v1_0/localstorage.html
81 // https://blog.whatwg.org/this-week-in-html-5-episode-30
82 'localStorage' in window &&
83
84 // http://caniuse.com/#feat=addeventlistener
85 'addEventListener' in window &&
86
87 // Hardcoded exceptions for browsers that pass the requirement but we don't want to
88 // support in the modern run-time.
89 // Note: Please extend the regex instead of adding new ones
90 !(
91 ua.match( /webOS\/1\.[0-4]|SymbianOS|Series60|NetFront|Opera Mini|S40OviBrowser|MeeGo|Android.+Glass|^Mozilla\/5\.0 .+ Gecko\/$|googleweblight/ ) ||
92 ua.match( /PlayStation/i )
93 )
94 );
95 }
96
97 // Conditional script injection
98 ( function () {
99 var NORLQ, script;
100 if ( !isCompatible() ) {
101 // Undo class swapping in case of an unsupported browser.
102 // See ResourceLoaderClientHtml::getDocumentAttributes().
103 document.documentElement.className = document.documentElement.className
104 .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' );
105
106 NORLQ = window.NORLQ || [];
107 while ( NORLQ.length ) {
108 NORLQ.shift()();
109 }
110 window.NORLQ = {
111 push: function ( fn ) {
112 fn();
113 }
114 };
115
116 // Clear and disable the other queue
117 window.RLQ = {
118 // No-op
119 push: function () {}
120 };
121
122 return;
123 }
124
125 /**
126 * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php.
127 */
128 function startUp() {
129 mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals );
130
131 $CODE.registrations();
132
133 mw.config.set( $VARS.configuration );
134
135 // Must be after mw.config.set because these callbacks may use mw.loader which
136 // needs to have values 'skin', 'debug' etc. from mw.config.
137 // eslint-disable-next-line vars-on-top
138 var RLQ = window.RLQ || [];
139 while ( RLQ.length ) {
140 RLQ.shift()();
141 }
142 window.RLQ = {
143 push: function ( fn ) {
144 fn();
145 }
146 };
147
148 // Clear and disable the other queue
149 window.NORLQ = {
150 // No-op
151 push: function () {}
152 };
153 }
154
155 mediaWikiLoadStart = mwNow();
156 mwPerformance.mark( 'mwLoadStart' );
157
158 script = document.createElement( 'script' );
159 script.src = $VARS.baseModulesUri;
160 script.onload = script.onreadystatechange = function () {
161 if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
162 // Clean up
163 script.onload = script.onreadystatechange = null;
164 script = null;
165 // Callback
166 startUp();
167 }
168 };
169 document.getElementsByTagName( 'head' )[ 0 ].appendChild( script );
170 }() );