| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
QUnit.module( 've.dm.Surface' ); |
| 8 |
|
| 9 |
ve.dm.SurfaceStub = function VeDmSurfaceStub( data, range ) { |
| 10 |
const doc = new ve.dm.Document( data || [ { type: 'paragraph' }, ...'hi', { type: '/paragraph' }, { type: 'internalList' }, { type: '/internalList' } ] ); |
| 11 |
|
| 12 |
|
| 13 |
ve.dm.SurfaceStub.super.call( this, doc ); |
| 14 |
|
| 15 |
|
| 16 |
this.setLinearSelection( range || new ve.Range( 1 ) ); |
| 17 |
}; |
| 18 |
|
| 19 |
OO.inheritClass( ve.dm.SurfaceStub, ve.dm.Surface ); |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
QUnit.test( 'getDocument', ( assert ) => { |
| 24 |
const surface = new ve.dm.SurfaceStub(); |
| 25 |
assert.strictEqual( surface.getDocument(), surface.documentModel ); |
| 26 |
} ); |
| 27 |
|
| 28 |
QUnit.test( 'getSelection', ( assert ) => { |
| 29 |
const surface = new ve.dm.SurfaceStub(); |
| 30 |
assert.strictEqual( surface.getSelection(), surface.selection ); |
| 31 |
} ); |
| 32 |
|
| 33 |
QUnit.test( 'setSelection out of range', ( assert ) => { |
| 34 |
const surface = new ve.dm.SurfaceStub( [ |
| 35 |
{ type: 'paragraph' }, |
| 36 |
...'Foo', |
| 37 |
{ type: '/paragraph' }, |
| 38 |
{ type: 'internalList' }, { type: '/internalList' } |
| 39 |
] ); |
| 40 |
|
| 41 |
surface.setSelection( new ve.dm.LinearSelection( new ve.Range( 2, 100 ) ) ); |
| 42 |
let range = surface.getSelection().getRange(); |
| 43 |
assert.deepEqual( [ range.start, range.end ], [ 2, 5 ] ); |
| 44 |
surface.setSelection( new ve.dm.LinearSelection( new ve.Range( 99, 100 ) ) ); |
| 45 |
range = surface.getSelection().getRange(); |
| 46 |
assert.deepEqual( [ range.start, range.end ], [ 5, 5 ] ); |
| 47 |
} ); |
| 48 |
|
| 49 |
QUnit.test( 'contextChange events', ( assert ) => { |
| 50 |
const surface = new ve.dm.SurfaceStub( ve.dm.example.preprocessAnnotations( [ |
| 51 |
{ type: 'paragraph' }, |
| 52 |
...'Foo', |
| 53 |
|
| 54 |
[ 'b', [ ve.dm.example.bold ] ], |
| 55 |
[ 'a', [ ve.dm.example.bold ] ], |
| 56 |
[ 'r', [ ve.dm.example.bold ] ], |
| 57 |
|
| 58 |
[ 'b', [ ve.dm.example.italic ] ], |
| 59 |
[ 'a', [ ve.dm.example.italic ] ], |
| 60 |
[ 'z', [ ve.dm.example.italic ] ], |
| 61 |
...'Foo', |
| 62 |
{ type: '/paragraph' }, |
| 63 |
{ type: 'list', attributes: { style: 'bullet' } }, |
| 64 |
{ type: 'listItem' }, |
| 65 |
{ type: 'paragraph' }, |
| 66 |
...'One', |
| 67 |
{ type: '/paragraph' }, |
| 68 |
{ type: '/listItem' }, |
| 69 |
{ type: 'listItem' }, |
| 70 |
{ type: 'paragraph' }, |
| 71 |
...'Two', |
| 72 |
{ type: '/paragraph' }, |
| 73 |
{ type: '/listItem' }, |
| 74 |
{ type: '/list' }, |
| 75 |
{ type: 'internalList' }, { type: '/internalList' } |
| 76 |
] ) ); |
| 77 |
|
| 78 |
let contextChanges = 0; |
| 79 |
surface.on( 'contextChange', () => { |
| 80 |
contextChanges++; |
| 81 |
} ); |
| 82 |
|
| 83 |
const cases = [ |
| 84 |
{ |
| 85 |
title: 'setSelection, to equivalent selection', |
| 86 |
initialSelection: new ve.Range( 1 ), |
| 87 |
selection: new ve.Range( 1 ), |
| 88 |
expected: 0 |
| 89 |
}, |
| 90 |
{ |
| 91 |
title: 'setSelection, to equivalent expanded selection', |
| 92 |
initialSelection: new ve.Range( 1, 2 ), |
| 93 |
selection: new ve.Range( 1, 2 ), |
| 94 |
expected: 0 |
| 95 |
}, |
| 96 |
{ |
| 97 |
title: 'setSelection, move within plain text', |
| 98 |
initialSelection: new ve.Range( 1 ), |
| 99 |
selection: new ve.Range( 2 ), |
| 100 |
expected: 0 |
| 101 |
}, |
| 102 |
{ |
| 103 |
|
| 104 |
title: 'setSelection, expands over plain text', |
| 105 |
initialSelection: new ve.Range( 1 ), |
| 106 |
selection: new ve.Range( 1, 2 ), |
| 107 |
expected: 1 |
| 108 |
}, |
| 109 |
{ |
| 110 |
title: 'setSelection, collapses', |
| 111 |
initialSelection: new ve.Range( 1, 2 ), |
| 112 |
selection: new ve.Range( 1 ), |
| 113 |
expected: 1 |
| 114 |
}, |
| 115 |
{ |
| 116 |
title: 'setSelection, non-collapsed selection expands over plain text', |
| 117 |
initialSelection: new ve.Range( 1, 2 ), |
| 118 |
selection: new ve.Range( 1, 3 ), |
| 119 |
expected: 0 |
| 120 |
}, |
| 121 |
{ |
| 122 |
title: 'setSelection, non-collapsed selection expands into annotated text', |
| 123 |
initialSelection: new ve.Range( 1, 2 ), |
| 124 |
selection: new ve.Range( 1, 5 ), |
| 125 |
expected: 1 |
| 126 |
}, |
| 127 |
{ |
| 128 |
title: 'setSelection, non-collapsed selection expands into annotated text, other direction', |
| 129 |
initialSelection: new ve.Range( 12, 11 ), |
| 130 |
selection: new ve.Range( 12, 8 ), |
| 131 |
expected: 2 |
| 132 |
}, |
| 133 |
{ |
| 134 |
title: 'setSelection, move collapsed selection to edge of annotated text', |
| 135 |
initialSelection: new ve.Range( 2 ), |
| 136 |
selection: new ve.Range( 4 ), |
| 137 |
expected: 1 |
| 138 |
}, |
| 139 |
{ |
| 140 |
title: 'setSelection, move collapsed selection to within annotated text', |
| 141 |
initialSelection: new ve.Range( 2 ), |
| 142 |
selection: new ve.Range( 5 ), |
| 143 |
expected: 2 |
| 144 |
}, |
| 145 |
{ |
| 146 |
title: 'setSelection, selection collapsed in end node (but overally non-collapsed) becomes non-collapsed in end node', |
| 147 |
initialSelection: new ve.Range( 18, 24 ), |
| 148 |
selection: new ve.Range( 18, 26 ), |
| 149 |
expected: 1 |
| 150 |
} |
| 151 |
]; |
| 152 |
|
| 153 |
cases.forEach( ( caseItem ) => { |
| 154 |
surface.setLinearSelection( caseItem.initialSelection || new ve.Range( 1 ) ); |
| 155 |
contextChanges = 0; |
| 156 |
surface.setLinearSelection( caseItem.selection ); |
| 157 |
assert.strictEqual( contextChanges, caseItem.expected, caseItem.title ); |
| 158 |
} ); |
| 159 |
} ); |
| 160 |
|
| 161 |
QUnit.test( 'documentUpdate/select events', ( assert ) => { |
| 162 |
const surface = new ve.dm.SurfaceStub(), |
| 163 |
doc = surface.getDocument(), |
| 164 |
|
| 165 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 3, [ 'i' ] ), |
| 166 |
events = { |
| 167 |
documentUpdate: 0, |
| 168 |
select: 0 |
| 169 |
}; |
| 170 |
|
| 171 |
surface.on( 'documentUpdate', () => { |
| 172 |
events.documentUpdate++; |
| 173 |
} ); |
| 174 |
surface.on( 'select', () => { |
| 175 |
events.select++; |
| 176 |
} ); |
| 177 |
surface.change( tx.clone() ); |
| 178 |
assert.deepEqual( events, { documentUpdate: 1, select: 0 }, 'change with transaction only' ); |
| 179 |
surface.setLinearSelection( new ve.Range( 2 ) ); |
| 180 |
assert.deepEqual( events, { documentUpdate: 1, select: 1 }, 'setSelection' ); |
| 181 |
surface.change( tx.clone(), new ve.dm.LinearSelection( new ve.Range( 3 ) ) ); |
| 182 |
assert.deepEqual( events, { documentUpdate: 2, select: 2 }, 'change with transaction and selection' ); |
| 183 |
} ); |
| 184 |
|
| 185 |
QUnit.test( 'breakpoint/undo/redo', ( assert ) => { |
| 186 |
const range = new ve.Range( 1, 3 ), |
| 187 |
surface = new ve.dm.SurfaceStub( null, range ), |
| 188 |
fragment = surface.getFragment(), |
| 189 |
doc = surface.getDocument(), |
| 190 |
selection = new ve.dm.LinearSelection( range ), |
| 191 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 1, [ 'x' ] ); |
| 192 |
|
| 193 |
assert.strictEqual( surface.breakpoint(), false, 'Returns false if no transactions applied' ); |
| 194 |
|
| 195 |
surface.change( tx ); |
| 196 |
assert.deepEqual( surface.undoStack, [], 'Undo stack data matches before breakpoint' ); |
| 197 |
assert.deepEqual( surface.newTransactions, [ tx ], 'New transactions match before breakpoint' ); |
| 198 |
|
| 199 |
assert.strictEqual( surface.breakpoint(), true, 'Returns true after transaction applied' ); |
| 200 |
assert.strictEqual( surface.breakpoint(), false, 'Returns false if no transactions applied since last breakpoint' ); |
| 201 |
|
| 202 |
assert.deepEqual( |
| 203 |
surface.undoStack, [ { |
| 204 |
start: 0, |
| 205 |
transactions: [ tx ], |
| 206 |
selection: new ve.dm.LinearSelection( tx.translateRange( selection.getRange() ) ), |
| 207 |
selectionBefore: selection |
| 208 |
} ], |
| 209 |
'Undo stack data matches after breakpoint' |
| 210 |
); |
| 211 |
assert.deepEqual( surface.newTransactions, [], 'New transactions empty after breakpoint' ); |
| 212 |
|
| 213 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Range changed' ); |
| 214 |
|
| 215 |
surface.setLinearSelection( new ve.Range( 3 ) ); |
| 216 |
surface.undo(); |
| 217 |
assert.equalHash( surface.getSelection().getRange(), range, 'Range restored after undo' ); |
| 218 |
assert.strictEqual( fragment.getText(), 'hi', 'Text restored after undo' ); |
| 219 |
|
| 220 |
surface.setLinearSelection( new ve.Range( 3 ) ); |
| 221 |
surface.redo(); |
| 222 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Range changed after redo' ); |
| 223 |
assert.strictEqual( fragment.getText(), 'xhi', 'Text changed after redo' ); |
| 224 |
|
| 225 |
} ); |
| 226 |
|
| 227 |
QUnit.test( 'multi-user undo', ( assert ) => { |
| 228 |
const surfaces = []; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
for ( let i = 1; i <= 2; i++ ) { |
| 233 |
const range = new ve.Range( 1 ); |
| 234 |
const surface = new ve.dm.SurfaceStub( [ |
| 235 |
{ type: 'paragraph' }, { type: '/paragraph' }, |
| 236 |
{ type: 'paragraph' }, { type: '/paragraph' }, |
| 237 |
{ type: 'internalList' }, { type: '/internalList' } |
| 238 |
], range ); |
| 239 |
surface.setAuthorId( i ); |
| 240 |
surface.setMultiUser( true ); |
| 241 |
|
| 242 |
const doc = surface.getDocument(); |
| 243 |
|
| 244 |
let tx = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 1, [ ...'foo' ] ); |
| 245 |
tx.authorId = 1; |
| 246 |
surface.change( tx ); |
| 247 |
surface.breakpoint(); |
| 248 |
|
| 249 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 6, [ ...'123' ] ); |
| 250 |
tx.authorId = 2; |
| 251 |
surface.change( tx ); |
| 252 |
surface.breakpoint(); |
| 253 |
|
| 254 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 4, [ ...'bar' ] ); |
| 255 |
tx.authorId = 1; |
| 256 |
surface.change( tx ); |
| 257 |
surface.breakpoint(); |
| 258 |
|
| 259 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 12, [ ...'456' ] ); |
| 260 |
tx.authorId = 2; |
| 261 |
surface.change( tx ); |
| 262 |
surface.breakpoint(); |
| 263 |
|
| 264 |
surfaces.push( surface ); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
surfaces[ 0 ].undo(); |
| 269 |
surfaces[ 0 ].undo(); |
| 270 |
assert.equalLinearData( |
| 271 |
surfaces[ 0 ].getDocument().getData(), |
| 272 |
[ |
| 273 |
{ type: 'paragraph' }, { type: '/paragraph' }, |
| 274 |
{ type: 'paragraph' }, ...'123456', { type: '/paragraph' }, |
| 275 |
{ type: 'internalList' }, { type: '/internalList' } |
| 276 |
] |
| 277 |
); |
| 278 |
assert.strictEqual( surfaces[ 0 ].canUndo(), false, 'No more steps for user on surface 1 to undo' ); |
| 279 |
|
| 280 |
|
| 281 |
surfaces[ 1 ].undo(); |
| 282 |
surfaces[ 1 ].undo(); |
| 283 |
assert.equalLinearData( |
| 284 |
surfaces[ 1 ].getDocument().getData(), |
| 285 |
[ |
| 286 |
{ type: 'paragraph' }, ...'foobar', { type: '/paragraph' }, |
| 287 |
{ type: 'paragraph' }, { type: '/paragraph' }, |
| 288 |
{ type: 'internalList' }, { type: '/internalList' } |
| 289 |
] |
| 290 |
); |
| 291 |
|
| 292 |
assert.strictEqual( surfaces[ 1 ].canUndo(), true, 'User on surface 2 thinks they can still undo' ); |
| 293 |
surfaces[ 1 ].undo(); |
| 294 |
assert.strictEqual( surfaces[ 1 ].canUndo(), false, 'User on surface 2 realises they can\'t undo' ); |
| 295 |
|
| 296 |
assert.strictEqual( surfaces[ 1 ].getDocument().getCompleteHistoryLength(), 6, 'Final undo was a no-op' ); |
| 297 |
|
| 298 |
} ); |
| 299 |
|
| 300 |
QUnit.test( 'change rollback', ( assert ) => { |
| 301 |
const range = new ve.Range( 1, 3 ), |
| 302 |
surface = new ve.dm.SurfaceStub( null, range ), |
| 303 |
doc = surface.getDocument(); |
| 304 |
|
| 305 |
const tx = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 1, [ { type: '/heading' } ] ); |
| 306 |
assert.throws( |
| 307 |
() => { |
| 308 |
surface.change( tx ); |
| 309 |
}, |
| 310 |
new Error( 'Expected closing for paragraph but got closing for heading' ), |
| 311 |
'Transaction throws an exception' |
| 312 |
); |
| 313 |
|
| 314 |
assert.strictEqual( surface.canUndo(), false, 'No history to undo after failed change' ); |
| 315 |
} ); |
| 316 |
|
| 317 |
QUnit.test( 'range translation', ( assert ) => { |
| 318 |
const surface = new ve.dm.SurfaceStub( null, new ve.Range( 3 ) ), |
| 319 |
doc = surface.getDocument(), |
| 320 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 3, [ 'x' ] ); |
| 321 |
surface.change( tx ); |
| 322 |
const selection = surface.getSelection(); |
| 323 |
assert.true( selection instanceof ve.dm.LinearSelection, 'Selection is linear' ); |
| 324 |
assert.equalRange( selection.getRange(), new ve.Range( 3 ), 'Cursor is unmoved' ); |
| 325 |
} ); |
| 326 |
|
| 327 |
QUnit.test( 'staging', ( assert ) => { |
| 328 |
const surface = new ve.dm.SurfaceStub( null, new ve.Range( 1, 3 ) ), |
| 329 |
fragment = surface.getFragment(), |
| 330 |
doc = surface.getDocument(); |
| 331 |
|
| 332 |
assert.strictEqual( surface.isStaging(), false, 'isStaging false when not staging' ); |
| 333 |
assert.strictEqual( surface.getStagingTransactions(), undefined, 'getStagingTransactions undefined when not staging' ); |
| 334 |
assert.strictEqual( surface.doesStagingAllowUndo(), undefined, 'doesStagingAllowUndo undefined when not staging' ); |
| 335 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Surface range matches fragment range' ); |
| 336 |
|
| 337 |
surface.change( ve.dm.TransactionBuilder.static.newFromInsertion( doc, 1, [ 'a' ] ) ); |
| 338 |
|
| 339 |
surface.pushStaging(); |
| 340 |
assert.strictEqual( surface.isStaging(), true, 'isStaging true after pushStaging' ); |
| 341 |
assert.deepEqual( surface.getStagingTransactions(), [], 'getStagingTransactions empty array after pushStaging' ); |
| 342 |
assert.strictEqual( surface.doesStagingAllowUndo(), false, 'doesStagingAllowUndo false when staging without undo' ); |
| 343 |
|
| 344 |
let tx1 = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 2, [ 'b' ] ); |
| 345 |
surface.change( tx1 ); |
| 346 |
|
| 347 |
assert.strictEqual( fragment.getText(), 'abhi', 'document contents match after first transaction' ); |
| 348 |
assert.deepEqual( surface.getStagingTransactions(), [ tx1 ], 'getStagingTransactions contains first transaction after change' ); |
| 349 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Surface selection matches fragment range' ); |
| 350 |
|
| 351 |
surface.pushStaging( true ); |
| 352 |
assert.strictEqual( surface.isStaging(), true, 'isStaging true after nested pushStaging' ); |
| 353 |
assert.deepEqual( surface.getStagingTransactions(), [], 'getStagingTransactions empty array after nested pushStaging' ); |
| 354 |
assert.strictEqual( surface.doesStagingAllowUndo(), true, 'doesStagingAllowUndo true when staging with undo' ); |
| 355 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Surface selection matches fragment range' ); |
| 356 |
|
| 357 |
let tx2 = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 3, [ 'c' ] ); |
| 358 |
surface.change( tx2 ); |
| 359 |
|
| 360 |
assert.strictEqual( fragment.getText(), 'abchi', 'document contents match after second transaction' ); |
| 361 |
assert.deepEqual( surface.getStagingTransactions(), [ tx2 ], 'getStagingTransactions contains second transaction after change in nested staging' ); |
| 362 |
|
| 363 |
assert.deepEqual( surface.popStaging(), [ tx2 ], 'popStaging returns second transaction list' ); |
| 364 |
assert.strictEqual( surface.isStaging(), true, 'isStaging true after nested popStaging' ); |
| 365 |
assert.strictEqual( fragment.getText(), 'abhi', 'document contents match after nested popStaging' ); |
| 366 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Surface selection matches fragment range' ); |
| 367 |
|
| 368 |
assert.deepEqual( surface.popStaging(), [ tx1 ], 'popStaging returns first transaction list' ); |
| 369 |
assert.strictEqual( surface.isStaging(), false, 'isStaging false after outer popStaging' ); |
| 370 |
assert.strictEqual( fragment.getText(), 'ahi', 'document contents match after outer popStaging' ); |
| 371 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Surface selection matches fragment range' ); |
| 372 |
|
| 373 |
surface.pushStaging(); |
| 374 |
tx1 = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 2, [ 'b' ] ); |
| 375 |
surface.change( tx1 ); |
| 376 |
|
| 377 |
surface.pushStaging(); |
| 378 |
tx2 = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 3, [ 'c' ] ); |
| 379 |
surface.change( tx2 ); |
| 380 |
|
| 381 |
assert.deepEqual( surface.popAllStaging(), [ tx1, tx2 ], 'popAllStaging returns full transaction list' ); |
| 382 |
assert.strictEqual( fragment.getText(), 'ahi', 'document contents match after outer clearStaging' ); |
| 383 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Surface selection matches fragment range' ); |
| 384 |
|
| 385 |
surface.pushStaging(); |
| 386 |
tx1 = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 2, [ 'b' ] ); |
| 387 |
surface.change( tx1 ); |
| 388 |
|
| 389 |
surface.pushStaging(); |
| 390 |
tx2 = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 3, [ 'c' ] ); |
| 391 |
surface.change( tx2 ); |
| 392 |
|
| 393 |
surface.applyStaging(); |
| 394 |
assert.deepEqual( surface.getStagingTransactions(), [ tx1, tx2 ], 'applyStaging merges transactions' ); |
| 395 |
|
| 396 |
surface.applyStaging(); |
| 397 |
assert.strictEqual( surface.isStaging(), false, 'isStaging false after outer applyStaging' ); |
| 398 |
assert.strictEqual( fragment.getText(), 'abchi', 'document contents changed after applyStaging' ); |
| 399 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Surface selection matches fragment range' ); |
| 400 |
|
| 401 |
surface.pushStaging(); |
| 402 |
tx1 = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 4, [ 'd' ] ); |
| 403 |
surface.change( tx1 ); |
| 404 |
|
| 405 |
surface.pushStaging(); |
| 406 |
tx2 = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 5, [ 'e' ] ); |
| 407 |
surface.change( tx2 ); |
| 408 |
|
| 409 |
surface.applyAllStaging(); |
| 410 |
assert.strictEqual( surface.isStaging(), false, 'isStaging false after outer applyAllStaging' ); |
| 411 |
assert.strictEqual( fragment.getText(), 'abcdehi', 'document contents changed after applyAllStaging' ); |
| 412 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Surface selection matches fragment range' ); |
| 413 |
|
| 414 |
surface.undo(); |
| 415 |
assert.strictEqual( fragment.getText(), 'abchi', 'document contents changed after undo' ); |
| 416 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Surface selection matches fragment range' ); |
| 417 |
|
| 418 |
surface.pushStaging(); |
| 419 |
surface.pushStaging(); |
| 420 |
|
| 421 |
tx1 = ve.dm.TransactionBuilder.static.newFromInsertion( doc, 4, [ 'd' ] ); |
| 422 |
surface.change( tx1 ); |
| 423 |
surface.applyAllStaging(); |
| 424 |
|
| 425 |
surface.setLinearSelection( new ve.Range( 1 ) ); |
| 426 |
|
| 427 |
surface.undo(); |
| 428 |
|
| 429 |
assert.equalHash( surface.getSelection(), fragment.getSelection(), 'Surface selection matches fragment range' ); |
| 430 |
|
| 431 |
} ); |
| 432 |
|
| 433 |
QUnit.test( 'getOffsetFromSourceOffset / getSourceOffsetFromOffset / getRangeFromSourceOffsets', ( assert ) => { |
| 434 |
const surface = new ve.dm.SurfaceStub( [ |
| 435 |
{ type: 'paragraph' }, ...'Foo', { type: '/paragraph' }, |
| 436 |
{ type: 'paragraph' }, ...'ba', { type: '/paragraph' }, |
| 437 |
{ type: 'paragraph' }, ...'Quux', { type: '/paragraph' }, |
| 438 |
{ type: 'internalList' }, { type: '/internalList' } |
| 439 |
] ), |
| 440 |
expectedOffsets = [ |
| 441 |
1, 2, 3, 4, |
| 442 |
6, 7, 8, |
| 443 |
10, 11, 12, 13, 14 |
| 444 |
], |
| 445 |
expectedSourceOffsets = [ |
| 446 |
0, 0, 1, 2, 3, |
| 447 |
4, 4, 5, 6, |
| 448 |
7, 7, 8, 9, 10, 11, |
| 449 |
12 |
| 450 |
]; |
| 451 |
|
| 452 |
for ( let i = 0; i < expectedOffsets.length; i++ ) { |
| 453 |
assert.strictEqual( surface.getOffsetFromSourceOffset( i ), expectedOffsets[ i ], 'Correct offset at ' + i ); |
| 454 |
} |
| 455 |
assert.throws( |
| 456 |
() => { |
| 457 |
surface.getOffsetFromSourceOffset( -1 ); |
| 458 |
}, |
| 459 |
Error, 'Offset -1 is out of bounds' ); |
| 460 |
assert.throws( |
| 461 |
() => { |
| 462 |
surface.getOffsetFromSourceOffset( expectedOffsets.length ); |
| 463 |
}, |
| 464 |
Error, 'Offset ' + expectedOffsets.length + ' is out of bounds' |
| 465 |
); |
| 466 |
for ( let i = 0; i < expectedSourceOffsets.length; i++ ) { |
| 467 |
assert.strictEqual( surface.getSourceOffsetFromOffset( i ), expectedSourceOffsets[ i ], 'Correct source offset at ' + i ); |
| 468 |
} |
| 469 |
assert.throws( |
| 470 |
() => { |
| 471 |
surface.getSourceOffsetFromOffset( -1 ); |
| 472 |
}, |
| 473 |
Error, 'Offset -1 is out of bounds' ); |
| 474 |
assert.throws( |
| 475 |
() => { |
| 476 |
surface.getSourceOffsetFromOffset( expectedSourceOffsets.length ); |
| 477 |
}, |
| 478 |
Error, 'Offset ' + expectedSourceOffsets.length + ' is out of bounds' |
| 479 |
); |
| 480 |
|
| 481 |
assert.equalRange( surface.getRangeFromSourceOffsets( 1, 5 ), new ve.Range( 2, 7 ), 'Simple forwards range' ); |
| 482 |
assert.equalRange( surface.getRangeFromSourceOffsets( 6, 2 ), new ve.Range( 8, 3 ), 'Simple backwards range' ); |
| 483 |
assert.equalRange( surface.getRangeFromSourceOffsets( 7, 7 ), new ve.Range( 10 ), 'Collapsed range (2 args)' ); |
| 484 |
assert.equalRange( surface.getRangeFromSourceOffsets( 8 ), new ve.Range( 11 ), 'Collapsed range (1 arg)' ); |
| 485 |
} ); |
| 486 |
|
| 487 |
QUnit.test( 'autosave', function ( assert ) { |
| 488 |
const storage = ve.init.platform.sessionStorage, |
| 489 |
done = assert.async(), |
| 490 |
state = { |
| 491 |
name: 'name', |
| 492 |
id: 1 |
| 493 |
}; |
| 494 |
|
| 495 |
let surface = new ve.dm.SurfaceStub(), |
| 496 |
fragment = surface.getLinearFragment( new ve.Range( 3 ) ), |
| 497 |
autosaveFailed = 0; |
| 498 |
|
| 499 |
assert.strictEqual( surface.restoreChanges(), false, 'restoreChanges returns false when nothing to restore' ); |
| 500 |
assert.strictEqual( surface.storeDocState( state, '<p>foo</p>' ), true, 'storeDocState returns true' ); |
| 501 |
assert.deepEqual( storage.getObject( 've-docstate' ), state, 'storeDocState writes doc state to session storage' ); |
| 502 |
assert.strictEqual( storage.get( 've-dochtml' ), '<p>foo</p>', 'storeDocState writes custom HTML to session storage' ); |
| 503 |
surface.storeDocState( state, '' ); |
| 504 |
assert.strictEqual( storage.get( 've-dochtml' ), '', 'storeDocState can set HTML to empty string' ); |
| 505 |
surface.storeDocState(); |
| 506 |
assert.strictEqual( storage.get( 've-dochtml' ), '<p>hi</p>', 'storeDocState writes current HTML to session storage' ); |
| 507 |
assert.strictEqual( storage.getObject( 've-docstate' ), null, 'docstate is empty if not provided' ); |
| 508 |
|
| 509 |
fragment.insertContent( ' bar' ); |
| 510 |
surface.breakpoint(); |
| 511 |
assert.deepEqual( storage.getObject( 've-changes' ), null, 'Changes aren\'t stored before startStoringChanges' ); |
| 512 |
|
| 513 |
surface = new ve.dm.SurfaceStub(); |
| 514 |
fragment = surface.getLinearFragment( new ve.Range( 3 ) ); |
| 515 |
surface.startStoringChanges(); |
| 516 |
surface.storeDocState( state ); |
| 517 |
fragment.insertContent( ' bar' ); |
| 518 |
surface.breakpoint(); |
| 519 |
assert.deepEqual( |
| 520 |
storage.getObject( 've-changes' ), |
| 521 |
[ { |
| 522 |
start: 0, |
| 523 |
transactions: [ [ 3, [ '', ' bar' ], 3 ] ] |
| 524 |
} ], |
| 525 |
'First change stored' |
| 526 |
); |
| 527 |
|
| 528 |
surface.storeChanges(); |
| 529 |
assert.strictEqual( storage.getObject( 've-changes' ).length, 1, 'No extra change stored if no changes since last store' ); |
| 530 |
|
| 531 |
fragment.convertNodes( 'heading', { level: 1 } ); |
| 532 |
surface.breakpoint(); |
| 533 |
assert.deepEqual( |
| 534 |
storage.getObject( 've-changes' )[ 1 ], |
| 535 |
{ |
| 536 |
start: 1, |
| 537 |
transactions: [ [ |
| 538 |
[ [ { type: 'paragraph' } ], [ { type: 'heading', attributes: { level: 1 } } ] ], |
| 539 |
6, |
| 540 |
[ [ { type: '/paragraph' } ], [ { type: '/heading' } ] ], |
| 541 |
2 |
| 542 |
] ] |
| 543 |
}, |
| 544 |
'Second change stored' |
| 545 |
); |
| 546 |
fragment.collapseToEnd().insertContent( ' baz' ); |
| 547 |
surface.setLinearSelection( new ve.Range( 5 ) ); |
| 548 |
surface.breakpoint(); |
| 549 |
assert.strictEqual( |
| 550 |
storage.getObject( 've-changes' ).length, 3, 'Fourth change stored' |
| 551 |
); |
| 552 |
assert.deepEqual( |
| 553 |
storage.getObject( 've-selection' ), |
| 554 |
{ type: 'linear', range: { type: 'range', from: 5, to: 5 } }, |
| 555 |
'Selection state stored' |
| 556 |
); |
| 557 |
|
| 558 |
surface.stopStoringChanges(); |
| 559 |
fragment.collapseToEnd().insertContent( ' quux' ); |
| 560 |
surface.breakpoint(); |
| 561 |
assert.strictEqual( |
| 562 |
storage.getObject( 've-changes' ).length, 3, 'Change not stored after stopStoringChanges' |
| 563 |
); |
| 564 |
|
| 565 |
assert.throws( |
| 566 |
() => { |
| 567 |
surface.documentModel.completeHistory = new ve.dm.Change(); |
| 568 |
surface.restoreChanges(); |
| 569 |
}, |
| 570 |
/Failed to restore/, |
| 571 |
'Calling restoreChanges on the wrong document state throws "Failed to restoreā¦" error' |
| 572 |
); |
| 573 |
|
| 574 |
surface = new ve.dm.SurfaceStub(); |
| 575 |
fragment = null; |
| 576 |
assert.strictEqual( surface.getHtml(), '<p>hi</p>', 'Document HTML before restoreChanges' ); |
| 577 |
assert.strictEqual( surface.restoreChanges(), true, 'restoreChanges returns true on success' ); |
| 578 |
assert.strictEqual( surface.getHtml(), '<h1>hi bar baz</h1>', 'Document HTML restored' ); |
| 579 |
assert.strictEqual( surface.getDocument().getCompleteHistoryLength(), 3, 'Document history restored' ); |
| 580 |
setTimeout( ( ( s ) => { |
| 581 |
assert.equalHash( s.getSelection(), { type: 'linear', range: { type: 'range', from: 5, to: 5 } }, 'Document selection restored (async)' ); |
| 582 |
s.undo(); |
| 583 |
assert.equalHash( s.getSelection(), { type: 'linear', range: { type: 'range', from: 7, to: 7 } }, 'Document selection guessed after undo' ); |
| 584 |
done(); |
| 585 |
} ).bind( this, surface ) ); |
| 586 |
|
| 587 |
ve.init.platform.storageDisabled = true; |
| 588 |
assert.strictEqual( surface.restoreChanges(), false, 'restoreChanges returns false if session storage disabled' ); |
| 589 |
ve.init.platform.storageDisabled = false; |
| 590 |
|
| 591 |
surface.removeDocStateAndChanges(); |
| 592 |
assert.strictEqual( storage.get( 've-html' ), null, 'HTML empty after removeDocStateAndChanges' ); |
| 593 |
assert.strictEqual( storage.getObject( 've-docstate' ), null, 'Doc state empty after removeDocStateAndChanges' ); |
| 594 |
assert.deepEqual( storage.getObject( 've-changes' ), null, 'Changes empty after removeDocStateAndChanges' ); |
| 595 |
|
| 596 |
surface = new ve.dm.SurfaceStub(); |
| 597 |
fragment = surface.getLinearFragment( new ve.Range( 3 ) ); |
| 598 |
surface.startStoringChanges(); |
| 599 |
|
| 600 |
assert.strictEqual( surface.storeDocState( state, '__FAIL__' ), false, 'storeDocState returns false when HTML can\'t be stored' ); |
| 601 |
assert.strictEqual( storage.getObject( 've-docstate' ), null, 'docstate is wiped if HTML storage failed' ); |
| 602 |
|
| 603 |
ve.init.platform.storageDisabled = true; |
| 604 |
surface = new ve.dm.SurfaceStub(); |
| 605 |
fragment = surface.getLinearFragment( new ve.Range( 3 ) ); |
| 606 |
surface.startStoringChanges(); |
| 607 |
assert.strictEqual( surface.storeDocState( state ), false, 'storeDocState returns false when sessionStorage disabled' ); |
| 608 |
fragment.insertContent( ' bar' ); |
| 609 |
surface.breakpoint(); |
| 610 |
assert.strictEqual( storage.getObject( 've-changes' ), false, 'No changes recorded' ); |
| 611 |
ve.init.platform.storageDisabled = false; |
| 612 |
|
| 613 |
surface.on( 'autosaveFailed', () => { |
| 614 |
autosaveFailed++; |
| 615 |
} ); |
| 616 |
surface.startStoringChanges(); |
| 617 |
surface.storeDocState( state ); |
| 618 |
ve.init.platform.storageDisabled = true; |
| 619 |
assert.strictEqual( autosaveFailed, 0, 'Autosave hasn\'t failed before first change' ); |
| 620 |
fragment.insertContent( ' bar' ); |
| 621 |
surface.breakpoint(); |
| 622 |
assert.strictEqual( autosaveFailed, 1, 'Autosave fails after first change' ); |
| 623 |
fragment.insertContent( ' baz' ); |
| 624 |
surface.breakpoint(); |
| 625 |
assert.strictEqual( autosaveFailed, 1, 'Subsequent failures don\'t fire autosaveFailed again' ); |
| 626 |
assert.strictEqual( storage.getObject( 've-changes' ), false, 'No changes recorded' ); |
| 627 |
ve.init.platform.storageDisabled = false; |
| 628 |
|
| 629 |
surface.storeDocState( state, '<p>foo</p>' ); |
| 630 |
|
| 631 |
assert.strictEqual( surface.lastStoredChange, 1, 'storeDocState with custom HTML doesn\'t advance the lastStoredChange pointer' ); |
| 632 |
surface.storeDocState( state ); |
| 633 |
assert.strictEqual( surface.lastStoredChange, 5, 'storeDocState without custom HTML advances the lastStoredChange pointer' ); |
| 634 |
|
| 635 |
} ); |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|