From 5b9aa7cf4a06ab55f2eed1715fbdab834e2942a0 Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Tue, 26 Nov 2013 14:30:28 -0800 Subject: [PATCH] mediawiki.inspect: add method for grepping loaded modules Example usage: >>> mw.inspect.grep(':hover .settings-text') ["ext.uls.init"] >>> mw.inspect.grep('options.expire') ["jquery.cookie", "mediawiki.user"] Use cases: * You're debugging a layout bug and want to trace a CSS rule to the module which inserts it. * A JavaScript error contains a distinct and plausibly unique substring (such as a variable name), and you want to identify the module that is causing the error. You can re-load the page with debug=1, but you still need to decipher load.php URLs for non-file modules, and you might not be able to reproduce the issue in debug mode. Alternately, you can grep a full clone of the production branch that the wiki is running (assuming you have one handy), but that won't help you if the module is a gadget. Change-Id: Ie5f0e7d1c7022f8d399e895f157db8acefc8abee --- resources/mediawiki/mediawiki.inspect.js | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/resources/mediawiki/mediawiki.inspect.js b/resources/mediawiki/mediawiki.inspect.js index e76141d3a1..62fa410459 100644 --- a/resources/mediawiki/mediawiki.inspect.js +++ b/resources/mediawiki/mediawiki.inspect.js @@ -237,6 +237,41 @@ } return [stats]; } + }, + + /** + * Perform a substring search across the JavaScript and CSS source code + * of all loaded modules and return an array of the names of the + * modules that matched. + * + * @param {string|RegExp} pattern String or regexp to match. + * @return {Array} Array of the names of modules that matched. + */ + grep: function ( pattern ) { + if ( typeof pattern.test !== 'function' ) { + // Based on Y.Escape.regex from YUI v3.15.0 + pattern = new RegExp( pattern.replace( /[\-$\^*()+\[\]{}|\\,.?\s]/g, '\\$&' ), 'g' ); + } + + return $.grep( inspect.getLoadedModules(), function ( moduleName ) { + var module = mw.loader.moduleRegistry[moduleName]; + + // Grep module's JavaScript + if ( $.isFunction( module.script ) && pattern.test( module.script.toString() ) ) { + return true; + } + + // Grep module's CSS + if ( + $.isPlainObject( module.style ) && $.isArray( module.style.css ) + && pattern.test( module.style.css.join( '' ) ) + ) { + // Module's CSS source matches + return true; + } + + return false; + } ); } }; -- 2.20.1