Merge "Enforce load.php's no-session constraint"
[lhc/web/wiklou.git] / resources / src / startup.js
index 0c2d6d6..312e745 100644 (file)
@@ -1,10 +1,10 @@
 /**
- * This script provides a function which is run to evaluate whether or not to
- * continue loading jQuery and the MediaWiki modules. This code should work on
- * even the most ancient of browsers, so be very careful when editing.
+ * Code in this file MUST work on even the most ancient of browsers!
+ *
+ * This file is where we decide whether to initialise the modern run-time.
  */
 /*jshint unused: false, evil: true */
-/*globals mw, RLQ: true, $VARS, $CODE, performance */
+/*globals mw, RLQ: true, NORLQ: true, $VARS, $CODE, performance */
 
 var mediaWikiLoadStart = ( new Date() ).getTime(),
 
@@ -15,63 +15,88 @@ var mediaWikiLoadStart = ( new Date() ).getTime(),
 mwPerformance.mark( 'mwLoadStart' );
 
 /**
- * Returns false for Grade C supported browsers.
+ * See <https://www.mediawiki.org/wiki/Compatibility#Browsers>
+ *
+ * Capabilities required for modern run-time:
+ * - DOM Level 4 & Selectors API Level 1
+ * - HTML5 & Web Storage
+ * - DOM Level 2 Events
+ *
+ * Browsers we support in our modern run-time (Grade A):
+ * - Chrome
+ * - IE 9+
+ * - Firefox 3.5+
+ * - Safari 4+
+ * - Opera 10.5+
+ * - Mobile Safari (iOS 1+)
+ * - Android 2.0+
  *
- * This function should only be used by the Startup module, do not expand it to
- * be generally useful beyond startup.
+ * Browsers we support in our no-javascript run-time (Grade C):
+ * - IE 6+
+ * - Firefox 3+
+ * - Safari 3+
+ * - Opera 10+
+ * - WebOS < 1.5
+ * - PlayStation
+ * - Symbian-based browsers
+ * - NetFront-based browser
+ * - Opera Mini
+ * - Nokia's Ovi Browser
+ * - MeeGo's browser
+ * - Google Glass
  *
- * See also:
- * - https://www.mediawiki.org/wiki/Compatibility#Browsers
- * - https://jquery.com/browser-support/
+ * Other browsers that pass the check are considered Grade X.
  */
-function isCompatible( ua ) {
-       if ( ua === undefined ) {
-               ua = navigator.userAgent;
-       }
+function isCompatible( str ) {
+       var ua = str || navigator.userAgent;
+       return !!(
+               // http://caniuse.com/#feat=queryselector
+               'querySelector' in document
+
+               // http://caniuse.com/#feat=namevalue-storage
+               // https://developer.blackberry.com/html5/apis/v1_0/localstorage.html
+               // https://blog.whatwg.org/this-week-in-html-5-episode-30
+               && 'localStorage' in window
 
-       // Browsers with outdated or limited JavaScript engines get the no-JS experience
-       return !(
-               // Internet Explorer < 9
-               ( ua.indexOf( 'MSIE' ) !== -1 && parseFloat( ua.split( 'MSIE' )[ 1 ] ) < 9 ) ||
-               // Firefox < 3
-               ( ua.indexOf( 'Firefox/' ) !== -1 && parseFloat( ua.split( 'Firefox/' )[ 1 ] ) < 3 ) ||
-               // Opera < 12
-               ( ua.indexOf( 'Opera/' ) !== -1 && ( ua.indexOf( 'Version/' ) === -1 ?
-                       // "Opera/x.y"
-                       parseFloat( ua.split( 'Opera/' )[ 1 ] ) < 10 :
-                       // "Opera/9.80 ... Version/x.y"
-                       parseFloat( ua.split( 'Version/' )[ 1 ] ) < 12
-               ) ) ||
-               // "Mozilla/0.0 ... Opera x.y"
-               ( ua.indexOf( 'Opera ' ) !== -1 && parseFloat( ua.split( ' Opera ' )[ 1 ] ) < 10 ) ||
-               // BlackBerry < 6
-               ua.match( /BlackBerry[^\/]*\/[1-5]\./ ) ||
-               // Open WebOS < 1.5
-               ua.match( /webOS\/1\.[0-4]/ ) ||
-               // Anything PlayStation based.
-               ua.match( /PlayStation/i ) ||
-               // Any Symbian based browsers
-               ua.match( /SymbianOS|Series60/ ) ||
-               // Any NetFront based browser
-               ua.match( /NetFront/ ) ||
-               // Opera Mini, all versions
-               ua.match( /Opera Mini/ ) ||
-               // Nokia's Ovi Browser
-               ua.match( /S40OviBrowser/ ) ||
-               // MeeGo's browser
-               ua.match( /MeeGo/ ) ||
-               // Google Glass browser groks JS but UI is too limited
-               ( ua.match( /Glass/ ) && ua.match( /Android/ ) )
+               // http://caniuse.com/#feat=addeventlistener
+               && 'addEventListener' in window
+
+               // Hardcoded exceptions for browsers that pass the requirement but we don't want to
+               // support in the modern run-time.
+               && !(
+                       ua.match( /webOS\/1\.[0-4]/ ) ||
+                       ua.match( /PlayStation/i ) ||
+                       ua.match( /SymbianOS|Series60|NetFront|Opera Mini|S40OviBrowser|MeeGo/ ) ||
+                       ( ua.match( /Glass/ ) && ua.match( /Android/ ) )
+               )
        );
 }
 
 // Conditional script injection
 ( function () {
+       var NORLQ, script;
        if ( !isCompatible() ) {
                // Undo class swapping in case of an unsupported browser.
                // See OutputPage::getHeadScripts().
                document.documentElement.className = document.documentElement.className
                        .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' );
+
+               NORLQ = window.NORLQ || [];
+               while ( NORLQ.length ) {
+                       NORLQ.shift()();
+               }
+               window.NORLQ = {
+                       push: function ( fn ) {
+                               fn();
+                       }
+               };
+
+               // Clear and disable the other queue
+               window.RLQ = {
+                       // No-op
+                       push: function () {}
+               };
+
                return;
        }
 
@@ -96,9 +121,15 @@ function isCompatible( ua ) {
                                fn();
                        }
                };
+
+               // Clear and disable the other queue
+               window.NORLQ = {
+                       // No-op
+                       push: function () {}
+               };
        }
 
-       var script = document.createElement( 'script' );
+       script = document.createElement( 'script' );
        script.src = $VARS.baseModulesUri;
        script.onload = script.onreadystatechange = function () {
                if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {