8
0

codeblockediting.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  16. describe( 'CodeBlockEditing', () => {
  17. let editor, element, model, view;
  18. beforeEach( () => {
  19. element = document.createElement( 'div' );
  20. document.body.appendChild( element );
  21. return ClassicTestEditor
  22. .create( element, {
  23. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. model = editor.model;
  28. view = editor.editing.view;
  29. } );
  30. } );
  31. afterEach( () => {
  32. return editor.destroy().then( () => element.remove() );
  33. } );
  34. it( 'defines plugin name', () => {
  35. expect( CodeBlockEditing.pluginName ).to.equal( 'CodeBlockEditing' );
  36. } );
  37. it( 'defines plugin dependencies', () => {
  38. expect( CodeBlockEditing.requires ).to.have.members( [ ShiftEnter ] );
  39. } );
  40. describe( 'config', () => {
  41. describe( 'languages', () => {
  42. describe( 'default value', () => {
  43. it( 'should be set', () => {
  44. expect( editor.config.get( 'codeBlock.languages' ) ).to.deep.equal( [
  45. { class: 'plaintext', label: 'Plain text' },
  46. { class: 'c', label: 'C' },
  47. { class: 'cs', label: 'C#' },
  48. { class: 'cpp', label: 'C++' },
  49. { class: 'css', label: 'CSS' },
  50. { class: 'diff', label: 'Diff' },
  51. { class: 'xml', label: 'HTML/XML' },
  52. { class: 'java', label: 'Java' },
  53. { class: 'javascript', label: 'JavaScript' },
  54. { class: 'php', label: 'PHP' },
  55. { class: 'python', label: 'Python' },
  56. { class: 'ruby', label: 'Ruby' },
  57. { class: 'typescript', label: 'TypeScript' }
  58. ] );
  59. } );
  60. } );
  61. it( 'should be recognized when loading data', () => {
  62. return ClassicTestEditor.create(
  63. '<pre><code class="foo">bar</code></pre>' +
  64. '<pre><code class="bar">baz</code></pre>',
  65. {
  66. plugins: [ CodeBlockEditing ],
  67. codeBlock: {
  68. languages: [
  69. { class: 'foo', label: 'Foo' },
  70. { class: 'bar', label: 'Bar' },
  71. ]
  72. }
  73. } )
  74. .then( editor => {
  75. model = editor.model;
  76. expect( getModelData( model ) ).to.equal(
  77. '<codeBlock language="foo">[]bar</codeBlock>' +
  78. '<codeBlock language="bar">baz</codeBlock>'
  79. );
  80. return editor.destroy();
  81. } );
  82. } );
  83. it( 'should use the first if the code in data has no language', () => {
  84. return ClassicTestEditor
  85. .create( '<pre><code>bar</code></pre>', {
  86. plugins: [ CodeBlockEditing ],
  87. codeBlock: {
  88. languages: [
  89. { class: 'foo', label: 'Foo' },
  90. { class: 'bar', label: 'Bar' }
  91. ]
  92. }
  93. } )
  94. .then( editor => {
  95. model = editor.model;
  96. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">[]bar</codeBlock>' );
  97. return editor.destroy();
  98. } );
  99. } );
  100. it( 'should use the first if the code in data has an invalid language', () => {
  101. return ClassicTestEditor
  102. .create( '<pre><code class="baz">bar</code></pre>', {
  103. plugins: [ CodeBlockEditing ],
  104. codeBlock: {
  105. languages: [
  106. { class: 'foo', label: 'Foo' },
  107. { class: 'bar', label: 'Bar' }
  108. ]
  109. }
  110. } )
  111. .then( editor => {
  112. model = editor.model;
  113. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">[]bar</codeBlock>' );
  114. return editor.destroy();
  115. } );
  116. } );
  117. } );
  118. } );
  119. it( 'adds a codeBlock command', () => {
  120. expect( editor.commands.get( 'codeBlock' ) ).to.be.instanceOf( CodeBlockCommand );
  121. } );
  122. it( 'allows for codeBlock in the $root', () => {
  123. expect( model.schema.checkChild( [ '$root' ], 'codeBlock' ) ).to.be.true;
  124. } );
  125. it( 'allows only for $text in codeBlock', () => {
  126. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$text' ) ).to.equal( true );
  127. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$block' ) ).to.equal( false );
  128. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], 'codeBlock' ) ).to.equal( false );
  129. } );
  130. it( 'disallows all attributes (except "language") for codeBlock', () => {
  131. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  132. editor.execute( 'alignment', { value: 'right' } );
  133. editor.execute( 'bold' );
  134. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">f[o]o</codeBlock>' );
  135. } );
  136. it( 'should force shiftEnter command when pressing enter inside a codeBlock', () => {
  137. const enterCommand = editor.commands.get( 'enter' );
  138. const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
  139. sinon.spy( enterCommand, 'execute' );
  140. sinon.spy( shiftEnterCommand, 'execute' );
  141. setModelData( model, '<codeBlock>foo[]bar</codeBlock>' );
  142. editor.editing.view.document.fire( 'enter', {
  143. preventDefault: () => {}
  144. } );
  145. expect( getModelData( model ) ).to.equal( '<codeBlock>foo<softBreak></softBreak>[]bar</codeBlock>' );
  146. sinon.assert.calledOnce( shiftEnterCommand.execute );
  147. sinon.assert.notCalled( enterCommand.execute );
  148. } );
  149. it( 'should execute enter command when pressing enter out of codeBlock', () => {
  150. const enterCommand = editor.commands.get( 'enter' );
  151. const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
  152. sinon.spy( enterCommand, 'execute' );
  153. sinon.spy( shiftEnterCommand, 'execute' );
  154. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  155. editor.editing.view.document.fire( 'enter', {
  156. preventDefault: () => {}
  157. } );
  158. expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  159. sinon.assert.calledOnce( enterCommand.execute );
  160. sinon.assert.notCalled( shiftEnterCommand.execute );
  161. } );
  162. describe( 'editing pipeline m -> v', () => {
  163. it( 'should convert empty codeBlock to empty pre tag', () => {
  164. setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
  165. expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">[]</code></pre>' );
  166. } );
  167. it( 'should convert non-empty codeBlock to pre tag', () => {
  168. setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
  169. expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">{}Foo</code></pre>' );
  170. } );
  171. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  172. setModelData( model,
  173. '<codeBlock language="plaintext">' +
  174. 'Foo<softBreak></softBreak>' +
  175. 'Bar<softBreak></softBreak>' +
  176. 'Biz' +
  177. '</codeBlock>'
  178. );
  179. expect( getViewData( view ) ).to.equal(
  180. '<pre data-language="Plain text">' +
  181. '<code class="plaintext">{}Foo<br></br>Bar<br></br>Biz</code>' +
  182. '</pre>' );
  183. } );
  184. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  185. setModelData( model,
  186. '<codeBlock language="plaintext">' +
  187. '<softBreak></softBreak>' +
  188. '<softBreak></softBreak>' +
  189. 'Foo' +
  190. '<softBreak></softBreak>' +
  191. '<softBreak></softBreak>' +
  192. '</codeBlock>'
  193. );
  194. expect( getViewData( view ) ).to.equal(
  195. '<pre data-language="Plain text">' +
  196. '<code class="plaintext">[]<br></br><br></br>Foo<br></br><br></br></code>' +
  197. '</pre>' );
  198. } );
  199. } );
  200. describe( 'data pipeline m -> v conversion ', () => {
  201. it( 'should convert empty codeBlock to empty pre tag', () => {
  202. setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
  203. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<pre><code class="plaintext">&nbsp;</code></pre>' );
  204. } );
  205. it( 'should convert non-empty codeBlock to pre tag', () => {
  206. setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
  207. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo</code></pre>' );
  208. } );
  209. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  210. setModelData( model,
  211. '<codeBlock language="plaintext">' +
  212. 'Foo<softBreak></softBreak>' +
  213. 'Bar<softBreak></softBreak>' +
  214. 'Biz' +
  215. '</codeBlock>' +
  216. '<paragraph>A<softBreak></softBreak>B</paragraph>'
  217. );
  218. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo\nBar\nBiz</code></pre><p>A<br>B</p>' );
  219. } );
  220. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  221. setModelData( model,
  222. '<codeBlock language="plaintext">' +
  223. '<softBreak></softBreak>' +
  224. '<softBreak></softBreak>' +
  225. 'Foo' +
  226. '<softBreak></softBreak>' +
  227. '<softBreak></softBreak>' +
  228. '</codeBlock>'
  229. );
  230. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">\n\nFoo\n\n</code></pre>' );
  231. } );
  232. it( 'should convert codeBlock with html content', () => {
  233. setModelData( model, '<codeBlock language="plaintext">[]</codeBlock>' );
  234. model.change( writer => writer.insertText( '<div><p>Foo</p></div>', model.document.selection.getFirstPosition() ) );
  235. expect( editor.getData() ).to.equal(
  236. '<pre>' +
  237. '<code class="plaintext">&lt;div&gt;&lt;p&gt;Foo&lt;/p&gt;&lt;/div&gt;</code>' +
  238. '</pre>' );
  239. } );
  240. it( 'should be overridable', () => {
  241. editor.data.downcastDispatcher.on( 'insert:codeBlock', ( evt, data, api ) => {
  242. const targetViewPosition = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
  243. const code = api.writer.createContainerElement( 'code' );
  244. api.consumable.consume( data.item, 'insert' );
  245. api.writer.insert( targetViewPosition, code );
  246. api.mapper.bindElements( data.item, code );
  247. }, { priority: 'high' } );
  248. editor.data.downcastDispatcher.on( 'insert:softBreak', ( evt, data, api ) => {
  249. const position = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
  250. api.consumable.consume( data.item, 'insert' );
  251. api.writer.insert( position, api.writer.createText( '\n' ) );
  252. }, { priority: 'highest' } );
  253. setModelData( model, '<codeBlock language="plaintext">Foo<softBreak></softBreak>Bar</codeBlock>' );
  254. expect( editor.getData() ).to.equal( '<code>Foo\nBar</code>' );
  255. } );
  256. } );
  257. describe( 'data pipeline v -> m conversion ', () => {
  258. it( 'should not convert empty pre tag to code block', () => {
  259. editor.setData( '<pre></pre>' );
  260. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  261. } );
  262. it( 'should not convert pre with no code child to code block', () => {
  263. editor.setData( '<pre><samp></samp></pre>' );
  264. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  265. } );
  266. it( 'should convert pre > code to code block', () => {
  267. editor.setData( '<pre><code></code></pre>' );
  268. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
  269. } );
  270. it( 'should convert pre > code with multi-line text to code block #1', () => {
  271. editor.setData( '<pre><code>foo\nbar</code></pre>' );
  272. expect( getModelData( model ) ).to.equal(
  273. '<codeBlock language="plaintext">[]' +
  274. 'foo' +
  275. '<softBreak></softBreak>' +
  276. 'bar' +
  277. '</codeBlock>'
  278. );
  279. } );
  280. it( 'should convert pre > code with multi-line text to code block #2', () => {
  281. editor.setData( '<pre><code>\n\nfoo\n\n</code></pre>' );
  282. expect( getModelData( model ) ).to.equal(
  283. '<codeBlock language="plaintext">[]' +
  284. '<softBreak></softBreak>' +
  285. '<softBreak></softBreak>' +
  286. 'foo' +
  287. '<softBreak></softBreak>' +
  288. '<softBreak></softBreak>' +
  289. '</codeBlock>'
  290. );
  291. } );
  292. it( 'should convert pre > code with HTML inside', () => {
  293. editor.setData( '<pre><code><p>Foo</p>\n<p>Bar</p></code></pre>' );
  294. expect( getModelData( model ) ).to.equal(
  295. '<codeBlock language="plaintext">[]' +
  296. '<p>Foo</p>' +
  297. '<softBreak></softBreak>' +
  298. '<p>Bar</p>' +
  299. '</codeBlock>'
  300. );
  301. } );
  302. it( 'should convert pre > code tag with HTML and nested pre > code tag', () => {
  303. editor.setData( '<pre><code><p>Foo</p><pre>Bar</pre><p>Biz</p></code></pre>' );
  304. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<p>Foo</p><pre>Bar</pre><p>Biz</p></codeBlock>' );
  305. } );
  306. it( 'should convert pre > code tag with escaped html content', () => {
  307. editor.setData( '<pre><code>&lt;div&gt;&lt;p&gt;Foo&lt;/p&gt;&lt;/div&gt;</code></pre>' );
  308. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<div><p>Foo</p></div></codeBlock>' );
  309. } );
  310. it( 'should be overridable', () => {
  311. editor.data.upcastDispatcher.on( 'element:pre', ( evt, data, api ) => {
  312. const modelItem = api.writer.createElement( 'codeBlock' );
  313. api.writer.appendText( 'Hello World!', modelItem );
  314. api.writer.insert( modelItem, data.modelCursor );
  315. api.consumable.consume( data.viewItem, { name: true } );
  316. data.modelCursor = api.writer.createPositionAfter( modelItem );
  317. data.modelRange = api.writer.createRangeOn( modelItem );
  318. }, { priority: 'high' } );
  319. editor.setData( '<pre><code>Foo Bar</code></pre>' );
  320. expect( getModelData( model ) ).to.equal( '<codeBlock>[]Hello World!</codeBlock>' );
  321. } );
  322. } );
  323. describe( 'clipboard integration', () => {
  324. it( 'should not intercept input when selection anchored outside any code block', () => {
  325. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  326. const dataTransferMock = {
  327. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar' )
  328. };
  329. editor.editing.view.document.fire( 'clipboardInput', {
  330. dataTransfer: dataTransferMock,
  331. stop: sinon.spy()
  332. } );
  333. expect( getModelData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  334. sinon.assert.notCalled( dataTransferMock.getData );
  335. } );
  336. it( 'should intercept input when selection anchored in the code block', () => {
  337. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  338. const dataTransferMock = {
  339. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar\nbaz\n' )
  340. };
  341. editor.editing.view.document.fire( 'clipboardInput', {
  342. dataTransfer: dataTransferMock,
  343. stop: sinon.spy()
  344. } );
  345. expect( getModelData( model ) ).to.equal(
  346. '<codeBlock language="css">' +
  347. 'fbar' +
  348. '<softBreak></softBreak>' +
  349. 'baz' +
  350. '<softBreak></softBreak>' +
  351. '[]o' +
  352. '</codeBlock>' );
  353. sinon.assert.calledOnce( dataTransferMock.getData );
  354. } );
  355. } );
  356. } );