Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.js |
— | — | @@ -226,55 +226,58 @@ |
227 | 227 | }, |
228 | 228 | |
229 | 229 | /** |
230 | | - * Try to find the wiki action for a given URL with actions paths support |
| 230 | + * Try to find the wiki action for a given URL with actions paths support. |
| 231 | + * Defaults to 'view'. |
231 | 232 | * |
232 | 233 | * @param url URL to search for a wiki action |
| 234 | + * @return Action |
233 | 235 | */ |
234 | 236 | 'getActionFrom' : function( url ) { |
235 | 237 | // attempt to get the action from the parameter [&?]action= |
236 | 238 | var action = mw.util.getParamValue( 'action', url ); |
237 | | - if( action !== null ) { |
| 239 | + if ( action !== null ) { |
238 | 240 | return action; |
239 | 241 | } |
240 | 242 | |
241 | 243 | // now from the action paths |
242 | 244 | var actionPaths = mw.config.get( 'wgActionPaths' ); |
243 | | - if( actionPaths.length == 0 ) { |
244 | | - actionPaths['view'] = mw.config.get( 'wgArticlePath' ); |
| 245 | + if ( actionPaths.length === 0 ) { |
| 246 | + actionPaths.view = mw.config.get( 'wgArticlePath' ); |
245 | 247 | } |
246 | | - var action = ''; |
| 248 | + var actionRe; |
247 | 249 | for ( action in actionPaths ) { |
248 | | - var actionRe = new RegExp( actionPaths[action].replace( '$1', '.*?' ) ); |
249 | | - if( url.match( actionRe ) ) { |
| 250 | + actionRe = new RegExp( actionPaths[action].replace( '$1', '.*?' ) ); |
| 251 | + if ( url.match( actionRe ) ) { |
250 | 252 | return action; |
251 | 253 | } |
252 | 254 | } |
253 | 255 | |
254 | | - return null; |
| 256 | + return 'view'; |
255 | 257 | }, |
256 | 258 | |
257 | 259 | /** |
258 | 260 | * Try to find the wiki title for a given URL with actions paths support |
259 | 261 | * |
260 | 262 | * @param url URL to search for a title |
| 263 | + * @return Title or null. |
261 | 264 | */ |
262 | 265 | 'getTitleFrom' : function( url ) { |
263 | 266 | // attempt to get the title from the parameter [&?]title= |
264 | 267 | var title = mw.util.getParamValue( 'title', url ); |
265 | | - if( title !== null ) { |
| 268 | + if ( title !== null ) { |
266 | 269 | return title; |
267 | 270 | } |
268 | 271 | |
269 | 272 | // now from the action paths |
270 | 273 | var actionPaths = mw.config.get( 'wgActionPaths' ); |
271 | | - if( actionPaths.length == 0 ) { |
272 | | - actionPaths['view'] = mw.config.get( 'wgArticlePath' ); |
| 274 | + if ( actionPaths.length === 0 ) { |
| 275 | + actionPaths.view = mw.config.get( 'wgArticlePath' ); |
273 | 276 | } |
274 | | - var action = ''; |
| 277 | + var action, actionRe; |
275 | 278 | for ( action in actionPaths ) { |
276 | | - var actionRe = new RegExp( '.*' + actionPaths[action].replace( '$1', '([^&?#]+)' ) ); |
277 | | - var title = url.match( actionRe ); |
278 | | - if( title !== null ) { |
| 279 | + actionRe = new RegExp( '.*' + actionPaths[action].replace( '$1', '([^&?#]+)' ) ); |
| 280 | + title = url.match( actionRe ); |
| 281 | + if ( title !== null ) { |
279 | 282 | return title[1]; |
280 | 283 | } |
281 | 284 | } |
Index: trunk/phase3/resources/test/unit/mediawiki.util/mediawiki.util.js |
— | — | @@ -53,11 +53,82 @@ |
54 | 54 | |
55 | 55 | test( 'getParamValue', function(){ |
56 | 56 | |
57 | | - equals( mw.util.getParamValue( 'foo', 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad' ), 'right', 'Use latest one, ignore hash' ); |
58 | | - same( mw.util.getParamValue( 'bar', 'http://mediawiki.org/?foo=right' ), null, 'Return null when not found' ); |
| 57 | + var url = 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad'; |
59 | 58 | |
| 59 | + equal( mw.util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' ); |
| 60 | + deepEqual( mw.util.getParamValue( 'bar', url ), null, 'Return null when not found' ); |
| 61 | + |
60 | 62 | }); |
61 | 63 | |
| 64 | +test( 'getActionFrom', function(){ |
| 65 | + |
| 66 | + // Example urls |
| 67 | + var urlA = 'http://mediawiki.org/wiki/Article', |
| 68 | + urlB = 'http://mediawiki.org/w/index.php?title=Article&action=edit', |
| 69 | + urlC = 'http://mediawiki.org/edit/Article', |
| 70 | + urlD = 'http://mediawiki.org/w/index.php/Article'; |
| 71 | + |
| 72 | + // Common settings |
| 73 | + mw.config.set( { |
| 74 | + 'wgActionPaths': [], |
| 75 | + 'wgArticlePath': '/wiki/$1' |
| 76 | + }); |
| 77 | + |
| 78 | + equal( mw.util.getActionFrom( urlA ), 'view', 'wgArticlePath (/wiki/$1) support' ); |
| 79 | + equal( mw.util.getActionFrom( urlB ), 'edit', 'action-parameter support' ); |
| 80 | + |
| 81 | + // Custom settings |
| 82 | + mw.config.set( 'wgActionPaths', { |
| 83 | + 'view': '/view/$1', |
| 84 | + 'edit': '/edit/$1' |
| 85 | + }); |
| 86 | + |
| 87 | + equal( mw.util.getActionFrom( urlC ), 'edit', 'wgActionPaths support' ); |
| 88 | + |
| 89 | + // Default settings |
| 90 | + mw.config.set( { |
| 91 | + 'wgActionPaths': [], |
| 92 | + 'wgArticlePath': '/w/index.php/$1' |
| 93 | + }); |
| 94 | + equal( mw.util.getActionFrom( urlD ), 'view', 'wgArticlePath (/index.php/$1) support' ); |
| 95 | + |
| 96 | +}); |
| 97 | + |
| 98 | +test( 'getTitleFrom', function(){ |
| 99 | + |
| 100 | + // Example urls |
| 101 | + var urlA = 'http://mediawiki.org/wiki/Article', |
| 102 | + urlB = 'http://mediawiki.org/w/index.php?title=Article&action=edit', |
| 103 | + urlC = 'http://mediawiki.org/edit/Article', |
| 104 | + urlD = 'http://mediawiki.org/w/index.php/Article'; |
| 105 | + |
| 106 | + // Common settings |
| 107 | + mw.config.set( { |
| 108 | + 'wgActionPaths': [], |
| 109 | + 'wgArticlePath': '/wiki/$1' |
| 110 | + }); |
| 111 | + |
| 112 | + equal( mw.util.getTitleFrom( urlA ), 'Article', 'wgArticlePath (/wiki/$1) support' ); |
| 113 | + equal( mw.util.getTitleFrom( urlB ), 'Article', 'action-parameter support' ); |
| 114 | + |
| 115 | + // Custom settings |
| 116 | + mw.config.set( 'wgActionPaths', { |
| 117 | + 'view': '/view/$1', |
| 118 | + 'edit': '/edit/$1' |
| 119 | + }); |
| 120 | + |
| 121 | + equal( mw.util.getTitleFrom( urlC ), 'Article', 'wgActionPaths support' ); |
| 122 | + |
| 123 | + // Default settings |
| 124 | + mw.config.set( { |
| 125 | + 'wgActionPaths': [], |
| 126 | + 'wgArticlePath': '/w/index.php/$1' |
| 127 | + }); |
| 128 | + |
| 129 | + equal( mw.util.getTitleFrom( urlD ), 'Article', 'wgArticlePath (/index.php/$1) support' ); |
| 130 | + |
| 131 | +}); |
| 132 | + |
62 | 133 | test( 'tooltipAccessKey', function(){ |
63 | 134 | |
64 | 135 | equals( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' ); |
Index: trunk/phase3/resources/test/index.html |
— | — | @@ -29,7 +29,7 @@ |
30 | 30 | |
31 | 31 | <meta name="ResourceLoaderDynamicStyles" content="" /> |
32 | 32 | |
33 | | - <!-- QUnit dependancies and scripts --> |
| 33 | + <!-- QUnit --> |
34 | 34 | <link rel="stylesheet" href="../jquery/jquery.qunit.css" /> |
35 | 35 | <script src="../jquery/jquery.qunit.js"></script> |
36 | 36 | |