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