mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-12-03 18:36:20 +00:00
55aec8f977
The ...target.wt property contains the wikitext used to generate the template name. It can contain trailing newlines (T234817) and all kinds of funny wikitext syntax. Instead, use ...target.href, which is the title of the page that is actually transcluded. Compare the new code to ve.dm.MWTransclusionNode.prototype.getPartsList. Additionally, fix some confusion about namespaces (treating template names as titles in the main namespace). The template names in the configuration page (visualeditor-template-tools-definition.json) now support overriding namespaces in the same way as in wikitext. Bug: T234817 Change-Id: I7c557d28e961d0b9117fc0380c65cdd42035ae96
124 lines
3.5 KiB
JavaScript
124 lines
3.5 KiB
JavaScript
/*!
|
|
* VisualEditor MWDefinedTransclusionContextItem class.
|
|
*
|
|
* @copyright 2011-2018 VisualEditor Team and others; see http://ve.mit-license.org
|
|
*/
|
|
|
|
/**
|
|
* Context item for a defined MWTransclusion.
|
|
*
|
|
* Templates are defined on-wiki using the message page
|
|
* [[MediaWiki:Visualeditor-template-tools-definition.json]]
|
|
*
|
|
* Example:
|
|
* {
|
|
* // This key is the static name of the ve.ui.ContextItem
|
|
* "citationNeeded": [
|
|
* {
|
|
* // Normalized title string, or list of strings (for redirects)
|
|
* "title": [ "Citation needed", "cn" ],
|
|
* // Extra params. This whole object can be accessed
|
|
* // via #getMatchedTool
|
|
* "params": {
|
|
* "reason": "reason",
|
|
* "date": "date"
|
|
* }
|
|
* },
|
|
* {
|
|
* "title": "Cite quote",
|
|
* "params": {
|
|
* "date": "date"
|
|
* }
|
|
* }
|
|
* ]
|
|
* }
|
|
*
|
|
* @class
|
|
* @abstract
|
|
* @extends ve.ui.MWTransclusionContextItem
|
|
*
|
|
* @constructor
|
|
* @param {ve.ui.Context} context Context item is in
|
|
* @param {ve.dm.Model} model Model item is related to
|
|
* @param {Object} config Configuration options
|
|
*/
|
|
ve.ui.MWDefinedTransclusionContextItem = function VeUiMWDefinedTransclusionContextItem() {
|
|
// Parent constructor
|
|
ve.ui.MWDefinedTransclusionContextItem.super.apply( this, arguments );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
OO.inheritClass( ve.ui.MWDefinedTransclusionContextItem, ve.ui.MWTransclusionContextItem );
|
|
|
|
/* Static Properties */
|
|
|
|
ve.ui.MWDefinedTransclusionContextItem.static.name = null;
|
|
|
|
ve.ui.MWDefinedTransclusionContextItem.static.toolDefinitions = ( function () {
|
|
var tools;
|
|
|
|
try {
|
|
// Must use mw.message to avoid JSON being parsed as Wikitext
|
|
tools = JSON.parse( mw.message( 'visualeditor-template-tools-definition.json' ).plain() );
|
|
} catch ( e ) {}
|
|
|
|
return tools || {};
|
|
}() );
|
|
|
|
/**
|
|
* Only display item for single-template transclusions of these templates.
|
|
*
|
|
* @property {string|string[]|null}
|
|
* @static
|
|
* @inheritable
|
|
*/
|
|
ve.ui.MWDefinedTransclusionContextItem.static.template = null;
|
|
|
|
/* Static Methods */
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
ve.ui.MWDefinedTransclusionContextItem.static.isCompatibleWith = function ( model ) {
|
|
// Parent method
|
|
return ve.ui.MWDefinedTransclusionContextItem.super.static.isCompatibleWith.apply( this, arguments ) &&
|
|
!!this.getMatchedTool( model );
|
|
};
|
|
|
|
/**
|
|
* Get tool definitions, indexed by normalized title
|
|
*
|
|
* @return {Object} Collection of tool definitions
|
|
*/
|
|
ve.ui.MWDefinedTransclusionContextItem.static.getToolsByTitle = function () {
|
|
var toolsByTitle;
|
|
if ( !this.toolsByTitle ) {
|
|
this.toolsByTitle = toolsByTitle = {};
|
|
( this.toolDefinitions[ this.name ] || [] ).forEach( function ( template ) {
|
|
var titles = Array.isArray( template.title ) ? template.title : [ template.title ];
|
|
// 'title' can be a single title, or list of titles (including redirects)
|
|
titles.forEach( function ( title ) {
|
|
toolsByTitle[ mw.Title.newFromText( title, mw.config.get( 'wgNamespaceIds' ).template ).getPrefixedText() ] = template;
|
|
} );
|
|
} );
|
|
}
|
|
return this.toolsByTitle;
|
|
};
|
|
|
|
/**
|
|
* Get the tool definition that matches a specific model, if any
|
|
*
|
|
* @param {ve.dm.Model} model Model
|
|
* @return {Object|null} Tool definition, or null if no match
|
|
*/
|
|
ve.ui.MWDefinedTransclusionContextItem.static.getMatchedTool = function ( model ) {
|
|
var resource, title;
|
|
resource = ve.getProp( model.getAttribute( 'mw' ), 'parts', 0, 'template', 'target', 'href' );
|
|
if ( resource ) {
|
|
title = mw.Title.newFromText( ve.normalizeParsoidResourceName( resource ) ).getPrefixedText();
|
|
return this.getToolsByTitle()[ title ] || null;
|
|
}
|
|
return null;
|
|
};
|