Index: trunk/extensions/VisualEditor/modules/es/es.TransactionProcessor.js |
— | — | @@ -89,7 +89,8 @@ |
90 | 90 | }; |
91 | 91 | |
92 | 92 | es.TransactionProcessor.prototype.rebuildNodes = function( newData, oldNodes, parent, index ) { |
93 | | - var remove = 0; |
| 93 | + var newNodes = es.DocumentModel.createNodesFromData( newData ), |
| 94 | + remove = 0; |
94 | 95 | if ( oldNodes ) { |
95 | 96 | if ( oldNodes[0] === oldNodes[0].getRoot() ) { |
96 | 97 | parent = oldNodes[0]; |
— | — | @@ -100,13 +101,34 @@ |
101 | 102 | index = parent.indexOf( oldNodes[0] ); |
102 | 103 | remove = oldNodes.length; |
103 | 104 | } |
| 105 | + // Try to preserve the first node |
| 106 | + if ( |
| 107 | + // There must be an old and new node to preserve |
| 108 | + newNodes.length && |
| 109 | + oldNodes.length && |
| 110 | + // Node types need to match |
| 111 | + newNodes[0].type === oldNodes[0].type && |
| 112 | + // Only for leaf nodes |
| 113 | + !newNodes[0].hasChildren() |
| 114 | + ) { |
| 115 | + var newNode = newNodes.shift(), |
| 116 | + oldNode = oldNodes.shift(); |
| 117 | + // Let's just leave this first node in place and adjust it's length |
| 118 | + var newAttributes = newNode.getElement().attributes, |
| 119 | + oldAttributes = oldNode.getElement().attributes; |
| 120 | + if ( oldAttributes || newAttributes ) { |
| 121 | + oldNode.getElement().attributes = newAttributes; |
| 122 | + } |
| 123 | + oldNode.adjustContentLength( newNode.getContentLength() - oldNode.getContentLength() ); |
| 124 | + index++; |
| 125 | + remove--; |
| 126 | + } |
104 | 127 | } |
105 | 128 | // Try to perform this in a single operation if possible, this reduces the number of UI updates |
106 | 129 | // TODO: Introduce a global for max argument length - 1024 is also assumed in es.insertIntoArray |
107 | | - var newNodes = es.DocumentModel.createNodesFromData( newData ); |
108 | 130 | if ( newNodes.length < 1024 ) { |
109 | 131 | parent.splice.apply( parent, [index, remove].concat( newNodes ) ); |
110 | | - } else { |
| 132 | + } else if ( newNodes.length ) { |
111 | 133 | parent.splice.apply( parent, [index, remove] ); |
112 | 134 | // Safe to call with arbitrary length of newNodes |
113 | 135 | es.insertIntoArray( parent, index, newNodes ); |