8
0

codeblockediting.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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, stringify } 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', getEvent() );
  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. viewDoc.fire( 'enter', getEvent() );
  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( 'indentation retention', () => {
  176. it( 'should work when indentation is with spaces', () => {
  177. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  178. model.change( writer => {
  179. // <codeBlock language="css"> foo[]</codeBlock>
  180. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  181. } );
  182. viewDoc.fire( 'enter', getEvent() );
  183. expect( getModelData( model ) ).to.equal(
  184. '<codeBlock language="css"> foo<softBreak></softBreak> []</codeBlock>' );
  185. editor.execute( 'undo' );
  186. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo[]</codeBlock>' );
  187. } );
  188. it( 'should work when indentation is with tabs', () => {
  189. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  190. model.change( writer => {
  191. // <codeBlock language="css"> foo[]</codeBlock>
  192. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  193. } );
  194. viewDoc.fire( 'enter', getEvent() );
  195. expect( getModelData( model ) ).to.equal(
  196. '<codeBlock language="css"> foo<softBreak></softBreak> []</codeBlock>' );
  197. editor.execute( 'undo' );
  198. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo[]</codeBlock>' );
  199. } );
  200. it( 'should retain only the last line', () => {
  201. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>bar[]</codeBlock>' );
  202. model.change( writer => {
  203. // <codeBlock language="css"> foo<softBreak></softBreak> bar[]</codeBlock>
  204. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  205. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  206. } );
  207. viewDoc.fire( 'enter', getEvent() );
  208. expect( getModelData( model ) ).to.equal(
  209. '<codeBlock language="css"> foo<softBreak></softBreak> bar<softBreak></softBreak> []</codeBlock>' );
  210. editor.execute( 'undo' );
  211. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo<softBreak></softBreak> bar[]</codeBlock>' );
  212. } );
  213. it( 'should retain when the selection is non–collapsed', () => {
  214. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  215. model.change( writer => {
  216. // <codeBlock language="css"> f[o]o</codeBlock>
  217. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  218. } );
  219. viewDoc.fire( 'enter', getEvent() );
  220. expect( getModelData( model ) ).to.equal(
  221. '<codeBlock language="css"> f<softBreak></softBreak> []o</codeBlock>' );
  222. editor.execute( 'undo' );
  223. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> f[o]o</codeBlock>' );
  224. } );
  225. it( 'should consider only leading white-spaces', () => {
  226. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  227. model.change( writer => {
  228. // <codeBlock language="css"> foo []</codeBlock>
  229. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 3 );
  230. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  231. } );
  232. viewDoc.fire( 'enter', getEvent() );
  233. expect( getModelData( model ) ).to.equal(
  234. '<codeBlock language="css"> foo <softBreak></softBreak> []</codeBlock>' );
  235. editor.execute( 'undo' );
  236. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo []</codeBlock>' );
  237. } );
  238. it( 'should not work when there is some non-whitespace character', () => {
  239. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  240. model.change( writer => {
  241. // <codeBlock language="css">foo []</codeBlock>
  242. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 3 );
  243. } );
  244. viewDoc.fire( 'enter', getEvent() );
  245. expect( getModelData( model ) ).to.equal(
  246. '<codeBlock language="css">foo <softBreak></softBreak>[]</codeBlock>' );
  247. } );
  248. } );
  249. describe( 'leaving block on double enter', () => {
  250. it( 'should leave the block when pressed twice at the end', () => {
  251. const spy = sinon.spy( editor.editing.view, 'scrollToTheSelection' );
  252. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  253. viewDoc.fire( 'enter', getEvent() );
  254. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  255. viewDoc.fire( 'enter', getEvent() );
  256. expect( getModelData( model ) ).to.equal(
  257. '<codeBlock language="css">foo</codeBlock>' +
  258. '<paragraph>[]</paragraph>'
  259. );
  260. sinon.assert.calledOnce( spy );
  261. editor.execute( 'undo' );
  262. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  263. editor.execute( 'undo' );
  264. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo[]</codeBlock>' );
  265. } );
  266. it( 'should not leave the block when the selection is not collapsed', () => {
  267. setModelData( model, '<codeBlock language="css">f[oo<softBreak></softBreak>]</codeBlock>' );
  268. viewDoc.fire( 'enter', getEvent() );
  269. expect( getModelData( model ) ).to.equal(
  270. '<codeBlock language="css">f<softBreak></softBreak>[]</codeBlock>' );
  271. } );
  272. it( 'should not leave the block when pressed twice when in the middle of the code', () => {
  273. setModelData( model, '<codeBlock language="css">fo[]o</codeBlock>' );
  274. viewDoc.fire( 'enter', getEvent() );
  275. expect( getModelData( model ) ).to.equal(
  276. '<codeBlock language="css">fo<softBreak></softBreak>[]o</codeBlock>' );
  277. viewDoc.fire( 'enter', getEvent() );
  278. expect( getModelData( model ) ).to.equal(
  279. '<codeBlock language="css">fo<softBreak></softBreak><softBreak></softBreak>[]o</codeBlock>' );
  280. } );
  281. it( 'should not leave the block when pressed twice at the beginning of the code', () => {
  282. setModelData( model, '<codeBlock language="css">[]foo</codeBlock>' );
  283. viewDoc.fire( 'enter', getEvent() );
  284. expect( getModelData( model ) ).to.equal(
  285. '<codeBlock language="css"><softBreak></softBreak>[]foo</codeBlock>' );
  286. viewDoc.fire( 'enter', getEvent() );
  287. expect( getModelData( model ) ).to.equal(
  288. '<codeBlock language="css"><softBreak></softBreak><softBreak></softBreak>[]foo</codeBlock>' );
  289. } );
  290. it( 'should not leave the block when pressed shift+enter twice at the end of the code', () => {
  291. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  292. viewDoc.fire( 'enter', getEvent( { isSoft: true } ) );
  293. expect( getModelData( model ) ).to.equal(
  294. '<codeBlock language="css">foo<softBreak></softBreak><softBreak></softBreak>[]</codeBlock>' );
  295. } );
  296. it( 'should clean up the last line if has white–space characters only', () => {
  297. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  298. model.change( writer => {
  299. // <codeBlock language="css">foo<softBreak></softBreak> []</codeBlock>
  300. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  301. } );
  302. viewDoc.fire( 'enter', getEvent() );
  303. expect( getModelData( model ) ).to.equal(
  304. '<codeBlock language="css">foo</codeBlock><paragraph>[]</paragraph>' );
  305. } );
  306. } );
  307. function getEvent( data = {} ) {
  308. return new DomEventData( viewDoc, {
  309. preventDefault: sinon.spy()
  310. }, data );
  311. }
  312. } );
  313. describe( 'editing pipeline m -> v', () => {
  314. it( 'should convert empty codeBlock to empty pre tag', () => {
  315. setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
  316. expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">[]</code></pre>' );
  317. } );
  318. it( 'should convert non-empty codeBlock to pre tag', () => {
  319. setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
  320. expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">{}Foo</code></pre>' );
  321. } );
  322. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  323. setModelData( model,
  324. '<codeBlock language="plaintext">' +
  325. 'Foo<softBreak></softBreak>' +
  326. 'Bar<softBreak></softBreak>' +
  327. 'Biz' +
  328. '</codeBlock>'
  329. );
  330. expect( getViewData( view ) ).to.equal(
  331. '<pre data-language="Plain text">' +
  332. '<code class="plaintext">{}Foo<br></br>Bar<br></br>Biz</code>' +
  333. '</pre>' );
  334. } );
  335. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  336. setModelData( model,
  337. '<codeBlock language="plaintext">' +
  338. '<softBreak></softBreak>' +
  339. '<softBreak></softBreak>' +
  340. 'Foo' +
  341. '<softBreak></softBreak>' +
  342. '<softBreak></softBreak>' +
  343. '</codeBlock>'
  344. );
  345. expect( getViewData( view ) ).to.equal(
  346. '<pre data-language="Plain text">' +
  347. '<code class="plaintext">[]<br></br><br></br>Foo<br></br><br></br></code>' +
  348. '</pre>' );
  349. } );
  350. it( 'should use localized "Plain text" label', () => {
  351. const element = document.createElement( 'div' );
  352. document.body.appendChild( element );
  353. return ClassicTestEditor
  354. .create( element, {
  355. language: 'pl',
  356. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph ]
  357. } )
  358. .then( newEditor => {
  359. const editor = newEditor;
  360. const model = editor.model;
  361. const view = editor.editing.view;
  362. setModelData( model,
  363. '<codeBlock language="plaintext">foo</codeBlock>'
  364. );
  365. expect( getViewData( view ) ).to.equal(
  366. '<pre data-language="Zwykły tekst">' +
  367. '<code class="plaintext">{}foo</code>' +
  368. '</pre>' );
  369. element.remove();
  370. return editor.destroy();
  371. } );
  372. } );
  373. } );
  374. describe( 'data pipeline m -> v conversion ', () => {
  375. it( 'should convert empty codeBlock to empty pre tag', () => {
  376. setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
  377. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<pre><code class="plaintext">&nbsp;</code></pre>' );
  378. } );
  379. it( 'should convert non-empty codeBlock to pre tag', () => {
  380. setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
  381. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo</code></pre>' );
  382. } );
  383. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  384. setModelData( model,
  385. '<codeBlock language="plaintext">' +
  386. 'Foo<softBreak></softBreak>' +
  387. 'Bar<softBreak></softBreak>' +
  388. 'Biz' +
  389. '</codeBlock>' +
  390. '<paragraph>A<softBreak></softBreak>B</paragraph>'
  391. );
  392. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo\nBar\nBiz</code></pre><p>A<br>B</p>' );
  393. } );
  394. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  395. setModelData( model,
  396. '<codeBlock language="plaintext">' +
  397. '<softBreak></softBreak>' +
  398. '<softBreak></softBreak>' +
  399. 'Foo' +
  400. '<softBreak></softBreak>' +
  401. '<softBreak></softBreak>' +
  402. '</codeBlock>'
  403. );
  404. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">\n\nFoo\n\n</code></pre>' );
  405. } );
  406. it( 'should convert codeBlock with html content', () => {
  407. setModelData( model, '<codeBlock language="plaintext">[]</codeBlock>' );
  408. model.change( writer => writer.insertText( '<div><p>Foo</p></div>', model.document.selection.getFirstPosition() ) );
  409. expect( editor.getData() ).to.equal(
  410. '<pre>' +
  411. '<code class="plaintext">&lt;div&gt;&lt;p&gt;Foo&lt;/p&gt;&lt;/div&gt;</code>' +
  412. '</pre>' );
  413. } );
  414. it( 'should be overridable', () => {
  415. editor.data.downcastDispatcher.on( 'insert:codeBlock', ( evt, data, api ) => {
  416. const targetViewPosition = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
  417. const code = api.writer.createContainerElement( 'code' );
  418. api.consumable.consume( data.item, 'insert' );
  419. api.writer.insert( targetViewPosition, code );
  420. api.mapper.bindElements( data.item, code );
  421. }, { priority: 'high' } );
  422. editor.data.downcastDispatcher.on( 'insert:softBreak', ( evt, data, api ) => {
  423. const position = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
  424. api.consumable.consume( data.item, 'insert' );
  425. api.writer.insert( position, api.writer.createText( '\n' ) );
  426. }, { priority: 'highest' } );
  427. setModelData( model, '<codeBlock language="plaintext">Foo<softBreak></softBreak>Bar</codeBlock>' );
  428. expect( editor.getData() ).to.equal( '<code>Foo\nBar</code>' );
  429. } );
  430. } );
  431. describe( 'data pipeline v -> m conversion ', () => {
  432. it( 'should not convert empty pre tag to code block', () => {
  433. editor.setData( '<pre></pre>' );
  434. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  435. } );
  436. it( 'should not convert pre with no code child to code block', () => {
  437. editor.setData( '<pre><samp></samp></pre>' );
  438. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  439. } );
  440. it( 'should convert pre > code to code block', () => {
  441. editor.setData( '<pre><code></code></pre>' );
  442. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
  443. } );
  444. it( 'should convert pre > code with multi-line text to code block #1', () => {
  445. editor.setData( '<pre><code>foo\nbar</code></pre>' );
  446. expect( getModelData( model ) ).to.equal(
  447. '<codeBlock language="plaintext">[]' +
  448. 'foo' +
  449. '<softBreak></softBreak>' +
  450. 'bar' +
  451. '</codeBlock>'
  452. );
  453. } );
  454. it( 'should convert pre > code with multi-line text to code block #2', () => {
  455. editor.setData( '<pre><code>\n\nfoo\n\n</code></pre>' );
  456. expect( getModelData( model ) ).to.equal(
  457. '<codeBlock language="plaintext">[]' +
  458. '<softBreak></softBreak>' +
  459. '<softBreak></softBreak>' +
  460. 'foo' +
  461. '<softBreak></softBreak>' +
  462. '<softBreak></softBreak>' +
  463. '</codeBlock>'
  464. );
  465. } );
  466. it( 'should convert pre > code with HTML inside', () => {
  467. editor.setData( '<pre><code><p>Foo</p>\n<p>Bar</p></code></pre>' );
  468. expect( getModelData( model ) ).to.equal(
  469. '<codeBlock language="plaintext">[]' +
  470. '<p>Foo</p>' +
  471. '<softBreak></softBreak>' +
  472. '<p>Bar</p>' +
  473. '</codeBlock>'
  474. );
  475. } );
  476. it( 'should convert pre > code tag with HTML and nested pre > code tag', () => {
  477. editor.setData( '<pre><code><p>Foo</p><pre>Bar</pre><p>Biz</p></code></pre>' );
  478. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<p>Foo</p><pre>Bar</pre><p>Biz</p></codeBlock>' );
  479. } );
  480. it( 'should convert pre > code tag with escaped html content', () => {
  481. editor.setData( '<pre><code>&lt;div&gt;&lt;p&gt;Foo&lt;/p&gt;&lt;/div&gt;</code></pre>' );
  482. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<div><p>Foo</p></div></codeBlock>' );
  483. } );
  484. it( 'should be overridable', () => {
  485. editor.data.upcastDispatcher.on( 'element:pre', ( evt, data, api ) => {
  486. const modelItem = api.writer.createElement( 'codeBlock' );
  487. api.writer.appendText( 'Hello World!', modelItem );
  488. api.writer.insert( modelItem, data.modelCursor );
  489. api.consumable.consume( data.viewItem, { name: true } );
  490. data.modelCursor = api.writer.createPositionAfter( modelItem );
  491. data.modelRange = api.writer.createRangeOn( modelItem );
  492. }, { priority: 'high' } );
  493. editor.setData( '<pre><code>Foo Bar</code></pre>' );
  494. expect( getModelData( model ) ).to.equal( '<codeBlock>[]Hello World!</codeBlock>' );
  495. } );
  496. it( 'should split parents to correctly upcast the code block', () => {
  497. editor.setData( '<p>foo<pre><code>x</code></pre>bar</p>' );
  498. // Note: The empty <paragraph> should not be here. It's a conversion/auto–paragraphing bug.
  499. expect( getModelData( model ) ).to.equal(
  500. '<paragraph>[]foo</paragraph>' +
  501. '<codeBlock language="plaintext">x</codeBlock>' +
  502. '<paragraph>bar</paragraph>' +
  503. '<paragraph></paragraph>' );
  504. } );
  505. it( 'should upcast two code blocks in a row (#1)', () => {
  506. editor.setData( '<pre><code>foo</code></pre><pre><code>bar</code></pre>' );
  507. expect( getModelData( model ) ).to.equal(
  508. '<codeBlock language="plaintext">[]foo</codeBlock><codeBlock language="plaintext">bar</codeBlock>' );
  509. } );
  510. it( 'should upcast two code blocks in a row (#2)', () => {
  511. editor.setData( `<pre><code>foo</code></pre>
  512. <pre><code>bar</code></pre>` );
  513. // Note: The empty <paragraph> in between should not be here. It's a conversion/auto–paragraphing bug.
  514. expect( getModelData( model ) ).to.equal(
  515. '<codeBlock language="plaintext">[]foo</codeBlock>' +
  516. '<paragraph> </paragraph>' +
  517. '<codeBlock language="plaintext">bar</codeBlock>' );
  518. } );
  519. it( 'should not convert when modelCursor and its ancestors disallow to insert codeBlock', () => {
  520. model.document.createRoot( '$title', 'title' );
  521. model.schema.register( '$title', {
  522. disallow: '$block',
  523. allow: 'inline'
  524. } );
  525. editor.data.set( { title: '<pre><code>foo</code></pre>' } );
  526. expect( getModelData( model, { rootName: 'title', withoutSelection: true } ) ).to.equal( '' );
  527. } );
  528. } );
  529. describe( 'clipboard integration', () => {
  530. it( 'should not intercept input when selection anchored outside any code block', () => {
  531. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  532. const dataTransferMock = {
  533. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar' )
  534. };
  535. viewDoc.fire( 'clipboardInput', {
  536. dataTransfer: dataTransferMock,
  537. stop: sinon.spy()
  538. } );
  539. expect( getModelData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  540. sinon.assert.notCalled( dataTransferMock.getData );
  541. } );
  542. it( 'should intercept input when selection anchored in the code block', () => {
  543. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  544. const dataTransferMock = {
  545. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar\nbaz\n' )
  546. };
  547. viewDoc.fire( 'clipboardInput', {
  548. dataTransfer: dataTransferMock,
  549. stop: sinon.spy()
  550. } );
  551. expect( getModelData( model ) ).to.equal(
  552. '<codeBlock language="css">' +
  553. 'fbar' +
  554. '<softBreak></softBreak>' +
  555. 'baz' +
  556. '<softBreak></softBreak>' +
  557. '[]o' +
  558. '</codeBlock>' );
  559. sinon.assert.calledOnce( dataTransferMock.getData );
  560. } );
  561. describe( 'getSelectedContent()', () => {
  562. it( 'should not engage when there is nothing selected', () => {
  563. setModelData( model, '<codeBlock language="css">fo[]o<softBreak></softBreak>bar</codeBlock>' );
  564. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal( '' );
  565. } );
  566. it( 'should wrap a partial multi-line selection into a code block (#1)', () => {
  567. setModelData( model, '<codeBlock language="css">fo[o<softBreak></softBreak>b]ar</codeBlock>' );
  568. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  569. '<codeBlock language="css">o<softBreak></softBreak>b</codeBlock>'
  570. );
  571. } );
  572. it( 'should wrap a partial multi-line selection into a code block (#2)', () => {
  573. setModelData( model, '<codeBlock language="css">fo[o<softBreak></softBreak>]bar</codeBlock>' );
  574. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  575. '<codeBlock language="css">o<softBreak></softBreak></codeBlock>'
  576. );
  577. } );
  578. it( 'should wrap a partial multi-line selection into a code block (#3)', () => {
  579. setModelData( model, '<codeBlock language="css">[foo<softBreak></softBreak>bar]</codeBlock>' );
  580. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  581. '<codeBlock language="css">foo<softBreak></softBreak>bar</codeBlock>'
  582. );
  583. } );
  584. it( 'should wrap a complete single-line selection into a code block', () => {
  585. setModelData( model, '<codeBlock language="css">[foo]</codeBlock>' );
  586. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  587. '<codeBlock language="css">foo</codeBlock>'
  588. );
  589. } );
  590. it( 'should wrap a partial single-line selection into an inline code (#1)', () => {
  591. setModelData( model, '<codeBlock language="css">[fo]o<softBreak></softBreak>bar</codeBlock>' );
  592. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  593. '<$text code="true">fo</$text>'
  594. );
  595. } );
  596. it( 'should wrap a partial single-line selection into an inline code (#2)', () => {
  597. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[a]r</codeBlock>' );
  598. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  599. '<$text code="true">a</$text>'
  600. );
  601. } );
  602. it( 'should preserve a code block in a cross-selection (#1)', () => {
  603. setModelData( model, '<paragraph>[x</paragraph><codeBlock language="css">fo]o<softBreak></softBreak>bar</codeBlock>' );
  604. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  605. '<paragraph>x</paragraph><codeBlock language="css">fo</codeBlock>'
  606. );
  607. } );
  608. it( 'should preserve a code block in a cross-selection (#2)', () => {
  609. setModelData( model, '<paragraph>[x</paragraph><codeBlock language="css">foo<softBreak></softBreak>b]ar</codeBlock>' );
  610. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  611. '<paragraph>x</paragraph><codeBlock language="css">foo<softBreak></softBreak>b</codeBlock>'
  612. );
  613. } );
  614. it( 'should preserve a code block in a cross-selection (#3)', () => {
  615. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[ar</codeBlock><paragraph>x]</paragraph>' );
  616. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  617. '<codeBlock language="css">ar</codeBlock><paragraph>x</paragraph>'
  618. );
  619. } );
  620. } );
  621. } );
  622. } );