insertparagraphcommand.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 before the provided model element and anchor the selection inside of it', () => {
  29. setData( model, '<heading1>foo[]</heading1>' );
  30. command.execute( {
  31. element: root.getChild( 0 ),
  32. position: 'before'
  33. } );
  34. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph><heading1>foo</heading1>' );
  35. } );
  36. it( 'should insert a paragraph after the provided model element and anchor the selection inside of it', () => {
  37. setData( model, '<heading1>foo[]</heading1>' );
  38. command.execute( {
  39. element: root.getChild( 0 ),
  40. position: 'after'
  41. } );
  42. expect( getData( model ) ).to.equal( '<heading1>foo</heading1><paragraph>[]</paragraph>' );
  43. } );
  44. it( 'should do nothing if the paragraph is not allowed in the provided context', () => {
  45. setData( model, '<headersOnly><heading1>foo[]</heading1></headersOnly>' );
  46. command.execute( {
  47. element: root.getChild( 0 ).getChild( 0 ),
  48. position: 'before'
  49. } );
  50. command.execute( {
  51. element: root.getChild( 0 ).getChild( 0 ),
  52. position: 'after'
  53. } );
  54. expect( getData( model ) ).to.equal( '<headersOnly><heading1>foo[]</heading1></headersOnly>' );
  55. } );
  56. describe( 'interation with existing paragraphs in the content', () => {
  57. it( 'should insert a paragraph before another paragraph', () => {
  58. setData( model, '<paragraph>foo[]</paragraph>' );
  59. command.execute( {
  60. element: root.getChild( 0 ),
  61. position: 'before'
  62. } );
  63. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph><paragraph>foo</paragraph>' );
  64. } );
  65. it( 'should insert a paragraph after another paragraph', () => {
  66. setData( model, '<paragraph>foo[]</paragraph>' );
  67. command.execute( {
  68. element: root.getChild( 0 ),
  69. position: 'after'
  70. } );
  71. expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]</paragraph>' );
  72. } );
  73. it( 'should not merge with a paragraph that precedes the model element before which a new paragraph is inserted', () => {
  74. setData( model, '<paragraph>bar</paragraph><heading1>foo[]</heading1>' );
  75. command.execute( {
  76. element: root.getChild( 1 ),
  77. position: 'before'
  78. } );
  79. expect( getData( model ) ).to.equal( '<paragraph>bar</paragraph><paragraph>[]</paragraph><heading1>foo</heading1>' );
  80. } );
  81. it( 'should not merge with a paragraph that follows the model element before which a new paragraph is inserted', () => {
  82. setData( model, '<heading1>foo[]</heading1><paragraph>bar</paragraph>' );
  83. command.execute( {
  84. element: root.getChild( 0 ),
  85. position: 'after'
  86. } );
  87. expect( getData( model ) ).to.equal( '<heading1>foo</heading1><paragraph>[]</paragraph><paragraph>bar</paragraph>' );
  88. } );
  89. } );
  90. } );
  91. } );