8
0

paragraphcommand.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import ParagraphCommand from '@ckeditor/ckeditor5-paragraph/src/paragraphcommand';
  7. import Selection from '@ckeditor/ckeditor5-engine/src/model/selection';
  8. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  9. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. describe( 'HeadingCommand', () => {
  11. let editor, document, command, root, schema;
  12. beforeEach( () => {
  13. return ModelTestEditor.create().then( newEditor => {
  14. editor = newEditor;
  15. document = editor.document;
  16. schema = document.schema;
  17. command = new ParagraphCommand( editor );
  18. root = document.getRoot();
  19. editor.commands.set( 'paragraph', command );
  20. schema.registerItem( 'paragraph', '$block' );
  21. schema.registerItem( 'heading1', '$block' );
  22. } );
  23. } );
  24. afterEach( () => {
  25. command.destroy();
  26. } );
  27. describe( 'value', () => {
  28. it( 'has default value', () => {
  29. setData( document, '' );
  30. expect( command.value ).to.be.false;
  31. } );
  32. it( 'responds to changes in selection', () => {
  33. setData( document, '<heading1>foo[]bar</heading1>' );
  34. expect( command.value ).to.be.false;
  35. setData( document, '<paragraph>foo[]bar</paragraph>' );
  36. expect( command.value ).to.be.true;
  37. } );
  38. } );
  39. describe( '_doExecute', () => {
  40. it( 'should update value after execution', () => {
  41. setData( document, '<heading1>[]</heading1>' );
  42. command._doExecute();
  43. expect( getData( document ) ).to.equal( '<paragraph>[]</paragraph>' );
  44. expect( command.value ).to.be.true;
  45. } );
  46. describe( 'custom options', () => {
  47. it( 'should use provided batch', () => {
  48. const batch = editor.document.batch();
  49. setData( document, '<heading1>foo[]bar</heading1>' );
  50. expect( batch.deltas.length ).to.equal( 0 );
  51. command._doExecute( { batch } );
  52. expect( batch.deltas.length ).to.be.above( 0 );
  53. } );
  54. it( 'should use provided selection', () => {
  55. setData( document, '<heading1>foo[]bar</heading1><heading1>baz</heading1><heading1>qux</heading1>' );
  56. const secondTolastHeading = root.getChild( 1 );
  57. const lastHeading = root.getChild( 2 );
  58. const selection = new Selection();
  59. selection.addRange( Range.createFromParentsAndOffsets( secondTolastHeading, 0, lastHeading, 0 ) );
  60. command._doExecute( { selection } );
  61. expect( getData( document ) ).to.equal( '<heading1>foo[]bar</heading1><paragraph>baz</paragraph><paragraph>qux</paragraph>' );
  62. } );
  63. } );
  64. describe( 'collapsed selection', () => {
  65. it( 'does nothing when executed with already applied', () => {
  66. setData( document, '<paragraph>foo[]bar</paragraph>' );
  67. command._doExecute();
  68. expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
  69. } );
  70. it( 'converts topmost blocks', () => {
  71. schema.registerItem( 'inlineImage', '$inline' );
  72. schema.allow( { name: '$text', inside: 'inlineImage' } );
  73. setData( document, '<heading1><inlineImage>foo[]</inlineImage>bar</heading1>' );
  74. command._doExecute();
  75. expect( getData( document ) ).to.equal( '<paragraph><inlineImage>foo[]</inlineImage>bar</paragraph>' );
  76. } );
  77. } );
  78. describe( 'non-collapsed selection', () => {
  79. it( 'converts all elements where selection is applied', () => {
  80. schema.registerItem( 'heading2', '$block' );
  81. setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
  82. command._doExecute();
  83. expect( getData( document ) ).to.equal(
  84. '<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>]baz</paragraph>'
  85. );
  86. } );
  87. } );
  88. } );
  89. } );