|
|
@@ -4,15 +4,15 @@
|
|
|
*/
|
|
|
|
|
|
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
|
|
|
+import ParagraphCommand from '@ckeditor/ckeditor5-paragraph/src/paragraphcommand';
|
|
|
import HeadingCommand from '../src/headingcommand';
|
|
|
import Range from '@ckeditor/ckeditor5-engine/src/model/range';
|
|
|
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
|
|
|
|
|
|
const options = [
|
|
|
- { id: 'paragraph', element: 'p', label: 'P' },
|
|
|
- { id: 'heading1', element: 'h2', label: 'H2' },
|
|
|
- { id: 'heading2', element: 'h3', label: 'H3' },
|
|
|
- { id: 'heading3', element: 'h4', label: 'H4' }
|
|
|
+ { modelElement: 'heading1', viewElement: 'h2', title: 'H2' },
|
|
|
+ { modelElement: 'heading2', viewElement: 'h3', title: 'H3' },
|
|
|
+ { modelElement: 'heading3', viewElement: 'h4', title: 'H4' }
|
|
|
];
|
|
|
|
|
|
describe( 'HeadingCommand', () => {
|
|
|
@@ -25,9 +25,12 @@ describe( 'HeadingCommand', () => {
|
|
|
commands = {};
|
|
|
schema = document.schema;
|
|
|
|
|
|
+ editor.commands.set( 'paragraph', new ParagraphCommand( editor ) );
|
|
|
+ schema.registerItem( 'paragraph', '$block' );
|
|
|
+
|
|
|
for ( let option of options ) {
|
|
|
- commands[ option.id ] = new HeadingCommand( editor, option );
|
|
|
- schema.registerItem( option.id, '$block' );
|
|
|
+ commands[ option.modelElement ] = new HeadingCommand( editor, option );
|
|
|
+ schema.registerItem( option.modelElement, '$block' );
|
|
|
}
|
|
|
|
|
|
root = document.getRoot();
|
|
|
@@ -35,8 +38,8 @@ describe( 'HeadingCommand', () => {
|
|
|
} );
|
|
|
|
|
|
afterEach( () => {
|
|
|
- for ( let id in commands ) {
|
|
|
- commands[ id ].destroy();
|
|
|
+ for ( let modelElement in commands ) {
|
|
|
+ commands[ modelElement ].destroy();
|
|
|
}
|
|
|
} );
|
|
|
|
|
|
@@ -45,11 +48,11 @@ describe( 'HeadingCommand', () => {
|
|
|
test( option );
|
|
|
}
|
|
|
|
|
|
- function test( { id, element, label } ) {
|
|
|
- it( `are set for option.id = ${ id }`, () => {
|
|
|
- expect( commands[ id ].id ).to.equal( id );
|
|
|
- expect( commands[ id ].element ).to.equal( element );
|
|
|
- expect( commands[ id ].label ).to.equal( label );
|
|
|
+ function test( { modelElement, viewElement, title } ) {
|
|
|
+ it( `are set for option.modelElement = ${ modelElement }`, () => {
|
|
|
+ expect( commands[ modelElement ].modelElement ).to.equal( modelElement );
|
|
|
+ expect( commands[ modelElement ].viewElement ).to.equal( viewElement );
|
|
|
+ expect( commands[ modelElement ].title ).to.equal( title );
|
|
|
} );
|
|
|
}
|
|
|
} );
|
|
|
@@ -59,13 +62,13 @@ describe( 'HeadingCommand', () => {
|
|
|
test( option );
|
|
|
}
|
|
|
|
|
|
- function test( { id } ) {
|
|
|
- it( `equals ${ id } when collapsed selection is placed inside ${ id } element`, () => {
|
|
|
- setData( document, `<${ id }>foobar</${ id }>` );
|
|
|
+ function test( { modelElement } ) {
|
|
|
+ it( `equals ${ modelElement } when collapsed selection is placed inside ${ modelElement } element`, () => {
|
|
|
+ setData( document, `<${ modelElement }>foobar</${ modelElement }>` );
|
|
|
const element = root.getChild( 0 );
|
|
|
document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
|
|
|
|
|
|
- expect( commands[ id ].value ).to.be.true;
|
|
|
+ expect( commands[ modelElement ].value ).to.be.true;
|
|
|
} );
|
|
|
}
|
|
|
} );
|
|
|
@@ -94,6 +97,19 @@ describe( 'HeadingCommand', () => {
|
|
|
|
|
|
expect( batch.deltas.length ).to.be.above( 0 );
|
|
|
} );
|
|
|
+
|
|
|
+ it( 'should use provided batch (converting to default option)', () => {
|
|
|
+ const batch = editor.document.batch();
|
|
|
+ const command = commands.heading1;
|
|
|
+
|
|
|
+ setData( document, '<heading1>foo[]bar</heading1>' );
|
|
|
+
|
|
|
+ expect( batch.deltas.length ).to.equal( 0 );
|
|
|
+
|
|
|
+ command._doExecute( { batch } );
|
|
|
+
|
|
|
+ expect( batch.deltas.length ).to.be.above( 0 );
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
describe( 'collapsed selection', () => {
|
|
|
@@ -104,38 +120,31 @@ describe( 'HeadingCommand', () => {
|
|
|
convertTo = option;
|
|
|
}
|
|
|
|
|
|
- it( 'uses paragraph as default value', () => {
|
|
|
+ it( 'converts to default option when executed with already applied option', () => {
|
|
|
+ const command = commands.heading1;
|
|
|
+
|
|
|
setData( document, '<heading1>foo[]bar</heading1>' );
|
|
|
- commands.paragraph._doExecute();
|
|
|
+ command._doExecute( { modelElement: 'heading1' } );
|
|
|
|
|
|
expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
|
|
|
} );
|
|
|
|
|
|
- // it( 'converts to default option when executed with already applied option', () => {
|
|
|
- // const command = commands.paragraph;
|
|
|
-
|
|
|
- // setData( document, '<heading1>foo[]bar</heading1>' );
|
|
|
- // command._doExecute( { id: 'heading1' } );
|
|
|
-
|
|
|
- // expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
|
|
|
- // } );
|
|
|
-
|
|
|
it( 'converts topmost blocks', () => {
|
|
|
schema.registerItem( 'inlineImage', '$inline' );
|
|
|
schema.allow( { name: '$text', inside: 'inlineImage' } );
|
|
|
|
|
|
- setData( document, '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
|
|
|
- commands.paragraph._doExecute();
|
|
|
+ setData( document, '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
|
|
|
+ commands.heading1._doExecute();
|
|
|
|
|
|
- expect( getData( document ) ).to.equal( '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
|
|
|
+ expect( getData( document ) ).to.equal( '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
|
|
|
} );
|
|
|
|
|
|
function test( from, to ) {
|
|
|
- it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
|
|
|
- setData( document, `<${ from.id }>foo[]bar</${ from.id }>` );
|
|
|
- commands[ to.id ]._doExecute();
|
|
|
+ it( `converts ${ from.modelElement } to ${ to.modelElement } on collapsed selection`, () => {
|
|
|
+ setData( document, `<${ from.modelElement }>foo[]bar</${ from.modelElement }>` );
|
|
|
+ commands[ to.modelElement ]._doExecute();
|
|
|
|
|
|
- expect( getData( document ) ).to.equal( `<${ to.id }>foo[]bar</${ to.id }>` );
|
|
|
+ expect( getData( document ) ).to.equal( `<${ to.modelElement }>foo[]bar</${ to.modelElement }>` );
|
|
|
} );
|
|
|
}
|
|
|
} );
|
|
|
@@ -150,28 +159,34 @@ describe( 'HeadingCommand', () => {
|
|
|
|
|
|
it( 'converts all elements where selection is applied', () => {
|
|
|
setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
|
|
|
- commands.paragraph._doExecute();
|
|
|
+ commands.heading3._doExecute();
|
|
|
|
|
|
expect( getData( document ) ).to.equal(
|
|
|
- '<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>]baz</paragraph>'
|
|
|
+ '<heading3>foo[</heading3><heading3>bar</heading3><heading3>]baz</heading3>'
|
|
|
);
|
|
|
} );
|
|
|
|
|
|
- // it( 'resets to default value all elements with same option', () => {
|
|
|
- // setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
|
|
|
- // commands.heading1._doExecute();
|
|
|
+ it( 'resets to default value all elements with same option', () => {
|
|
|
+ setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
|
|
|
+ commands.heading1._doExecute();
|
|
|
|
|
|
- // expect( getData( document ) ).to.equal(
|
|
|
- // '<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
|
|
|
- // );
|
|
|
- // } );
|
|
|
+ expect( getData( document ) ).to.equal(
|
|
|
+ '<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
|
|
|
+ );
|
|
|
+ } );
|
|
|
|
|
|
- function test( from, to ) {
|
|
|
- it( `converts ${ from.id } to ${ to.id } on non-collapsed selection`, () => {
|
|
|
- setData( document, `<${ from.id }>foo[bar</${ from.id }><${ from.id }>baz]qux</${ from.id }>` );
|
|
|
- commands[ to.id ]._doExecute();
|
|
|
+ function test( { modelElement: fromElement }, { modelElement: toElement } ) {
|
|
|
+ it( `converts ${ fromElement } to ${ toElement } on non-collapsed selection`, () => {
|
|
|
+ setData(
|
|
|
+ document,
|
|
|
+ `<${ fromElement }>foo[bar</${ fromElement }><${ fromElement }>baz]qux</${ fromElement }>`
|
|
|
+ );
|
|
|
+
|
|
|
+ commands[ toElement ]._doExecute();
|
|
|
|
|
|
- expect( getData( document ) ).to.equal( `<${ to.id }>foo[bar</${ to.id }><${ to.id }>baz]qux</${ to.id }>` );
|
|
|
+ expect( getData( document ) ).to.equal(
|
|
|
+ `<${ toElement }>foo[bar</${ toElement }><${ toElement }>baz]qux</${ toElement }>`
|
|
|
+ );
|
|
|
} );
|
|
|
}
|
|
|
} );
|