From 1ded5ff89cd72e3065f21ce2204e9abc4c7eb6a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Tue, 22 Dec 2015 19:01:12 +0100 Subject: [PATCH] mw.widgets.CategorySelector: Don't lose namespace-like prefixes from category names Also, handle queries which return non-category results. Bug: T122221 Change-Id: I205d2fe52a10bd3adcf0235108005dd96811ae65 --- .../mw.widgets.CategorySelector.js | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js b/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js index 1e65da5e50..ec38e2be41 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js +++ b/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js @@ -150,22 +150,30 @@ this.pushPending(); $.when.apply( $, promises ).done( function () { - var categories, categoryNames, + var categoryNames, allData = [], dataSets = Array.prototype.slice.apply( arguments ); // Collect values from all results allData = allData.concat.apply( allData, dataSets ); - // Remove duplicates - categories = allData.filter( function ( value, index, self ) { - return self.indexOf( value ) === index; - } ); - - // Get titles - categoryNames = categories.map( function ( name ) { - return mw.Title.newFromText( name, NS_CATEGORY ).getMainText(); - } ); + categoryNames = allData + // Remove duplicates + .filter( function ( value, index, self ) { + return self.indexOf( value ) === index; + } ) + // Get Title objects + .map( function ( name ) { + return mw.Title.newFromText( name ); + } ) + // Keep only titles from 'Category' namespace + .filter( function ( title ) { + return title && title.getNamespaceId() === NS_CATEGORY; + } ) + // Convert back to strings, strip 'Category:' prefix + .map( function ( title ) { + return title.getMainText(); + } ); deferred.resolve( categoryNames ); @@ -180,7 +188,7 @@ CSP.createItemWidget = function ( data ) { return new mw.widgets.CategoryCapsuleItemWidget( { apiUrl: this.api.apiUrl || undefined, - title: mw.Title.newFromText( data, NS_CATEGORY ) + title: mw.Title.makeTitle( NS_CATEGORY, data ) } ); }; @@ -190,7 +198,7 @@ CSP.getItemFromData = function ( data ) { // This is a bit of a hack... We have to canonicalize the data in the same way that // #createItemWidget and CategoryCapsuleItemWidget will do, otherwise we won't find duplicates. - data = mw.Title.newFromText( data, NS_CATEGORY ).getMainText(); + data = mw.Title.makeTitle( NS_CATEGORY, data ).getMainText(); return OO.ui.mixin.GroupElement.prototype.getItemFromData.call( this, data ); }; -- 2.20.1