codeblockcommand.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import CodeBlockEditing from '../src/codeblockediting';
  6. import CodeBlockCommand from '../src/codeblockcommand';
  7. import AlignmentEditing from '@ckeditor/ckeditor5-alignment/src/alignmentediting';
  8. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
  11. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  12. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. describe( 'CodeBlockEditing', () => {
  14. let editor, model, command;
  15. beforeEach( () => {
  16. return ModelTestEditor
  17. .create( {
  18. plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. command = new CodeBlockCommand( editor );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. describe( '#value', () => {
  30. it( 'should be true when the first selected element is a codeBlock element #1', () => {
  31. setModelData( model, '<codeBlock>f[]oo</codeBlock>' );
  32. expect( command.value ).to.equal( true );
  33. } );
  34. it( 'should be true when the first selected element is a codeBlock element', () => {
  35. setModelData( model, '<codeBlock>f[oo</codeBlock><paragraph>ba]r</paragraph>' );
  36. expect( command.value ).to.equal( true );
  37. } );
  38. it( 'should be false when the first selected element is not a code block #1', () => {
  39. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  40. expect( command.value ).to.equal( false );
  41. } );
  42. it( 'should be false when the first selected element is not a code block #2', () => {
  43. setModelData( model, '<paragraph>f[oo</paragraph><codeBlock>ba]r</codeBlock>' );
  44. expect( command.value ).to.equal( false );
  45. } );
  46. } );
  47. describe( '#isEnabled', () => {
  48. it( 'should be true when the first selected block is a codeBlock #1', () => {
  49. setModelData( model, '<codeBlock>f[]oo</codeBlock>' );
  50. expect( command.isEnabled ).to.equal( true );
  51. } );
  52. it( 'should be true when the first selected block is a codeBlock #2', () => {
  53. setModelData( model, '<codeBlock>f[oo</codeBlock><paragraph>ba]r</paragraph>' );
  54. expect( command.isEnabled ).to.equal( true );
  55. } );
  56. it( 'should be true when the first selected block can be a codeBlock #1', () => {
  57. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  58. expect( command.isEnabled ).to.equal( true );
  59. } );
  60. it( 'should be true when the first selected block can be a codeBlock #2', () => {
  61. setModelData( model, '<paragraph>f[oo</paragraph><codeBlock>ba]r</codeBlock>' );
  62. expect( command.isEnabled ).to.equal( true );
  63. } );
  64. it( 'should be false when selected element is a limit element #1', () => {
  65. model.schema.register( 'limit', {
  66. inheritAllFrom: '$block',
  67. isLimit: true
  68. } );
  69. setModelData( model, '[<limit>foo</limit>]' );
  70. expect( command.isEnabled ).to.equal( false );
  71. } );
  72. it( 'should be false when selected element is a limit element #2', () => {
  73. model.schema.register( 'limit', {
  74. inheritAllFrom: '$block',
  75. isLimit: true
  76. } );
  77. setModelData( model, '<limit>f[oo</limit><paragraph>ba]r</paragraph>' );
  78. expect( command.isEnabled ).to.equal( false );
  79. } );
  80. it( 'should be true when limit element is not the first selected element', () => {
  81. model.schema.register( 'limit', {
  82. inheritAllFrom: '$block',
  83. isLimit: true
  84. } );
  85. setModelData( model, '<paragraph>f[oo</paragraph><limit>bar</limit><paragraph>bi]z</paragraph>' );
  86. expect( command.isEnabled ).to.equal( true );
  87. } );
  88. it( 'should make it possible to disallow codeBlock using schema', () => {
  89. model.schema.addChildCheck( ( context, childDef ) => {
  90. if ( context.endsWith( 'blockQuote' ) && childDef.name === 'codeBlock' ) {
  91. return false;
  92. }
  93. } );
  94. setModelData( model, '<blockQuote><paragraph>f[o]o</paragraph></blockQuote>' );
  95. expect( command.isEnabled ).to.equal( false );
  96. } );
  97. } );
  98. describe( 'execute()', () => {
  99. it( 'should change selected empty block to codeBlock', () => {
  100. setModelData( model, '<paragraph>[]</paragraph>' );
  101. command.execute();
  102. expect( getModelData( model ) ).to.equal( '<codeBlock>[]</codeBlock>' );
  103. } );
  104. it( 'should change selected block to codeBlock', () => {
  105. setModelData( model, '<paragraph>fo[]o</paragraph>' );
  106. command.execute();
  107. expect( getModelData( model ) ).to.equal( '<codeBlock>fo[]o</codeBlock>' );
  108. } );
  109. it( 'should change multiple selected block to codeBlock', () => {
  110. setModelData( model, '<paragraph>f[oo</paragraph><paragraph>ba]r</paragraph>' );
  111. command.execute();
  112. expect( getModelData( model ) ).to.equal( '<codeBlock>f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  113. } );
  114. it( 'should merge selected blocks with selected codeBlocks', () => {
  115. setModelData( model, '<paragraph>f[oo</paragraph><codeBlock>ba]r</codeBlock>' );
  116. command.execute();
  117. expect( getModelData( model ) ).to.equal( '<codeBlock>f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  118. } );
  119. it( 'should not merge codeBlock with siblings when siblings are not selected', () => {
  120. setModelData( model,
  121. '<codeBlock>foo</codeBlock>' +
  122. '<paragraph>b[a]r</paragraph>' +
  123. '<codeBlock>biz</codeBlock>'
  124. );
  125. command.execute();
  126. expect( getModelData( model ) ).to.equal(
  127. '<codeBlock>foo</codeBlock>' +
  128. '<codeBlock>b[a]r</codeBlock>' +
  129. '<codeBlock>biz</codeBlock>'
  130. );
  131. } );
  132. it( 'should change selected empty codeBlock to paragraph', () => {
  133. setModelData( model, '<codeBlock>[]</codeBlock>' );
  134. command.execute();
  135. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  136. } );
  137. it( 'should change selected codeBlock to paragraph', () => {
  138. setModelData( model, '<codeBlock>f[o]o</codeBlock>' );
  139. command.execute();
  140. expect( getModelData( model ) ).to.equal( '<paragraph>f[o]o</paragraph>' );
  141. } );
  142. it( 'should change selected multi-line codeBlock to paragraphs', () => {
  143. setModelData( model,
  144. '<codeBlock>foo<softBreak></softBreak>b[]ar<softBreak></softBreak>biz</codeBlock>'
  145. );
  146. command.execute();
  147. expect( getModelData( model ) ).to.equal(
  148. '<paragraph>foo</paragraph>' +
  149. '<paragraph>b[]ar</paragraph>' +
  150. '<paragraph>biz</paragraph>'
  151. );
  152. } );
  153. it( 'should filter out attributes from nodes changed to codeBlock', () => {
  154. setModelData( model, '<paragraph alignment="right"><$text bold="true">f[o]o</$text></paragraph>' );
  155. command.execute( 'codeBlock' );
  156. expect( getModelData( model ) ).to.equal( '<codeBlock>f[o]o</codeBlock>' );
  157. } );
  158. it( 'should use forceValue parameter', () => {
  159. setModelData( model, '<codeBlock>f[o]o</codeBlock>' );
  160. command.execute( { forceValue: true } );
  161. expect( getModelData( model ) ).to.equal( '<codeBlock>f[o]o</codeBlock>' );
  162. } );
  163. } );
  164. describe( 'BlockQuote integration', () => {
  165. it( 'should change a paragraph inside a blockQuote to codeBlock', () => {
  166. setModelData( model, '<blockQuote><paragraph>f[o]o</paragraph></blockQuote>' );
  167. command.execute( 'codeBlock' );
  168. expect( getModelData( model ) ).to.equal( '<blockQuote><codeBlock>f[o]o</codeBlock></blockQuote>' );
  169. } );
  170. it( 'should change a paragraph inside a blockQuote to codeBlock when blockQuote is selected with siblings', () => {
  171. setModelData( model,
  172. '<paragraph>f[oo</paragraph>' +
  173. '<blockQuote><paragraph>bar</paragraph></blockQuote>' +
  174. '<paragraph>bi]z</paragraph>'
  175. );
  176. command.execute( 'codeBlock' );
  177. expect( getModelData( model ) ).to.equal(
  178. '<codeBlock>f[oo</codeBlock>' +
  179. '<blockQuote><codeBlock>bar</codeBlock></blockQuote>' +
  180. '<codeBlock>bi]z</codeBlock>'
  181. );
  182. } );
  183. } );
  184. } );