Reorganize text-only replacement detection

* Lift node assignment out of the if/else
* Flip the condition so we detect text-only replacements rather than
  non-text-only replacements
* Additionally assert that there is exactly one selected node, and that
  it is a text node

Change-Id: Iaaddf532f06709e860ac44457470e6d8bfcb6dd9
This commit is contained in:
Catrope 2012-10-22 17:41:00 -07:00
parent b8dc697f17
commit 5d5335b498

View file

@ -224,21 +224,26 @@ ve.dm.TransactionProcessor.processors.replace = function ( op ) {
),
'leaves'
);
if ( removeHasStructure || insertHasStructure ) {
node = selection[0].node;
if (
!removeHasStructure && !insertHasStructure &&
selection.length === 1 &&
node && node.getType() === 'text'
) {
// Text-only replacement
// Queue a resize for the text node
this.synchronizer.pushResize( node, insert.length - remove.length );
} else {
// Replacement is not exclusively text
// Rebuild all covered nodes
range = new ve.Range(
selection[0].nodeRange.start, selection[selection.length - 1].nodeRange.end
selection[0].nodeRange.start,
selection[selection.length - 1].nodeRange.end
);
this.synchronizer.pushRebuild( range,
new ve.Range( range.start + this.adjustment,
range.end + this.adjustment + insert.length - remove.length )
);
} else {
// Text-only replacement
// Queue a resize for this node
node = selection[0].node;
this.synchronizer.pushResize( node, insert.length - remove.length );
}
// Advance the cursor
this.cursor += insert.length;