| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
- import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import RestrictedEditingModeNavigationCommand from '../src/restrictededitingmodenavigationcommand';
- describe( 'RestrictedEditingModeNavigationCommand', () => {
- let editor, forwardCommand, backwardCommand, model;
- beforeEach( () => {
- return ModelTestEditor
- .create()
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- forwardCommand = new RestrictedEditingModeNavigationCommand( editor, 'forward' );
- backwardCommand = new RestrictedEditingModeNavigationCommand( editor, 'backward' );
- model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- editor.model.schema.extend( '$text', { allowAttributes: [ 'restrictedEditingException' ] } );
- } );
- } );
- afterEach( () => {
- forwardCommand.destroy();
- backwardCommand.destroy();
- return editor.destroy();
- } );
- describe( 'forward command', () => {
- describe( 'isEnabled', () => {
- describe( 'collapsed selection', () => {
- it( 'should be true when there is a marker after the selection position', () => {
- setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>[]foo <marker>bar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( forwardCommand.isEnabled ).to.be.true;
- } );
- it( 'should be false when there is no marker after the selection position', () => {
- setModelData( model, '<paragraph>foo bar baz[]</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>bar</marker> baz[]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( forwardCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when the selection position is at a marker start and there are no more markers', () => {
- setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo []<marker>bar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( forwardCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when the selection position is in a marker and there are no more markers', () => {
- setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>b[]ar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( forwardCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when the selection position is at a marker end and there are no more markers', () => {
- setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>bar</marker>[] baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( forwardCommand.isEnabled ).to.be.false;
- } );
- } );
- describe( 'expanded selection', () => {
- it( 'should be true when there is a marker after the first selection position', () => {
- setModelData( model, '<paragraph>[fo]o bar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>[fo]o <marker>bar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( forwardCommand.isEnabled ).to.be.true;
- } );
- it( 'should be true when the selection overlaps the marker but the start position is before it', () => {
- setModelData( model, '<paragraph>[foo ba]r baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>[foo <marker>ba]r</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( forwardCommand.isEnabled ).to.be.true;
- } );
- it( 'should be false when the selection overlaps the marker but the start position is after it', () => {
- setModelData( model, '<paragraph>foo ba[r baz]</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>ba[r</marker> baz]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( forwardCommand.isEnabled ).to.be.false;
- } );
- } );
- } );
- describe( 'execute()', () => {
- describe( 'collapsed selection', () => {
- it( 'should move to the next marker', () => {
- setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>[]foo <marker>bar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker></paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:2', {
- range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- forwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
- forwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo bar [baz]</paragraph>' );
- } );
- it( 'should move to the next marker when at the end of adjacent one', () => {
- setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
- const fiirstParagraph = model.document.getRoot().getChild( 0 );
- const secondParagraph = model.document.getRoot().getChild( 1 );
- // <paragraph><marker>foo</marker>[]</paragraph><paragraph>bar</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRangeIn( fiirstParagraph ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- // <paragraph><marker>foo</marker>[]</paragraph><paragraph><marker>bar</marker></paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:2', {
- range: writer.createRangeIn( secondParagraph ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- forwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[bar]</paragraph>' );
- } );
- it( 'should move to the closest marker when created in a reverse order', () => {
- setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>[]foo bar <marker>baz</marker></paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:2', {
- range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- // <paragraph>[]foo <marker>bar</marker> <marker>baz</marker></paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- forwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
- forwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo bar [baz]</paragraph>' );
- } );
- } );
- describe( 'expanded selection', () => {
- it( 'should move to the next marker when the selection end overlaps the marker', () => {
- setModelData( model, '<paragraph>[foo b]ar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>[foo <marker>b]ar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- // <paragraph>[foo <marker>b]ar</marker> <marker>baz</marker></paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:2', {
- range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- forwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
- forwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo bar [baz]</paragraph>' );
- } );
- it( 'should move to the next marker when the selection start overlaps the marker', () => {
- setModelData( model, '<paragraph>foo b[ar b]az</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>b[ar</marker> b]az</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- // <paragraph>foo <marker>b[ar</marker> <marker>b]az</marker></paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:2', {
- range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- forwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo bar [baz]</paragraph>' );
- } );
- } );
- } );
- } );
- describe( 'backward command', () => {
- describe( 'isEnabled', () => {
- describe( 'collapsed selection', () => {
- it( 'should be true when there is a marker before the selection position', () => {
- setModelData( model, '<paragraph>foo bar baz[]</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>bar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( backwardCommand.isEnabled ).to.be.true;
- } );
- it( 'should be false when there is no marker before the selection position', () => {
- setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>[]foo <marker>bar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( backwardCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when the selection position is at a marker end and there are no more markers', () => {
- setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>bar</marker>[] baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( backwardCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when the selection position is in a marker and there are no more markers', () => {
- setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>b[]ar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( backwardCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when the selection position is at a marker start and there are no more markers', () => {
- setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo []<marker>bar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( backwardCommand.isEnabled ).to.be.false;
- } );
- } );
- describe( 'expanded selection', () => {
- it( 'should be true when there is a marker before the first selection position', () => {
- setModelData( model, '<paragraph>foo bar b[az]</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>[fo]o <marker>bar</marker> b[az]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( backwardCommand.isEnabled ).to.be.true;
- } );
- it( 'should be false when the selection overlaps the marker but the start position is after it', () => {
- setModelData( model, '<paragraph>foo b[ar baz]</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>b[ar</marker> baz]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( backwardCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when the selection overlaps the marker but the after position is after it', () => {
- setModelData( model, '<paragraph>[foo b]ar baz</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>[foo <marker>b]ar</marker> baz</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- expect( backwardCommand.isEnabled ).to.be.false;
- } );
- } );
- } );
- describe( 'execute()', () => {
- describe( 'collapsed selection', () => {
- it( 'should move to the previous marker', () => {
- setModelData( model, '<paragraph>foo bar baz[]</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>bar</marker> baz[]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- // <paragraph>foo <marker>bar</marker> <marker>baz</marker>[]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:2', {
- range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- backwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
- } );
- it( 'should move to the previous marker when at the beginning of adjacent one', () => {
- setModelData( model, '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
- const fiirstParagraph = model.document.getRoot().getChild( 0 );
- const secondParagraph = model.document.getRoot().getChild( 1 );
- // <paragraph><marker>foo</marker></paragraph><paragraph>[]bar</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRangeIn( fiirstParagraph ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- // <paragraph><marker>foo</marker></paragraph><paragraph><marker>[]bar</marker></paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:2', {
- range: writer.createRangeIn( secondParagraph ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- backwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>[foo]</paragraph><paragraph>bar</paragraph>' );
- } );
- it( 'should move to the closest previous marker', () => {
- setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>bar</marker> baz qux[]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- // <paragraph>foo <marker>bar</marker> <marker>baz</marker> qux[]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:2', {
- range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- backwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo bar [baz] qux</paragraph>' );
- backwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz qux</paragraph>' );
- } );
- it( 'should move to the closest previous marker when created in a reverse order', () => {
- setModelData( model, '<paragraph>foo bar baz qux[]</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo bar <marker>baz</marker> qux[]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:2', {
- range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- // <paragraph>foo <marker>bar</marker> <marker>baz</marker> qux[]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- backwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo bar [baz] qux</paragraph>' );
- backwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz qux</paragraph>' );
- } );
- } );
- describe( 'expanded selection', () => {
- it( 'should move to the previous marker when the selection end overlaps the marker', () => {
- setModelData( model, '<paragraph>foo bar b[az]</paragraph>' );
- const paragraph = model.document.getRoot().getChild( 0 );
- // <paragraph>foo <marker>bar</marker> b[az]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:1', {
- range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- // <paragraph>foo <marker>bar</marker> <marker>b[az</marker>]</paragraph>
- model.change( writer => {
- writer.addMarker( 'restrictedEditingException:2', {
- range: writer.createRange( writer.createPositionAt( paragraph, 8 ), writer.createPositionAt( paragraph, 11 ) ),
- usingOperation: true,
- affectsData: true
- } );
- } );
- backwardCommand.execute();
- expect( getModelData( model ) ).to.equal( '<paragraph>foo [bar] baz</paragraph>' );
- } );
- } );
- } );
- } );
- } );
|