diff --git a/resources/src/mediawiki.util/util.js b/resources/src/mediawiki.util/util.js index 020542a8514..4beee8f0fa5 100644 --- a/resources/src/mediawiki.util/util.js +++ b/resources/src/mediawiki.util/util.js @@ -158,7 +158,11 @@ util = { if ( params ) { query = $.param( params ); } - if ( query ) { + + if ( !title && fragment ) { + // If only a fragment was given, make a fragment-only link (T288415) + url = ''; + } else if ( query ) { url = title ? util.wikiScript() + '?title=' + util.wikiUrlencode( title ) + '&' + query : util.wikiScript() + '?' + query; @@ -168,7 +172,7 @@ util = { } // Append the encoded fragment - if ( fragment && fragment.length ) { + if ( fragment ) { url += '#' + util.escapeIdForLink( fragment ); } diff --git a/tests/phpunit/includes/TitleMethodsTest.php b/tests/phpunit/includes/TitleMethodsTest.php index 555c7cd1e76..a2fe586d387 100644 --- a/tests/phpunit/includes/TitleMethodsTest.php +++ b/tests/phpunit/includes/TitleMethodsTest.php @@ -379,6 +379,17 @@ class TitleMethodsTest extends MediaWikiLangTestCase { 'Goatificatiön' ]; + yield 'Fragment only (query is ignored)' => [ + '#Goatificatiön', + NS_MAIN, + '', + 'Goatificatiön', + '', + [ + 'a' => 1, + ] + ]; + yield 'Unknown interwiki with fragment' => [ 'https://xx.wiki.test/wiki/xyzzy:Goats#Goatificatiön', NS_MAIN, diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js index 00357ed1c85..e3c4314ed1b 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js @@ -210,10 +210,10 @@ assert.strictEqual( href, '/wiki/Sandbox', 'title with empty query string' ); href = util.getUrl( '#Fragment' ); - assert.strictEqual( href, '/wiki/#Fragment', 'empty title with fragment' ); + assert.strictEqual( href, '#Fragment', 'empty title with fragment' ); href = util.getUrl( '#Fragment', { action: 'edit' } ); - assert.strictEqual( href, '/w/index.php?action=edit#Fragment', 'empty title with query string and fragment' ); + assert.strictEqual( href, '#Fragment', 'empty title with query string and fragment' ); mw.util.setOptionsForTest( { FragmentMode: [ 'legacy' ] } ); href = util.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action: 'edit' } ); @@ -227,12 +227,12 @@ assert.strictEqual( href, '/w/index.php?title=Foo:%2523&action=edit#Fragment', 'title containing %23 (#), fragment, and a query string' ); mw.util.setOptionsForTest( { FragmentMode: [ 'legacy' ] } ); - href = util.getUrl( '#+&=:;@$-_.!*/[]<>\'§', { action: 'edit' } ); - assert.strictEqual( href, '/w/index.php?action=edit#.2B.26.3D:.3B.40.24-_..21.2A.2F.5B.5D.3C.3E.27.C2.A7', 'fragment with various characters' ); + href = util.getUrl( 'Sandbox#+&=:;@$-_.!*/[]<>\'§', { action: 'edit' } ); + assert.strictEqual( href, '/w/index.php?title=Sandbox&action=edit#.2B.26.3D:.3B.40.24-_..21.2A.2F.5B.5D.3C.3E.27.C2.A7', 'fragment with various characters' ); mw.util.setOptionsForTest( { FragmentMode: [ 'html5' ] } ); - href = util.getUrl( '#+&=:;@$-_.!*/[]<>\'§', { action: 'edit' } ); - assert.strictEqual( href, '/w/index.php?action=edit#+&=:;@$-_.!*/[]<>\'§', 'fragment with various characters' ); + href = util.getUrl( 'Sandbox#+&=:;@$-_.!*/[]<>\'§', { action: 'edit' } ); + assert.strictEqual( href, '/w/index.php?title=Sandbox&action=edit#+&=:;@$-_.!*/[]<>\'§', 'fragment with various characters' ); } ); QUnit.test( 'wikiScript', function ( assert ) {