codeblockediting.js 17 KB

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