* @return {string} Url of the page with name of `str`
*/
getUrl: function ( str, params ) {
- var url = mw.config.get( 'wgArticlePath' ).replace(
- '$1',
- util.wikiUrlencode( typeof str === 'string' ? str : mw.config.get( 'wgPageName' ) )
- );
+ var titleFragmentStart,
+ url,
+ fragment = '',
+ pageName = util.wikiUrlencode( typeof str === 'string' ? str : mw.config.get( 'wgPageName' ) );
+
+ // Find any fragment should one exist
+ if ( typeof str === 'string' ) {
+ titleFragmentStart = pageName.indexOf( '%23' );
+ if ( titleFragmentStart !== -1 ) {
+ fragment = pageName.slice( titleFragmentStart + 3 );
+ // Exclude the fragment from the page name
+ pageName = pageName.slice( 0, titleFragmentStart );
+ }
+ }
+ url = mw.config.get( 'wgArticlePath' ).replace( '$1', pageName );
+
+ // Add query string if necessary
if ( params && !$.isEmptyObject( params ) ) {
url += ( url.indexOf( '?' ) !== -1 ? '&' : '?' ) + $.param( params );
}
+ // Insert the already URL-encoded fragment should it exist, replacing % with .
+ if ( fragment.length > 0 ) {
+ url += '#' + fragment.replace( /%/g, '.' );
+ }
+
return url;
},
} );
} );
- QUnit.test( 'getUrl', 5, function ( assert ) {
+ QUnit.test( 'getUrl', 8, function ( assert ) {
// Not part of startUp module
mw.config.set( 'wgArticlePath', '/wiki/$1' );
mw.config.set( 'wgPageName', 'Foobar' );
href = mw.util.getUrl( 'Sandbox', { action: 'edit' } );
assert.equal( href, '/wiki/Sandbox?action=edit', 'simple title with query string' );
+
+ // Test fragments
+ href = mw.util.getUrl( 'Foo:Sandbox#Fragment', { action: 'edit' } );
+ assert.equal( href, '/wiki/Foo:Sandbox?action=edit#Fragment', 'advanced title with query string and fragment' );
+
+ href = mw.util.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action: 'edit' } );
+ assert.equal( href, '/wiki/Foo:Sandbox_%C3%84?action=edit#Fragment_.C3.84', 'title with query string, fragment, and special characters' );
+
+ href = mw.util.getUrl( 'Foo:%23#Fragment', { action: 'edit' } );
+ assert.equal( href, '/wiki/Foo:%2523?action=edit#Fragment', 'title containing %23 (#), fragment, and a query string' );
} );
QUnit.test( 'wikiScript', 4, function ( assert ) {