mw.Debug.profile: Tweak support check to handle PhantomJS
authorMatthew Flaschen <mflaschen@wikimedia.org>
Fri, 11 Jul 2014 04:40:40 +0000 (00:40 -0400)
committerMatthew Flaschen <mflaschen@wikimedia.org>
Fri, 5 Sep 2014 22:57:32 +0000 (18:57 -0400)
* Latest PhantomJS (1.9.7) supports Array.prototype.map,
  Array.prototype.reduce, Array.prototype.filter, and
  document.createElementNS, but not Function.prototype.bind
* Check all of these in the init function, and if they're available
  continue (unless there's no profiling data) and initialize
  this.createSvgElement (previously done inline)
* Accordingly, rename buildRequiresES5 to buildRequiresBrowserFeatures
  and tweak message

Change-Id: If464b1093ec445afbb6c6b8c230e0e2669ad8c5c

resources/src/mediawiki/mediawiki.debug.profile.js

index 7bbbbd3..04f7acd 100644 (file)
                        // only drop events if requested
                        dropThresholdPx = dropThresholdPx || 0;
 
-                       if ( !Array.prototype.map || !Array.prototype.reduce || !Array.prototype.filter ) {
-                               profile.container = profile.buildRequiresES5();
+                       if (
+                               !Array.prototype.map ||
+                               !Array.prototype.reduce ||
+                               !Array.prototype.filter ||
+                               !document.createElementNS ||
+                               !document.createElementNS.bind
+                       ) {
+                               profile.container = profile.buildRequiresBrowserFeatures();
                        } else if ( data.length === 0 ) {
                                profile.container = profile.buildNoData();
                        } else {
+                               // Initialize createSvgElement (now that we know we have
+                               // document.createElementNS and bind)
+                               this.createSvgElement = document.createElementNS.bind( document, 'http://www.w3.org/2000/svg' );
+
                                // generate a flyout
                                profile.data = new ProfileData( data, profile.width, mergeThresholdPx, dropThresholdPx );
                                // draw it
@@ -52,9 +62,9 @@
                        return profile.container;
                },
 
-               buildRequiresES5: function () {
+               buildRequiresBrowserFeatures: function () {
                        return $( '<div>' )
-                               .text( 'An ES5 compatible javascript engine is required for the profile visualization.' )
+                               .text( 'Certain browser features, including parts of ECMAScript 5 and document.createElementNS, are required for the profile visualization.' )
                                .get( 0 );
                },
 
 
                /**
                 * Creates DOM nodes appropriately namespaced for SVG.
+                * Initialized in init after checking support
                 *
                 * @param string tag to create
                 * @return DOMElement
                 */
-               createSvgElement: ( document.createElementNS && Function.prototype.bind )
-                       ? document.createElementNS.bind( document, 'http://www.w3.org/2000/svg' )
-                       // throw a error for browsers which does not support document.createElementNS (IE<8)
-                       : function () { throw new Error( 'An ES5 compatible javascript engine is required for the profile visualization.' ); },
+               createSvgElement: null,
 
                /**
                 * @param DOMElement|undefined