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