RCFilters: Only attempt to remove tag if we can find an item for it
authorKosta Harlan <kharlan@wikimedia.org>
Tue, 26 Jun 2018 20:24:11 +0000 (16:24 -0400)
committerRoan Kattouw <roan.kattouw@gmail.com>
Wed, 27 Jun 2018 00:10:37 +0000 (17:10 -0700)
As noted in T198140 and T198231, removeTagByData leads us eventually to
OO.ui.mixin.GroupElement.prototype.removeItems(), which does not check if
any of the items are null values, and a change event is emitted.

This commit ensures that we can find an item for the data before attempting
to remove the tag.

Bug: T198140
Change-Id: I79a923a7b4e5f6c4d14fcce3c5855b4c56796384

resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.FilterTagMultiselectWidget.js

index d59fdfb..907c535 100644 (file)
                        ) {
                                this.addTag( item.getName(), item.getLabel() );
                        } else {
-                               this.removeTagByData( item.getName() );
+                               // Only attempt to remove the tag if we can find an item for it (T198140, T198231)
+                               if ( this.findItemFromData( item.getName() ) !== null ) {
+                                       this.removeTagByData( item.getName() );
+                               }
                        }
                }
 
                        // Remove capsule widgets if they're not selected
                        highlightedItems.forEach( function ( filterItem ) {
                                if ( !filterItem.isSelected() ) {
-                                       this.removeTagByData( filterItem.getName() );
+                                       // Only attempt to remove the tag if we can find an item for it (T198140, T198231)
+                                       if ( this.findItemFromData( filterItem.getName() ) !== null ) {
+                                               this.removeTagByData( filterItem.getName() );
+                                       }
                                }
                        }.bind( this ) );
                }