|
@@ -4,29 +4,33 @@
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
|
|
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
|
|
|
|
|
+import ParagraphCommand from '@ckeditor/ckeditor5-paragraph/src/paragraphcommand';
|
|
|
import HeadingCommand from '../src/headingcommand';
|
|
import HeadingCommand from '../src/headingcommand';
|
|
|
import Range from '@ckeditor/ckeditor5-engine/src/model/range';
|
|
import Range from '@ckeditor/ckeditor5-engine/src/model/range';
|
|
|
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
|
|
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
|
|
|
|
|
|
|
|
const options = [
|
|
const options = [
|
|
|
- { id: 'paragraph', element: 'p' },
|
|
|
|
|
- { id: 'heading1', element: 'h2' },
|
|
|
|
|
- { id: 'heading2', element: 'h3' },
|
|
|
|
|
- { id: 'heading3', element: 'h4' }
|
|
|
|
|
|
|
+ { modelElement: 'heading1', viewElement: 'h2', title: 'H2' },
|
|
|
|
|
+ { modelElement: 'heading2', viewElement: 'h3', title: 'H3' },
|
|
|
|
|
+ { modelElement: 'heading3', viewElement: 'h4', title: 'H4' }
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
describe( 'HeadingCommand', () => {
|
|
describe( 'HeadingCommand', () => {
|
|
|
- let editor, document, command, root, schema;
|
|
|
|
|
|
|
+ let editor, document, commands, root, schema;
|
|
|
|
|
|
|
|
beforeEach( () => {
|
|
beforeEach( () => {
|
|
|
return ModelTestEditor.create().then( newEditor => {
|
|
return ModelTestEditor.create().then( newEditor => {
|
|
|
editor = newEditor;
|
|
editor = newEditor;
|
|
|
document = editor.document;
|
|
document = editor.document;
|
|
|
- command = new HeadingCommand( editor, options, 'paragraph' );
|
|
|
|
|
|
|
+ commands = {};
|
|
|
schema = document.schema;
|
|
schema = document.schema;
|
|
|
|
|
|
|
|
|
|
+ editor.commands.set( 'paragraph', new ParagraphCommand( editor ) );
|
|
|
|
|
+ schema.registerItem( 'paragraph', '$block' );
|
|
|
|
|
+
|
|
|
for ( let option of options ) {
|
|
for ( let option of options ) {
|
|
|
- schema.registerItem( option.id, '$block' );
|
|
|
|
|
|
|
+ commands[ option.modelElement ] = new HeadingCommand( editor, option );
|
|
|
|
|
+ schema.registerItem( option.modelElement, '$block' );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
root = document.getRoot();
|
|
root = document.getRoot();
|
|
@@ -34,7 +38,23 @@ describe( 'HeadingCommand', () => {
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
afterEach( () => {
|
|
afterEach( () => {
|
|
|
- command.destroy();
|
|
|
|
|
|
|
+ for ( let modelElement in commands ) {
|
|
|
|
|
+ commands[ modelElement ].destroy();
|
|
|
|
|
+ }
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ describe( 'basic properties', () => {
|
|
|
|
|
+ for ( let option of options ) {
|
|
|
|
|
+ test( option );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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 );
|
|
|
|
|
+ } );
|
|
|
|
|
+ }
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
describe( 'value', () => {
|
|
describe( 'value', () => {
|
|
@@ -42,40 +62,33 @@ describe( 'HeadingCommand', () => {
|
|
|
test( option );
|
|
test( option );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function test( option ) {
|
|
|
|
|
- it( `equals ${ option.id } when collapsed selection is placed inside ${ option.id } element`, () => {
|
|
|
|
|
- setData( document, `<${ option.id }>foobar</${ option.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 );
|
|
const element = root.getChild( 0 );
|
|
|
document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
|
|
document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
|
|
|
|
|
|
|
|
- expect( command.value ).to.equal( option );
|
|
|
|
|
|
|
+ expect( commands[ modelElement ].value ).to.be.true;
|
|
|
} );
|
|
} );
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- it( 'should be equal to #defaultOption if option has not been found', () => {
|
|
|
|
|
- schema.registerItem( 'div', '$block' );
|
|
|
|
|
- setData( document, '<div>xyz</div>' );
|
|
|
|
|
- const element = root.getChild( 0 );
|
|
|
|
|
- document.selection.addRange( Range.createFromParentsAndOffsets( element, 1, element, 1 ) );
|
|
|
|
|
-
|
|
|
|
|
- expect( command.value ).to.equal( command.defaultOption );
|
|
|
|
|
- } );
|
|
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
describe( '_doExecute', () => {
|
|
describe( '_doExecute', () => {
|
|
|
it( 'should update value after execution', () => {
|
|
it( 'should update value after execution', () => {
|
|
|
|
|
+ const command = commands.heading1;
|
|
|
|
|
+
|
|
|
setData( document, '<paragraph>[]</paragraph>' );
|
|
setData( document, '<paragraph>[]</paragraph>' );
|
|
|
- command._doExecute( { id: 'heading1' } );
|
|
|
|
|
|
|
+ command._doExecute();
|
|
|
|
|
|
|
|
expect( getData( document ) ).to.equal( '<heading1>[]</heading1>' );
|
|
expect( getData( document ) ).to.equal( '<heading1>[]</heading1>' );
|
|
|
- expect( command.value ).to.be.object;
|
|
|
|
|
- expect( command.value.id ).to.equal( 'heading1' );
|
|
|
|
|
- expect( command.value.element ).to.equal( 'h2' );
|
|
|
|
|
|
|
+ expect( command.value ).to.be.true;
|
|
|
} );
|
|
} );
|
|
|
|
|
|
|
|
describe( 'custom options', () => {
|
|
describe( 'custom options', () => {
|
|
|
it( 'should use provided batch', () => {
|
|
it( 'should use provided batch', () => {
|
|
|
const batch = editor.document.batch();
|
|
const batch = editor.document.batch();
|
|
|
|
|
+ const command = commands.heading1;
|
|
|
|
|
+
|
|
|
setData( document, '<paragraph>foo[]bar</paragraph>' );
|
|
setData( document, '<paragraph>foo[]bar</paragraph>' );
|
|
|
|
|
|
|
|
expect( batch.deltas.length ).to.equal( 0 );
|
|
expect( batch.deltas.length ).to.equal( 0 );
|
|
@@ -84,6 +97,19 @@ describe( 'HeadingCommand', () => {
|
|
|
|
|
|
|
|
expect( batch.deltas.length ).to.be.above( 0 );
|
|
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', () => {
|
|
describe( 'collapsed selection', () => {
|
|
@@ -94,16 +120,11 @@ describe( 'HeadingCommand', () => {
|
|
|
convertTo = option;
|
|
convertTo = option;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- it( 'uses paragraph as default value', () => {
|
|
|
|
|
- setData( document, '<heading1>foo[]bar</heading1>' );
|
|
|
|
|
- command._doExecute();
|
|
|
|
|
-
|
|
|
|
|
- expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
|
|
|
|
|
- } );
|
|
|
|
|
-
|
|
|
|
|
it( 'converts to default option when executed with already applied option', () => {
|
|
it( 'converts to default option when executed with already applied option', () => {
|
|
|
|
|
+ const command = commands.heading1;
|
|
|
|
|
+
|
|
|
setData( document, '<heading1>foo[]bar</heading1>' );
|
|
setData( document, '<heading1>foo[]bar</heading1>' );
|
|
|
- command._doExecute( { id: 'heading1' } );
|
|
|
|
|
|
|
+ command._doExecute();
|
|
|
|
|
|
|
|
expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
|
|
expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
|
|
|
} );
|
|
} );
|
|
@@ -112,18 +133,18 @@ describe( 'HeadingCommand', () => {
|
|
|
schema.registerItem( 'inlineImage', '$inline' );
|
|
schema.registerItem( 'inlineImage', '$inline' );
|
|
|
schema.allow( { name: '$text', inside: 'inlineImage' } );
|
|
schema.allow( { name: '$text', inside: 'inlineImage' } );
|
|
|
|
|
|
|
|
- setData( document, '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
|
|
|
|
|
- command._doExecute( { id: 'heading1' } );
|
|
|
|
|
|
|
+ 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 ) {
|
|
function test( from, to ) {
|
|
|
- it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
|
|
|
|
|
- setData( document, `<${ from.id }>foo[]bar</${ from.id }>` );
|
|
|
|
|
- command._doExecute( { id: to.id } );
|
|
|
|
|
|
|
+ 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 }>` );
|
|
|
} );
|
|
} );
|
|
|
}
|
|
}
|
|
|
} );
|
|
} );
|
|
@@ -137,29 +158,35 @@ describe( 'HeadingCommand', () => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
it( 'converts all elements where selection is applied', () => {
|
|
it( 'converts all elements where selection is applied', () => {
|
|
|
- setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
|
|
|
|
|
- command._doExecute( { id: 'paragraph' } );
|
|
|
|
|
|
|
+ setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading3>]baz</heading3>' );
|
|
|
|
|
+ commands.heading3._doExecute();
|
|
|
|
|
|
|
|
expect( getData( document ) ).to.equal(
|
|
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', () => {
|
|
it( 'resets to default value all elements with same option', () => {
|
|
|
setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
|
|
setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
|
|
|
- command._doExecute( { id: 'heading1' } );
|
|
|
|
|
|
|
+ commands.heading1._doExecute();
|
|
|
|
|
|
|
|
expect( getData( document ) ).to.equal(
|
|
expect( getData( document ) ).to.equal(
|
|
|
'<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
|
|
'<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 }>` );
|
|
|
|
|
- command._doExecute( { id: to.id } );
|
|
|
|
|
|
|
+ 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 }>`
|
|
|
|
|
+ );
|
|
|
} );
|
|
} );
|
|
|
}
|
|
}
|
|
|
} );
|
|
} );
|