| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /* global document */
- import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
- import UndoEditing from '../src/undoediting';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
- import Typing from '@ckeditor/ckeditor5-typing/src/typing';
- import Enter from '@ckeditor/ckeditor5-enter/src/enter';
- import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
- import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
- import TableEditing from '@ckeditor/ckeditor5-table/src/tableediting';
- import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- describe( 'UndoEditing integration', () => {
- let editor, model, doc, root, div;
- beforeEach( () => {
- div = document.createElement( 'div' );
- document.body.appendChild( div );
- return ClassicEditor.create( div, {
- plugins: [ Paragraph, HeadingEditing, Typing, Enter, Clipboard, BoldEditing, UndoEditing, TableEditing ]
- } ).then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- root = doc.getRoot();
- } );
- } );
- afterEach( () => {
- div.remove();
- return editor.destroy();
- } );
- function setSelection( pathA, pathB ) {
- model.change( writer => {
- writer.setSelection( writer.createRange(
- writer.createPositionFromPath( root, pathA ), writer.createPositionFromPath( root, pathB )
- ) );
- } );
- }
- function input( input ) {
- model.enqueueChange( 'transparent', () => {
- setData( model, input );
- } );
- }
- function output( output ) {
- expect( getData( model ) ).to.equal( output );
- }
- function undoDisabled() {
- expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
- }
- function redoDisabled() {
- expect( editor.commands.get( 'redo' ).isEnabled ).to.be.false;
- }
- describe( 'adding and removing content', () => {
- it( 'add and undo', () => {
- input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- writer.insertText( 'zzz', doc.selection.getFirstPosition() );
- } );
- output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- undoDisabled();
- } );
- it( 'multiple adding and undo', () => {
- input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- writer.insertText( 'zzz', doc.selection.getFirstPosition() );
- writer.insertText( 'xxx', writer.createPositionFromPath( root, [ 1, 0 ] ) );
- } );
- output( '<paragraph>fozzz[]o</paragraph><paragraph>xxxbar</paragraph>' );
- model.change( writer => {
- setSelection( [ 1, 0 ], [ 1, 0 ] );
- writer.insertText( 'yyy', doc.selection.getFirstPosition() );
- } );
- output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]xxxbar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fozzzo</paragraph><paragraph>[]xxxbar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- undoDisabled();
- } );
- it( 'multiple adding mixed with undo', () => {
- input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- writer.insertText( 'zzz', doc.selection.getFirstPosition() );
- } );
- output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- setSelection( [ 1, 0 ], [ 1, 0 ] );
- writer.insertText( 'yyy', doc.selection.getFirstPosition() );
- } );
- output( '<paragraph>fozzzo</paragraph><paragraph>yyy[]bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fozzzo</paragraph><paragraph>[]bar</paragraph>' );
- model.change( writer => {
- setSelection( [ 0, 0 ], [ 0, 0 ] );
- writer.insertText( 'xxx', doc.selection.getFirstPosition() );
- } );
- output( '<paragraph>xxx[]fozzzo</paragraph><paragraph>bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>[]fozzzo</paragraph><paragraph>bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- undoDisabled();
- } );
- it( 'multiple remove and undo', () => {
- input( '<paragraph>[]foo</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- const start = doc.selection.getFirstPosition();
- writer.remove( writer.createRange( start, start.getShiftedBy( 2 ) ) );
- } );
- output( '<paragraph>[]o</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- setSelection( [ 1, 1 ], [ 1, 1 ] );
- const start = doc.selection.getFirstPosition();
- writer.remove( writer.createRange( start, start.getShiftedBy( 2 ) ) );
- } );
- output( '<paragraph>o</paragraph><paragraph>b[]</paragraph>' );
- editor.execute( 'undo' );
- // Here is an edge case that selection could be before or after `ar`.
- output( '<paragraph>o</paragraph><paragraph>bar[]</paragraph>' );
- editor.execute( 'undo' );
- // As above.
- output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- undoDisabled();
- } );
- it( 'add and remove different parts and undo', () => {
- input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- writer.insertText( 'zzz', doc.selection.getFirstPosition() );
- } );
- output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- setSelection( [ 1, 2 ], [ 1, 2 ] );
- const start = writer.createPositionFromPath( root, [ 1, 1 ] );
- writer.remove( writer.createRange( start, start.getShiftedBy( 1 ) ) );
- } );
- output( '<paragraph>fozzzo</paragraph><paragraph>b[]r</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fozzzo</paragraph><paragraph>ba[]r</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- undoDisabled();
- } );
- it( 'add and remove same part and undo', () => {
- input( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- writer.insertText( 'zzz', doc.selection.getFirstPosition() );
- } );
- output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- const start = writer.createPositionFromPath( root, [ 0, 2 ] );
- writer.remove( writer.createRange( start, start.getShiftedBy( 3 ) ) );
- } );
- output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
- undoDisabled();
- } );
- it( 'undo remove all content', () => {
- input( '<paragraph>foo[]</paragraph>' );
- model.change( writer => {
- writer.remove( writer.createRangeIn( root ) );
- } );
- output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!
- editor.execute( 'undo' );
- output( '<paragraph>foo[]</paragraph>' );
- undoDisabled();
- } );
- it( 'undo insert first content', () => {
- input( '' );
- output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!
- model.change( writer => {
- writer.remove( writer.createRangeIn( root ) );
- writer.insertElement( 'heading1', doc.selection.getFirstPosition() );
- } );
- output( '<heading1>[]</heading1>' );
- editor.execute( 'undo' );
- output( '<paragraph>[]</paragraph>' );
- undoDisabled();
- } );
- // #72.
- it( 'undo paste multiple elements simulation', () => {
- input( '<paragraph></paragraph>' );
- const p = root.getChild( 0 );
- const pos = model.createPositionFromPath( root, [ 0 ] );
- model.change( writer => {
- writer.remove( p );
- writer.insertElement( 'heading1', pos );
- writer.insertElement( 'heading2', pos.getShiftedBy( 1 ) );
- } );
- output( '<heading1>[]</heading1><heading2></heading2>' );
- editor.execute( 'undo' );
- output( '<paragraph>[]</paragraph>' );
- undoDisabled();
- } );
- } );
- describe( 'moving', () => {
- it( 'move same content twice then undo', () => {
- input( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( root, [ 1, 0 ] ) );
- } );
- output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
- model.change( writer => {
- writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( root, [ 0, 2 ] ) );
- } );
- output( '<paragraph>fz[o]</paragraph><paragraph>bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
- undoDisabled();
- } );
- it( 'move content and new parent then undo', () => {
- input( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
- model.change( writer => {
- writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( root, [ 1, 0 ] ) );
- } );
- output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
- model.change( writer => {
- setSelection( [ 1 ], [ 2 ] );
- writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( root, [ 0 ] ) );
- } );
- output( '<paragraph>[obar]</paragraph><paragraph>fz</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fz</paragraph><paragraph>[obar]</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
- undoDisabled();
- } );
- } );
- describe( 'attributes with other', () => {
- it( 'attributes then insert inside then undo', () => {
- input( '<paragraph>fo[ob]ar</paragraph>' );
- model.change( writer => {
- writer.setAttribute( 'bold', true, doc.selection.getFirstRange() );
- } );
- output( '<paragraph>fo[<$text bold="true">ob</$text>]ar</paragraph>' );
- expect( doc.selection.getAttribute( 'bold' ) ).to.be.true;
- model.change( writer => {
- setSelection( [ 0, 3 ], [ 0, 3 ] );
- writer.insertText( 'zzz', doc.selection.getFirstPosition() );
- } );
- output( '<paragraph>fo<$text bold="true">o</$text>zzz[]<$text bold="true">b</$text>ar</paragraph>' );
- expect( doc.selection.getAttribute( 'bold' ) ).to.be.undefined;
- editor.execute( 'undo' );
- output( '<paragraph>fo<$text bold="true">o[]b</$text>ar</paragraph>' );
- expect( doc.selection.getAttribute( 'bold' ) ).to.be.true;
- editor.execute( 'undo' );
- output( '<paragraph>fo[ob]ar</paragraph>' );
- expect( doc.selection.getAttribute( 'bold' ) ).to.be.undefined;
- undoDisabled();
- } );
- } );
- describe( 'wrapping, unwrapping, merging, splitting', () => {
- it( 'wrap and undo', () => {
- model.schema.extend( '$text', { allowIn: '$root' } );
- input( 'fo[zb]ar' );
- model.change( writer => {
- writer.wrap( doc.selection.getFirstRange(), 'paragraph' );
- } );
- output( 'fo<paragraph>[zb]</paragraph>ar' );
- editor.execute( 'undo' );
- output( 'fo[zb]ar' );
- undoDisabled();
- } );
- it( 'wrap, move and undo', () => {
- model.schema.extend( '$text', { allowIn: '$root' } );
- input( 'fo[zb]ar' );
- model.change( writer => {
- writer.wrap( doc.selection.getFirstRange(), 'paragraph' );
- } );
- // Would be better if selection was inside P.
- output( 'fo<paragraph>[zb]</paragraph>ar' );
- model.change( writer => {
- setSelection( [ 2, 0 ], [ 2, 1 ] );
- writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( root, [ 0 ] ) );
- } );
- output( '[z]fo<paragraph>b</paragraph>ar' );
- editor.execute( 'undo' );
- output( 'fo<paragraph>[z]b</paragraph>ar' );
- editor.execute( 'undo' );
- output( 'fo[zb]ar' );
- undoDisabled();
- } );
- it( 'unwrap and undo', () => {
- input( '<paragraph>foo[]bar</paragraph>' );
- model.change( writer => {
- writer.unwrap( doc.selection.getFirstPosition().parent );
- } );
- output( 'foo[]bar' );
- editor.execute( 'undo' );
- output( '<paragraph>foo[]bar</paragraph>' );
- undoDisabled();
- } );
- it( 'merge and undo', () => {
- input( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
- model.change( writer => {
- writer.merge( writer.createPositionFromPath( root, [ 1 ] ) );
- } );
- output( '<paragraph>foo[]bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
- undoDisabled();
- } );
- it( 'split and undo', () => {
- input( '<paragraph>foo[]bar</paragraph>' );
- model.change( writer => {
- writer.split( doc.selection.getFirstPosition() );
- } );
- output( '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>foo[]bar</paragraph>' );
- undoDisabled();
- } );
- it.skip( 'move, merge and undo', () => {
- // Add a "div feature".
- model.schema.register( 'div', {
- allowWhere: '$block',
- allowContentOf: '$root'
- } );
- editor.conversion.for( 'downcast' ).elementToElement( { model: 'div', view: 'div' } );
- editor.conversion.for( 'upcast' ).elementToElement( { model: 'div', view: 'div' } );
- input(
- '<div><paragraph>[]00.</paragraph><paragraph>01.</paragraph><paragraph>02.</paragraph></div>' +
- '<div><paragraph>10.</paragraph><paragraph>11.</paragraph><paragraph>12.</paragraph></div>'
- );
- const elements = [
- root.getNodeByPath( [ 0, 1 ] ),
- root.getNodeByPath( [ 1, 0 ] ),
- root.getNodeByPath( [ 1, 1 ] )
- ];
- model.change( writer => {
- for ( let i = elements.length - 1; i > 0; i-- ) {
- merge( elements[ i ], elements[ i - 1 ], writer );
- }
- } );
- output(
- '<div><paragraph>[]00.</paragraph><paragraph>01.10.11.</paragraph><paragraph>02.</paragraph></div>' +
- '<div><paragraph>12.</paragraph></div>'
- );
- editor.execute( 'undo' );
- output(
- '<div><paragraph>[]00.</paragraph><paragraph>01.</paragraph><paragraph>02.</paragraph></div>' +
- '<div><paragraph>10.</paragraph><paragraph>11.</paragraph><paragraph>12.</paragraph></div>'
- );
- function merge( element, targetElement, writer ) {
- const positionAfterTarget = writer.createPositionAfter( targetElement );
- const positionBeforeElement = writer.createPositionBefore( element );
- if ( !positionAfterTarget.isEqual( positionBeforeElement ) ) {
- writer.move( writer.createRangeOn( element ), positionAfterTarget );
- }
- writer.merge( positionAfterTarget );
- }
- } );
- } );
- // Restoring selection in those examples may be completely off.
- describe( 'multiple enters, deletes and typing', () => {
- it( 'split, split, split', () => {
- input( '<paragraph>12345678</paragraph>' );
- split( [ 0, 3 ] );
- output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
- split( [ 1, 4 ] );
- output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
- split( [ 1, 2 ] );
- output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123</paragraph><paragraph>45[]67</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]45678</paragraph>' );
- undoDisabled();
- editor.execute( 'redo' );
- output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
- redoDisabled();
- } );
- it( 'merge, merge, merge', () => {
- input( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
- merge( [ 2 ] );
- output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>12345[]678</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>12345[]</paragraph><paragraph>678</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>12345</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
- undoDisabled();
- editor.execute( 'redo' );
- output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>12345[]678</paragraph>' );
- redoDisabled();
- } );
- it( 'split, merge, split, merge (same position)', () => {
- input( '<paragraph>12345678</paragraph>' );
- split( [ 0, 3 ] );
- output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>123[]45678</paragraph>' );
- split( [ 0, 3 ] );
- output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>123[]45678</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]45678</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]45678</paragraph>' );
- undoDisabled();
- editor.execute( 'redo' );
- output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123[]45678</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123[]45678</paragraph>' );
- redoDisabled();
- } );
- it( 'split, split, split, merge, merge, merge', () => {
- input( '<paragraph>12345678</paragraph>' );
- split( [ 0, 3 ] );
- output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
- split( [ 1, 4 ] );
- output( '<paragraph>123</paragraph><paragraph>4567</paragraph><paragraph>[]8</paragraph>' );
- split( [ 1, 2 ] );
- output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67</paragraph><paragraph>8</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
- merge( [ 2 ] );
- output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>12345[]678</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>12345[]</paragraph><paragraph>678</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>12345</paragraph><paragraph>67[]</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123</paragraph><paragraph>45[]67</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]45678</paragraph>' );
- undoDisabled();
- editor.execute( 'redo' );
- output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123</paragraph><paragraph>4567[]</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123[]45</paragraph><paragraph>67</paragraph><paragraph>8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>12345</paragraph><paragraph>67[]8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>12345[]678</paragraph>' );
- redoDisabled();
- } );
- it( 'split, split, merge, split, merge (different order)', () => {
- input( '<paragraph>12345678</paragraph>' );
- split( [ 0, 3 ] );
- output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
- split( [ 1, 2 ] );
- output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]678</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>123[]45</paragraph><paragraph>678</paragraph>' );
- split( [ 1, 1 ] );
- output( '<paragraph>12345</paragraph><paragraph>6</paragraph><paragraph>[]78</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>12345[]6</paragraph><paragraph>78</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>12345[]</paragraph><paragraph>6</paragraph><paragraph>78</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>12345</paragraph><paragraph>6[]78</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>678</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123</paragraph><paragraph>45[]678</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]45678</paragraph>' );
- undoDisabled();
- editor.execute( 'redo' );
- output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>678</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123[]45</paragraph><paragraph>678</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>12345</paragraph><paragraph>6[]</paragraph><paragraph>78</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>12345[]6</paragraph><paragraph>78</paragraph>' );
- redoDisabled();
- } );
- it( 'split, remove, split, merge, merge', () => {
- input( '<paragraph>12345678</paragraph>' );
- split( [ 0, 3 ] );
- output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
- remove( [ 1, 4 ] );
- remove( [ 1, 3 ] );
- output( '<paragraph>123</paragraph><paragraph>45[]8</paragraph>' );
- split( [ 1, 1 ] );
- output( '<paragraph>123</paragraph><paragraph>4</paragraph><paragraph>[]58</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>123[]4</paragraph><paragraph>58</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>1234[]58</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>1234[]</paragraph><paragraph>58</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]</paragraph><paragraph>4</paragraph><paragraph>58</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123</paragraph><paragraph>4[]58</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]45678</paragraph>' );
- undoDisabled();
- editor.execute( 'redo' );
- output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123</paragraph><paragraph>45[]8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123</paragraph><paragraph>4[]</paragraph><paragraph>58</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123[]4</paragraph><paragraph>58</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>1234[]58</paragraph>' );
- redoDisabled();
- } );
- it( 'split, typing, split, merge, merge', () => {
- input( '<paragraph>12345678</paragraph>' );
- split( [ 0, 3 ] );
- output( '<paragraph>123</paragraph><paragraph>[]45678</paragraph>' );
- type( [ 1, 4 ], 'x' );
- type( [ 1, 5 ], 'y' );
- output( '<paragraph>123</paragraph><paragraph>4567xy[]8</paragraph>' );
- split( [ 1, 2 ] );
- output( '<paragraph>123</paragraph><paragraph>45</paragraph><paragraph>[]67xy8</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>123[]45</paragraph><paragraph>67xy8</paragraph>' );
- merge( [ 1 ] );
- output( '<paragraph>12345[]67xy8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>12345[]</paragraph><paragraph>67xy8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]</paragraph><paragraph>45</paragraph><paragraph>67xy8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123</paragraph><paragraph>45[]67xy8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123</paragraph><paragraph>4567[]8</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>123[]45678</paragraph>' );
- undoDisabled();
- editor.execute( 'redo' );
- output( '<paragraph>123[]</paragraph><paragraph>45678</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123</paragraph><paragraph>4567xy[]8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123</paragraph><paragraph>45[]</paragraph><paragraph>67xy8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>123[]45</paragraph><paragraph>67xy8</paragraph>' );
- editor.execute( 'redo' );
- output( '<paragraph>12345[]67xy8</paragraph>' );
- redoDisabled();
- } );
- } );
- describe( 'other reported cases', () => {
- // ckeditor5-engine#t/1051
- it( 'rename leaks to other elements on undo #1', () => {
- input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
- model.change( writer => {
- writer.rename( root.getChild( 0 ), 'paragraph' );
- } );
- output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
- model.change( writer => {
- writer.split( writer.createPositionAt( root.getChild( 0 ), 1 ) );
- } );
- output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
- model.change( writer => {
- writer.merge( writer.createPositionAt( root, 2 ) );
- } );
- output( '<paragraph>[]F</paragraph><paragraph>ooBar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
- } );
- // Similar issue that bases on the same error as above, however here we first merge (above we first split).
- it( 'rename leaks to other elements on undo #2', () => {
- input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
- model.change( writer => {
- writer.rename( root.getChild( 0 ), 'heading2' );
- } );
- output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
- model.change( writer => {
- writer.merge( writer.createPositionAt( root, 1 ) );
- } );
- output( '<heading2>[]FooBar</heading2>' );
- editor.execute( 'undo' );
- output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
- } );
- // Reverse issue, this time first operation is merge and then rename.
- it( 'merge, rename, undo, undo is correct', () => {
- input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
- model.change( writer => {
- writer.merge( writer.createPositionAt( root, 1 ) );
- } );
- output( '<heading1>[]FooBar</heading1>' );
- model.change( writer => {
- writer.rename( root.getChild( 0 ), 'heading2' );
- } );
- output( '<heading2>[]FooBar</heading2>' );
- editor.execute( 'undo' );
- output( '<heading1>[]FooBar</heading1>' );
- editor.execute( 'undo' );
- output( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
- } );
- // ckeditor5-engine#t/1053
- it( 'wrap, split, undo, undo is correct', () => {
- // Add a "div feature".
- model.schema.register( 'div', {
- allowWhere: '$block',
- allowContentOf: '$root'
- } );
- editor.conversion.for( 'downcast' ).elementToElement( { model: 'div', view: 'div' } );
- editor.conversion.for( 'upcast' ).elementToElement( { model: 'div', view: 'div' } );
- input( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
- model.change( writer => {
- writer.wrap( writer.createRangeIn( root ), 'div' );
- } );
- output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
- model.change( writer => {
- writer.split( writer.createPositionFromPath( root, [ 0, 0, 1 ] ) );
- } );
- output( '<div><paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph></div>' );
- editor.execute( 'undo' );
- output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
- editor.execute( 'undo' );
- output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
- } );
- // ckeditor5-engine#t/1055
- it( 'selection attribute setting: split, bold, merge, undo, undo, undo', () => {
- input( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
- editor.execute( 'enter' );
- output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
- editor.execute( 'bold' );
- output(
- '<paragraph>Foo</paragraph>' +
- '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
- '<paragraph>Bar</paragraph>'
- );
- editor.execute( 'forwardDelete' );
- output( '<paragraph>Foo</paragraph><paragraph>[]Bar</paragraph>' );
- editor.execute( 'undo' );
- output(
- '<paragraph>Foo</paragraph>' +
- '<paragraph selection:bold="true"><$text bold="true">[]</$text></paragraph>' +
- '<paragraph>Bar</paragraph>'
- );
- editor.execute( 'undo' );
- output( '<paragraph>Foo</paragraph><paragraph>[]</paragraph><paragraph>Bar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>Foo[]</paragraph><paragraph>Bar</paragraph>' );
- } );
- // https://github.com/ckeditor/ckeditor5-undo/issues/65#issuecomment-323682195
- it( 'undoing split after the element created by split has been removed', () => {
- input( '<paragraph>Foo[]bar</paragraph>' );
- editor.execute( 'enter' );
- model.change( writer => {
- const range = writer.createRange(
- writer.createPositionFromPath( root, [ 0, 3 ] ),
- writer.createPositionFromPath( root, [ 1, 3 ] )
- );
- writer.setSelection( range );
- editor.execute( 'delete' );
- } );
- output( '<paragraph>Foo[]</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>Foo[</paragraph><paragraph>bar]</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>Foo[]bar</paragraph>' );
- } );
- } );
- describe( 'clipboard', () => {
- function pasteHtml( editor, html ) {
- editor.editing.view.document.fire( 'paste', {
- dataTransfer: createDataTransfer( { 'text/html': html } ),
- stopPropagation() {},
- preventDefault() {}
- } );
- }
- function createDataTransfer( data ) {
- return {
- getData( type ) {
- return data[ type ];
- },
- setData() {}
- };
- }
- // ckeditor5-engine#t/1065
- it( 'undo paste into non empty element should not throw and be correct', () => {
- model.change( () => {
- input( '<paragraph>Foo[]</paragraph>' );
- } );
- model.change( () => {
- pasteHtml( editor, '<p>a</p><p>b</p>' );
- } );
- output( '<paragraph>Fooa</paragraph><paragraph>b[]</paragraph>' );
- model.change( () => {
- pasteHtml( editor, '<p>c</p><p>d</p>' );
- } );
- output( '<paragraph>Fooa</paragraph><paragraph>bc</paragraph><paragraph>d[]</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>Fooa</paragraph><paragraph>b[]</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>Foo[]</paragraph>' );
- } );
- // ckeditor5#781
- it( 'cutting should not create empty undo step', () => {
- input( '<paragraph>Fo[oba]r</paragraph>' );
- editor.editing.view.document.fire( 'cut', {
- dataTransfer: createDataTransfer(),
- preventDefault() {},
- method: 'cut'
- } );
- editor.execute( 'undo' );
- output( '<paragraph>Fo[oba]r</paragraph>' );
- undoDisabled();
- } );
- } );
- describe( 'other edge cases', () => {
- it( 'deleteContent between two nodes', () => {
- input( '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
- editor.model.deleteContent( doc.selection );
- output( '<paragraph>fo[]ar</paragraph>' );
- editor.execute( 'undo' );
- output( '<paragraph>fo[o</paragraph><paragraph>b]ar</paragraph>' );
- } );
- // Related to ckeditor5-engine#891 and ckeditor5-list#51.
- it( 'change attribute of removed node then undo and redo', () => {
- input( '<paragraph></paragraph>' );
- const gy = doc.graveyard;
- const p = doc.getRoot().getChild( 0 );
- model.change( writer => {
- writer.remove( p );
- } );
- model.change( writer => {
- writer.setAttribute( 'bold', true, p );
- } );
- editor.execute( 'undo' );
- editor.execute( 'redo' );
- expect( p.root ).to.equal( gy );
- expect( p.getAttribute( 'bold' ) ).to.be.true;
- } );
- it( 'undo table cells merge', () => {
- input(
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>00</paragraph></tableCell>' +
- '[<tableCell><paragraph>01</paragraph></tableCell>]' +
- '[<tableCell><paragraph>02</paragraph></tableCell>]' +
- '<tableCell><paragraph>03</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>10</paragraph></tableCell>' +
- '[<tableCell><paragraph>11</paragraph></tableCell>]' +
- '[<tableCell><paragraph>12</paragraph></tableCell>]' +
- '<tableCell><paragraph>13</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>20</paragraph></tableCell>' +
- '<tableCell><paragraph>21</paragraph></tableCell>' +
- '<tableCell><paragraph>22</paragraph></tableCell>' +
- '<tableCell><paragraph>23</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- editor.execute( 'mergeTableCells' );
- output(
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>00</paragraph></tableCell>' +
- '<tableCell colspan="2" rowspan="2">' +
- '<paragraph>[01</paragraph>' +
- '<paragraph>02</paragraph>' +
- '<paragraph>11</paragraph>' +
- '<paragraph>12]</paragraph>' +
- '</tableCell>' +
- '<tableCell><paragraph>03</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>10</paragraph></tableCell>' +
- '<tableCell><paragraph>13</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>20</paragraph></tableCell>' +
- '<tableCell><paragraph>21</paragraph></tableCell>' +
- '<tableCell><paragraph>22</paragraph></tableCell>' +
- '<tableCell><paragraph>23</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- editor.execute( 'undo' );
- output(
- '<table>' +
- '<tableRow>' +
- '<tableCell><paragraph>00</paragraph></tableCell>' +
- '[<tableCell><paragraph>01</paragraph></tableCell>]' +
- '[<tableCell><paragraph>02</paragraph></tableCell>]' +
- '<tableCell><paragraph>03</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>10</paragraph></tableCell>' +
- '[<tableCell><paragraph>11</paragraph></tableCell>]' +
- '[<tableCell><paragraph>12</paragraph></tableCell>]' +
- '<tableCell><paragraph>13</paragraph></tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell><paragraph>20</paragraph></tableCell>' +
- '<tableCell><paragraph>21</paragraph></tableCell>' +
- '<tableCell><paragraph>22</paragraph></tableCell>' +
- '<tableCell><paragraph>23</paragraph></tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- } );
- } );
- it( 'postfixers should not add another undo step when fixing undo changes', () => {
- input( '<paragraph>[]</paragraph>' );
- const paragraph = root.getChild( 0 );
- // 1. Step - add text and marker to the paragraph.
- model.change( writer => {
- writer.appendText( 'foo', paragraph );
- writer.addMarker( 'marker', { range: writer.createRangeIn( paragraph ), usingOperation: true } );
- } );
- // 2. Step - remove text from paragraph.
- model.change( writer => {
- writer.remove( writer.createRangeIn( paragraph ) );
- } );
- // Register post fixer to remove marker from non-empty paragraph.
- model.document.registerPostFixer( writer => {
- const hasMarker = model.markers.has( 'marker' );
- const isEmpty = paragraph.childCount === 0;
- // Remove marker when paragraph is empty.
- if ( hasMarker && !isEmpty ) {
- writer.removeMarker( 'marker' );
- return true;
- }
- return false;
- } );
- editor.execute( 'undo' );
- // Check if postfixer changes are executed together with undo and there is no additional undo steps.
- expect( model.markers.has( 'marker' ) ).to.be.false;
- expect( editor.commands.get( 'undo' )._stack.length ).to.equal( 1 );
- output( '<paragraph>foo[]</paragraph>' );
- } );
- function split( path ) {
- setSelection( path.slice(), path.slice() );
- editor.execute( 'enter' );
- }
- function merge( path ) {
- const selPath = path.slice();
- selPath.push( 0 );
- setSelection( selPath, selPath.slice() );
- editor.execute( 'delete' );
- }
- function type( path, text ) {
- setSelection( path.slice(), path.slice() );
- editor.execute( 'input', { text } );
- }
- function remove( path ) {
- setSelection( path.slice(), path.slice() );
- editor.execute( 'delete' );
- }
- } );
|