8
0

codeblockediting.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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 IndentCodeBlockCommand from '../src/indentcodeblockcommand';
  9. import AlignmentEditing from '@ckeditor/ckeditor5-alignment/src/alignmentediting';
  10. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  11. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  12. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  15. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  16. import IndentEditing from '@ckeditor/ckeditor5-indent/src/indentediting';
  17. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  18. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  19. import { getData as getModelData, setData as setModelData, stringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  20. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  21. import { _clear as clearTranslations, add as addTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  22. describe( 'CodeBlockEditing', () => {
  23. let editor, element, model, view, viewDoc;
  24. before( () => {
  25. addTranslations( 'en', {
  26. 'Plain text': 'Plain text'
  27. } );
  28. addTranslations( 'pl', {
  29. 'Plain text': 'Zwykły tekst'
  30. } );
  31. } );
  32. after( () => {
  33. clearTranslations();
  34. } );
  35. beforeEach( () => {
  36. element = document.createElement( 'div' );
  37. document.body.appendChild( element );
  38. return ClassicTestEditor
  39. .create( element, {
  40. language: 'en',
  41. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph, Undo ]
  42. } )
  43. .then( newEditor => {
  44. editor = newEditor;
  45. model = editor.model;
  46. view = editor.editing.view;
  47. viewDoc = view.document;
  48. } );
  49. } );
  50. afterEach( () => {
  51. return editor.destroy().then( () => element.remove() );
  52. } );
  53. it( 'defines plugin name', () => {
  54. expect( CodeBlockEditing.pluginName ).to.equal( 'CodeBlockEditing' );
  55. } );
  56. it( 'defines plugin dependencies', () => {
  57. expect( CodeBlockEditing.requires ).to.have.members( [ ShiftEnter ] );
  58. } );
  59. describe( 'config', () => {
  60. describe( 'languages', () => {
  61. describe( 'default value', () => {
  62. it( 'should be set', () => {
  63. expect( editor.config.get( 'codeBlock.languages' ) ).to.deep.equal( [
  64. { class: 'plaintext', label: 'Plain text' },
  65. { class: 'c', label: 'C' },
  66. { class: 'cs', label: 'C#' },
  67. { class: 'cpp', label: 'C++' },
  68. { class: 'css', label: 'CSS' },
  69. { class: 'diff', label: 'Diff' },
  70. { class: 'xml', label: 'HTML/XML' },
  71. { class: 'java', label: 'Java' },
  72. { class: 'javascript', label: 'JavaScript' },
  73. { class: 'php', label: 'PHP' },
  74. { class: 'python', label: 'Python' },
  75. { class: 'ruby', label: 'Ruby' },
  76. { class: 'typescript', label: 'TypeScript' }
  77. ] );
  78. } );
  79. } );
  80. it( 'should be recognized when loading data', () => {
  81. return ClassicTestEditor.create(
  82. '<pre><code class="foo">bar</code></pre>' +
  83. '<pre><code class="bar">baz</code></pre>',
  84. {
  85. plugins: [ CodeBlockEditing ],
  86. codeBlock: {
  87. languages: [
  88. { class: 'foo', label: 'Foo' },
  89. { class: 'bar', label: 'Bar' },
  90. ]
  91. }
  92. } )
  93. .then( editor => {
  94. model = editor.model;
  95. expect( getModelData( model ) ).to.equal(
  96. '<codeBlock language="foo">[]bar</codeBlock>' +
  97. '<codeBlock language="bar">baz</codeBlock>'
  98. );
  99. return editor.destroy();
  100. } );
  101. } );
  102. it( 'should use the first if the code in data has no language', () => {
  103. return ClassicTestEditor
  104. .create( '<pre><code>bar</code></pre>', {
  105. plugins: [ CodeBlockEditing ],
  106. codeBlock: {
  107. languages: [
  108. { class: 'foo', label: 'Foo' },
  109. { class: 'bar', label: 'Bar' }
  110. ]
  111. }
  112. } )
  113. .then( editor => {
  114. model = editor.model;
  115. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">[]bar</codeBlock>' );
  116. return editor.destroy();
  117. } );
  118. } );
  119. it( 'should use the first if the code in data has an invalid language', () => {
  120. return ClassicTestEditor
  121. .create( '<pre><code class="baz">bar</code></pre>', {
  122. plugins: [ CodeBlockEditing ],
  123. codeBlock: {
  124. languages: [
  125. { class: 'foo', label: 'Foo' },
  126. { class: 'bar', label: 'Bar' }
  127. ]
  128. }
  129. } )
  130. .then( editor => {
  131. model = editor.model;
  132. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">[]bar</codeBlock>' );
  133. return editor.destroy();
  134. } );
  135. } );
  136. } );
  137. describe( 'indentSequence', () => {
  138. describe( 'default value', () => {
  139. it( 'should be set', () => {
  140. expect( editor.config.get( 'codeBlock.indentSequence' ) ).to.equal( ' ' );
  141. } );
  142. } );
  143. } );
  144. } );
  145. it( 'adds a "codeBlock" command', () => {
  146. expect( editor.commands.get( 'codeBlock' ) ).to.be.instanceOf( CodeBlockCommand );
  147. } );
  148. it( 'adds an "indentCodeBlock" command', () => {
  149. expect( editor.commands.get( 'indentCodeBlock' ) ).to.be.instanceOf( IndentCodeBlockCommand );
  150. } );
  151. it( 'adds an "outdentCodeBlock" command', () => {
  152. expect( editor.commands.get( 'outdentCodeBlock' ) ).to.be.instanceOf( IndentCodeBlockCommand );
  153. } );
  154. it( 'allows for codeBlock in the $root', () => {
  155. expect( model.schema.checkChild( [ '$root' ], 'codeBlock' ) ).to.be.true;
  156. } );
  157. it( 'allows only for $text in codeBlock', () => {
  158. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$text' ) ).to.equal( true );
  159. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$block' ) ).to.equal( false );
  160. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], 'codeBlock' ) ).to.equal( false );
  161. } );
  162. it( 'disallows all attributes (except "language") for codeBlock', () => {
  163. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  164. editor.execute( 'alignment', { value: 'right' } );
  165. editor.execute( 'bold' );
  166. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">f[o]o</codeBlock>' );
  167. } );
  168. describe( 'tab key handling', () => {
  169. let domEvtDataStub;
  170. beforeEach( () => {
  171. domEvtDataStub = {
  172. keyCode: getCode( 'Tab' ),
  173. preventDefault: sinon.spy(),
  174. stopPropagation: sinon.spy()
  175. };
  176. sinon.spy( editor, 'execute' );
  177. } );
  178. afterEach( () => {
  179. editor.execute.restore();
  180. } );
  181. it( 'should execute indentCodeBlock command on tab key', () => {
  182. setModelData( model, '<codeBlock language="plaintext">[]foo</codeBlock>' );
  183. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  184. sinon.assert.calledOnce( editor.execute );
  185. sinon.assert.calledWithExactly( editor.execute, 'indentCodeBlock' );
  186. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  187. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  188. } );
  189. it( 'should execute outdentCodeBlock command on Shift+Tab keystroke', () => {
  190. domEvtDataStub.keyCode += getCode( 'Shift' );
  191. setModelData( model, '<codeBlock language="plaintext">[]foo</codeBlock>' );
  192. // '<codeBlock language="plaintext"> []foo</codeBlock>
  193. model.change( writer => {
  194. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  195. } );
  196. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  197. sinon.assert.calledOnce( editor.execute );
  198. sinon.assert.calledWithExactly( editor.execute, 'outdentCodeBlock' );
  199. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  200. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  201. } );
  202. it( 'should not indent if command is disabled', () => {
  203. setModelData( model, '<paragraph>[]foo</paragraph>' );
  204. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  205. expect( editor.execute.called ).to.be.false;
  206. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  207. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  208. } );
  209. it( 'should not indent or outdent if alt+tab is pressed', () => {
  210. domEvtDataStub.keyCode += getCode( 'alt' );
  211. setModelData( model, '<codeBlock language="plaintext">[]foo</codeBlock>' );
  212. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  213. expect( editor.execute.called ).to.be.false;
  214. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  215. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  216. } );
  217. } );
  218. describe( 'enter key handling', () => {
  219. it( 'should force shiftEnter command when pressing enter inside a codeBlock', () => {
  220. const enterCommand = editor.commands.get( 'enter' );
  221. const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
  222. sinon.spy( enterCommand, 'execute' );
  223. sinon.spy( shiftEnterCommand, 'execute' );
  224. setModelData( model, '<codeBlock>foo[]bar</codeBlock>' );
  225. viewDoc.fire( 'enter', getEvent() );
  226. expect( getModelData( model ) ).to.equal( '<codeBlock>foo<softBreak></softBreak>[]bar</codeBlock>' );
  227. sinon.assert.calledOnce( shiftEnterCommand.execute );
  228. sinon.assert.notCalled( enterCommand.execute );
  229. } );
  230. it( 'should execute enter command when pressing enter out of codeBlock', () => {
  231. const enterCommand = editor.commands.get( 'enter' );
  232. const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
  233. sinon.spy( enterCommand, 'execute' );
  234. sinon.spy( shiftEnterCommand, 'execute' );
  235. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  236. viewDoc.fire( 'enter', getEvent() );
  237. expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  238. sinon.assert.calledOnce( enterCommand.execute );
  239. sinon.assert.notCalled( shiftEnterCommand.execute );
  240. } );
  241. describe( 'indentation retention', () => {
  242. it( 'should work when indentation is with spaces', () => {
  243. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  244. model.change( writer => {
  245. // <codeBlock language="css"> foo[]</codeBlock>
  246. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  247. } );
  248. viewDoc.fire( 'enter', getEvent() );
  249. expect( getModelData( model ) ).to.equal(
  250. '<codeBlock language="css"> foo<softBreak></softBreak> []</codeBlock>' );
  251. editor.execute( 'undo' );
  252. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo[]</codeBlock>' );
  253. } );
  254. it( 'should work when indentation is with tabs', () => {
  255. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  256. model.change( writer => {
  257. // <codeBlock language="css"> foo[]</codeBlock>
  258. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  259. } );
  260. viewDoc.fire( 'enter', getEvent() );
  261. expect( getModelData( model ) ).to.equal(
  262. '<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 retain only the last line', () => {
  267. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>bar[]</codeBlock>' );
  268. model.change( writer => {
  269. // <codeBlock language="css"> foo<softBreak></softBreak> bar[]</codeBlock>
  270. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  271. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  272. } );
  273. viewDoc.fire( 'enter', getEvent() );
  274. expect( getModelData( model ) ).to.equal(
  275. '<codeBlock language="css"> foo<softBreak></softBreak> bar<softBreak></softBreak> []</codeBlock>' );
  276. editor.execute( 'undo' );
  277. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo<softBreak></softBreak> bar[]</codeBlock>' );
  278. } );
  279. it( 'should retain when the selection is non–collapsed', () => {
  280. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  281. model.change( writer => {
  282. // <codeBlock language="css"> f[o]o</codeBlock>
  283. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  284. } );
  285. viewDoc.fire( 'enter', getEvent() );
  286. expect( getModelData( model ) ).to.equal(
  287. '<codeBlock language="css"> f<softBreak></softBreak> []o</codeBlock>' );
  288. editor.execute( 'undo' );
  289. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> f[o]o</codeBlock>' );
  290. } );
  291. it( 'should consider only leading white-spaces', () => {
  292. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  293. model.change( writer => {
  294. // <codeBlock language="css"> foo []</codeBlock>
  295. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 3 );
  296. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  297. } );
  298. viewDoc.fire( 'enter', getEvent() );
  299. expect( getModelData( model ) ).to.equal(
  300. '<codeBlock language="css"> foo <softBreak></softBreak> []</codeBlock>' );
  301. editor.execute( 'undo' );
  302. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo []</codeBlock>' );
  303. } );
  304. it( 'should not work when there is some non-whitespace character', () => {
  305. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  306. model.change( writer => {
  307. // <codeBlock language="css">foo []</codeBlock>
  308. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 3 );
  309. } );
  310. viewDoc.fire( 'enter', getEvent() );
  311. expect( getModelData( model ) ).to.equal(
  312. '<codeBlock language="css">foo <softBreak></softBreak>[]</codeBlock>' );
  313. } );
  314. } );
  315. describe( 'leaving block on double enter', () => {
  316. it( 'should leave the block when pressed twice at the end', () => {
  317. const spy = sinon.spy( editor.editing.view, 'scrollToTheSelection' );
  318. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  319. viewDoc.fire( 'enter', getEvent() );
  320. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  321. viewDoc.fire( 'enter', getEvent() );
  322. expect( getModelData( model ) ).to.equal(
  323. '<codeBlock language="css">foo</codeBlock>' +
  324. '<paragraph>[]</paragraph>'
  325. );
  326. sinon.assert.calledOnce( spy );
  327. editor.execute( 'undo' );
  328. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  329. editor.execute( 'undo' );
  330. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo[]</codeBlock>' );
  331. } );
  332. it( 'should not leave the block when the selection is not collapsed', () => {
  333. setModelData( model, '<codeBlock language="css">f[oo<softBreak></softBreak>]</codeBlock>' );
  334. viewDoc.fire( 'enter', getEvent() );
  335. expect( getModelData( model ) ).to.equal(
  336. '<codeBlock language="css">f<softBreak></softBreak>[]</codeBlock>' );
  337. } );
  338. it( 'should not leave the block when pressed twice when in the middle of the code', () => {
  339. setModelData( model, '<codeBlock language="css">fo[]o</codeBlock>' );
  340. viewDoc.fire( 'enter', getEvent() );
  341. expect( getModelData( model ) ).to.equal(
  342. '<codeBlock language="css">fo<softBreak></softBreak>[]o</codeBlock>' );
  343. viewDoc.fire( 'enter', getEvent() );
  344. expect( getModelData( model ) ).to.equal(
  345. '<codeBlock language="css">fo<softBreak></softBreak><softBreak></softBreak>[]o</codeBlock>' );
  346. } );
  347. it( 'should not leave the block when pressed twice at the beginning of the code', () => {
  348. setModelData( model, '<codeBlock language="css">[]foo</codeBlock>' );
  349. viewDoc.fire( 'enter', getEvent() );
  350. expect( getModelData( model ) ).to.equal(
  351. '<codeBlock language="css"><softBreak></softBreak>[]foo</codeBlock>' );
  352. viewDoc.fire( 'enter', getEvent() );
  353. expect( getModelData( model ) ).to.equal(
  354. '<codeBlock language="css"><softBreak></softBreak><softBreak></softBreak>[]foo</codeBlock>' );
  355. } );
  356. it( 'should not leave the block when pressed shift+enter twice at the end of the code', () => {
  357. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  358. viewDoc.fire( 'enter', getEvent( { isSoft: true } ) );
  359. expect( getModelData( model ) ).to.equal(
  360. '<codeBlock language="css">foo<softBreak></softBreak><softBreak></softBreak>[]</codeBlock>' );
  361. } );
  362. it( 'should clean up the last line if has white–space characters only', () => {
  363. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  364. model.change( writer => {
  365. // <codeBlock language="css">foo<softBreak></softBreak> []</codeBlock>
  366. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  367. } );
  368. viewDoc.fire( 'enter', getEvent() );
  369. expect( getModelData( model ) ).to.equal(
  370. '<codeBlock language="css">foo</codeBlock><paragraph>[]</paragraph>' );
  371. } );
  372. } );
  373. function getEvent( data = {} ) {
  374. return new DomEventData( viewDoc, {
  375. preventDefault: sinon.spy()
  376. }, data );
  377. }
  378. } );
  379. describe( 'intent plugin integration', () => {
  380. it( 'should add indent code block command to indent command', () => {
  381. const element = document.createElement( 'div' );
  382. document.body.appendChild( element );
  383. return ClassicTestEditor
  384. .create( element, {
  385. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph, Undo, IndentEditing ]
  386. } )
  387. .then( newEditor => {
  388. const editor = newEditor;
  389. const indentCodeBlockCommand = editor.commands.get( 'indentCodeBlock' );
  390. const indentCommand = editor.commands.get( 'indent' );
  391. const spy = sinon.spy( indentCodeBlockCommand, 'execute' );
  392. indentCodeBlockCommand.isEnabled = true;
  393. indentCommand.execute();
  394. sinon.assert.calledOnce( spy );
  395. element.remove();
  396. return editor.destroy();
  397. } );
  398. } );
  399. it( 'should add outdent code block command to outdent command', () => {
  400. return ClassicTestEditor
  401. .create( element, {
  402. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph, Undo, IndentEditing ]
  403. } )
  404. .then( newEditor => {
  405. const editor = newEditor;
  406. const outdentCodeBlockCommand = editor.commands.get( 'outdentCodeBlock' );
  407. const outdentCommand = editor.commands.get( 'outdent' );
  408. const spy = sinon.spy( outdentCodeBlockCommand, 'execute' );
  409. outdentCodeBlockCommand.isEnabled = true;
  410. outdentCommand.execute();
  411. sinon.assert.calledOnce( spy );
  412. element.remove();
  413. return editor.destroy();
  414. } );
  415. } );
  416. } );
  417. describe( 'editing pipeline m -> v', () => {
  418. it( 'should convert empty codeBlock to empty pre tag', () => {
  419. setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
  420. expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">[]</code></pre>' );
  421. } );
  422. it( 'should convert non-empty codeBlock to pre tag', () => {
  423. setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
  424. expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">{}Foo</code></pre>' );
  425. } );
  426. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  427. setModelData( model,
  428. '<codeBlock language="plaintext">' +
  429. 'Foo<softBreak></softBreak>' +
  430. 'Bar<softBreak></softBreak>' +
  431. 'Biz' +
  432. '</codeBlock>'
  433. );
  434. expect( getViewData( view ) ).to.equal(
  435. '<pre data-language="Plain text">' +
  436. '<code class="plaintext">{}Foo<br></br>Bar<br></br>Biz</code>' +
  437. '</pre>' );
  438. } );
  439. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  440. setModelData( model,
  441. '<codeBlock language="plaintext">' +
  442. '<softBreak></softBreak>' +
  443. '<softBreak></softBreak>' +
  444. 'Foo' +
  445. '<softBreak></softBreak>' +
  446. '<softBreak></softBreak>' +
  447. '</codeBlock>'
  448. );
  449. expect( getViewData( view ) ).to.equal(
  450. '<pre data-language="Plain text">' +
  451. '<code class="plaintext">[]<br></br><br></br>Foo<br></br><br></br></code>' +
  452. '</pre>' );
  453. } );
  454. it( 'should use localized "Plain text" label', () => {
  455. const element = document.createElement( 'div' );
  456. document.body.appendChild( element );
  457. return ClassicTestEditor
  458. .create( element, {
  459. language: 'pl',
  460. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph ]
  461. } )
  462. .then( newEditor => {
  463. const editor = newEditor;
  464. const model = editor.model;
  465. const view = editor.editing.view;
  466. setModelData( model,
  467. '<codeBlock language="plaintext">foo</codeBlock>'
  468. );
  469. expect( getViewData( view ) ).to.equal(
  470. '<pre data-language="Zwykły tekst">' +
  471. '<code class="plaintext">{}foo</code>' +
  472. '</pre>' );
  473. element.remove();
  474. return editor.destroy();
  475. } );
  476. } );
  477. } );
  478. describe( 'data pipeline m -> v conversion ', () => {
  479. it( 'should convert empty codeBlock to empty pre tag', () => {
  480. setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
  481. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<pre><code class="plaintext">&nbsp;</code></pre>' );
  482. } );
  483. it( 'should convert non-empty codeBlock to pre tag', () => {
  484. setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
  485. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo</code></pre>' );
  486. } );
  487. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  488. setModelData( model,
  489. '<codeBlock language="plaintext">' +
  490. 'Foo<softBreak></softBreak>' +
  491. 'Bar<softBreak></softBreak>' +
  492. 'Biz' +
  493. '</codeBlock>' +
  494. '<paragraph>A<softBreak></softBreak>B</paragraph>'
  495. );
  496. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo\nBar\nBiz</code></pre><p>A<br>B</p>' );
  497. } );
  498. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  499. setModelData( model,
  500. '<codeBlock language="plaintext">' +
  501. '<softBreak></softBreak>' +
  502. '<softBreak></softBreak>' +
  503. 'Foo' +
  504. '<softBreak></softBreak>' +
  505. '<softBreak></softBreak>' +
  506. '</codeBlock>'
  507. );
  508. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">\n\nFoo\n\n</code></pre>' );
  509. } );
  510. it( 'should convert codeBlock with html content', () => {
  511. setModelData( model, '<codeBlock language="plaintext">[]</codeBlock>' );
  512. model.change( writer => writer.insertText( '<div><p>Foo</p></div>', model.document.selection.getFirstPosition() ) );
  513. expect( editor.getData() ).to.equal(
  514. '<pre>' +
  515. '<code class="plaintext">&lt;div&gt;&lt;p&gt;Foo&lt;/p&gt;&lt;/div&gt;</code>' +
  516. '</pre>' );
  517. } );
  518. it( 'should be overridable', () => {
  519. editor.data.downcastDispatcher.on( 'insert:codeBlock', ( evt, data, api ) => {
  520. const targetViewPosition = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
  521. const code = api.writer.createContainerElement( 'code' );
  522. api.consumable.consume( data.item, 'insert' );
  523. api.writer.insert( targetViewPosition, code );
  524. api.mapper.bindElements( data.item, code );
  525. }, { priority: 'high' } );
  526. editor.data.downcastDispatcher.on( 'insert:softBreak', ( evt, data, api ) => {
  527. const position = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
  528. api.consumable.consume( data.item, 'insert' );
  529. api.writer.insert( position, api.writer.createText( '\n' ) );
  530. }, { priority: 'highest' } );
  531. setModelData( model, '<codeBlock language="plaintext">Foo<softBreak></softBreak>Bar</codeBlock>' );
  532. expect( editor.getData() ).to.equal( '<code>Foo\nBar</code>' );
  533. } );
  534. } );
  535. describe( 'data pipeline v -> m conversion ', () => {
  536. it( 'should not convert empty pre tag to code block', () => {
  537. editor.setData( '<pre></pre>' );
  538. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  539. } );
  540. it( 'should not convert pre with no code child to code block', () => {
  541. editor.setData( '<pre><samp></samp></pre>' );
  542. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  543. } );
  544. it( 'should convert pre > code to code block', () => {
  545. editor.setData( '<pre><code></code></pre>' );
  546. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
  547. } );
  548. it( 'should convert pre > code with multi-line text to code block #1', () => {
  549. editor.setData( '<pre><code>foo\nbar</code></pre>' );
  550. expect( getModelData( model ) ).to.equal(
  551. '<codeBlock language="plaintext">[]' +
  552. 'foo' +
  553. '<softBreak></softBreak>' +
  554. 'bar' +
  555. '</codeBlock>'
  556. );
  557. } );
  558. it( 'should convert pre > code with multi-line text to code block #2', () => {
  559. editor.setData( '<pre><code>\n\nfoo\n\n</code></pre>' );
  560. expect( getModelData( model ) ).to.equal(
  561. '<codeBlock language="plaintext">[]' +
  562. '<softBreak></softBreak>' +
  563. '<softBreak></softBreak>' +
  564. 'foo' +
  565. '<softBreak></softBreak>' +
  566. '<softBreak></softBreak>' +
  567. '</codeBlock>'
  568. );
  569. } );
  570. it( 'should convert pre > code with HTML inside', () => {
  571. editor.setData( '<pre><code><p>Foo</p>\n<p>Bar</p></code></pre>' );
  572. expect( getModelData( model ) ).to.equal(
  573. '<codeBlock language="plaintext">[]' +
  574. '<p>Foo</p>' +
  575. '<softBreak></softBreak>' +
  576. '<p>Bar</p>' +
  577. '</codeBlock>'
  578. );
  579. } );
  580. it( 'should convert pre > code tag with HTML and nested pre > code tag', () => {
  581. editor.setData( '<pre><code><p>Foo</p><pre>Bar</pre><p>Biz</p></code></pre>' );
  582. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<p>Foo</p><pre>Bar</pre><p>Biz</p></codeBlock>' );
  583. } );
  584. it( 'should convert pre > code tag with escaped html content', () => {
  585. editor.setData( '<pre><code>&lt;div&gt;&lt;p&gt;Foo&lt;/p&gt;&lt;/div&gt;</code></pre>' );
  586. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<div><p>Foo</p></div></codeBlock>' );
  587. } );
  588. it( 'should be overridable', () => {
  589. editor.data.upcastDispatcher.on( 'element:pre', ( evt, data, api ) => {
  590. const modelItem = api.writer.createElement( 'codeBlock' );
  591. api.writer.appendText( 'Hello World!', modelItem );
  592. api.writer.insert( modelItem, data.modelCursor );
  593. api.consumable.consume( data.viewItem, { name: true } );
  594. data.modelCursor = api.writer.createPositionAfter( modelItem );
  595. data.modelRange = api.writer.createRangeOn( modelItem );
  596. }, { priority: 'high' } );
  597. editor.setData( '<pre><code>Foo Bar</code></pre>' );
  598. expect( getModelData( model ) ).to.equal( '<codeBlock>[]Hello World!</codeBlock>' );
  599. } );
  600. it( 'should split parents to correctly upcast the code block', () => {
  601. editor.setData( '<p>foo<pre><code>x</code></pre>bar</p>' );
  602. // Note: The empty <paragraph> should not be here. It's a conversion/auto–paragraphing bug.
  603. expect( getModelData( model ) ).to.equal(
  604. '<paragraph>[]foo</paragraph>' +
  605. '<codeBlock language="plaintext">x</codeBlock>' +
  606. '<paragraph>bar</paragraph>' +
  607. '<paragraph></paragraph>' );
  608. } );
  609. it( 'should upcast two code blocks in a row (#1)', () => {
  610. editor.setData( '<pre><code>foo</code></pre><pre><code>bar</code></pre>' );
  611. expect( getModelData( model ) ).to.equal(
  612. '<codeBlock language="plaintext">[]foo</codeBlock><codeBlock language="plaintext">bar</codeBlock>' );
  613. } );
  614. it( 'should upcast two code blocks in a row (#2)', () => {
  615. editor.setData( `<pre><code>foo</code></pre>
  616. <pre><code>bar</code></pre>` );
  617. // Note: The empty <paragraph> in between should not be here. It's a conversion/auto–paragraphing bug.
  618. expect( getModelData( model ) ).to.equal(
  619. '<codeBlock language="plaintext">[]foo</codeBlock>' +
  620. '<paragraph> </paragraph>' +
  621. '<codeBlock language="plaintext">bar</codeBlock>' );
  622. } );
  623. it( 'should not convert when modelCursor and its ancestors disallow to insert codeBlock', () => {
  624. model.document.createRoot( '$title', 'title' );
  625. model.schema.register( '$title', {
  626. disallow: '$block',
  627. allow: 'inline'
  628. } );
  629. editor.data.set( { title: '<pre><code>foo</code></pre>' } );
  630. expect( getModelData( model, { rootName: 'title', withoutSelection: true } ) ).to.equal( '' );
  631. } );
  632. } );
  633. describe( 'clipboard integration', () => {
  634. it( 'should not intercept input when selection anchored outside any code block', () => {
  635. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  636. const dataTransferMock = {
  637. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar' )
  638. };
  639. viewDoc.fire( 'clipboardInput', {
  640. dataTransfer: dataTransferMock,
  641. stop: sinon.spy()
  642. } );
  643. expect( getModelData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  644. sinon.assert.notCalled( dataTransferMock.getData );
  645. } );
  646. it( 'should intercept input when selection anchored in the code block', () => {
  647. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  648. const dataTransferMock = {
  649. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar\nbaz\n' )
  650. };
  651. viewDoc.fire( 'clipboardInput', {
  652. dataTransfer: dataTransferMock,
  653. stop: sinon.spy()
  654. } );
  655. expect( getModelData( model ) ).to.equal(
  656. '<codeBlock language="css">' +
  657. 'fbar' +
  658. '<softBreak></softBreak>' +
  659. 'baz' +
  660. '<softBreak></softBreak>' +
  661. '[]o' +
  662. '</codeBlock>' );
  663. sinon.assert.calledOnce( dataTransferMock.getData );
  664. } );
  665. describe( 'getSelectedContent()', () => {
  666. it( 'should not engage when there is nothing selected', () => {
  667. setModelData( model, '<codeBlock language="css">fo[]o<softBreak></softBreak>bar</codeBlock>' );
  668. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal( '' );
  669. } );
  670. it( 'should wrap a partial multi-line selection into a code block (#1)', () => {
  671. setModelData( model, '<codeBlock language="css">fo[o<softBreak></softBreak>b]ar</codeBlock>' );
  672. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  673. '<codeBlock language="css">o<softBreak></softBreak>b</codeBlock>'
  674. );
  675. } );
  676. it( 'should wrap a partial multi-line selection into a code block (#2)', () => {
  677. setModelData( model, '<codeBlock language="css">fo[o<softBreak></softBreak>]bar</codeBlock>' );
  678. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  679. '<codeBlock language="css">o<softBreak></softBreak></codeBlock>'
  680. );
  681. } );
  682. it( 'should wrap a partial multi-line selection into a code block (#3)', () => {
  683. setModelData( model, '<codeBlock language="css">[foo<softBreak></softBreak>bar]</codeBlock>' );
  684. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  685. '<codeBlock language="css">foo<softBreak></softBreak>bar</codeBlock>'
  686. );
  687. } );
  688. it( 'should wrap a complete single-line selection into a code block', () => {
  689. setModelData( model, '<codeBlock language="css">[foo]</codeBlock>' );
  690. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  691. '<codeBlock language="css">foo</codeBlock>'
  692. );
  693. } );
  694. it( 'should wrap a partial single-line selection into an inline code (#1)', () => {
  695. setModelData( model, '<codeBlock language="css">[fo]o<softBreak></softBreak>bar</codeBlock>' );
  696. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  697. '<$text code="true">fo</$text>'
  698. );
  699. } );
  700. it( 'should wrap a partial single-line selection into an inline code (#2)', () => {
  701. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[a]r</codeBlock>' );
  702. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  703. '<$text code="true">a</$text>'
  704. );
  705. } );
  706. it( 'should preserve a code block in a cross-selection (#1)', () => {
  707. setModelData( model, '<paragraph>[x</paragraph><codeBlock language="css">fo]o<softBreak></softBreak>bar</codeBlock>' );
  708. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  709. '<paragraph>x</paragraph><codeBlock language="css">fo</codeBlock>'
  710. );
  711. } );
  712. it( 'should preserve a code block in a cross-selection (#2)', () => {
  713. setModelData( model, '<paragraph>[x</paragraph><codeBlock language="css">foo<softBreak></softBreak>b]ar</codeBlock>' );
  714. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  715. '<paragraph>x</paragraph><codeBlock language="css">foo<softBreak></softBreak>b</codeBlock>'
  716. );
  717. } );
  718. it( 'should preserve a code block in a cross-selection (#3)', () => {
  719. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[ar</codeBlock><paragraph>x]</paragraph>' );
  720. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  721. '<codeBlock language="css">ar</codeBlock><paragraph>x</paragraph>'
  722. );
  723. } );
  724. } );
  725. } );
  726. } );