| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- /**
- * @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 CodeBlockEditing from '../src/codeblockediting';
- import OutdentCodeBlockCommand from '../src/outdentcodeblockcommand';
- import AlignmentEditing from '@ckeditor/ckeditor5-alignment/src/alignmentediting';
- import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
- import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
- import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- describe( 'OutdentCodeBlockCommand', () => {
- let editor, model, outdentCommand;
- beforeEach( () => {
- return ModelTestEditor
- .create( {
- plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ]
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- outdentCommand = new OutdentCodeBlockCommand( editor, 'backward' );
- } );
- } );
- afterEach( () => {
- return editor.destroy();
- } );
- describe( '#isEnabled', () => {
- it( 'should be true when the selection is in a line containing the indent sequence', () => {
- setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
- // <codeBlock language="foo"> f[]oo</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
- } );
- expect( outdentCommand.isEnabled ).to.be.true;
- } );
- it( 'should be true when any line in the selection contains more than the indent sequence', () => {
- setModelData( model, '<codeBlock language="foo">f[oo</codeBlock><paragraph>ba]r</paragraph>' );
- // <codeBlock language="foo"> f[oo</codeBlock><paragraph>ba]r</paragraph>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
- } );
- expect( outdentCommand.isEnabled ).to.be.true;
- } );
- it( 'should be true when any line in the selection contains the indent sequence', () => {
- setModelData( model, '<codeBlock language="foo">f[oo</codeBlock><codeBlock language="foo">ba]r</codeBlock>' );
- // <codeBlock language="foo">f[oo</codeBlock><codeBlock language="foo"> ba]r</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 1 ) );
- } );
- expect( outdentCommand.isEnabled ).to.be.true;
- } );
- it( 'should be false when the indent sequence is in other element', () => {
- setModelData( model, '<paragraph>foo[]</paragraph>' );
- // <paragraph> foo[]</paragraph>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
- } );
- expect( outdentCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when there is no indent sequence in the line (caret inside text)', () => {
- setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
- expect( outdentCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when there is no indent sequence in the line (empty line)', () => {
- setModelData( model, '<codeBlock language="foo">[]</codeBlock>' );
- expect( outdentCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when there is no indent sequence in the line (caret at the end of a block)', () => {
- setModelData( model, '<codeBlock language="foo">foo[]</codeBlock>' );
- expect( outdentCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when there is no corrent sequence in the line', () => {
- setModelData( model, '<codeBlock language="foo">foo[]</codeBlock>' );
- // <codeBlock language="foo"> foo[]</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
- } );
- expect( outdentCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when the sequence is not in leading characters of the line', () => {
- setModelData( model, '<codeBlock language="foo">barfoo[]</codeBlock>' );
- // <codeBlock language="foo">bar foo[]</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 3 );
- } );
- expect( outdentCommand.isEnabled ).to.be.false;
- } );
- it( 'should be false when the sequence is not in leading characters of the line (after other white-space characters)', () => {
- setModelData( model, '<codeBlock language="foo">foo[]</codeBlock>' );
- // <codeBlock language="foo">foo[]</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
- } );
- expect( outdentCommand.isEnabled ).to.be.false;
- } );
- describe( 'config.codeBlock.indentSequence', () => {
- it( 'should be respected (#1)', () => {
- return ModelTestEditor
- .create( {
- plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ],
- codeBlock: {
- indentSequence: ' '
- }
- } )
- .then( newEditor => {
- const editor = newEditor;
- const model = editor.model;
- const outdentCommand = new OutdentCodeBlockCommand( editor );
- setModelData( model, '<codeBlock language="foo">foo[]</codeBlock>' );
- // <codeBlock language="foo"> foo[]</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
- } );
- expect( outdentCommand.isEnabled ).to.be.true;
- return editor.destroy();
- } );
- } );
- it( 'should be respected (#2)', () => {
- return ModelTestEditor
- .create( {
- plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ],
- codeBlock: {
- indentSequence: ' '
- }
- } )
- .then( newEditor => {
- const editor = newEditor;
- const model = editor.model;
- const outdentCommand = new OutdentCodeBlockCommand( editor );
- setModelData( model, '<codeBlock language="foo"> foo[]</codeBlock>' );
- // <codeBlock language="foo"> foo[]</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
- } );
- expect( outdentCommand.isEnabled ).to.be.false;
- return editor.destroy();
- } );
- } );
- it( 'should disable the command when the config is not set', () => {
- return ModelTestEditor
- .create( {
- plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ],
- codeBlock: {
- indentSequence: false
- }
- } )
- .then( newEditor => {
- const editor = newEditor;
- const model = editor.model;
- const outdentCommand = new OutdentCodeBlockCommand( editor );
- setModelData( model, '<codeBlock language="foo">foo[]</codeBlock>' );
- // <codeBlock language="foo"> foo[]</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
- } );
- expect( outdentCommand.isEnabled ).to.be.false;
- return editor.destroy();
- } );
- } );
- } );
- } );
- describe( 'execute()', () => {
- it( 'should outdent a single line', () => {
- setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
- // <codeBlock language="foo"> f[]oo</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
- } );
- outdentCommand.execute();
- expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f[]oo</codeBlock>' );
- } );
- it( 'should outdent only one level in a single line', () => {
- setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
- // <codeBlock language="foo"> f[]oo</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
- } );
- outdentCommand.execute();
- expect( getModelData( model ) ).to.equal( '<codeBlock language="foo"> f[]oo</codeBlock>' );
- } );
- it( 'should outdent multiple lines', () => {
- setModelData( model, '<codeBlock language="foo">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
- // <codeBlock language="foo"> f[oo<softBreak></softBreak> ba]r</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
- } );
- outdentCommand.execute();
- expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
- } );
- it( 'should outdent only one level across multiple lines', () => {
- setModelData( model, '<codeBlock language="foo">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
- // <codeBlock language="foo"> f[oo<softBreak></softBreak> ba]r</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
- } );
- outdentCommand.execute();
- expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f[oo<softBreak></softBreak> ba]r</codeBlock>' );
- } );
- it( 'should outdent some lines', () => {
- setModelData( model, '<codeBlock language="foo">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
- // <codeBlock language="foo">f[oo<softBreak></softBreak> ba]r</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
- } );
- outdentCommand.execute();
- expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
- } );
- describe( 'config.codeBlock.indentSequence', () => {
- it( 'should be respected', () => {
- return ModelTestEditor
- .create( {
- plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ],
- codeBlock: {
- indentSequence: ' '
- }
- } )
- .then( newEditor => {
- const editor = newEditor;
- const model = editor.model;
- const outdentCommand = new OutdentCodeBlockCommand( editor );
- setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
- // <codeBlock language="foo"> f[]oo</codeBlock>
- model.change( writer => {
- writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
- } );
- outdentCommand.execute();
- expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f[]oo</codeBlock>' );
- return editor.destroy();
- } );
- } );
- } );
- } );
- } );
|