From ace27b159bc3604d18cfcc02ff1a9e4c3cbaf3a4 Mon Sep 17 00:00:00 2001 From: Trevor Parscal Date: Wed, 5 Feb 2014 11:21:19 -0800 Subject: [PATCH] Fix-up offset when transplanting transclusion parts Symptoms: * When adding a transclusion part that already exists, if the item is being moved to the right it will inserted at a position one place too far Diagnoses: * When an index is provided with an item in a queue given to TransclusionModel's process method, the part is automatically removed, but the index, if given, is never adjusted accordingly Prognosis: * Cautiously optimistic Treatment: * Decrement the insertion index, if given and after the existing index Change-Id: If321df8f63dae07c76663e76e14864e2f4518920 --- .../ve-mw/dm/models/ve.dm.MWTransclusionModel.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js b/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js index abd1df17b0..33a501fc04 100644 --- a/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js +++ b/modules/ve-mw/dm/models/ve.dm.MWTransclusionModel.js @@ -98,7 +98,7 @@ ve.dm.MWTransclusionModel.prototype.load = function ( data ) { * @fires change */ ve.dm.MWTransclusionModel.prototype.process = function ( queue ) { - var i, len, item, title, index, remove; + var i, len, item, title, index, remove, existing; for ( i = 0, len = queue.length; i < len; i++ ) { remove = 0; @@ -111,11 +111,16 @@ ve.dm.MWTransclusionModel.prototype.process = function ( queue ) { } } - // Auto-remove if already existing - this.removePart( item.add ); - // Use specified index index = item.index; + // Auto-remove if already existing, preserving index + existing = ve.indexOf( item.add, this.parts ); + if ( existing !== -1 ) { + this.removePart( item.add ); + if ( index && index > existing ) { + index--; + } + } // Derive index from removal if given if ( index === undefined && item.remove ) { index = ve.indexOf( item.remove, this.parts );