Support fragments in mw.util.getUrl()
authorAndrew H <crazy4sb@gmail.com>
Sat, 19 Dec 2015 22:47:31 +0000 (22:47 +0000)
committerAndrew H <crazy4sb@gmail.com>
Sun, 20 Dec 2015 19:37:26 +0000 (19:37 +0000)
Bug: T103553
Change-Id: I25c5ce2fde468202ce7ba5aa1c8cf0e3576c6057

resources/src/mediawiki/mediawiki.util.js
tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js

index 4cec813..7b45248 100644 (file)
                 * @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;
                },
 
index d40c00a..75dd8cd 100644 (file)
                } );
        } );
 
-       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 ) {