diff --git a/modules/ve-mw/dm/annotations/ve.dm.MWInternalLinkAnnotation.js b/modules/ve-mw/dm/annotations/ve.dm.MWInternalLinkAnnotation.js index c85d8dfc40..9bdf8fa255 100644 --- a/modules/ve-mw/dm/annotations/ve.dm.MWInternalLinkAnnotation.js +++ b/modules/ve-mw/dm/annotations/ve.dm.MWInternalLinkAnnotation.js @@ -204,12 +204,12 @@ ve.dm.MWInternalLinkAnnotation.static.getLookupTitle = function ( original ) { * * @static * @param {string|mw.Title} original Original title - * @return {string} Fragment for the title, or an empty string if it was invalid + * @return {string|null} Fragment for the title, or null if it was invalid or missing */ ve.dm.MWInternalLinkAnnotation.static.getFragment = function ( original ) { var title = original instanceof mw.Title ? original : mw.Title.newFromText( original ); if ( !title ) { - return ''; + return null; } return title.getFragment(); }; diff --git a/modules/ve-mw/tests/dm/ve.dm.MWInternalLinkAnnotation.test.js b/modules/ve-mw/tests/dm/ve.dm.MWInternalLinkAnnotation.test.js index fc7e7b8efa..cc7db561fa 100644 --- a/modules/ve-mw/tests/dm/ve.dm.MWInternalLinkAnnotation.test.js +++ b/modules/ve-mw/tests/dm/ve.dm.MWInternalLinkAnnotation.test.js @@ -61,3 +61,50 @@ QUnit.test( 'toDataElement', function ( assert ) { assert.deepEqual( ve.dm.MWInternalLinkAnnotation.static.toDataElement( [ cases[ i ].element ], converter ), cases[ i ].expected, cases[ i ].msg ); } } ); + +QUnit.test( 'getFragment', function ( assert ) { + var i, l, + cases = [ + { + msg: 'No fragment returns null', + original: 'Foo', + expected: null + }, + { + msg: 'Invalid title returns null', + original: 'A%20B', + expected: null + }, + { + msg: 'Blank fragment returns empty string', + original: 'Foo#', + expected: '' + }, + { + msg: 'Extant fragment returns same string', + original: 'Foo#bar', + expected: 'bar' + }, + { + msg: 'Hash-bang works returns full string', + original: 'Foo#!bar', + expected: '!bar' + }, + { + msg: 'Double-hash returns everything after the first hash', + original: 'Foo##bar', + expected: '#bar' + }, + { + msg: 'Multi-fragment returns everything after the first hash', + original: 'Foo#bar#baz#bat', + expected: 'bar#baz#bat' + } + ]; + + QUnit.expect( cases.length ); + + for ( i = 0, l = cases.length; i < l; i++ ) { + assert.deepEqual( ve.dm.MWInternalLinkAnnotation.static.getFragment( cases[ i ].original ), cases[ i ].expected, cases[ i ].msg ); + } +} );