mw.widgets.CategorySelector: Avoid JS exceptions for invalid user input
authorBartosz Dziewoński <matma.rex@gmail.com>
Mon, 4 Jul 2016 18:42:54 +0000 (20:42 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Mon, 4 Jul 2016 18:52:27 +0000 (20:52 +0200)
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

resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js

index 45f52b7..ffb7736 100644 (file)
         * @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
                } );
        };
 
        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() );
        };
 
        /**