jenkins-bot has submitted this change and it was merged.

Change subject: mw.Title: Add method to get title relative to an arbitrary 
namespace
......................................................................


mw.Title: Add method to get title relative to an arbitrary namespace

I'm not sure where this kind of thing could be used outside of
dealing with templates/transclusion. Making it generic anyway.

Bug: 67448
Change-Id: Ie554adefec43997d362b5d7b45c30403912743b5
(cherry picked from commit 63e6ebb8fac41e8d57aa7f9677d4419155b01fc1)
---
M resources/src/mediawiki/mediawiki.Title.js
M tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js
2 files changed, 58 insertions(+), 0 deletions(-)

Approvals:
  Legoktm: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/resources/src/mediawiki/mediawiki.Title.js 
b/resources/src/mediawiki/mediawiki.Title.js
index fc8e7e9..95b18a8 100644
--- a/resources/src/mediawiki/mediawiki.Title.js
+++ b/resources/src/mediawiki/mediawiki.Title.js
@@ -536,6 +536,28 @@
                },
 
                /**
+                * 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).
                 *
                 * Note that this method (by design) does not include the hash 
character and
diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js 
b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js
index 077ce70..5ece31b 100644
--- a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js
+++ b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js
@@ -436,4 +436,40 @@
                }
        } );
 
+       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 ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/162016
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie554adefec43997d362b5d7b45c30403912743b5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: REL1_24
Gerrit-Owner: Jforrester <[email protected]>
Gerrit-Reviewer: Alex Monk <[email protected]>
Gerrit-Reviewer: Daniel Friesen <[email protected]>
Gerrit-Reviewer: Jack Phoenix <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to