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