jenkins-bot has submitted this change and it was merged. Change subject: Make citation tools' compatibility check use normalized titles ......................................................................
Make citation tools' compatibility check use normalized titles Greatly enhance the functionality of ve.dm.MWTransclusionNode#isSingleTemplate and actually use it places. Use mw.Title to normalize titles, accounting for case differences and spaces vs underscores. Also allow an array of template names to be specified. Use isSingleTemplate() in the transclusion and citation dialog tools, which were duplicating this logic. Also document the .static.template properties. Without this, the citation tool will appear for a reference using {{Cite news}}, but not for one using {{cite news}} or {{Cite_news}}. Change-Id: I18d2bb1b22a5ab269694ad0818b1bb326ef8d1fd (cherry picked from commit a15fa5a17cb9c44fca25dfda5a84783c159215d0) --- M modules/ve-mw/dm/nodes/ve.dm.MWTransclusionNode.js M modules/ve-mw/ui/tools/ve.ui.MWCitationDialogTool.js M modules/ve-mw/ui/tools/ve.ui.MWTransclusionDialogTool.js 3 files changed, 48 insertions(+), 17 deletions(-) Approvals: Manybubbles: Looks good to me, approved jenkins-bot: Verified diff --git a/modules/ve-mw/dm/nodes/ve.dm.MWTransclusionNode.js b/modules/ve-mw/dm/nodes/ve.dm.MWTransclusionNode.js index 746113a..7e52d0b 100644 --- a/modules/ve-mw/dm/nodes/ve.dm.MWTransclusionNode.js +++ b/modules/ve-mw/dm/nodes/ve.dm.MWTransclusionNode.js @@ -5,6 +5,8 @@ * @license The MIT License (MIT); see LICENSE.txt */ +/*global mw*/ + /** * DataModel MediaWiki transclusion node. * @@ -230,13 +232,31 @@ /** * Check if transclusion contains only a single template. * - * @param {string} [template] Name of single template, omit to allow any template name - * @return {boolean} Transclusion only contains a single template + * @param {string|string[]} [templates] Names of templates to allow, omit to allow any template name + * @return {boolean} Transclusion only contains a single template, which is one of the ones in templates */ -ve.dm.MWTransclusionNode.prototype.isSingleTemplate = function ( template ) { - var partsList = this.getPartsList(); - return partsList.length === 1 && - ( template === undefined || partsList[0].template === template ); +ve.dm.MWTransclusionNode.prototype.isSingleTemplate = function ( templates ) { + function normalizeTitle( name ) { + var title = mw.Title.newFromText( name ); + return title ? title.getPrefixedText() : name; + } + + var i, len, partsList = this.getPartsList(); + if ( partsList.length !== 1 ) { + return false; + } + if ( templates === undefined ) { + return true; + } + if ( typeof templates === 'string' ) { + templates = [ templates ]; + } + for ( i = 0, len = templates.length; i < len; i++ ) { + if ( normalizeTitle( partsList[0].template ) === normalizeTitle( templates[i] ) ) { + return true; + } + } + return false; }; /** diff --git a/modules/ve-mw/ui/tools/ve.ui.MWCitationDialogTool.js b/modules/ve-mw/ui/tools/ve.ui.MWCitationDialogTool.js index f0ae1e4..03cd6af 100644 --- a/modules/ve-mw/ui/tools/ve.ui.MWCitationDialogTool.js +++ b/modules/ve-mw/ui/tools/ve.ui.MWCitationDialogTool.js @@ -28,11 +28,18 @@ ve.ui.MWCitationDialogTool.static.group = 'cite'; -ve.ui.MWCitationDialogTool.static.template = null; - ve.ui.MWCitationDialogTool.static.modelClasses = [ ve.dm.MWReferenceNode ]; ve.ui.MWCitationDialogTool.static.requiresRange = true; + +/** + * Only display tool for single-template transclusions of these templates. + * + * @property {string|string[]|null} + * @static + * @inheritable + */ +ve.ui.MWCitationDialogTool.static.template = null; /* Methods */ @@ -40,7 +47,7 @@ * @inheritdoc */ ve.ui.MWCitationDialogTool.static.isCompatibleWith = function ( model ) { - var internalItem, branches, leaves, partsList, + var internalItem, branches, leaves, compatible = ve.ui.MWCitationDialogTool.super.static.isCompatibleWith.call( this, model ); if ( compatible && this.template ) { @@ -51,9 +58,7 @@ if ( branches.length === 1 && branches[0].canContainContent() ) { leaves = branches[0].getChildren(); if ( leaves.length === 1 && leaves[0] instanceof ve.dm.MWTransclusionNode ) { - partsList = leaves[0].getPartsList(); - return partsList.length === 1 && - partsList[0].template === this.template; + return leaves[0].isSingleTemplate( this.template ); } } return false; diff --git a/modules/ve-mw/ui/tools/ve.ui.MWTransclusionDialogTool.js b/modules/ve-mw/ui/tools/ve.ui.MWTransclusionDialogTool.js index d7bea63..0eb7d15 100644 --- a/modules/ve-mw/ui/tools/ve.ui.MWTransclusionDialogTool.js +++ b/modules/ve-mw/ui/tools/ve.ui.MWTransclusionDialogTool.js @@ -33,13 +33,20 @@ ve.ui.MWTransclusionDialogTool.static.title = OO.ui.deferMsg( 'visualeditor-dialogbutton-template-tooltip' ); -ve.ui.MWTransclusionDialogTool.static.template = null; - ve.ui.MWTransclusionDialogTool.static.modelClasses = [ ve.dm.MWTransclusionNode ]; ve.ui.MWTransclusionDialogTool.static.requiresRange = true; ve.ui.MWTransclusionDialogTool.static.commandName = 'transclusion'; + +/** + * Only display tool for single-template transclusions of these templates. + * + * @property {string|string[]|null} + * @static + * @inheritable + */ +ve.ui.MWTransclusionDialogTool.static.template = null; /* Methods */ @@ -47,14 +54,13 @@ * @inheritdoc */ ve.ui.MWTransclusionDialogTool.static.isCompatibleWith = function ( model ) { - var partsList, compatible; + var compatible; // Parent method compatible = ve.ui.DialogTool.static.isCompatibleWith.call( this, model ); if ( compatible && this.template ) { - partsList = model.getPartsList(); - return partsList.length === 1 && partsList[0].template === this.template; + return model.isSingleTemplate( this.template ); } return compatible; -- To view, visit https://gerrit.wikimedia.org/r/132773 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I18d2bb1b22a5ab269694ad0818b1bb326ef8d1fd Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/VisualEditor Gerrit-Branch: wmf/1.24wmf3 Gerrit-Owner: Jforrester <jforres...@wikimedia.org> Gerrit-Reviewer: Catrope <roan.katt...@gmail.com> Gerrit-Reviewer: Manybubbles <never...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits