codeblockcommand.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 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( 'CodeBlockCommand', () => {
  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 (selection inside code block)', () => {
  31. setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
  32. expect( command.value ).to.equal( 'foo' );
  33. } );
  34. it( 'should be true when the first selected element is a codeBlock element (other blocks in selection are not code block)', () => {
  35. setModelData( model, '<codeBlock language="foo">f[oo</codeBlock><paragraph>ba]r</paragraph>' );
  36. expect( command.value ).to.equal( 'foo' );
  37. } );
  38. it( 'should be false when the first selected element is not a code block (all blocks are not code block)', () => {
  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 (selection ends in code block)', () => {
  43. setModelData( model, '<paragraph>f[oo</paragraph><codeBlock language="foo">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 (selection inside code block)', () => {
  49. setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
  50. expect( command.isEnabled ).to.equal( true );
  51. } );
  52. it( 'should be true when the first selected block is a codeBlock (other blocks in selection are not code block)', () => {
  53. setModelData( model, '<codeBlock language="foo">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 (collapsed selection)', () => {
  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 (non-collapsed selection, ends in code block)', () => {
  61. setModelData( model, '<paragraph>f[oo</paragraph><codeBlock language="foo">ba]r</codeBlock>' );
  62. expect( command.isEnabled ).to.equal( true );
  63. } );
  64. it( 'should be false when selected element is a limit element (selection on element)', () => {
  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 selection starts in a blockless space', () => {
  73. model.schema.extend( '$text', { allowIn: '$root' } );
  74. setModelData( model, 'x[]x' );
  75. expect( command.isEnabled ).to.equal( false );
  76. } );
  77. it( 'should be false when selected element is a limit element (selection has mixed limit and non-limit elements)', () => {
  78. model.schema.register( 'limit', {
  79. inheritAllFrom: '$block',
  80. isLimit: true
  81. } );
  82. setModelData( model, '<limit>f[oo</limit><paragraph>ba]r</paragraph>' );
  83. expect( command.isEnabled ).to.equal( false );
  84. } );
  85. it( 'should be true when limit element is not the first selected element', () => {
  86. model.schema.register( 'limit', {
  87. inheritAllFrom: '$block',
  88. isLimit: true
  89. } );
  90. setModelData( model, '<paragraph>f[oo</paragraph><limit>bar</limit><paragraph>bi]z</paragraph>' );
  91. expect( command.isEnabled ).to.equal( true );
  92. } );
  93. it( 'should make it possible to disallow codeBlock using schema', () => {
  94. model.schema.addChildCheck( ( context, childDef ) => {
  95. if ( context.endsWith( 'blockQuote' ) && childDef.name === 'codeBlock' ) {
  96. return false;
  97. }
  98. } );
  99. setModelData( model, '<blockQuote><paragraph>f[o]o</paragraph></blockQuote>' );
  100. expect( command.isEnabled ).to.equal( false );
  101. } );
  102. } );
  103. describe( 'execute()', () => {
  104. it( 'should change selected empty block to codeBlock', () => {
  105. setModelData( model, '<paragraph>[]</paragraph>' );
  106. command.execute();
  107. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
  108. } );
  109. it( 'should change selected block to codeBlock', () => {
  110. setModelData( model, '<paragraph>fo[]o</paragraph>' );
  111. command.execute();
  112. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">fo[]o</codeBlock>' );
  113. } );
  114. it( 'should change multiple selected block to codeBlock', () => {
  115. setModelData( model, '<paragraph>f[oo</paragraph><paragraph>ba]r</paragraph>' );
  116. command.execute();
  117. expect( getModelData( model ) ).to.equal(
  118. '<codeBlock language="plaintext">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  119. } );
  120. it( 'should merge selected blocks with selected codeBlocks', () => {
  121. setModelData( model, '<paragraph>f[oo</paragraph><codeBlock language="plaintext">ba]r</codeBlock>' );
  122. command.execute();
  123. expect( getModelData( model ) ).to.equal(
  124. '<codeBlock language="plaintext">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  125. } );
  126. it( 'should not merge codeBlock with siblings when siblings are not selected', () => {
  127. setModelData( model,
  128. '<codeBlock language="plaintext">foo</codeBlock>' +
  129. '<paragraph>b[a]r</paragraph>' +
  130. '<codeBlock language="plaintext">biz</codeBlock>'
  131. );
  132. command.execute();
  133. expect( getModelData( model ) ).to.equal(
  134. '<codeBlock language="plaintext">foo</codeBlock>' +
  135. '<codeBlock language="plaintext">b[a]r</codeBlock>' +
  136. '<codeBlock language="plaintext">biz</codeBlock>'
  137. );
  138. } );
  139. it( 'should change selected empty codeBlock to paragraph', () => {
  140. setModelData( model, '<codeBlock language="plaintext">[]</codeBlock>' );
  141. command.execute();
  142. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  143. } );
  144. it( 'should change selected codeBlock to paragraph', () => {
  145. setModelData( model, '<codeBlock language="plaintext">f[o]o</codeBlock>' );
  146. command.execute();
  147. expect( getModelData( model ) ).to.equal( '<paragraph>f[o]o</paragraph>' );
  148. } );
  149. it( 'should change selected multi-line codeBlock to paragraphs', () => {
  150. setModelData( model,
  151. '<codeBlock language="plaintext">foo<softBreak></softBreak>b[]ar<softBreak></softBreak>biz</codeBlock>'
  152. );
  153. command.execute();
  154. expect( getModelData( model ) ).to.equal(
  155. '<paragraph>foo</paragraph>' +
  156. '<paragraph>b[]ar</paragraph>' +
  157. '<paragraph>biz</paragraph>'
  158. );
  159. } );
  160. it( 'should filter out attributes from nodes changed to codeBlock', () => {
  161. setModelData( model, '<paragraph alignment="right"><$text bold="true">f[o]o</$text></paragraph>' );
  162. command.execute();
  163. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">f[o]o</codeBlock>' );
  164. } );
  165. it( 'should use forceValue parameter', () => {
  166. setModelData( model, '<codeBlock language="plaintext">f[o]o</codeBlock>' );
  167. command.execute( { forceValue: true } );
  168. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">f[o]o</codeBlock>' );
  169. } );
  170. it( 'should allow setting the language of the new block', () => {
  171. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  172. command.execute( { language: 'css' } );
  173. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">f[o]o</codeBlock>' );
  174. } );
  175. it( 'should allow changing the language of the existing block', () => {
  176. setModelData( model, '<codeBlock language="plaintext">f[o]o</codeBlock>' );
  177. command.execute( { language: 'css', forceValue: true } );
  178. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">f[o]o</codeBlock>' );
  179. } );
  180. } );
  181. describe( 'BlockQuote integration', () => {
  182. it( 'should change a paragraph inside a blockQuote to codeBlock', () => {
  183. setModelData( model, '<blockQuote><paragraph>f[o]o</paragraph></blockQuote>' );
  184. command.execute();
  185. expect( getModelData( model ) ).to.equal(
  186. '<blockQuote><codeBlock language="plaintext">f[o]o</codeBlock></blockQuote>' );
  187. } );
  188. it( 'should change a paragraph inside a blockQuote to codeBlock when blockQuote is selected with siblings', () => {
  189. setModelData( model,
  190. '<paragraph>f[oo</paragraph>' +
  191. '<blockQuote><paragraph>bar</paragraph></blockQuote>' +
  192. '<paragraph>bi]z</paragraph>'
  193. );
  194. command.execute();
  195. expect( getModelData( model ) ).to.equal(
  196. '<codeBlock language="plaintext">f[oo</codeBlock>' +
  197. '<blockQuote><codeBlock language="plaintext">bar</codeBlock></blockQuote>' +
  198. '<codeBlock language="plaintext">bi]z</codeBlock>'
  199. );
  200. } );
  201. } );
  202. } );