8
0

insertparagraphcommand.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import InsertParagraphCommand from '../src/insertparagraphcommand';
  7. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. describe( 'InsertParagraphCommand', () => {
  9. let editor, model, document, command, root, schema;
  10. beforeEach( () => {
  11. return ModelTestEditor.create().then( newEditor => {
  12. editor = newEditor;
  13. model = editor.model;
  14. document = model.document;
  15. schema = model.schema;
  16. command = new InsertParagraphCommand( editor );
  17. root = document.getRoot();
  18. editor.commands.add( 'insertParagraph', command );
  19. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  20. schema.register( 'heading1', { inheritAllFrom: '$block', allowIn: 'headersOnly' } );
  21. schema.register( 'headersOnly', { inheritAllFrom: '$block' } );
  22. } );
  23. } );
  24. afterEach( () => {
  25. command.destroy();
  26. } );
  27. describe( 'execute()', () => {
  28. it( 'should insert a paragraph at a specific document position and anchor the selection inside of it', () => {
  29. setData( model, '<heading1>foo[]</heading1>' );
  30. command.execute( {
  31. position: model.createPositionBefore( root.getChild( 0 ) )
  32. } );
  33. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph><heading1>foo</heading1>' );
  34. } );
  35. it( 'should do nothing if the paragraph is not allowed at the provided position', () => {
  36. setData( model, '<headersOnly><heading1>foo[]</heading1></headersOnly>' );
  37. command.execute( {
  38. position: model.createPositionBefore( root.getChild( 0 ).getChild( 0 ) )
  39. } );
  40. command.execute( {
  41. position: model.createPositionAfter( root.getChild( 0 ).getChild( 0 ) )
  42. } );
  43. expect( getData( model ) ).to.equal( '<headersOnly><heading1>foo[]</heading1></headersOnly>' );
  44. } );
  45. describe( 'interation with existing paragraphs in the content', () => {
  46. it( 'should insert a paragraph before another paragraph', () => {
  47. setData( model, '<paragraph>foo[]</paragraph>' );
  48. command.execute( {
  49. position: model.createPositionBefore( root.getChild( 0 ) )
  50. } );
  51. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph><paragraph>foo</paragraph>' );
  52. } );
  53. it( 'should insert a paragraph after another paragraph', () => {
  54. setData( model, '<paragraph>foo[]</paragraph>' );
  55. command.execute( {
  56. position: model.createPositionAfter( root.getChild( 0 ) )
  57. } );
  58. expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]</paragraph>' );
  59. } );
  60. it( 'should not merge with a paragraph that precedes the position at which a new paragraph is inserted', () => {
  61. setData( model, '<paragraph>bar</paragraph><heading1>foo[]</heading1>' );
  62. command.execute( {
  63. position: model.createPositionBefore( root.getChild( 1 ) )
  64. } );
  65. expect( getData( model ) ).to.equal( '<paragraph>bar</paragraph><paragraph>[]</paragraph><heading1>foo</heading1>' );
  66. } );
  67. it( 'should not merge with a paragraph that follows the position at which a new paragraph is inserted', () => {
  68. setData( model, '<heading1>foo[]</heading1><paragraph>bar</paragraph>' );
  69. command.execute( {
  70. position: model.createPositionAfter( root.getChild( 0 ) )
  71. } );
  72. expect( getData( model ) ).to.equal( '<heading1>foo</heading1><paragraph>[]</paragraph><paragraph>bar</paragraph>' );
  73. } );
  74. } );
  75. } );
  76. } );