codeblockediting.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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 using the enter key', () => {
  316. describe( 'leaving the block end', () => {
  317. it( 'should leave the block when pressed twice at the end', () => {
  318. const spy = sinon.spy( editor.editing.view, 'scrollToTheSelection' );
  319. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  320. viewDoc.fire( 'enter', getEvent() );
  321. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  322. viewDoc.fire( 'enter', getEvent() );
  323. expect( getModelData( model ) ).to.equal(
  324. '<codeBlock language="css">foo</codeBlock>' +
  325. '<paragraph>[]</paragraph>'
  326. );
  327. sinon.assert.calledOnce( spy );
  328. editor.execute( 'undo' );
  329. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  330. editor.execute( 'undo' );
  331. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo[]</codeBlock>' );
  332. } );
  333. it( 'should not leave the block when the selection is not collapsed', () => {
  334. setModelData( model, '<codeBlock language="css">f[oo<softBreak></softBreak>]</codeBlock>' );
  335. viewDoc.fire( 'enter', getEvent() );
  336. expect( getModelData( model ) ).to.equal(
  337. '<codeBlock language="css">f<softBreak></softBreak>[]</codeBlock>' );
  338. } );
  339. it( 'should not leave the block when pressed twice when in the middle of the code', () => {
  340. setModelData( model, '<codeBlock language="css">fo[]o</codeBlock>' );
  341. viewDoc.fire( 'enter', getEvent() );
  342. expect( getModelData( model ) ).to.equal(
  343. '<codeBlock language="css">fo<softBreak></softBreak>[]o</codeBlock>' );
  344. viewDoc.fire( 'enter', getEvent() );
  345. expect( getModelData( model ) ).to.equal(
  346. '<codeBlock language="css">fo<softBreak></softBreak><softBreak></softBreak>[]o</codeBlock>' );
  347. } );
  348. it( 'should not leave the block when pressed twice at the beginning of the code', () => {
  349. setModelData( model, '<codeBlock language="css">[]foo</codeBlock>' );
  350. viewDoc.fire( 'enter', getEvent() );
  351. expect( getModelData( model ) ).to.equal(
  352. '<codeBlock language="css"><softBreak></softBreak>[]foo</codeBlock>' );
  353. viewDoc.fire( 'enter', getEvent() );
  354. expect( getModelData( model ) ).to.equal(
  355. '<codeBlock language="css"><softBreak></softBreak><softBreak></softBreak>[]foo</codeBlock>' );
  356. } );
  357. it( 'should not leave the block when pressed shift+enter twice at the end of the code', () => {
  358. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  359. viewDoc.fire( 'enter', getEvent( { isSoft: true } ) );
  360. expect( getModelData( model ) ).to.equal(
  361. '<codeBlock language="css">foo<softBreak></softBreak><softBreak></softBreak>[]</codeBlock>' );
  362. } );
  363. it( 'should clean up the last line if has white–space characters only', () => {
  364. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  365. model.change( writer => {
  366. // <codeBlock language="css">foo<softBreak></softBreak> []</codeBlock>
  367. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  368. } );
  369. viewDoc.fire( 'enter', getEvent() );
  370. expect( getModelData( model ) ).to.equal(
  371. '<codeBlock language="css">foo</codeBlock><paragraph>[]</paragraph>' );
  372. } );
  373. } );
  374. describe( 'leaving the block at the beginning', () => {
  375. it( 'should leave the block when pressed at the beginning in a new line', () => {
  376. const spy = sinon.spy( editor.editing.view, 'scrollToTheSelection' );
  377. setModelData( model, '<codeBlock language="css">[]<softBreak></softBreak>foo</codeBlock>' );
  378. viewDoc.fire( 'enter', getEvent() );
  379. expect( getModelData( model ) ).to.equal(
  380. '<paragraph>[]</paragraph>' +
  381. '<codeBlock language="css">foo</codeBlock>'
  382. );
  383. sinon.assert.calledOnce( spy );
  384. editor.execute( 'undo' );
  385. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">[]<softBreak></softBreak>foo</codeBlock>' );
  386. } );
  387. it( 'should not leave the block when the selection is not collapsed (#1)', () => {
  388. setModelData( model, '<codeBlock language="css">[f]<softBreak></softBreak>oo</codeBlock>' );
  389. viewDoc.fire( 'enter', getEvent() );
  390. expect( getModelData( model ) ).to.equal(
  391. '<codeBlock language="css"><softBreak></softBreak>[]<softBreak></softBreak>oo</codeBlock>' );
  392. } );
  393. it( 'should not leave the block when the selection is not collapsed (#2)', () => {
  394. setModelData( model, '<codeBlock language="css">[<softBreak></softBreak>oo]</codeBlock>' );
  395. viewDoc.fire( 'enter', getEvent() );
  396. expect( getModelData( model ) ).to.equal(
  397. '<codeBlock language="css"><softBreak></softBreak>[]</codeBlock>' );
  398. } );
  399. it( 'should not leave the block when pressed shift+enter at the beginning of the code', () => {
  400. setModelData( model, '<codeBlock language="css">[]<softBreak></softBreak>foo</codeBlock>' );
  401. viewDoc.fire( 'enter', getEvent( { isSoft: true } ) );
  402. expect( getModelData( model ) ).to.equal(
  403. '<codeBlock language="css"><softBreak></softBreak>[]<softBreak></softBreak>foo</codeBlock>' );
  404. } );
  405. it( 'should not leave the block when there is some text after the selection', () => {
  406. setModelData( model, '<codeBlock language="css">[]foo<softBreak></softBreak>foo</codeBlock>' );
  407. viewDoc.fire( 'enter', getEvent() );
  408. expect( getModelData( model ) ).to.equal(
  409. '<codeBlock language="css"><softBreak></softBreak>[]foo<softBreak></softBreak>foo</codeBlock>' );
  410. } );
  411. it( 'should not leave the block when there is some text before the selection', () => {
  412. setModelData( model, '<codeBlock language="css">[]<softBreak></softBreak>foo</codeBlock>' );
  413. // <codeBlock language="css"> []<softBreak></softBreak>foo</codeBlock>
  414. model.change( writer => {
  415. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  416. } );
  417. viewDoc.fire( 'enter', getEvent() );
  418. // Extra spaces before "[]" come from the indentation retention mechanism.
  419. expect( getModelData( model ) ).to.equal(
  420. '<codeBlock language="css"> <softBreak></softBreak> []<softBreak></softBreak>foo</codeBlock>' );
  421. } );
  422. } );
  423. } );
  424. function getEvent( data = {} ) {
  425. return new DomEventData( viewDoc, {
  426. preventDefault: sinon.spy()
  427. }, data );
  428. }
  429. } );
  430. describe( 'intent plugin integration', () => {
  431. it( 'should add indent code block command to indent command', () => {
  432. const element = document.createElement( 'div' );
  433. document.body.appendChild( element );
  434. return ClassicTestEditor
  435. .create( element, {
  436. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph, Undo, IndentEditing ]
  437. } )
  438. .then( newEditor => {
  439. const editor = newEditor;
  440. const indentCodeBlockCommand = editor.commands.get( 'indentCodeBlock' );
  441. const indentCommand = editor.commands.get( 'indent' );
  442. const spy = sinon.spy( indentCodeBlockCommand, 'execute' );
  443. indentCodeBlockCommand.isEnabled = true;
  444. indentCommand.execute();
  445. sinon.assert.calledOnce( spy );
  446. element.remove();
  447. return editor.destroy();
  448. } );
  449. } );
  450. it( 'should add outdent code block command to outdent command', () => {
  451. return ClassicTestEditor
  452. .create( element, {
  453. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph, Undo, IndentEditing ]
  454. } )
  455. .then( newEditor => {
  456. const editor = newEditor;
  457. const outdentCodeBlockCommand = editor.commands.get( 'outdentCodeBlock' );
  458. const outdentCommand = editor.commands.get( 'outdent' );
  459. const spy = sinon.spy( outdentCodeBlockCommand, 'execute' );
  460. outdentCodeBlockCommand.isEnabled = true;
  461. outdentCommand.execute();
  462. sinon.assert.calledOnce( spy );
  463. element.remove();
  464. return editor.destroy();
  465. } );
  466. } );
  467. } );
  468. describe( 'editing pipeline m -> v', () => {
  469. it( 'should convert empty codeBlock to empty pre tag', () => {
  470. setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
  471. expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">[]</code></pre>' );
  472. } );
  473. it( 'should convert non-empty codeBlock to pre tag', () => {
  474. setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
  475. expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">{}Foo</code></pre>' );
  476. } );
  477. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  478. setModelData( model,
  479. '<codeBlock language="plaintext">' +
  480. 'Foo<softBreak></softBreak>' +
  481. 'Bar<softBreak></softBreak>' +
  482. 'Biz' +
  483. '</codeBlock>'
  484. );
  485. expect( getViewData( view ) ).to.equal(
  486. '<pre data-language="Plain text">' +
  487. '<code class="plaintext">{}Foo<br></br>Bar<br></br>Biz</code>' +
  488. '</pre>' );
  489. } );
  490. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  491. setModelData( model,
  492. '<codeBlock language="plaintext">' +
  493. '<softBreak></softBreak>' +
  494. '<softBreak></softBreak>' +
  495. 'Foo' +
  496. '<softBreak></softBreak>' +
  497. '<softBreak></softBreak>' +
  498. '</codeBlock>'
  499. );
  500. expect( getViewData( view ) ).to.equal(
  501. '<pre data-language="Plain text">' +
  502. '<code class="plaintext">[]<br></br><br></br>Foo<br></br><br></br></code>' +
  503. '</pre>' );
  504. } );
  505. it( 'should use localized "Plain text" label', () => {
  506. const element = document.createElement( 'div' );
  507. document.body.appendChild( element );
  508. return ClassicTestEditor
  509. .create( element, {
  510. language: 'pl',
  511. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph ]
  512. } )
  513. .then( newEditor => {
  514. const editor = newEditor;
  515. const model = editor.model;
  516. const view = editor.editing.view;
  517. setModelData( model,
  518. '<codeBlock language="plaintext">foo</codeBlock>'
  519. );
  520. expect( getViewData( view ) ).to.equal(
  521. '<pre data-language="Zwykły tekst">' +
  522. '<code class="plaintext">{}foo</code>' +
  523. '</pre>' );
  524. element.remove();
  525. return editor.destroy();
  526. } );
  527. } );
  528. } );
  529. describe( 'data pipeline m -> v conversion ', () => {
  530. it( 'should convert empty codeBlock to empty pre tag', () => {
  531. setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
  532. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<pre><code class="plaintext">&nbsp;</code></pre>' );
  533. } );
  534. it( 'should convert non-empty codeBlock to pre tag', () => {
  535. setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
  536. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo</code></pre>' );
  537. } );
  538. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  539. setModelData( model,
  540. '<codeBlock language="plaintext">' +
  541. 'Foo<softBreak></softBreak>' +
  542. 'Bar<softBreak></softBreak>' +
  543. 'Biz' +
  544. '</codeBlock>' +
  545. '<paragraph>A<softBreak></softBreak>B</paragraph>'
  546. );
  547. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo\nBar\nBiz</code></pre><p>A<br>B</p>' );
  548. } );
  549. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  550. setModelData( model,
  551. '<codeBlock language="plaintext">' +
  552. '<softBreak></softBreak>' +
  553. '<softBreak></softBreak>' +
  554. 'Foo' +
  555. '<softBreak></softBreak>' +
  556. '<softBreak></softBreak>' +
  557. '</codeBlock>'
  558. );
  559. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">\n\nFoo\n\n</code></pre>' );
  560. } );
  561. it( 'should convert codeBlock with html content', () => {
  562. setModelData( model, '<codeBlock language="plaintext">[]</codeBlock>' );
  563. model.change( writer => writer.insertText( '<div><p>Foo</p></div>', model.document.selection.getFirstPosition() ) );
  564. expect( editor.getData() ).to.equal(
  565. '<pre>' +
  566. '<code class="plaintext">&lt;div&gt;&lt;p&gt;Foo&lt;/p&gt;&lt;/div&gt;</code>' +
  567. '</pre>' );
  568. } );
  569. it( 'should be overridable', () => {
  570. editor.data.downcastDispatcher.on( 'insert:codeBlock', ( evt, data, api ) => {
  571. const targetViewPosition = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
  572. const code = api.writer.createContainerElement( 'code' );
  573. api.consumable.consume( data.item, 'insert' );
  574. api.writer.insert( targetViewPosition, code );
  575. api.mapper.bindElements( data.item, code );
  576. }, { priority: 'high' } );
  577. editor.data.downcastDispatcher.on( 'insert:softBreak', ( evt, data, api ) => {
  578. const position = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
  579. api.consumable.consume( data.item, 'insert' );
  580. api.writer.insert( position, api.writer.createText( '\n' ) );
  581. }, { priority: 'highest' } );
  582. setModelData( model, '<codeBlock language="plaintext">Foo<softBreak></softBreak>Bar</codeBlock>' );
  583. expect( editor.getData() ).to.equal( '<code>Foo\nBar</code>' );
  584. } );
  585. } );
  586. describe( 'data pipeline v -> m conversion ', () => {
  587. it( 'should not convert empty pre tag to code block', () => {
  588. editor.setData( '<pre></pre>' );
  589. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  590. } );
  591. it( 'should not convert pre with no code child to code block', () => {
  592. editor.setData( '<pre><samp></samp></pre>' );
  593. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  594. } );
  595. it( 'should convert pre > code to code block', () => {
  596. editor.setData( '<pre><code></code></pre>' );
  597. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
  598. } );
  599. it( 'should convert pre > code with multi-line text to code block #1', () => {
  600. editor.setData( '<pre><code>foo\nbar</code></pre>' );
  601. expect( getModelData( model ) ).to.equal(
  602. '<codeBlock language="plaintext">[]' +
  603. 'foo' +
  604. '<softBreak></softBreak>' +
  605. 'bar' +
  606. '</codeBlock>'
  607. );
  608. } );
  609. it( 'should convert pre > code with multi-line text to code block #2', () => {
  610. editor.setData( '<pre><code>\n\nfoo\n\n</code></pre>' );
  611. expect( getModelData( model ) ).to.equal(
  612. '<codeBlock language="plaintext">[]' +
  613. '<softBreak></softBreak>' +
  614. '<softBreak></softBreak>' +
  615. 'foo' +
  616. '<softBreak></softBreak>' +
  617. '<softBreak></softBreak>' +
  618. '</codeBlock>'
  619. );
  620. } );
  621. it( 'should convert pre > code with HTML inside', () => {
  622. editor.setData( '<pre><code><p>Foo</p>\n<p>Bar</p></code></pre>' );
  623. expect( getModelData( model ) ).to.equal(
  624. '<codeBlock language="plaintext">[]' +
  625. '<p>Foo</p>' +
  626. '<softBreak></softBreak>' +
  627. '<p>Bar</p>' +
  628. '</codeBlock>'
  629. );
  630. } );
  631. it( 'should convert pre > code tag with HTML and nested pre > code tag', () => {
  632. editor.setData( '<pre><code><p>Foo</p><pre>Bar</pre><p>Biz</p></code></pre>' );
  633. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<p>Foo</p><pre>Bar</pre><p>Biz</p></codeBlock>' );
  634. } );
  635. it( 'should convert pre > code tag with escaped html content', () => {
  636. editor.setData( '<pre><code>&lt;div&gt;&lt;p&gt;Foo&lt;/p&gt;&lt;/div&gt;</code></pre>' );
  637. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<div><p>Foo</p></div></codeBlock>' );
  638. } );
  639. it( 'should be overridable', () => {
  640. editor.data.upcastDispatcher.on( 'element:pre', ( evt, data, api ) => {
  641. const modelItem = api.writer.createElement( 'codeBlock' );
  642. api.writer.appendText( 'Hello World!', modelItem );
  643. api.writer.insert( modelItem, data.modelCursor );
  644. api.consumable.consume( data.viewItem, { name: true } );
  645. data.modelCursor = api.writer.createPositionAfter( modelItem );
  646. data.modelRange = api.writer.createRangeOn( modelItem );
  647. }, { priority: 'high' } );
  648. editor.setData( '<pre><code>Foo Bar</code></pre>' );
  649. expect( getModelData( model ) ).to.equal( '<codeBlock>[]Hello World!</codeBlock>' );
  650. } );
  651. it( 'should split parents to correctly upcast the code block', () => {
  652. editor.setData( '<p>foo<pre><code>x</code></pre>bar</p>' );
  653. // Note: The empty <paragraph> should not be here. It's a conversion/auto–paragraphing bug.
  654. expect( getModelData( model ) ).to.equal(
  655. '<paragraph>[]foo</paragraph>' +
  656. '<codeBlock language="plaintext">x</codeBlock>' +
  657. '<paragraph>bar</paragraph>' +
  658. '<paragraph></paragraph>' );
  659. } );
  660. it( 'should upcast two code blocks in a row (#1)', () => {
  661. editor.setData( '<pre><code>foo</code></pre><pre><code>bar</code></pre>' );
  662. expect( getModelData( model ) ).to.equal(
  663. '<codeBlock language="plaintext">[]foo</codeBlock><codeBlock language="plaintext">bar</codeBlock>' );
  664. } );
  665. it( 'should upcast two code blocks in a row (#2)', () => {
  666. editor.setData( `<pre><code>foo</code></pre>
  667. <pre><code>bar</code></pre>` );
  668. // Note: The empty <paragraph> in between should not be here. It's a conversion/auto–paragraphing bug.
  669. expect( getModelData( model ) ).to.equal(
  670. '<codeBlock language="plaintext">[]foo</codeBlock>' +
  671. '<paragraph> </paragraph>' +
  672. '<codeBlock language="plaintext">bar</codeBlock>' );
  673. } );
  674. it( 'should not convert when modelCursor and its ancestors disallow to insert codeBlock', () => {
  675. model.document.createRoot( '$title', 'title' );
  676. model.schema.register( '$title', {
  677. disallow: '$block',
  678. allow: 'inline'
  679. } );
  680. editor.data.set( { title: '<pre><code>foo</code></pre>' } );
  681. expect( getModelData( model, { rootName: 'title', withoutSelection: true } ) ).to.equal( '' );
  682. } );
  683. } );
  684. describe( 'clipboard integration', () => {
  685. it( 'should not intercept input when selection anchored outside any code block', () => {
  686. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  687. const dataTransferMock = {
  688. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar' )
  689. };
  690. viewDoc.fire( 'clipboardInput', {
  691. dataTransfer: dataTransferMock,
  692. stop: sinon.spy()
  693. } );
  694. expect( getModelData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  695. sinon.assert.notCalled( dataTransferMock.getData );
  696. } );
  697. it( 'should intercept input when selection anchored in the code block', () => {
  698. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  699. const dataTransferMock = {
  700. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar\nbaz\n' )
  701. };
  702. viewDoc.fire( 'clipboardInput', {
  703. dataTransfer: dataTransferMock,
  704. stop: sinon.spy()
  705. } );
  706. expect( getModelData( model ) ).to.equal(
  707. '<codeBlock language="css">' +
  708. 'fbar' +
  709. '<softBreak></softBreak>' +
  710. 'baz' +
  711. '<softBreak></softBreak>' +
  712. '[]o' +
  713. '</codeBlock>' );
  714. sinon.assert.calledOnce( dataTransferMock.getData );
  715. } );
  716. describe( 'getSelectedContent()', () => {
  717. it( 'should not engage when there is nothing selected', () => {
  718. setModelData( model, '<codeBlock language="css">fo[]o<softBreak></softBreak>bar</codeBlock>' );
  719. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal( '' );
  720. } );
  721. it( 'should wrap a partial multi-line selection into a code block (#1)', () => {
  722. setModelData( model, '<codeBlock language="css">fo[o<softBreak></softBreak>b]ar</codeBlock>' );
  723. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  724. '<codeBlock language="css">o<softBreak></softBreak>b</codeBlock>'
  725. );
  726. } );
  727. it( 'should wrap a partial multi-line selection into a code block (#2)', () => {
  728. setModelData( model, '<codeBlock language="css">fo[o<softBreak></softBreak>]bar</codeBlock>' );
  729. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  730. '<codeBlock language="css">o<softBreak></softBreak></codeBlock>'
  731. );
  732. } );
  733. it( 'should wrap a partial multi-line selection into a code block (#3)', () => {
  734. setModelData( model, '<codeBlock language="css">[foo<softBreak></softBreak>bar]</codeBlock>' );
  735. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  736. '<codeBlock language="css">foo<softBreak></softBreak>bar</codeBlock>'
  737. );
  738. } );
  739. it( 'should wrap a complete single-line selection into a code block', () => {
  740. setModelData( model, '<codeBlock language="css">[foo]</codeBlock>' );
  741. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  742. '<codeBlock language="css">foo</codeBlock>'
  743. );
  744. } );
  745. it( 'should wrap a partial single-line selection into an inline code (#1)', () => {
  746. setModelData( model, '<codeBlock language="css">[fo]o<softBreak></softBreak>bar</codeBlock>' );
  747. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  748. '<$text code="true">fo</$text>'
  749. );
  750. } );
  751. it( 'should wrap a partial single-line selection into an inline code (#2)', () => {
  752. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[a]r</codeBlock>' );
  753. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  754. '<$text code="true">a</$text>'
  755. );
  756. } );
  757. it( 'should preserve a code block in a cross-selection (#1)', () => {
  758. setModelData( model, '<paragraph>[x</paragraph><codeBlock language="css">fo]o<softBreak></softBreak>bar</codeBlock>' );
  759. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  760. '<paragraph>x</paragraph><codeBlock language="css">fo</codeBlock>'
  761. );
  762. } );
  763. it( 'should preserve a code block in a cross-selection (#2)', () => {
  764. setModelData( model, '<paragraph>[x</paragraph><codeBlock language="css">foo<softBreak></softBreak>b]ar</codeBlock>' );
  765. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  766. '<paragraph>x</paragraph><codeBlock language="css">foo<softBreak></softBreak>b</codeBlock>'
  767. );
  768. } );
  769. it( 'should preserve a code block in a cross-selection (#3)', () => {
  770. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[ar</codeBlock><paragraph>x]</paragraph>' );
  771. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  772. '<codeBlock language="css">ar</codeBlock><paragraph>x</paragraph>'
  773. );
  774. } );
  775. } );
  776. } );
  777. } );