1
1
import Diff from './base' ;
2
- import { longestCommonPrefix , longestCommonSuffix , replacePrefix , replaceSuffix , removePrefix , removeSuffix , maximumOverlap } from '../util/string' ;
2
+ import { longestCommonPrefix , longestCommonSuffix , replacePrefix , replaceSuffix , removePrefix , removeSuffix , maximumOverlap , leadingWs , trailingWs } from '../util/string' ;
3
3
4
4
// Based on https://en.wikipedia.org/wiki/Latin_script_in_Unicode
5
5
//
@@ -193,10 +193,10 @@ function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep
193
193
// * Just a "delete"
194
194
// We handle the three cases separately.
195
195
if ( deletion && insertion ) {
196
- const oldWsPrefix = deletion . value . match ( / ^ \s * / ) [ 0 ] ;
197
- const oldWsSuffix = deletion . value . match ( / \s * $ / ) [ 0 ] ;
198
- const newWsPrefix = insertion . value . match ( / ^ \s * / ) [ 0 ] ;
199
- const newWsSuffix = insertion . value . match ( / \s * $ / ) [ 0 ] ;
196
+ const oldWsPrefix = leadingWs ( deletion . value ) ;
197
+ const oldWsSuffix = trailingWs ( deletion . value ) ;
198
+ const newWsPrefix = leadingWs ( insertion . value ) ;
199
+ const newWsSuffix = trailingWs ( insertion . value ) ;
200
200
201
201
if ( startKeep ) {
202
202
const commonWsPrefix = longestCommonPrefix ( oldWsPrefix , newWsPrefix ) ;
@@ -218,16 +218,18 @@ function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep
218
218
// whitespace and deleting duplicate leading whitespace where
219
219
// present.
220
220
if ( startKeep ) {
221
- insertion . value = insertion . value . replace ( / ^ \s * / , '' ) ;
221
+ const ws = leadingWs ( insertion . value ) ;
222
+ insertion . value = insertion . value . substring ( ws . length ) ;
222
223
}
223
224
if ( endKeep ) {
224
- endKeep . value = endKeep . value . replace ( / ^ \s * / , '' ) ;
225
+ const ws = leadingWs ( endKeep . value ) ;
226
+ endKeep . value = endKeep . value . substring ( ws . length ) ;
225
227
}
226
228
// otherwise we've got a deletion and no insertion
227
229
} else if ( startKeep && endKeep ) {
228
- const newWsFull = endKeep . value . match ( / ^ \s * / ) [ 0 ] ,
229
- delWsStart = deletion . value . match ( / ^ \s * / ) [ 0 ] ,
230
- delWsEnd = deletion . value . match ( / \s * $ / ) [ 0 ] ;
230
+ const newWsFull = leadingWs ( endKeep . value ) ,
231
+ delWsStart = leadingWs ( deletion . value ) ,
232
+ delWsEnd = trailingWs ( deletion . value ) ;
231
233
232
234
// Any whitespace that comes straight after startKeep in both the old and
233
235
// new texts, assign to startKeep and remove from the deletion.
@@ -255,16 +257,16 @@ function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep
255
257
// We are at the start of the text. Preserve all the whitespace on
256
258
// endKeep, and just remove whitespace from the end of deletion to the
257
259
// extent that it overlaps with the start of endKeep.
258
- const endKeepWsPrefix = endKeep . value . match ( / ^ \s * / ) [ 0 ] ;
259
- const deletionWsSuffix = deletion . value . match ( / \s * $ / ) [ 0 ] ;
260
+ const endKeepWsPrefix = leadingWs ( endKeep . value ) ;
261
+ const deletionWsSuffix = trailingWs ( deletion . value ) ;
260
262
const overlap = maximumOverlap ( deletionWsSuffix , endKeepWsPrefix ) ;
261
263
deletion . value = removeSuffix ( deletion . value , overlap ) ;
262
264
} else if ( startKeep ) {
263
265
// We are at the END of the text. Preserve all the whitespace on
264
266
// startKeep, and just remove whitespace from the start of deletion to
265
267
// the extent that it overlaps with the end of startKeep.
266
- const startKeepWsSuffix = startKeep . value . match ( / \s * $ / ) [ 0 ] ;
267
- const deletionWsPrefix = deletion . value . match ( / ^ \s * / ) [ 0 ] ;
268
+ const startKeepWsSuffix = trailingWs ( startKeep . value ) ;
269
+ const deletionWsPrefix = leadingWs ( deletion . value ) ;
268
270
const overlap = maximumOverlap ( startKeepWsSuffix , deletionWsPrefix ) ;
269
271
deletion . value = removePrefix ( deletion . value , overlap ) ;
270
272
}
0 commit comments