outdentcodeblockcommand.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 OutdentCodeBlockCommand from '../src/outdentcodeblockcommand';
  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( 'OutdentCodeBlockCommand', () => {
  14. let editor, model, outdentCommand;
  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. outdentCommand = new OutdentCodeBlockCommand( editor, 'backward' );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. describe( '#isEnabled', () => {
  30. it( 'should be true when the selection is in a line containing the indent sequence', () => {
  31. setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
  32. // <codeBlock language="foo"> f[]oo</codeBlock>
  33. model.change( writer => {
  34. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  35. } );
  36. expect( outdentCommand.isEnabled ).to.be.true;
  37. } );
  38. it( 'should be true when any line in the selection contains more than the indent sequence', () => {
  39. setModelData( model, '<codeBlock language="foo">f[oo</codeBlock><paragraph>ba]r</paragraph>' );
  40. // <codeBlock language="foo"> f[oo</codeBlock><paragraph>ba]r</paragraph>
  41. model.change( writer => {
  42. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  43. } );
  44. expect( outdentCommand.isEnabled ).to.be.true;
  45. } );
  46. it( 'should be true when any line in the selection contains the indent sequence', () => {
  47. setModelData( model, '<codeBlock language="foo">f[oo</codeBlock><codeBlock language="foo">ba]r</codeBlock>' );
  48. // <codeBlock language="foo">f[oo</codeBlock><codeBlock language="foo"> ba]r</codeBlock>
  49. model.change( writer => {
  50. writer.insertText( ' ', model.document.getRoot().getChild( 1 ) );
  51. } );
  52. expect( outdentCommand.isEnabled ).to.be.true;
  53. } );
  54. it( 'should be false when the indent sequence is in other element', () => {
  55. setModelData( model, '<paragraph>foo[]</paragraph>' );
  56. // <paragraph> foo[]</paragraph>
  57. model.change( writer => {
  58. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  59. } );
  60. expect( outdentCommand.isEnabled ).to.be.false;
  61. } );
  62. it( 'should be false when there is no indent sequence in the line (caret inside text)', () => {
  63. setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
  64. expect( outdentCommand.isEnabled ).to.be.false;
  65. } );
  66. it( 'should be false when there is no indent sequence in the line (empty line)', () => {
  67. setModelData( model, '<codeBlock language="foo">[]</codeBlock>' );
  68. expect( outdentCommand.isEnabled ).to.be.false;
  69. } );
  70. it( 'should be false when there is no indent sequence in the line (caret at the end of a block)', () => {
  71. setModelData( model, '<codeBlock language="foo">foo[]</codeBlock>' );
  72. expect( outdentCommand.isEnabled ).to.be.false;
  73. } );
  74. it( 'should be false when there is no corrent sequence in the line', () => {
  75. setModelData( model, '<codeBlock language="foo">foo[]</codeBlock>' );
  76. // <codeBlock language="foo"> foo[]</codeBlock>
  77. model.change( writer => {
  78. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  79. } );
  80. expect( outdentCommand.isEnabled ).to.be.false;
  81. } );
  82. it( 'should be false when the sequence is not in leading characters of the line', () => {
  83. setModelData( model, '<codeBlock language="foo">barfoo[]</codeBlock>' );
  84. // <codeBlock language="foo">bar foo[]</codeBlock>
  85. model.change( writer => {
  86. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 3 );
  87. } );
  88. expect( outdentCommand.isEnabled ).to.be.false;
  89. } );
  90. it( 'should be false when the sequence is not in leading characters of the line (after other white-space characters)', () => {
  91. setModelData( model, '<codeBlock language="foo">foo[]</codeBlock>' );
  92. // <codeBlock language="foo">foo[]</codeBlock>
  93. model.change( writer => {
  94. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  95. } );
  96. expect( outdentCommand.isEnabled ).to.be.false;
  97. } );
  98. describe( 'config.codeBlock.indentSequence', () => {
  99. it( 'should be respected (#1)', () => {
  100. return ModelTestEditor
  101. .create( {
  102. plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ],
  103. codeBlock: {
  104. indentSequence: ' '
  105. }
  106. } )
  107. .then( newEditor => {
  108. const editor = newEditor;
  109. const model = editor.model;
  110. const outdentCommand = new OutdentCodeBlockCommand( editor );
  111. setModelData( model, '<codeBlock language="foo">foo[]</codeBlock>' );
  112. // <codeBlock language="foo"> foo[]</codeBlock>
  113. model.change( writer => {
  114. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  115. } );
  116. expect( outdentCommand.isEnabled ).to.be.true;
  117. return editor.destroy();
  118. } );
  119. } );
  120. it( 'should be respected (#2)', () => {
  121. return ModelTestEditor
  122. .create( {
  123. plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ],
  124. codeBlock: {
  125. indentSequence: ' '
  126. }
  127. } )
  128. .then( newEditor => {
  129. const editor = newEditor;
  130. const model = editor.model;
  131. const outdentCommand = new OutdentCodeBlockCommand( editor );
  132. setModelData( model, '<codeBlock language="foo"> foo[]</codeBlock>' );
  133. // <codeBlock language="foo"> foo[]</codeBlock>
  134. model.change( writer => {
  135. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  136. } );
  137. expect( outdentCommand.isEnabled ).to.be.false;
  138. return editor.destroy();
  139. } );
  140. } );
  141. it( 'should disable the command when the config is not set', () => {
  142. return ModelTestEditor
  143. .create( {
  144. plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ],
  145. codeBlock: {
  146. indentSequence: false
  147. }
  148. } )
  149. .then( newEditor => {
  150. const editor = newEditor;
  151. const model = editor.model;
  152. const outdentCommand = new OutdentCodeBlockCommand( editor );
  153. setModelData( model, '<codeBlock language="foo">foo[]</codeBlock>' );
  154. // <codeBlock language="foo"> foo[]</codeBlock>
  155. model.change( writer => {
  156. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  157. } );
  158. expect( outdentCommand.isEnabled ).to.be.false;
  159. return editor.destroy();
  160. } );
  161. } );
  162. } );
  163. } );
  164. describe( 'execute()', () => {
  165. it( 'should outdent a single line', () => {
  166. setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
  167. // <codeBlock language="foo"> f[]oo</codeBlock>
  168. model.change( writer => {
  169. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  170. } );
  171. outdentCommand.execute();
  172. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f[]oo</codeBlock>' );
  173. } );
  174. it( 'should outdent only one level in a single line', () => {
  175. setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
  176. // <codeBlock language="foo"> f[]oo</codeBlock>
  177. model.change( writer => {
  178. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  179. } );
  180. outdentCommand.execute();
  181. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo"> f[]oo</codeBlock>' );
  182. } );
  183. it( 'should outdent multiple lines', () => {
  184. setModelData( model, '<codeBlock language="foo">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  185. // <codeBlock language="foo"> f[oo<softBreak></softBreak> ba]r</codeBlock>
  186. model.change( writer => {
  187. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  188. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  189. } );
  190. outdentCommand.execute();
  191. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  192. } );
  193. it( 'should outdent only one level across multiple lines', () => {
  194. setModelData( model, '<codeBlock language="foo">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  195. // <codeBlock language="foo"> f[oo<softBreak></softBreak> ba]r</codeBlock>
  196. model.change( writer => {
  197. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  198. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  199. } );
  200. outdentCommand.execute();
  201. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f[oo<softBreak></softBreak> ba]r</codeBlock>' );
  202. } );
  203. it( 'should outdent some lines', () => {
  204. setModelData( model, '<codeBlock language="foo">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  205. // <codeBlock language="foo">f[oo<softBreak></softBreak> ba]r</codeBlock>
  206. model.change( writer => {
  207. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  208. } );
  209. outdentCommand.execute();
  210. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f[oo<softBreak></softBreak>ba]r</codeBlock>' );
  211. } );
  212. describe( 'config.codeBlock.indentSequence', () => {
  213. it( 'should be respected', () => {
  214. return ModelTestEditor
  215. .create( {
  216. plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ],
  217. codeBlock: {
  218. indentSequence: ' '
  219. }
  220. } )
  221. .then( newEditor => {
  222. const editor = newEditor;
  223. const model = editor.model;
  224. const outdentCommand = new OutdentCodeBlockCommand( editor );
  225. setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
  226. // <codeBlock language="foo"> f[]oo</codeBlock>
  227. model.change( writer => {
  228. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  229. } );
  230. outdentCommand.execute();
  231. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f[]oo</codeBlock>' );
  232. return editor.destroy();
  233. } );
  234. } );
  235. } );
  236. } );
  237. } );