From 3ef6aa0728f66bc163b9522210d9e53105bf78bd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Mon, 4 Jul 2016 20:42:54 +0200 Subject: [PATCH] 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 --- .../mw.widgets.CategorySelector.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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() ); }; /** -- 2.20.1