From: Bartosz DziewoƄski Date: Mon, 4 Jul 2016 18:42:54 +0000 (+0200) Subject: mw.widgets.CategorySelector: Avoid JS exceptions for invalid user input X-Git-Tag: 1.31.0-rc.0~6381^2 X-Git-Url: http://git.cyclocoop.org/%22.%24image2.%22?a=commitdiff_plain;h=3ef6aa0728f66bc163b9522210d9e53105bf78bd;p=lhc%2Fweb%2Fwiklou.git mw.widgets.CategorySelector: Avoid JS exceptions for invalid user input Previously, if the user entered a category name that was not a valid title, we'd pass `null` to CategoryCapsuleItemWidget constructor, which would then fail with 'TypeError: Cannot read property 'getMainText' of null'. Depends on Ifb016e1d2c73ae701cbce4505583e48769fd4ed6 in OOjs UI. Bug: T136238 Change-Id: I7446d85bce37a4bd9398e2d258aa2f6dc0e593a8 --- diff --git a/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js b/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js index 45f52b70e2..ffb7736470 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js +++ b/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js @@ -186,9 +186,13 @@ * @inheritdoc */ CSP.createItemWidget = function ( data ) { + var title = mw.Title.makeTitle( NS_CATEGORY, data ); + if ( !title ) { + return null; + } return new mw.widgets.CategoryCapsuleItemWidget( { apiUrl: this.api.apiUrl || undefined, - title: mw.Title.makeTitle( NS_CATEGORY, data ) + title: title } ); }; @@ -198,8 +202,11 @@ 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.makeTitle( NS_CATEGORY, data ).getMainText(); - return OO.ui.mixin.GroupElement.prototype.getItemFromData.call( this, data ); + var title = mw.Title.makeTitle( NS_CATEGORY, data ); + if ( !title ) { + return null; + } + return OO.ui.mixin.GroupElement.prototype.getItemFromData.call( this, title.getMainText() ); }; /**