mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-09-24 02:38:40 +00:00
Merge "Add input validation to the add parameter page"
This commit is contained in:
commit
022db20850
|
@ -2202,6 +2202,10 @@
|
|||
"visualeditor-dialog-transclusion-add-content",
|
||||
"visualeditor-dialog-transclusion-add-wikitext",
|
||||
"visualeditor-dialog-transclusion-add-param",
|
||||
"visualeditor-dialog-transclusion-add-param-error-alias",
|
||||
"visualeditor-dialog-transclusion-add-param-error-deprecated",
|
||||
"visualeditor-dialog-transclusion-add-param-error-exists-selected",
|
||||
"visualeditor-dialog-transclusion-add-param-error-exists-unselected",
|
||||
"visualeditor-dialog-transclusion-add-param-help",
|
||||
"visualeditor-dialog-transclusion-add-param-placeholder",
|
||||
"visualeditor-dialog-transclusion-add-param-save",
|
||||
|
|
|
@ -174,6 +174,10 @@
|
|||
"visualeditor-dialog-transclusion-add-content": "Add content",
|
||||
"visualeditor-dialog-transclusion-add-wikitext": "Add wikitext",
|
||||
"visualeditor-dialog-transclusion-add-param": "Add more information",
|
||||
"visualeditor-dialog-transclusion-add-param-error-alias": "\"$1\" is an [//www.mediawiki.org/wiki/Help:TemplateData#aliases alias] of the parameter \"$2\" and cannot be added because it would be a duplicate.",
|
||||
"visualeditor-dialog-transclusion-add-param-error-deprecated": "\"$1\" cannot be added because the parameter has been marked as [//www.mediawiki.org/wiki/Help:TemplateData#deprecated deprecated].",
|
||||
"visualeditor-dialog-transclusion-add-param-error-exists-selected": "Cannot add two parameters of the same name.",
|
||||
"visualeditor-dialog-transclusion-add-param-error-exists-unselected": "This parameter is already available for use. Please check the options in the sidebar.",
|
||||
"visualeditor-dialog-transclusion-add-param-help": "If known, enter undocumented parameter names. Note that only parameters known by the template will have an effect. You may find information about existing parameters on the [[$1|template's page]].",
|
||||
"visualeditor-dialog-transclusion-add-param-placeholder": "Parameter name",
|
||||
"visualeditor-dialog-transclusion-add-param-save": "Add parameter",
|
||||
|
|
|
@ -192,6 +192,10 @@
|
|||
"visualeditor-dialog-transclusion-add-content": "Label for button that adds parameter content to a transclusion.",
|
||||
"visualeditor-dialog-transclusion-add-wikitext": "Label for button that adds parameter wikitext to a transclusion.",
|
||||
"visualeditor-dialog-transclusion-add-param": "Label for button that adds a parameter to a transcluded template.",
|
||||
"visualeditor-dialog-transclusion-add-param-error-alias": "Message shown to an editor when they attempt adding an alias of a another parameter already present in the sidebar.\n\nParameters:\n* $1 - The alias of the parameter.\n* $2 - The name of the parameter.",
|
||||
"visualeditor-dialog-transclusion-add-param-error-deprecated": "Message shown to an editor when they attempt adding a parameter which is deprecated.\n\nParameters:\n* $1 - The name of the parameter.",
|
||||
"visualeditor-dialog-transclusion-add-param-error-exists-selected": "Message shown to an editor when they attempt adding a parameter which is already present and checked in the sidebar.\n\nParameters:\n* $1 - The name of the parameter.",
|
||||
"visualeditor-dialog-transclusion-add-param-error-exists-unselected": "Message shown to an editor when they attempt adding a parameter which is already present and unchecked in the sidebar.\n\nParameters:\n* $1 - The name of the parameter.",
|
||||
"visualeditor-dialog-transclusion-add-param-help": "Help text for new undocumented parameter input field.\n\nParameters:\n* $1 - The title of the template.",
|
||||
"visualeditor-dialog-transclusion-add-param-placeholder": "Placeholder in the input field that adds a new undocumented parameter to a transcluded template.",
|
||||
"visualeditor-dialog-transclusion-add-param-save": "Label for save button that adds a new undocumented parameter to a transcluded template.",
|
||||
|
|
|
@ -41,3 +41,46 @@ QUnit.test( 'Outline item initialization', ( assert ) => {
|
|||
assert.notOk( outlineItem.$element.hasClass( 'oo-ui-outlineOptionWidget' ),
|
||||
'Outline item should not be styled' );
|
||||
} );
|
||||
|
||||
[
|
||||
[ '', 0 ],
|
||||
[ 'a', 0 ],
|
||||
[ 'used', '(visualeditor-dialog-transclusion-add-param-error-exists-selected: used, used)' ],
|
||||
[ 'unused', '(visualeditor-dialog-transclusion-add-param-error-exists-unselected: unused, unused)' ],
|
||||
[ 'usedAlias', '(visualeditor-dialog-transclusion-add-param-error-alias: usedAlias, x)' ],
|
||||
[ 'unusedAlias', '(visualeditor-dialog-transclusion-add-param-error-alias: unusedAlias, y)' ],
|
||||
[ 'usedDeprecated', '(visualeditor-dialog-transclusion-add-param-error-exists-selected: usedDeprecated, usedDeprecated)' ],
|
||||
[ 'unusedDeprecated', '(visualeditor-dialog-transclusion-add-param-error-deprecated: unusedDeprecated, unusedDeprecated)' ]
|
||||
].forEach( ( [ input, expected ] ) =>
|
||||
QUnit.test( 'getValidationErrors: ' + input, ( assert ) => {
|
||||
const data = {
|
||||
target: {},
|
||||
params: {
|
||||
used: {},
|
||||
usedAlias: {},
|
||||
usedDeprecated: {}
|
||||
}
|
||||
};
|
||||
|
||||
const transclusion = new ve.dm.MWTransclusionModel(),
|
||||
template = ve.dm.MWTemplateModel.newFromData( transclusion, data ),
|
||||
parameter = new ve.dm.MWParameterModel( template ),
|
||||
page = new ve.ui.MWAddParameterPage( parameter );
|
||||
|
||||
template.getSpec().setTemplateData( { params: {
|
||||
used: {},
|
||||
unused: {},
|
||||
x: { aliases: [ 'usedAlias' ] },
|
||||
y: { aliases: [ 'unusedAlias' ] },
|
||||
usedDeprecated: { deprecated: true },
|
||||
unusedDeprecated: { deprecated: true }
|
||||
} } );
|
||||
template.addParameter( parameter );
|
||||
|
||||
const errors = page.getValidationErrors( input );
|
||||
assert.strictEqual( errors.length, expected ? 1 : 0 );
|
||||
if ( expected ) {
|
||||
assert.strictEqual( errors[ 0 ].text(), expected );
|
||||
}
|
||||
} )
|
||||
);
|
||||
|
|
|
@ -41,10 +41,10 @@ ve.ui.MWAddParameterPage = function VeUiMWAddParameterPage( parameter, name, con
|
|||
} );
|
||||
this.saveButton = new OO.ui.ButtonWidget( {
|
||||
label: ve.msg( 'visualeditor-dialog-transclusion-add-param-save' ),
|
||||
flags: [ 'primary', 'progressive' ]
|
||||
flags: [ 'primary', 'progressive' ],
|
||||
disabled: true
|
||||
} )
|
||||
.connect( this, { click: 'onParameterNameSubmitted' } )
|
||||
.setDisabled( true );
|
||||
.connect( this, { click: 'onParameterNameSubmitted' } );
|
||||
|
||||
this.addParameterInputField = new OO.ui.ActionFieldLayout(
|
||||
this.paramInputField,
|
||||
|
@ -89,8 +89,11 @@ OO.inheritClass( ve.ui.MWAddParameterPage, OO.ui.PageLayout );
|
|||
*/
|
||||
ve.ui.MWAddParameterPage.prototype.onParameterNameChanged = function ( value ) {
|
||||
var paramName = value.trim(),
|
||||
isValid = /^[^={|}]+$/.test( paramName );
|
||||
this.saveButton.setDisabled( !isValid );
|
||||
isValid = /^[^={|}]+$/.test( paramName ),
|
||||
errors = this.getValidationErrors( paramName );
|
||||
|
||||
this.addParameterInputField.setErrors( errors );
|
||||
this.saveButton.setDisabled( !isValid || errors.length );
|
||||
};
|
||||
|
||||
ve.ui.MWAddParameterPage.prototype.onParameterNameSubmitted = function () {
|
||||
|
@ -105,11 +108,6 @@ ve.ui.MWAddParameterPage.prototype.onParameterNameSubmitted = function () {
|
|||
return;
|
||||
}
|
||||
|
||||
if ( this.template.getSpec().isParameterDocumented( name ) ) {
|
||||
// TODO: This special case needs a proper error message for the user
|
||||
return;
|
||||
}
|
||||
|
||||
this.template.addParameter( new ve.dm.MWParameterModel( this.template, name ) );
|
||||
|
||||
ve.track( 'activity.transclusion', {
|
||||
|
@ -117,6 +115,35 @@ ve.ui.MWAddParameterPage.prototype.onParameterNameSubmitted = function () {
|
|||
} );
|
||||
};
|
||||
|
||||
ve.ui.MWAddParameterPage.prototype.getValidationErrors = function ( name ) {
|
||||
if ( !name ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
var key,
|
||||
spec = this.template.getSpec();
|
||||
|
||||
if ( spec.getParameterAliases( name ).indexOf( name ) !== -1 ) {
|
||||
key = 'visualeditor-dialog-transclusion-add-param-error-alias';
|
||||
} else if ( this.template.hasParameter( name ) ) {
|
||||
key = 'visualeditor-dialog-transclusion-add-param-error-exists-selected';
|
||||
} else if ( spec.isParameterDeprecated( name ) ) {
|
||||
key = 'visualeditor-dialog-transclusion-add-param-error-deprecated';
|
||||
} else if ( spec.isKnownParameterOrAlias( name ) ) {
|
||||
key = 'visualeditor-dialog-transclusion-add-param-error-exists-unselected';
|
||||
}
|
||||
|
||||
if ( !key ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
var label = spec.getParameterLabel( spec.getPrimaryParameterName( name ) ),
|
||||
// eslint-disable-next-line mediawiki/msg-doc
|
||||
$msg = mw.message( key, name, label ).parseDom();
|
||||
ve.targetLinksToNewWindow( $( '<div>' ).append( $msg )[ 0 ] );
|
||||
return [ $msg ];
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {boolean} [expand]
|
||||
|
|
|
@ -41,6 +41,11 @@
|
|||
width: 100%;
|
||||
}
|
||||
|
||||
.ve-ui-mwTransclusionDialog-addParameterFieldset .oo-ui-fieldLayout-messages .oo-ui-messageWidget {
|
||||
margin-left: 0;
|
||||
margin-right: 0;
|
||||
}
|
||||
|
||||
.ve-ui-mwTransclusionDialog-addParameterFieldset-header.oo-ui-iconElement .oo-ui-iconElement-icon {
|
||||
height: 100%;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue