r112150 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112149‎ | r112150 | r112151 >
Date:21:23, 22 February 2012
Author:tparscal
Status:deferred
Tags:
Comment:
Replaced "set" and "clear" method for attribute transactions with "replace" method, which allows correct reversion. Also fixed list item tools to correctly use the new function signature.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/ve/dm/nodes/ve.dm.DocumentNode.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.Transaction.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.TransactionProcessor.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.IndentationButtonTool.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.ListButtonTool.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.ListButtonTool.js
@@ -78,21 +78,19 @@
7979 }
8080 }
8181
82 - for( i = 0; i < listItems.length; i++ ) {
 82+ for ( i = 0; i < listItems.length; i++ ) {
8383 styles = listItems[i].getElementAttribute( 'styles' );
8484 if ( styles[styles.length - 1] !== style ) {
85 - styles.splice( styles.length - 1, 1, style );
8685 tx = surface.model.getDocument().prepareElementAttributeChange(
8786 surface.documentView.model.getOffsetFromNode( listItems[i], false ),
88 - 'set',
8987 'styles',
90 - styles
 88+ styles.slice( 0, styles.length - 1 ).concat( style )
9189 );
9290 surface.model.transact( tx );
9391 }
9492 }
9593
96 - for( i = 0; i < stacks.length; i++ ) {
 94+ for ( i = 0; i < stacks.length; i++ ) {
9795 removeLength = 0;
9896 insertAt = surface.documentView.model.getOffsetFromNode( stacks[i][0], false );
9997
Index: trunk/extensions/VisualEditor/modules/ve/ui/tools/ve.ui.IndentationButtonTool.js
@@ -45,12 +45,10 @@
4646 for ( i = 0; i < listItems.length; i++ ) {
4747 styles = listItems[i].getElementAttribute( 'styles' );
4848 if ( styles.length < 6 ) {
49 - styles.push( styles[styles.length - 1] );
5049 tx = surface.model.getDocument().prepareElementAttributeChange(
5150 surface.documentView.model.getOffsetFromNode( listItems[i], false ),
52 - 'set',
5351 'styles',
54 - styles
 52+ styles.concat( styles[styles.length - 1] )
5553 );
5654 surface.model.transact( tx );
5755 }
@@ -66,12 +64,10 @@
6765 for ( i = 0; i < listItems.length; i++ ) {
6866 styles = listItems[i].getElementAttribute( 'styles' );
6967 if ( styles.length > 1 ) {
70 - styles.splice( styles.length - 1, 1);
7168 tx = surface.model.getDocument().prepareElementAttributeChange(
7269 surface.documentView.model.getOffsetFromNode( listItems[i], false ),
73 - 'set',
7470 'styles',
75 - styles
 71+ styles.slice( 0, styles.length - 1 )
7672 );
7773 surface.model.transact( tx );
7874 }
Index: trunk/extensions/VisualEditor/modules/ve/dm/nodes/ve.dm.DocumentNode.js
@@ -1175,7 +1175,7 @@
11761176 * @method
11771177 * @returns {ve.dm.Transaction}
11781178 */
1179 -ve.dm.DocumentNode.prototype.prepareElementAttributeChange = function( offset, method, key, value ) {
 1179+ve.dm.DocumentNode.prototype.prepareElementAttributeChange = function( offset, key, to ) {
11801180 var tx = new ve.dm.Transaction();
11811181 if ( offset ) {
11821182 tx.pushRetain( offset );
@@ -1186,7 +1186,8 @@
11871187 if ( this.data[offset].type[0] === '/' ) {
11881188 throw 'Invalid element offset error. Can not set attributes on closing element.';
11891189 }
1190 - tx.pushChangeElementAttribute( method, key, value );
 1190+ var from = 'attributes' in this.data[offset] ? this.data[offset].attributes[key] : undefined;
 1191+ tx.pushReplaceElementAttribute( key, from, to );
11911192 if ( offset < this.data.length ) {
11921193 tx.pushRetain( this.data.length - offset );
11931194 }
Index: trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.Transaction.js
@@ -94,14 +94,15 @@
9595 * @method
9696 * @param {String} method Method to use, either "set" or "clear"
9797 * @param {String} key Name of attribute to change
98 - * @param {Mixed} value Value to set attribute to, or value of attribute being cleared
 98+ * @param {Mixed} from Value change attribute from
 99+ * @param {Mixed} to Value to change attribute to
99100 */
100 -ve.dm.Transaction.prototype.pushChangeElementAttribute = function( method, key, value ) {
 101+ve.dm.Transaction.prototype.pushReplaceElementAttribute = function( key, from, to ) {
101102 this.operations.push( {
102103 'type': 'attribute',
103 - 'method': method,
104104 'key': key,
105 - 'value': value
 105+ 'from': from,
 106+ 'to': to
106107 } );
107108 };
108109
Index: trunk/extensions/VisualEditor/modules/ve/dm/ve.dm.TransactionProcessor.js
@@ -450,13 +450,9 @@
451451 if ( element.type === undefined ) {
452452 throw 'Invalid element error. Can not set attributes on non-element data.';
453453 }
454 - if ( ( op.method === 'set' && !invert ) || ( op.method === 'clear' && invert ) ) {
455 - // Automatically initialize attributes object
456 - if ( !element.attributes ) {
457 - element.attributes = {};
458 - }
459 - element.attributes[op.key] = op.value;
460 - } else if ( ( op.method === 'clear' && !invert ) || ( op.method === 'set' && invert ) ) {
 454+ var to = invert ? op.from : op.to;
 455+ if ( to === undefined ) {
 456+ // Clear
461457 if ( element.attributes ) {
462458 delete element.attributes[op.key];
463459 }
@@ -470,7 +466,12 @@
471467 delete element.attributes;
472468 }
473469 } else {
474 - throw 'Invalid method error. Can not operate attributes this way: ' + method;
 470+ // Automatically initialize attributes object
 471+ if ( !element.attributes ) {
 472+ element.attributes = {};
 473+ }
 474+ // Set
 475+ element.attributes[op.key] = to;
475476 }
476477 var node = this.model.getNodeFromOffset( this.cursor + 1 );
477478 if ( node.hasChildren() ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r112927Fixed tests that were broken by r112150.tparscal23:12, 2 March 2012

Status & tagging log