insertparagraphcommand.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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( 'allowP', { inheritAllFrom: '$block' } );
  22. schema.register( 'disallowP', { inheritAllFrom: '$block', allowIn: [ 'allowP' ] } );
  23. model.schema.extend( 'paragraph', { allowIn: [ 'allowP' ] } );
  24. } );
  25. } );
  26. afterEach( () => {
  27. command.destroy();
  28. } );
  29. describe( 'execute()', () => {
  30. it( 'should insert a paragraph at a specific document position and anchor the selection inside of it', () => {
  31. setData( model, '<heading1>foo[]</heading1>' );
  32. command.execute( {
  33. position: model.createPositionBefore( root.getChild( 0 ) )
  34. } );
  35. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph><heading1>foo</heading1>' );
  36. } );
  37. it( 'should split ancestors down to a limit where a paragraph is allowed', () => {
  38. setData( model, '<allowP><disallowP>foo</disallowP></allowP>' );
  39. command.execute( {
  40. // fo[]o
  41. position: model.createPositionAt( root.getChild( 0 ).getChild( 0 ), 2 )
  42. } );
  43. expect( getData( model ) ).to.equal(
  44. '<allowP>' +
  45. '<disallowP>fo</disallowP>' +
  46. '<paragraph>[]</paragraph>' +
  47. '<disallowP>o</disallowP>' +
  48. '</allowP>'
  49. );
  50. } );
  51. it( 'should do nothing if the paragraph is not allowed at the provided position', () => {
  52. // Create a situation where "paragraph" is disallowed even in the "root".
  53. schema.addChildCheck( ( context, childDefinition ) => {
  54. if ( context.endsWith( '$root' ) && childDefinition.name == 'paragraph' ) {
  55. return false;
  56. }
  57. } );
  58. setData( model, '<heading1>foo[]</heading1>' );
  59. command.execute( {
  60. position: model.createPositionBefore( root.getChild( 0 ) )
  61. } );
  62. expect( getData( model ) ).to.equal( '<heading1>foo[]</heading1>' );
  63. } );
  64. describe( 'interation with existing paragraphs in the content', () => {
  65. it( 'should insert a paragraph before another paragraph', () => {
  66. setData( model, '<paragraph>foo[]</paragraph>' );
  67. command.execute( {
  68. position: model.createPositionBefore( root.getChild( 0 ) )
  69. } );
  70. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph><paragraph>foo</paragraph>' );
  71. } );
  72. it( 'should insert a paragraph after another paragraph', () => {
  73. setData( model, '<paragraph>foo[]</paragraph>' );
  74. command.execute( {
  75. position: model.createPositionAfter( root.getChild( 0 ) )
  76. } );
  77. expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]</paragraph>' );
  78. } );
  79. it( 'should not merge with a paragraph that precedes the position at which a new paragraph is inserted', () => {
  80. setData( model, '<paragraph>bar</paragraph><heading1>foo[]</heading1>' );
  81. command.execute( {
  82. position: model.createPositionBefore( root.getChild( 1 ) )
  83. } );
  84. expect( getData( model ) ).to.equal( '<paragraph>bar</paragraph><paragraph>[]</paragraph><heading1>foo</heading1>' );
  85. } );
  86. it( 'should not merge with a paragraph that follows the position at which a new paragraph is inserted', () => {
  87. setData( model, '<heading1>foo[]</heading1><paragraph>bar</paragraph>' );
  88. command.execute( {
  89. position: model.createPositionAfter( root.getChild( 0 ) )
  90. } );
  91. expect( getData( model ) ).to.equal( '<heading1>foo</heading1><paragraph>[]</paragraph><paragraph>bar</paragraph>' );
  92. } );
  93. } );
  94. } );
  95. } );