return text( this.getPrefixedDb() );
},
+ /**
+ * Get the page name relative to a namespace
+ *
+ * Example:
+ *
+ * - "Foo:Bar" relative to the Foo namespace becomes "Bar".
+ * - "Bar" relative to any non-main namespace becomes ":Bar".
+ * - "Foo:Bar" relative to any namespace other than Foo stays "Foo:Bar".
+ *
+ * @param {number} namespace The namespace to be relative to
+ * @return {string}
+ */
+ getRelativeText: function ( namespace ) {
+ if ( this.getNamespaceId() === namespace ) {
+ return this.getMainText();
+ } else if ( this.getNamespaceId() === NS_MAIN ) {
+ return ':' + this.getPrefixedText();
+ } else {
+ return this.getPrefixedText();
+ }
+ },
+
/**
* Get the fragment (if any).
*
}
} );
+ QUnit.test( 'getRelativeText', 5, function ( assert ) {
+ var cases = [
+ {
+ text: 'asd',
+ relativeTo: 123,
+ expectedResult: ':Asd'
+ },
+ {
+ text: 'dfg',
+ relativeTo: 0,
+ expectedResult: 'Dfg'
+ },
+ {
+ text: 'Template:Ghj',
+ relativeTo: 0,
+ expectedResult: 'Template:Ghj'
+ },
+ {
+ text: 'Template:1',
+ relativeTo: 10,
+ expectedResult: '1'
+ },
+ {
+ text: 'User:Hi',
+ relativeTo: 10,
+ expectedResult: 'User:Hi'
+ }
+ ], i, thisCase, title;
+
+ for ( i = 0; i < cases.length; i++ ) {
+ thisCase = cases[i];
+
+ title = mw.Title.newFromText( thisCase.text );
+ assert.equal( title.getRelativeText( thisCase.relativeTo ), thisCase.expectedResult );
+ }
+ } );
}( mediaWiki, jQuery ) );