codeblockediting.js 20 KB

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