/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /* bender-tags: browser-only */ 'use strict'; import Editor from '/ckeditor5/editor.js'; import ModelDocument from '/ckeditor5/engine/model/document.js'; import Range from '/ckeditor5/engine/model/range.js'; import Position from '/ckeditor5/engine/model/position.js'; import Undo from '/ckeditor5/undo/undo.js'; import Creator from '/ckeditor5/creator/creator.js'; import { setData, getData } from '/tests/engine/_utils/model.js'; // import deleteContents from '/ckeditor5/engine/model/composer/deletecontents.js'; let element, editor, doc, root; beforeEach( () => { element = document.createElement( 'div' ); document.body.appendChild( element ); doc = new ModelDocument(); root = doc.createRoot( 'root' ); editor = new Editor( element, { creator: Creator, features: [ Undo ] } ); editor.document = doc; return editor.init(); } ); function setSelection( pathA, pathB ) { doc.selection.setRanges( [ new Range( new Position( root, pathA ), new Position( root, pathB ) ) ] ); } function input( input ) { setData( doc, input, { rootName: 'root' } ); } function output( output ) { expect( getData( doc, { rootName: 'root' } ) ).to.equal( output ); } function undoDisabled() { expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false; } describe( 'undo integration', () => { describe( 'adding and removing content', () => { it( 'add and undo', () => { input( '
fo
bar
' ); doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' ); output( 'fozzz
bar
' ); editor.execute( 'undo' ); output( 'fo
bar
' ); undoDisabled(); } ); it( 'multiple adding and undo', () => { input( 'fo
bar
' ); doc.batch() .insert( doc.selection.getFirstPosition(), 'zzz' ) .insert( new Position( root, [ 1, 0 ] ), 'xxx' ); output( 'fozzz
xxxbar
' ); setSelection( [ 1, 0 ], [ 1, 0 ] ); doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' ); output( 'fozzzo
yyy
fozzzo
fo
bar
' ); undoDisabled(); } ); it( 'multiple adding mixed with undo', () => { input( 'fo
bar
' ); doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' ); output( 'fozzz
bar
' ); setSelection( [ 1, 0 ], [ 1, 0 ] ); doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' ); output( 'fozzzo
yyy
fozzzo
xxx
bar
' ); editor.execute( 'undo' ); output( 'bar
' ); editor.execute( 'undo' ); output( 'fo
bar
' ); undoDisabled(); } ); it( 'multiple remove and undo', () => { input( 'bar
' ); doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) ); output( 'bar
' ); setSelection( [ 1, 1 ], [ 1, 1 ] ); doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) ); output( 'o
b
o
bar
fo
bar
' ); undoDisabled(); } ); it( 'add and remove different parts and undo', () => { input( 'fo
bar
' ); doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' ); output( 'fozzz
bar
' ); setSelection( [ 1, 2 ], [ 1, 2 ] ); doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 1, 1 ] ) , 1 ) ); output( 'fozzzo
b
fozzzo
ba
fo
bar
' ); undoDisabled(); } ); //it( 'add and remove same part and undo', () => { // // This test case fails because some operations are transformed to NoOperations incorrectly. // input( 'fo
bar
' ); // // doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' ); // output( 'fozzz
bar
' ); // // doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ) , 3 ) ); // output( 'fo
bar
' ); // // editor.execute( 'undo' ); // output( 'fozzz
bar
' ); // // editor.execute( 'undo' ); // output( 'fo
bar
' ); // // undoDisabled(); //} ); } ); describe( 'moving', () => { //it( 'move same content twice then undo', () => { // // This test case fails because some operations are transformed to NoOperations incorrectly. // input( 'f
bar
' ); // // doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) ); // output( 'fz
fz
bar
' ); // // editor.execute( 'undo' ); // output( 'fz
f
bar
' ); // // undoDisabled(); //} ); it( 'move content and new parent then undo', () => { input( 'f
bar
' ); doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) ); output( 'fz
obar
fz
' ); editor.execute( 'undo' ); output( 'fz
obar
f
bar
' ); undoDisabled(); } ); } ); describe( 'attributes with other', () => { it( 'attributes then insert inside then undo', () => { input( 'fo
fo
fo<$text bold=true>o$text>zzz
fo<$text bold=true>o
fo
b
ar' ); // // editor.execute( 'undo' ); // output( 'fofoo
foo
foo
element and ends up in graveyard. // // AssertionError: expected '
foobar
' to equal 'foo
foo
but when // // "bar" is inserted, it gets moved to the right. // // AssertionError: expected '
foo
bar
foo
foo
foo
foo
bar
' to equal 'foo
foo
foobar
foo
foo
fo
bar
' ); // // deleteContents( doc.batch(), doc.selection, { merge: true } ); // output( 'fo
. // editor.execute( 'undo' ); // output( '
fo
bar
' ); //} ); } ); } );