codeblockediting.js 20 KB

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