mediawiki-extensions-Cite/modules/ve-cite/ve.ui.MWUseExistingReferenceCommand.js
thiemowmde 341847a410 Fix inconsistent usage of lastIndexOf in VE reference code
I assume the code was using lastIndexOf as some kind of performance
optimization. Certain StackOverflow threads suggest it without going
into detail. It's not correct here. You can actually name a group
"mwReference/", which will result in the (valid) internal name
"mwReference/mwReference/". This works as expected with indexOf but
not with lastIndexOf.

Change-Id: I8e85ae5c11a74016c7720fcdb6ac6478431aaa8e
2023-06-06 13:37:25 +02:00

51 lines
1.3 KiB
JavaScript

/*!
* VisualEditor UserInterface MediaWiki UseExistingReferenceCommand class.
*
* @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
* @license MIT
*/
/**
* Use existing reference command.
*
* @class
* @extends ve.ui.Command
*
* @constructor
*/
ve.ui.MWUseExistingReferenceCommand = function VeUiMWUseExistingReferenceCommand() {
// Parent constructor
ve.ui.MWUseExistingReferenceCommand.super.call(
this, 'reference/existing', 'window', 'open',
{ args: [ 'reference', { useExisting: true } ], supportedSelections: [ 'linear' ] }
);
};
/* Inheritance */
OO.inheritClass( ve.ui.MWUseExistingReferenceCommand, ve.ui.Command );
/* Methods */
/**
* @inheritdoc
*/
ve.ui.MWUseExistingReferenceCommand.prototype.isExecutable = function ( fragment ) {
// Parent method
if ( !ve.ui.MWUseExistingReferenceCommand.super.prototype.isExecutable.apply( this, arguments ) ) {
return false;
}
var groups = fragment.getDocument().getInternalList().getNodeGroups();
for ( var groupName in groups ) {
if ( groupName.indexOf( 'mwReference/' ) === 0 && groups[ groupName ].indexOrder.length ) {
return true;
}
}
return false;
};
/* Registration */
ve.ui.commandRegistry.register( new ve.ui.MWUseExistingReferenceCommand() );