codeblockcommand.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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( '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 #1', () => {
  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', () => {
  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 #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 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 #1', () => {
  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 #2', () => {
  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 #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 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 #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 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 #2', () => {
  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( '<codeBlock language="plaintext">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  118. } );
  119. it( 'should merge selected blocks with selected codeBlocks', () => {
  120. setModelData( model, '<paragraph>f[oo</paragraph><codeBlock language="plaintext">ba]r</codeBlock>' );
  121. command.execute();
  122. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  123. } );
  124. it( 'should not merge codeBlock with siblings when siblings are not selected', () => {
  125. setModelData( model,
  126. '<codeBlock language="plaintext">foo</codeBlock>' +
  127. '<paragraph>b[a]r</paragraph>' +
  128. '<codeBlock language="plaintext">biz</codeBlock>'
  129. );
  130. command.execute();
  131. expect( getModelData( model ) ).to.equal(
  132. '<codeBlock language="plaintext">foo</codeBlock>' +
  133. '<codeBlock language="plaintext">b[a]r</codeBlock>' +
  134. '<codeBlock language="plaintext">biz</codeBlock>'
  135. );
  136. } );
  137. it( 'should change selected empty codeBlock to paragraph', () => {
  138. setModelData( model, '<codeBlock language="plaintext">[]</codeBlock>' );
  139. command.execute();
  140. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  141. } );
  142. it( 'should change selected codeBlock to paragraph', () => {
  143. setModelData( model, '<codeBlock language="plaintext">f[o]o</codeBlock>' );
  144. command.execute();
  145. expect( getModelData( model ) ).to.equal( '<paragraph>f[o]o</paragraph>' );
  146. } );
  147. it( 'should change selected multi-line codeBlock to paragraphs', () => {
  148. setModelData( model,
  149. '<codeBlock language="plaintext">foo<softBreak></softBreak>b[]ar<softBreak></softBreak>biz</codeBlock>'
  150. );
  151. command.execute();
  152. expect( getModelData( model ) ).to.equal(
  153. '<paragraph>foo</paragraph>' +
  154. '<paragraph>b[]ar</paragraph>' +
  155. '<paragraph>biz</paragraph>'
  156. );
  157. } );
  158. it( 'should filter out attributes from nodes changed to codeBlock', () => {
  159. setModelData( model, '<paragraph alignment="right"><$text bold="true">f[o]o</$text></paragraph>' );
  160. command.execute( 'codeBlock' );
  161. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">f[o]o</codeBlock>' );
  162. } );
  163. it( 'should use forceValue parameter', () => {
  164. setModelData( model, '<codeBlock language="plaintext">f[o]o</codeBlock>' );
  165. command.execute( { forceValue: true } );
  166. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">f[o]o</codeBlock>' );
  167. } );
  168. it( 'should allow setting the language of the new block', () => {
  169. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  170. command.execute( { language: 'css' } );
  171. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">f[o]o</codeBlock>' );
  172. } );
  173. it( 'should allow changing the language of the existing block', () => {
  174. setModelData( model, '<codeBlock language="plaintext">f[o]o</codeBlock>' );
  175. command.execute( { language: 'css', forceValue: true } );
  176. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">f[o]o</codeBlock>' );
  177. } );
  178. } );
  179. describe( 'BlockQuote integration', () => {
  180. it( 'should change a paragraph inside a blockQuote to codeBlock', () => {
  181. setModelData( model, '<blockQuote><paragraph>f[o]o</paragraph></blockQuote>' );
  182. command.execute( 'codeBlock' );
  183. expect( getModelData( model ) ).to.equal( '<blockQuote><codeBlock language="plaintext">f[o]o</codeBlock></blockQuote>' );
  184. } );
  185. it( 'should change a paragraph inside a blockQuote to codeBlock when blockQuote is selected with siblings', () => {
  186. setModelData( model,
  187. '<paragraph>f[oo</paragraph>' +
  188. '<blockQuote><paragraph>bar</paragraph></blockQuote>' +
  189. '<paragraph>bi]z</paragraph>'
  190. );
  191. command.execute( 'codeBlock' );
  192. expect( getModelData( model ) ).to.equal(
  193. '<codeBlock language="plaintext">f[oo</codeBlock>' +
  194. '<blockQuote><codeBlock language="plaintext">bar</codeBlock></blockQuote>' +
  195. '<codeBlock language="plaintext">bi]z</codeBlock>'
  196. );
  197. } );
  198. } );
  199. } );