Block object descriptions from edit

Some descriptions allow for language objects. For the moment, these
should be blocked for editing so the original JSON string won't be
corrupted. The current fix blocks any input that is not a string
or an expected array in aliases as uneditable.

Bug: 60089
Change-Id: I9b13e2f3cfd805d382564e270484557567932a0f
This commit is contained in:
Moriel Schottlender 2014-05-17 18:36:54 -04:00 committed by James D. Forrester
parent 64a39ae460
commit 9a7f8de499
5 changed files with 93 additions and 60 deletions

View file

@ -107,6 +107,7 @@ $wgResourceModules['ext.templateDataGenerator.core'] = array(
'templatedata-modal-table-param-type-string',
'templatedata-modal-table-param-type-undefined',
'templatedata-modal-table-param-type-user',
'templatedata-modal-table-param-uneditablefield',
'templatedata-modal-title',
'templatedata-modal-title-templatedesc',
'templatedata-modal-title-templateparams',

View file

@ -54,6 +54,7 @@
"templatedata-modal-table-param-type-string": "String",
"templatedata-modal-table-param-type-undefined": "Undefined",
"templatedata-modal-table-param-type-user": "User",
"templatedata-modal-table-param-uneditablefield": "Uneditable",
"templatedata-modal-title": "Template documentation editor",
"templatedata-modal-title-templatedesc": "Template description",
"templatedata-modal-title-templateparams": "Template parameters"

View file

@ -56,6 +56,7 @@
"templatedata-modal-table-param-type-string": "A possible parameter type: String\n{{Related|Templatedata-modal-table-param-type}}\n{{Identical|String}}",
"templatedata-modal-table-param-type-undefined": "A possible parameter type: Undefined\n{{Related|Templatedata-modal-table-param-type}}\n{{Identical|Undefined}}",
"templatedata-modal-table-param-type-user": "A possible parameter type: User\n{{Related|Templatedata-modal-table-param-type}}\n{{Identical|User}}",
"templatedata-modal-table-param-uneditablefield": "Placeholder text notifying the user the field is uneditable",
"templatedata-modal-title": "Title of the modal popup.",
"templatedata-modal-title-templatedesc": "The title for the template description textbox",
"templatedata-modal-title-templateparams": "The title for the template parameters table"

View file

@ -278,11 +278,20 @@
paramsJson.params[paramid] &&
paramsJson.params[paramid][paramAttr]
) {
// make sure we set the value correctly based on the DOM element
if ( paramAttrObj[paramAttr].prop( 'type' ) === 'checkbox' ) {
// Only accept values that are expected editable
if (
typeof paramsJson.params[paramid][paramAttr] === 'string' ||
( paramAttr === 'aliases' && $.isArray( paramsJson.params[paramid][paramAttr] ) )
) {
paramAttrObj[paramAttr].val( paramsJson.params[paramid][paramAttr] );
} else if ( paramAttrObj[paramAttr].prop( 'type' ) === 'checkbox' ) {
paramAttrObj[paramAttr].prop( 'checked', paramsJson.params[paramid][paramAttr] );
} else {
paramAttrObj[paramAttr].val( paramsJson.params[paramid][paramAttr] );
// For the moment, objects are uneditable
paramAttrObj[paramAttr]
.prop( 'disabled', true )
.data( 'tdg-uneditable', true )
.val( mw.msg( 'brackets', mw.msg( 'templatedata-modal-table-param-uneditablefield' ) ) );
}
}
@ -465,7 +474,8 @@
* @returns {Object|boolean} Amended json object or false if changes are invalid.
*/
function applyChangeToJSON( originalJsonObject, modalDomElements, doNotCheckForm ) {
var paramid,
var templateDescriptionObject,
paramid,
paramName,
paramProp,
$domEl,
@ -487,8 +497,13 @@
}
// Update the description
if ( $( '.tdg-template-description' ).length ) {
outputJson.description = $( '.tdg-template-description' ).val();
templateDescriptionObject = $( '.tdg-template-description' );
if (
templateDescriptionObject.length &&
!templateDescriptionObject.data( 'tdg-uneditable' )
) {
outputJson.description = templateDescriptionObject.val();
}
// First check if there's outpuJson.params
@ -531,59 +546,62 @@
propExists = ( paramObj.hasOwnProperty( paramProp ) );
$domEl = domElements[paramProp];
// Check if value changed
switch ( paramProp ) {
// Skip:
case 'name':
case 'delbutton':
break;
case 'aliases':
newValue = cleanupAliasArray( $domEl.val() );
if ( propExists &&
newValue.sort().join( '|' ) !== paramObj.aliases.sort().join( '|' ) ) {
// Replace:
if ( newValue.length === 0 ) {
delete paramObj.aliases;
continue;
} else {
paramObj.aliases = newValue;
// Skip all inputs that are marked as uneditable
if ( !$domEl.data( 'tdg-uneditable' ) ) {
// Check if value changed
switch ( paramProp ) {
// Skip:
case 'name':
case 'delbutton':
break;
case 'aliases':
newValue = cleanupAliasArray( $domEl.val() );
if ( propExists &&
newValue.sort().join( '|' ) !== paramObj.aliases.sort().join( '|' ) ) {
// Replace:
if ( newValue.length === 0 ) {
delete paramObj.aliases;
continue;
} else {
paramObj.aliases = newValue;
}
} else if ( !propExists ) {
if ( newValue.length > 0 ) {
paramObj.aliases = newValue;
}
}
} else if ( !propExists ) {
if ( newValue.length > 0 ) {
paramObj.aliases = newValue;
break;
case 'description':
case 'default':
case 'label':
newValue = $domEl.val();
if ( paramObj[paramProp] !== newValue ) {
if ( !newValue || newValue.length === 0 ) {
delete paramObj[paramProp];
continue;
} else {
paramObj[paramProp] = newValue;
}
}
}
break;
case 'description':
case 'default':
case 'label':
newValue = $domEl.val();
if ( paramObj[paramProp] !== newValue ) {
if ( !newValue || newValue.length === 0 ) {
delete paramObj[paramProp];
continue;
} else {
break;
case 'type':
newValue = $domEl.val();
if ( paramObj[paramProp] !== newValue ) {
if ( newValue === 'undefined' ) {
delete paramObj[paramProp];
continue;
} else {
paramObj[paramProp] = newValue;
}
}
break;
case 'required':
newValue = $domEl.prop( 'checked' );
if ( paramObj[paramProp] !== undefined || newValue === true ) {
paramObj[paramProp] = newValue;
}
}
break;
case 'type':
newValue = $domEl.val();
if ( paramObj[paramProp] !== newValue ) {
if ( newValue === 'undefined' ) {
delete paramObj[paramProp];
continue;
} else {
paramObj[paramProp] = newValue;
}
}
break;
case 'required':
newValue = $domEl.prop( 'checked' );
if ( paramObj[paramProp] !== undefined || newValue === true ) {
paramObj[paramProp] = newValue;
}
break;
break;
}
}
}
}
@ -741,7 +759,14 @@
curr.paramsJson = parseTemplateData( wikitext );
if ( !$.isEmptyObject( curr.paramsJson ) ) {
if ( curr.paramsJson.description ) {
$descBox.text( curr.paramsJson.description );
if ( typeof curr.paramsJson.description === 'string' ) {
$descBox.text( curr.paramsJson.description );
} else {
$descBox
.prop( 'disabled', true )
.data( 'tdg-uneditable', true )
.val( mw.msg( 'brackets', mw.msg( 'templatedata-modal-table-param-uneditablefield' ) ) );
}
}
// Build the parameter row DOMs

View file

@ -17,7 +17,7 @@
' "type": "string/wiki-user-name",\n' +
' "default": "some default value here.",\n' +
' "required": true,\n' +
' "description": "User name of person who forgot to sign their comment.",\n' +
' "description": { "en": "User who forgot to sign their comment." },\n' +
' "aliases": ["1"]\n' +
' },\n' +
' "date": {\n' +
@ -82,10 +82,16 @@
'Parameter details: label.'
);
assert.equal(
// HACK: This is commented-out because the functionality is temporarily disabled for blocks
/* assert.equal(
$modalBox.find( '#param-user .tdg-element-attr-description' ).val(),
'User name of person who forgot to sign their comment.',
'User who forgot to sign their comment.',
'Parameter details: description.'
);*/
assert.equal(
$modalBox.find( '#param-user .tdg-element-attr-description' ).data( 'tdg-uneditable' ),
true,
'Block description as object: parameter description.'
);
assert.equal(
@ -111,7 +117,6 @@
false,
'Parameter details: non required.'
);
} );
QUnit.test( 'TemplateData JSON manipulation', 4, function ( assert ) {