From 9a7f8de4992f7d040fe9eca04300fc747f12b1c0 Mon Sep 17 00:00:00 2001 From: Moriel Schottlender Date: Sat, 17 May 2014 18:36:54 -0400 Subject: [PATCH] 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 --- TemplateData.php | 1 + i18n/en.json | 1 + i18n/qqq.json | 1 + modules/ext.templateDataGenerator.core.js | 137 +++++++++++++--------- tests/ext.templateData.tests.js | 13 +- 5 files changed, 93 insertions(+), 60 deletions(-) diff --git a/TemplateData.php b/TemplateData.php index 218b713b..ad1e4c31 100644 --- a/TemplateData.php +++ b/TemplateData.php @@ -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', diff --git a/i18n/en.json b/i18n/en.json index 586f98ea..f79f17b2 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -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" diff --git a/i18n/qqq.json b/i18n/qqq.json index 9181acea..c3d2037e 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -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" diff --git a/modules/ext.templateDataGenerator.core.js b/modules/ext.templateDataGenerator.core.js index ee3279bf..1a155cdc 100644 --- a/modules/ext.templateDataGenerator.core.js +++ b/modules/ext.templateDataGenerator.core.js @@ -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 diff --git a/tests/ext.templateData.tests.js b/tests/ext.templateData.tests.js index 2ddbe926..7f03a57e 100644 --- a/tests/ext.templateData.tests.js +++ b/tests/ext.templateData.tests.js @@ -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 ) {