codeblockediting.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /* global document */
  6. import CodeBlockEditing from '../src/codeblockediting';
  7. import CodeBlockCommand from '../src/codeblockcommand';
  8. import AlignmentEditing from '@ckeditor/ckeditor5-alignment/src/alignmentediting';
  9. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  10. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  11. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  14. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. describe( 'CodeBlockEditing', () => {
  16. let editor, element, model;
  17. beforeEach( () => {
  18. element = document.createElement( 'div' );
  19. document.body.appendChild( element );
  20. return ClassicTestEditor
  21. .create( element, {
  22. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. model = editor.model;
  27. } );
  28. } );
  29. afterEach( () => {
  30. return editor.destroy().then( () => element.remove() );
  31. } );
  32. it( 'defines plugin name', () => {
  33. expect( CodeBlockEditing.pluginName ).to.equal( 'CodeBlockEditing' );
  34. } );
  35. it( 'defines plugin dependencies', () => {
  36. expect( CodeBlockEditing.requires ).to.have.members( [ ShiftEnter ] );
  37. } );
  38. it( 'adds a codeBlock command', () => {
  39. expect( editor.commands.get( 'codeBlock' ) ).to.be.instanceOf( CodeBlockCommand );
  40. } );
  41. it( 'allows for codeBlock in the $root', () => {
  42. expect( model.schema.checkChild( [ '$root' ], 'codeBlock' ) ).to.be.true;
  43. } );
  44. it( 'allows only for $text in codeBlock', () => {
  45. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$text' ) ).to.equal( true );
  46. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$block' ) ).to.equal( false );
  47. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], 'codeBlock' ) ).to.equal( false );
  48. } );
  49. it( 'disallows all attributes for codeBlock', () => {
  50. setModelData( model, '<codeBlock>f[o]o</codeBlock>' );
  51. editor.execute( 'alignment', { value: 'right' } );
  52. editor.execute( 'bold' );
  53. expect( getModelData( model ) ).to.equal( '<codeBlock>f[o]o</codeBlock>' );
  54. } );
  55. it( 'adds converters to the data pipeline', () => {
  56. const data = '<pre>x</pre>';
  57. editor.setData( data );
  58. expect( getModelData( model ) ).to.equal( '<codeBlock>[]x</codeBlock>' );
  59. expect( editor.getData() ).to.equal( data );
  60. } );
  61. it( 'adds a converter to the view pipeline', () => {
  62. setModelData( model, '<codeBlock>x</codeBlock>' );
  63. expect( editor.getData() ).to.equal( '<pre>x</pre>' );
  64. } );
  65. it( 'should force shiftEnter command when pressing enter inside a codeBlock', () => {
  66. const enterCommand = editor.commands.get( 'enter' );
  67. const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
  68. sinon.spy( enterCommand, 'execute' );
  69. sinon.spy( shiftEnterCommand, 'execute' );
  70. setModelData( model, '<codeBlock>foo[]bar</codeBlock>' );
  71. editor.editing.view.document.fire( 'enter', {
  72. preventDefault: () => {}
  73. } );
  74. expect( getModelData( model ) ).to.equal( '<codeBlock>foo<softBreak></softBreak>[]bar</codeBlock>' );
  75. sinon.assert.calledOnce( shiftEnterCommand.execute );
  76. sinon.assert.notCalled( enterCommand.execute );
  77. } );
  78. it( 'should execute enter command when pressing enter out of codeBlock', () => {
  79. const enterCommand = editor.commands.get( 'enter' );
  80. const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
  81. sinon.spy( enterCommand, 'execute' );
  82. sinon.spy( shiftEnterCommand, 'execute' );
  83. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  84. editor.editing.view.document.fire( 'enter', {
  85. preventDefault: () => {}
  86. } );
  87. expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  88. sinon.assert.calledOnce( enterCommand.execute );
  89. sinon.assert.notCalled( shiftEnterCommand.execute );
  90. } );
  91. describe( 'data pipeline m -> v conversion ', () => {
  92. it( 'should convert empty codeBlock to empty pre tag', () => {
  93. setModelData( model, '<codeBlock></codeBlock>' );
  94. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<pre>&nbsp;</pre>' );
  95. } );
  96. it( 'should convert non-empty codeBlock to pre tag', () => {
  97. setModelData( model, '<codeBlock>Foo</codeBlock>' );
  98. expect( editor.getData() ).to.equal( '<pre>Foo</pre>' );
  99. } );
  100. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  101. setModelData( model,
  102. '<codeBlock>' +
  103. 'Foo<softBreak></softBreak>' +
  104. 'Bar<softBreak></softBreak>' +
  105. 'Biz' +
  106. '</codeBlock>'
  107. );
  108. expect( editor.getData() ).to.equal( '<pre>Foo\nBar\nBiz</pre>' );
  109. } );
  110. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  111. setModelData( model,
  112. '<codeBlock>' +
  113. '<softBreak></softBreak>' +
  114. '<softBreak></softBreak>' +
  115. 'Foo' +
  116. '<softBreak></softBreak>' +
  117. '<softBreak></softBreak>' +
  118. '</codeBlock>'
  119. );
  120. expect( editor.getData() ).to.equal( '<pre>\n\nFoo\n\n</pre>' );
  121. } );
  122. } );
  123. describe( 'data pipeline v -> m conversion ', () => {
  124. it( 'should convert empty pre tag to code block', () => {
  125. editor.setData( '<pre></pre>' );
  126. expect( getModelData( model ) ).to.equal( '<codeBlock>[]</codeBlock>' );
  127. } );
  128. it( 'should convert pre tag with multi-line text to code block #1', () => {
  129. editor.setData( '<pre>foo\nbar</pre>' );
  130. expect( getModelData( model ) ).to.equal(
  131. '<codeBlock>[]' +
  132. 'foo' +
  133. '<softBreak></softBreak>' +
  134. 'bar' +
  135. '</codeBlock>'
  136. );
  137. } );
  138. it( 'should convert pre tag with multi-line text to code block #2', () => {
  139. editor.setData( '<pre>\n\n\nfoo\n\n</pre>' );
  140. // The result may be unclear. 3 new line characters are converted to 2 softBreak elements.
  141. // That's because of HTML rule, leading new line character for <pre/> element is stripped by the DOMParser.
  142. expect( getModelData( model ) ).to.equal(
  143. '<codeBlock>[]' +
  144. '<softBreak></softBreak>' +
  145. '<softBreak></softBreak>' +
  146. 'foo' +
  147. '<softBreak></softBreak>' +
  148. '<softBreak></softBreak>' +
  149. '</codeBlock>'
  150. );
  151. } );
  152. it( 'should convert pre tag with HTML inside', () => {
  153. editor.setData( '<pre><p>Foo</p>\n<p>Bar</p></pre>' );
  154. expect( getModelData( model ) ).to.equal(
  155. '<codeBlock>[]' +
  156. '<p>Foo</p>' +
  157. '<softBreak></softBreak>' +
  158. '<p>Bar</p>' +
  159. '</codeBlock>'
  160. );
  161. } );
  162. it( 'should convert pre tag with HTML and nested pre tag', () => {
  163. editor.setData( '<pre><p>Foo</p><pre>Bar</pre><p>Biz</p></pre>' );
  164. expect( getModelData( model ) ).to.equal( '<codeBlock>[]<p>Foo</p><pre>Bar</pre><p>Biz</p></codeBlock>' );
  165. } );
  166. } );
  167. } );