From fd93e5890425ca07876f6048a56bf01e7a601485 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Wed, 27 Jan 2010 05:17:26 +0000 Subject: [PATCH] * Removed JS2 stuff from jquery.js. If there's anything we really need from it, it can be re-added to wikibits.js, but that can wait for the extension analysis which I'm going to do next. * Added Ryan Grove's jsmin.php and wrote a maintenance script wrapper called minify.php * Added a makefile to rebuild skins/common * Rebuilt jquery.min.js --- includes/AutoLoader.php | 1 + includes/JSMin.php | 291 +++++++++++++++++++++++++++++++++++++ maintenance/minify.php | 111 ++++++++++++++ skins/common/Makefile | 2 + skins/common/jquery.js | 67 +-------- skins/common/jquery.min.js | 5 +- skins/common/wikibits.js | 5 + 7 files changed, 413 insertions(+), 69 deletions(-) create mode 100644 includes/JSMin.php create mode 100644 maintenance/minify.php create mode 100644 skins/common/Makefile diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 4504b1c10e..e049e9a51b 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -132,6 +132,7 @@ $wgAutoloadLocalClasses = array( 'Interwiki' => 'includes/Interwiki.php', 'IP' => 'includes/IP.php', 'Job' => 'includes/JobQueue.php', + 'JSMin' => 'includes/JSMin.php', 'LCStore_DB' => 'includes/LocalisationCache.php', 'LCStore_CDB' => 'includes/LocalisationCache.php', 'LCStore_Null' => 'includes/LocalisationCache.php', diff --git a/includes/JSMin.php b/includes/JSMin.php new file mode 100644 index 0000000000..3c2f859dfd --- /dev/null +++ b/includes/JSMin.php @@ -0,0 +1,291 @@ + + * @copyright 2002 Douglas Crockford (jsmin.c) + * @copyright 2008 Ryan Grove (PHP port) + * @license http://opensource.org/licenses/mit-license.php MIT License + * @version 1.1.1 (2008-03-02) + * @link http://code.google.com/p/jsmin-php/ + */ + +class JSMin { + const ORD_LF = 10; + const ORD_SPACE = 32; + + protected $a = ''; + protected $b = ''; + protected $input = ''; + protected $inputIndex = 0; + protected $inputLength = 0; + protected $lookAhead = null; + protected $output = ''; + + // -- Public Static Methods -------------------------------------------------- + + public static function minify($js) { + $jsmin = new JSMin($js); + return $jsmin->min(); + } + + // -- Public Instance Methods ------------------------------------------------ + + public function __construct($input) { + $this->input = str_replace("\r\n", "\n", $input); + $this->inputLength = strlen($this->input); + } + + // -- Protected Instance Methods --------------------------------------------- + + protected function action($d) { + switch($d) { + case 1: + $this->output .= $this->a; + + case 2: + $this->a = $this->b; + + if ($this->a === "'" || $this->a === '"') { + for (;;) { + $this->output .= $this->a; + $this->a = $this->get(); + + if ($this->a === $this->b) { + break; + } + + if (ord($this->a) <= self::ORD_LF) { + throw new JSMinException('Unterminated string literal.'); + } + + if ($this->a === '\\') { + $this->output .= $this->a; + $this->a = $this->get(); + } + } + } + + case 3: + $this->b = $this->next(); + + if ($this->b === '/' && ( + $this->a === '(' || $this->a === ',' || $this->a === '=' || + $this->a === ':' || $this->a === '[' || $this->a === '!' || + $this->a === '&' || $this->a === '|' || $this->a === '?')) { + + $this->output .= $this->a . $this->b; + + for (;;) { + $this->a = $this->get(); + + if ($this->a === '/') { + break; + } elseif ($this->a === '\\') { + $this->output .= $this->a; + $this->a = $this->get(); + } elseif (ord($this->a) <= self::ORD_LF) { + throw new JSMinException('Unterminated regular expression '. + 'literal.'); + } + + $this->output .= $this->a; + } + + $this->b = $this->next(); + } + } + } + + protected function get() { + $c = $this->lookAhead; + $this->lookAhead = null; + + if ($c === null) { + if ($this->inputIndex < $this->inputLength) { + $c = substr($this->input, $this->inputIndex, 1); + $this->inputIndex += 1; + } else { + $c = null; + } + } + + if ($c === "\r") { + return "\n"; + } + + if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) { + return $c; + } + + return ' '; + } + + protected function isAlphaNum($c) { + return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1; + } + + protected function min() { + $this->a = "\n"; + $this->action(3); + + while ($this->a !== null) { + switch ($this->a) { + case ' ': + if ($this->isAlphaNum($this->b)) { + $this->action(1); + } else { + $this->action(2); + } + break; + + case "\n": + switch ($this->b) { + case '{': + case '[': + case '(': + case '+': + case '-': + $this->action(1); + break; + + case ' ': + $this->action(3); + break; + + default: + if ($this->isAlphaNum($this->b)) { + $this->action(1); + } + else { + $this->action(2); + } + } + break; + + default: + switch ($this->b) { + case ' ': + if ($this->isAlphaNum($this->a)) { + $this->action(1); + break; + } + + $this->action(3); + break; + + case "\n": + switch ($this->a) { + case '}': + case ']': + case ')': + case '+': + case '-': + case '"': + case "'": + $this->action(1); + break; + + default: + if ($this->isAlphaNum($this->a)) { + $this->action(1); + } + else { + $this->action(3); + } + } + break; + + default: + $this->action(1); + break; + } + } + } + + return $this->output; + } + + protected function next() { + $c = $this->get(); + + if ($c === '/') { + switch($this->peek()) { + case '/': + for (;;) { + $c = $this->get(); + + if (ord($c) <= self::ORD_LF) { + return $c; + } + } + + case '*': + $this->get(); + + for (;;) { + switch($this->get()) { + case '*': + if ($this->peek() === '/') { + $this->get(); + return ' '; + } + break; + + case null: + throw new JSMinException('Unterminated comment.'); + } + } + + default: + return $c; + } + } + + return $c; + } + + protected function peek() { + $this->lookAhead = $this->get(); + return $this->lookAhead; + } +} + +// -- Exceptions --------------------------------------------------------------- +class JSMinException extends Exception {} +?> \ No newline at end of file diff --git a/maintenance/minify.php b/maintenance/minify.php new file mode 100644 index 0000000000..601a4d67ca --- /dev/null +++ b/maintenance/minify.php @@ -0,0 +1,111 @@ +addOption( 'outfile', + 'File for output. Only a single file may be specified for input.', + false, true ); + $this->addOption( 'outdir', + "Directory for output. If this is not specified, and neither is --outfile, then the\n" . + "output files will be sent to the same directories as the input files.", + false, true ); + $this->mDescription = "Minify a file or set of files.\n\n" . + "If --outfile is not specified, then the output file names will have a .min extension\n" . + "added, e.g. jquery.js -> jquery.min.js."; + + } + + public function execute() { + if ( !count( $this->mArgs ) ) { + $this->error( "minify.php: At least one input file must be specified." ); + exit( 1 ); + } + + if ( $this->hasOption( 'outfile' ) ) { + if ( count( $this->mArgs ) > 1 ) { + $this->error( '--outfile may only be used with a single input file.' ); + exit( 1 ); + } + + // Minify one file + $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) ); + return; + } + + $outDir = $this->getOption( 'outdir', false ); + + foreach ( $this->mArgs as $arg ) { + $inPath = realpath( $arg ); + $inName = basename( $inPath ); + $inDir = dirname( $inPath ); + + if ( strpos( $inName, '.min.' ) !== false ) { + echo "Skipping $inName\n"; + continue; + } + + if ( !file_exists( $inPath ) ) { + $this->error( "File does not exist: $arg" ); + exit( 1 ); + } + + $extension = $this->getExtension( $inName ); + $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension; + if ( $outDir === false ) { + $outPath = $inDir . '/' . $outName; + } else { + $outPath = $outDir . '/' . $outName; + } + + $this->minify( $inPath, $outPath ); + } + } + + public function getExtension( $fileName ) { + $dotPos = strrpos( $fileName, '.' ); + if ( $dotPos === false ) { + $this->error( "No file extension, cannot determine type: $arg" ); + exit( 1 ); + } + return substr( $fileName, $dotPos + 1 ); + } + + public function minify( $inPath, $outPath ) { + $extension = $this->getExtension( $inPath ); + echo basename( $inPath ) . ' -> ' . basename( $outPath ) . '...'; + + $inText = file_get_contents( $inPath ); + if ( $inText === false ) { + $this->error( "Unable to open file $inPath for reading." ); + exit( 1 ); + } + $outFile = fopen( $outPath, 'w' ); + if ( !$outFile ) { + $this->error( "Unable to open file $outPath for writing." ); + exit( 1 ); + } + + switch ( $extension ) { + case 'js': + $outText = JSMin::minify( $inText ); + break; + default: + $this->error( "No minifier defined for extension \"$extension\"" ); + } + + fwrite( $outFile, $outText ); + fclose( $outFile ); + echo " ok\n"; + } +} + +$maintClass = 'MinifyScript'; +require_once( DO_MAINTENANCE ); diff --git a/skins/common/Makefile b/skins/common/Makefile new file mode 100644 index 0000000000..56e60bd4a0 --- /dev/null +++ b/skins/common/Makefile @@ -0,0 +1,2 @@ +jquery.min.js: jquery.js + php ../../maintenance/minify.php $< --outfile $@ diff --git a/skins/common/jquery.js b/skins/common/jquery.js index b61567b75e..377bb51e95 100644 --- a/skins/common/jquery.js +++ b/skins/common/jquery.js @@ -4377,71 +4377,8 @@ jQuery.each([ "Height", "Width" ], function(i, name){ }); })(); -/* JavaScript for MediaWIki JS2 */ - -/** - * This is designed to be directly compatible with (and is essentially taken - * directly from) the mv_embed code for bringing internationalized messages into - * the JavaScript space. As such, if we get to the point of merging that stuff - * into the main branch this code will be uneeded and probably cause issues. - */ - /** - * Mimics the no-conflict method used by the js2 stuff + * Add a suitable MW-specific alias */ $j = jQuery.noConflict(); -/** - * Provides js2 compatible mw functions - */ -if( typeof mw == 'undefined' || !mw ){ - mw = { }; - /** - * Provides js2 compatible onload hook - * @param func Function to call when ready - */ - mw.ready = function( func ) { - $j(document).ready( func ); - } - // Define a dummy mw.load function: - mw.load = function( deps, callback ) { callback(); }; - - // Deinfe a dummy mw.loadDone function: - mw.loadDone = function( className ) { }; - - // Creates global message object if not already in existence - if ( !gMsg ) var gMsg = {}; - - /** - * Caches a list of messages for later retrieval - * @param {Object} msgSet Hash of key:value pairs of messages to cache - */ - mw.addMessages = function ( msgSet ){ - for ( var i in msgSet ){ - gMsg[ i ] = msgSet[i]; - } - } - /** - * Retieves a message from the global message cache, performing on-the-fly - * replacements using MediaWiki message syntax ($1, $2, etc.) - * @param {String} key Name of message as it is in MediaWiki - * @param {Array} args Array of replacement arguments - */ - function gM( key, args ) { - var ms = ''; - if ( key in gMsg ) { - ms = gMsg[ key ]; - if ( typeof args == 'object' || typeof args == 'array' ) { - for ( var v in args ){ - var rep = '\$'+ ( parseInt(v) + 1 ); - ms = ms.replace( rep, args[v]); - } - } else if ( typeof args =='string' || typeof args =='number' ) { - ms = ms.replace( /\$1/, args ); - } - return ms; - } else { - return '[' + key + ']'; - } - } - -} + diff --git a/skins/common/jquery.min.js b/skins/common/jquery.min.js index a493887ec4..6574100c64 100644 --- a/skins/common/jquery.min.js +++ b/skins/common/jquery.min.js @@ -430,7 +430,4 @@ top+=body.offsetTop,left+=body.offsetLeft;if(prevComputedStyle.position==="fixed top+=Math.max(docElem.scrollTop,body.scrollTop),left+=Math.max(docElem.scrollLeft,body.scrollLeft);return{top:top,left:left};};jQuery.offset={initialize:function(){if(this.initialized)return;var body=document.body,container=document.createElement('div'),innerDiv,checkDiv,table,td,rules,prop,bodyMarginTop=body.style.marginTop,html='
';rules={position:'absolute',top:0,left:0,margin:0,border:0,width:'1px',height:'1px',visibility:'hidden'};for(prop in rules)container.style[prop]=rules[prop];container.innerHTML=html;body.insertBefore(container,body.firstChild);innerDiv=container.firstChild,checkDiv=innerDiv.firstChild,td=innerDiv.nextSibling.firstChild.firstChild;this.doesNotAddBorder=(checkDiv.offsetTop!==5);this.doesAddBorderForTableAndCells=(td.offsetTop===5);innerDiv.style.overflow='hidden',innerDiv.style.position='relative';this.subtractsBorderForOverflowNotVisible=(checkDiv.offsetTop===-5);body.style.marginTop='1px';this.doesNotIncludeMarginInBodyOffset=(body.offsetTop===0);body.style.marginTop=bodyMarginTop;body.removeChild(container);this.initialized=true;},bodyOffset:function(body){jQuery.offset.initialized||jQuery.offset.initialize();var top=body.offsetTop,left=body.offsetLeft;if(jQuery.offset.doesNotIncludeMarginInBodyOffset) top+=parseInt(jQuery.curCSS(body,'marginTop',true),10)||0,left+=parseInt(jQuery.curCSS(body,'marginLeft',true),10)||0;return{top:top,left:left};}};jQuery.fn.extend({position:function(){var left=0,top=0,results;if(this[0]){var offsetParent=this.offsetParent(),offset=this.offset(),parentOffset=/^body|html$/i.test(offsetParent[0].tagName)?{top:0,left:0}:offsetParent.offset();offset.top-=num(this,'marginTop');offset.left-=num(this,'marginLeft');parentOffset.top+=num(offsetParent,'borderTopWidth');parentOffset.left+=num(offsetParent,'borderLeftWidth');results={top:offset.top-parentOffset.top,left:offset.left-parentOffset.left};} return results;},offsetParent:function(){var offsetParent=this[0].offsetParent||document.body;while(offsetParent&&(!/^body|html$/i.test(offsetParent.tagName)&&jQuery.css(offsetParent,'position')=='static')) -offsetParent=offsetParent.offsetParent;return jQuery(offsetParent);}});jQuery.each(['Left','Top'],function(i,name){var method='scroll'+name;jQuery.fn[method]=function(val){if(!this[0])return null;return val!==undefined?this.each(function(){this==window||this==document?window.scrollTo(!i?val:jQuery(window).scrollLeft(),i?val:jQuery(window).scrollTop()):this[method]=val;}):this[0]==window||this[0]==document?self[i?'pageYOffset':'pageXOffset']||jQuery.boxModel&&document.documentElement[method]||document.body[method]:this[0][method];};});jQuery.each(["Height","Width"],function(i,name){var tl=i?"Left":"Top",br=i?"Right":"Bottom",lower=name.toLowerCase();jQuery.fn["inner"+name]=function(){return this[0]?jQuery.css(this[0],lower,false,"padding"):null;};jQuery.fn["outer"+name]=function(margin){return this[0]?jQuery.css(this[0],lower,false,margin?"margin":"border"):null;};var type=name.toLowerCase();jQuery.fn[type]=function(size){return this[0]==window?document.compatMode=="CSS1Compat"&&document.documentElement["client"+name]||document.body["client"+name]:this[0]==document?Math.max(document.documentElement["client"+name],document.body["scroll"+name],document.documentElement["scroll"+name],document.body["offset"+name],document.documentElement["offset"+name]):size===undefined?(this.length?jQuery.css(this[0],type):null):this.css(type,typeof size==="string"?size:size+"px");};});})();$j=jQuery.noConflict();if(typeof mw=='undefined'||!mw){mw={};mw.ready=function(func){$j(document).ready(func);} -mw.load=function(deps,callback){callback();};mw.loadDone=function(className){};if(!gMsg)var gMsg={};mw.addMessages=function(msgSet){for(var i in msgSet){gMsg[i]=msgSet[i];}} -function gM(key,args){var ms='';if(key in gMsg){ms=gMsg[key];if(typeof args=='object'||typeof args=='array'){for(var v in args){var rep='\$'+(parseInt(v)+1);ms=ms.replace(rep,args[v]);}}else if(typeof args=='string'||typeof args=='number'){ms=ms.replace(/\$1/,args);} -return ms;}else{return'['+key+']';}}} \ No newline at end of file +offsetParent=offsetParent.offsetParent;return jQuery(offsetParent);}});jQuery.each(['Left','Top'],function(i,name){var method='scroll'+name;jQuery.fn[method]=function(val){if(!this[0])return null;return val!==undefined?this.each(function(){this==window||this==document?window.scrollTo(!i?val:jQuery(window).scrollLeft(),i?val:jQuery(window).scrollTop()):this[method]=val;}):this[0]==window||this[0]==document?self[i?'pageYOffset':'pageXOffset']||jQuery.boxModel&&document.documentElement[method]||document.body[method]:this[0][method];};});jQuery.each(["Height","Width"],function(i,name){var tl=i?"Left":"Top",br=i?"Right":"Bottom",lower=name.toLowerCase();jQuery.fn["inner"+name]=function(){return this[0]?jQuery.css(this[0],lower,false,"padding"):null;};jQuery.fn["outer"+name]=function(margin){return this[0]?jQuery.css(this[0],lower,false,margin?"margin":"border"):null;};var type=name.toLowerCase();jQuery.fn[type]=function(size){return this[0]==window?document.compatMode=="CSS1Compat"&&document.documentElement["client"+name]||document.body["client"+name]:this[0]==document?Math.max(document.documentElement["client"+name],document.body["scroll"+name],document.documentElement["scroll"+name],document.body["offset"+name],document.documentElement["offset"+name]):size===undefined?(this.length?jQuery.css(this[0],type):null):this.css(type,typeof size==="string"?size:size+"px");};});})();$j=jQuery.noConflict(); \ No newline at end of file diff --git a/skins/common/wikibits.js b/skins/common/wikibits.js index 960112c268..7cee7614d3 100644 --- a/skins/common/wikibits.js +++ b/skins/common/wikibits.js @@ -1028,3 +1028,8 @@ hookEvent( 'load', runOnloadHook ); if ( ie6_bugs ) { importScriptURI( stylepath + '/common/IEFixes.js' ); } + +// For future use. +mw = {}; + + -- 2.20.1