http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88276

Revision: 88276
Author:   krinkle
Date:     2011-05-16 22:51:29 +0000 (Mon, 16 May 2011)
Log Message:
-----------
mw.util.getActionFrom incorrectly returns null
* Return 'view' for urls like:
 - /w/index.php?title=Foobar
* Anything else, fallback to 'view' (just like PHP does)
* JSHint
 - ['view'] is better written in dot notation.
 - 'action' was already defined on line 235.
 - 'actionRe' is defined multiple times (loop).
 - Use '===' to compare with '0'.
 - 'title' is already defined.
 >> The code check passed 100%!
* Adding QUnit test for getActionFrom and getTitleFrom


(Follow-up r87964)

Modified Paths:
--------------
    trunk/phase3/resources/mediawiki.util/mediawiki.util.js
    trunk/phase3/resources/test/index.html
    trunk/phase3/resources/test/unit/mediawiki.util/mediawiki.util.js

Modified: trunk/phase3/resources/mediawiki.util/mediawiki.util.js
===================================================================
--- trunk/phase3/resources/mediawiki.util/mediawiki.util.js     2011-05-16 
22:34:46 UTC (rev 88275)
+++ trunk/phase3/resources/mediawiki.util/mediawiki.util.js     2011-05-16 
22:51:29 UTC (rev 88276)
@@ -226,55 +226,58 @@
                },
 
                /** 
-                * Try to find the wiki action for a given URL with actions 
paths support
+                * Try to find the wiki action for a given URL with actions 
paths support.
+                * Defaults to 'view'.
                 *
                 * @param url URL to search for a wiki action
+                * @return Action
                 */
                'getActionFrom' : function( url ) {
                        // attempt to get the action from the parameter 
[&?]action=
                        var action = mw.util.getParamValue( 'action', url );
-                       if( action !== null ) {
+                       if ( action !== null ) {
                                return action;
                        }
 
                        // now from the action paths
                        var actionPaths = mw.config.get( 'wgActionPaths' );
-                       if( actionPaths.length == 0 ) {
-                               actionPaths['view'] = mw.config.get( 
'wgArticlePath' );
+                       if ( actionPaths.length === 0 ) {
+                               actionPaths.view = mw.config.get( 
'wgArticlePath' );
                        }
-                       var action = '';
+                       var actionRe;
                        for ( action in actionPaths ) {
-                               var actionRe = new RegExp( 
actionPaths[action].replace( '$1', '.*?' ) );
-                               if( url.match( actionRe ) ) {
+                               actionRe = new RegExp( 
actionPaths[action].replace( '$1', '.*?' ) );
+                               if ( url.match( actionRe ) ) {
                                        return action;
                                }
                        }
 
-                       return null;
+                       return 'view';
                },
 
                /**
                 * Try to find the wiki title for a given URL with actions 
paths support
                 *
                 * @param url URL to search for a title
+                * @return Title or null.
                 */
                'getTitleFrom' : function( url ) {
                        // attempt to get the title from the parameter 
[&?]title=
                        var title = mw.util.getParamValue( 'title', url );
-                       if( title !== null ) {
+                       if ( title !== null ) {
                                return title;
                        }
 
                        // now from the action paths    
                        var actionPaths = mw.config.get( 'wgActionPaths' );
-                       if( actionPaths.length == 0 ) {
-                               actionPaths['view'] = mw.config.get( 
'wgArticlePath' );
+                       if ( actionPaths.length === 0 ) {
+                               actionPaths.view = mw.config.get( 
'wgArticlePath' );
                        }
-                       var action = '';
+                       var action, actionRe;
                        for ( action in actionPaths ) {
-                               var actionRe = new RegExp( '.*' + 
actionPaths[action].replace( '$1', '([^&?#]+)' ) );
-                               var title = url.match( actionRe );
-                               if( title !== null ) {
+                               actionRe = new RegExp( '.*' + 
actionPaths[action].replace( '$1', '([^&?#]+)' ) );
+                               title = url.match( actionRe );
+                               if ( title !== null ) {
                                        return title[1];
                                }
                        }

Modified: trunk/phase3/resources/test/index.html
===================================================================
--- trunk/phase3/resources/test/index.html      2011-05-16 22:34:46 UTC (rev 
88275)
+++ trunk/phase3/resources/test/index.html      2011-05-16 22:51:29 UTC (rev 
88276)
@@ -29,7 +29,7 @@
 
        <meta name="ResourceLoaderDynamicStyles" content="" /> 
 
-       <!-- QUnit dependancies and scripts -->
+       <!-- QUnit -->
        <link rel="stylesheet" href="../jquery/jquery.qunit.css" />
        <script src="../jquery/jquery.qunit.js"></script>
 

Modified: trunk/phase3/resources/test/unit/mediawiki.util/mediawiki.util.js
===================================================================
--- trunk/phase3/resources/test/unit/mediawiki.util/mediawiki.util.js   
2011-05-16 22:34:46 UTC (rev 88275)
+++ trunk/phase3/resources/test/unit/mediawiki.util/mediawiki.util.js   
2011-05-16 22:51:29 UTC (rev 88276)
@@ -53,11 +53,82 @@
 
 test( 'getParamValue', function(){
 
-       equals( mw.util.getParamValue( 'foo', 
'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad' ), 'right', 'Use latest 
one, ignore hash' );
-       same( mw.util.getParamValue( 'bar', 'http://mediawiki.org/?foo=right' 
), null, 'Return null when not found' );
+       var url = 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad';
 
+       equal( mw.util.getParamValue( 'foo', url ), 'right', 'Use latest one, 
ignore hash' );
+       deepEqual( mw.util.getParamValue( 'bar', url ), null, 'Return null when 
not found' );
+
 });
 
+test( 'getActionFrom', function(){
+
+       // Example urls
+       var     urlA = 'http://mediawiki.org/wiki/Article',
+               urlB = 
'http://mediawiki.org/w/index.php?title=Article&action=edit',
+               urlC = 'http://mediawiki.org/edit/Article',
+               urlD = 'http://mediawiki.org/w/index.php/Article';
+
+       // Common settings
+       mw.config.set( {
+               'wgActionPaths': [],
+               'wgArticlePath': '/wiki/$1'
+       });
+
+       equal( mw.util.getActionFrom( urlA ), 'view', 'wgArticlePath (/wiki/$1) 
support' );
+       equal( mw.util.getActionFrom( urlB ), 'edit', 'action-parameter 
support' );
+
+       // Custom settings
+       mw.config.set( 'wgActionPaths', {
+               'view': '/view/$1',
+               'edit': '/edit/$1'
+       });
+
+       equal( mw.util.getActionFrom( urlC ), 'edit', 'wgActionPaths support' );
+
+       // Default settings
+       mw.config.set( {
+               'wgActionPaths': [],
+               'wgArticlePath': '/w/index.php/$1' 
+       });
+       equal( mw.util.getActionFrom( urlD ), 'view', 'wgArticlePath 
(/index.php/$1) support' );
+
+});
+
+test( 'getTitleFrom', function(){
+
+       // Example urls
+       var     urlA = 'http://mediawiki.org/wiki/Article',
+               urlB = 
'http://mediawiki.org/w/index.php?title=Article&action=edit',
+               urlC = 'http://mediawiki.org/edit/Article',
+               urlD = 'http://mediawiki.org/w/index.php/Article';
+
+       // Common settings
+       mw.config.set( {
+               'wgActionPaths': [],
+               'wgArticlePath': '/wiki/$1'
+       });
+
+       equal( mw.util.getTitleFrom( urlA ), 'Article', 'wgArticlePath 
(/wiki/$1) support' );
+       equal( mw.util.getTitleFrom( urlB ), 'Article', 'action-parameter 
support' );
+
+       // Custom settings
+       mw.config.set( 'wgActionPaths', {
+               'view': '/view/$1',
+               'edit': '/edit/$1'
+       });
+
+       equal( mw.util.getTitleFrom( urlC ), 'Article', 'wgActionPaths support' 
);
+
+       // Default settings
+       mw.config.set( {
+               'wgActionPaths': [],
+               'wgArticlePath': '/w/index.php/$1' 
+       });
+
+       equal( mw.util.getTitleFrom( urlD ), 'Article', 'wgArticlePath 
(/index.php/$1) support' );
+
+});
+
 test( 'tooltipAccessKey', function(){
 
        equals( typeof mw.util.tooltipAccessKeyPrefix, 'string', 
'mw.util.tooltipAccessKeyPrefix must be a string' );


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to