codeblockediting.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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( 'disallows for codeBlock in the other codeBlock', () => {
  158. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], 'codeBlock' ) ).to.be.false;
  159. } );
  160. it( 'allows only for $text in codeBlock', () => {
  161. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$text' ) ).to.equal( true );
  162. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$block' ) ).to.equal( false );
  163. expect( model.schema.checkChild( [ '$root', 'codeBlock' ], 'codeBlock' ) ).to.equal( false );
  164. } );
  165. it( 'disallows all attributes (except "language") for codeBlock', () => {
  166. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  167. editor.execute( 'alignment', { value: 'right' } );
  168. editor.execute( 'bold' );
  169. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">f[o]o</codeBlock>' );
  170. } );
  171. describe( 'tab key handling', () => {
  172. let domEvtDataStub;
  173. beforeEach( () => {
  174. domEvtDataStub = {
  175. keyCode: getCode( 'Tab' ),
  176. preventDefault: sinon.spy(),
  177. stopPropagation: sinon.spy()
  178. };
  179. sinon.spy( editor, 'execute' );
  180. } );
  181. afterEach( () => {
  182. editor.execute.restore();
  183. } );
  184. it( 'should execute indentCodeBlock command on tab key', () => {
  185. setModelData( model, '<codeBlock language="plaintext">[]foo</codeBlock>' );
  186. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  187. sinon.assert.calledOnce( editor.execute );
  188. sinon.assert.calledWithExactly( editor.execute, 'indentCodeBlock' );
  189. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  190. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  191. } );
  192. it( 'should execute outdentCodeBlock command on Shift+Tab keystroke', () => {
  193. domEvtDataStub.keyCode += getCode( 'Shift' );
  194. setModelData( model, '<codeBlock language="plaintext">[]foo</codeBlock>' );
  195. // '<codeBlock language="plaintext"> []foo</codeBlock>
  196. model.change( writer => {
  197. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  198. } );
  199. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  200. sinon.assert.calledOnce( editor.execute );
  201. sinon.assert.calledWithExactly( editor.execute, 'outdentCodeBlock' );
  202. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  203. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  204. } );
  205. it( 'should not indent if command is disabled', () => {
  206. setModelData( model, '<paragraph>[]foo</paragraph>' );
  207. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  208. expect( editor.execute.called ).to.be.false;
  209. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  210. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  211. } );
  212. it( 'should not indent or outdent if alt+tab is pressed', () => {
  213. domEvtDataStub.keyCode += getCode( 'alt' );
  214. setModelData( model, '<codeBlock language="plaintext">[]foo</codeBlock>' );
  215. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  216. expect( editor.execute.called ).to.be.false;
  217. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  218. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  219. } );
  220. } );
  221. describe( 'enter key handling', () => {
  222. it( 'should force shiftEnter command when pressing enter inside a codeBlock', () => {
  223. const enterCommand = editor.commands.get( 'enter' );
  224. const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
  225. sinon.spy( enterCommand, 'execute' );
  226. sinon.spy( shiftEnterCommand, 'execute' );
  227. setModelData( model, '<codeBlock>foo[]bar</codeBlock>' );
  228. viewDoc.fire( 'enter', getEvent() );
  229. expect( getModelData( model ) ).to.equal( '<codeBlock>foo<softBreak></softBreak>[]bar</codeBlock>' );
  230. sinon.assert.calledOnce( shiftEnterCommand.execute );
  231. sinon.assert.notCalled( enterCommand.execute );
  232. } );
  233. it( 'should execute enter command when pressing enter out of codeBlock', () => {
  234. const enterCommand = editor.commands.get( 'enter' );
  235. const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
  236. sinon.spy( enterCommand, 'execute' );
  237. sinon.spy( shiftEnterCommand, 'execute' );
  238. setModelData( model, '<paragraph>foo[]bar</paragraph>' );
  239. viewDoc.fire( 'enter', getEvent() );
  240. expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
  241. sinon.assert.calledOnce( enterCommand.execute );
  242. sinon.assert.notCalled( shiftEnterCommand.execute );
  243. } );
  244. describe( 'indentation retention', () => {
  245. it( 'should work when indentation is with spaces', () => {
  246. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  247. model.change( writer => {
  248. // <codeBlock language="css"> foo[]</codeBlock>
  249. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  250. } );
  251. viewDoc.fire( 'enter', getEvent() );
  252. expect( getModelData( model ) ).to.equal(
  253. '<codeBlock language="css"> foo<softBreak></softBreak> []</codeBlock>' );
  254. editor.execute( 'undo' );
  255. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo[]</codeBlock>' );
  256. } );
  257. it( 'should work when indentation is with tabs', () => {
  258. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  259. model.change( writer => {
  260. // <codeBlock language="css"> foo[]</codeBlock>
  261. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  262. } );
  263. viewDoc.fire( 'enter', getEvent() );
  264. expect( getModelData( model ) ).to.equal(
  265. '<codeBlock language="css"> foo<softBreak></softBreak> []</codeBlock>' );
  266. editor.execute( 'undo' );
  267. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo[]</codeBlock>' );
  268. } );
  269. it( 'should retain only the last line', () => {
  270. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>bar[]</codeBlock>' );
  271. model.change( writer => {
  272. // <codeBlock language="css"> foo<softBreak></softBreak> bar[]</codeBlock>
  273. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  274. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  275. } );
  276. viewDoc.fire( 'enter', getEvent() );
  277. expect( getModelData( model ) ).to.equal(
  278. '<codeBlock language="css"> foo<softBreak></softBreak> bar<softBreak></softBreak> []</codeBlock>' );
  279. editor.execute( 'undo' );
  280. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo<softBreak></softBreak> bar[]</codeBlock>' );
  281. } );
  282. it( 'should retain when the selection is non–collapsed', () => {
  283. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  284. model.change( writer => {
  285. // <codeBlock language="css"> f[o]o</codeBlock>
  286. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  287. } );
  288. viewDoc.fire( 'enter', getEvent() );
  289. expect( getModelData( model ) ).to.equal(
  290. '<codeBlock language="css"> f<softBreak></softBreak> []o</codeBlock>' );
  291. editor.execute( 'undo' );
  292. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> f[o]o</codeBlock>' );
  293. } );
  294. it( 'should consider only leading white-spaces', () => {
  295. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  296. model.change( writer => {
  297. // <codeBlock language="css"> foo []</codeBlock>
  298. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 3 );
  299. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  300. } );
  301. viewDoc.fire( 'enter', getEvent() );
  302. expect( getModelData( model ) ).to.equal(
  303. '<codeBlock language="css"> foo <softBreak></softBreak> []</codeBlock>' );
  304. editor.execute( 'undo' );
  305. expect( getModelData( model ) ).to.equal( '<codeBlock language="css"> foo []</codeBlock>' );
  306. } );
  307. it( 'should not work when there is some non-whitespace character', () => {
  308. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  309. model.change( writer => {
  310. // <codeBlock language="css">foo []</codeBlock>
  311. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 3 );
  312. } );
  313. viewDoc.fire( 'enter', getEvent() );
  314. expect( getModelData( model ) ).to.equal(
  315. '<codeBlock language="css">foo <softBreak></softBreak>[]</codeBlock>' );
  316. } );
  317. } );
  318. describe( 'leaving block using the enter key', () => {
  319. describe( 'leaving the block end', () => {
  320. it( 'should leave the block when pressed twice at the end', () => {
  321. const spy = sinon.spy( editor.editing.view, 'scrollToTheSelection' );
  322. setModelData( model, '<codeBlock language="css">foo[]</codeBlock>' );
  323. viewDoc.fire( 'enter', getEvent() );
  324. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  325. viewDoc.fire( 'enter', getEvent() );
  326. expect( getModelData( model ) ).to.equal(
  327. '<codeBlock language="css">foo</codeBlock>' +
  328. '<paragraph>[]</paragraph>'
  329. );
  330. sinon.assert.calledOnce( spy );
  331. editor.execute( 'undo' );
  332. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  333. editor.execute( 'undo' );
  334. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">foo[]</codeBlock>' );
  335. } );
  336. it( 'should not leave the block when the selection is not collapsed', () => {
  337. setModelData( model, '<codeBlock language="css">f[oo<softBreak></softBreak>]</codeBlock>' );
  338. viewDoc.fire( 'enter', getEvent() );
  339. expect( getModelData( model ) ).to.equal(
  340. '<codeBlock language="css">f<softBreak></softBreak>[]</codeBlock>' );
  341. } );
  342. it( 'should not leave the block when pressed twice when in the middle of the code', () => {
  343. setModelData( model, '<codeBlock language="css">fo[]o</codeBlock>' );
  344. viewDoc.fire( 'enter', getEvent() );
  345. expect( getModelData( model ) ).to.equal(
  346. '<codeBlock language="css">fo<softBreak></softBreak>[]o</codeBlock>' );
  347. viewDoc.fire( 'enter', getEvent() );
  348. expect( getModelData( model ) ).to.equal(
  349. '<codeBlock language="css">fo<softBreak></softBreak><softBreak></softBreak>[]o</codeBlock>' );
  350. } );
  351. it( 'should not leave the block when pressed twice at the beginning of the code', () => {
  352. setModelData( model, '<codeBlock language="css">[]foo</codeBlock>' );
  353. viewDoc.fire( 'enter', getEvent() );
  354. expect( getModelData( model ) ).to.equal(
  355. '<codeBlock language="css"><softBreak></softBreak>[]foo</codeBlock>' );
  356. viewDoc.fire( 'enter', getEvent() );
  357. expect( getModelData( model ) ).to.equal(
  358. '<codeBlock language="css"><softBreak></softBreak><softBreak></softBreak>[]foo</codeBlock>' );
  359. } );
  360. it( 'should not leave the block when pressed shift+enter twice at the end of the code', () => {
  361. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  362. viewDoc.fire( 'enter', getEvent( { isSoft: true } ) );
  363. expect( getModelData( model ) ).to.equal(
  364. '<codeBlock language="css">foo<softBreak></softBreak><softBreak></softBreak>[]</codeBlock>' );
  365. } );
  366. it( 'should clean up the last line if has white–space characters only', () => {
  367. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>[]</codeBlock>' );
  368. model.change( writer => {
  369. // <codeBlock language="css">foo<softBreak></softBreak> []</codeBlock>
  370. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  371. } );
  372. viewDoc.fire( 'enter', getEvent() );
  373. expect( getModelData( model ) ).to.equal(
  374. '<codeBlock language="css">foo</codeBlock><paragraph>[]</paragraph>' );
  375. } );
  376. } );
  377. describe( 'leaving the block at the beginning', () => {
  378. it( 'should leave the block when pressed at the beginning in a new line', () => {
  379. const spy = sinon.spy( editor.editing.view, 'scrollToTheSelection' );
  380. setModelData( model, '<codeBlock language="css">[]<softBreak></softBreak>foo</codeBlock>' );
  381. viewDoc.fire( 'enter', getEvent() );
  382. expect( getModelData( model ) ).to.equal(
  383. '<paragraph>[]</paragraph>' +
  384. '<codeBlock language="css">foo</codeBlock>'
  385. );
  386. sinon.assert.calledOnce( spy );
  387. editor.execute( 'undo' );
  388. expect( getModelData( model ) ).to.equal( '<codeBlock language="css">[]<softBreak></softBreak>foo</codeBlock>' );
  389. } );
  390. it( 'should not leave the block when the selection is not collapsed (#1)', () => {
  391. setModelData( model, '<codeBlock language="css">[f]<softBreak></softBreak>oo</codeBlock>' );
  392. viewDoc.fire( 'enter', getEvent() );
  393. expect( getModelData( model ) ).to.equal(
  394. '<codeBlock language="css"><softBreak></softBreak>[]<softBreak></softBreak>oo</codeBlock>' );
  395. } );
  396. it( 'should not leave the block when the selection is not collapsed (#2)', () => {
  397. setModelData( model, '<codeBlock language="css">[<softBreak></softBreak>oo]</codeBlock>' );
  398. viewDoc.fire( 'enter', getEvent() );
  399. expect( getModelData( model ) ).to.equal(
  400. '<codeBlock language="css"><softBreak></softBreak>[]</codeBlock>' );
  401. } );
  402. it( 'should not leave the block when pressed shift+enter at the beginning of the code', () => {
  403. setModelData( model, '<codeBlock language="css">[]<softBreak></softBreak>foo</codeBlock>' );
  404. viewDoc.fire( 'enter', getEvent( { isSoft: true } ) );
  405. expect( getModelData( model ) ).to.equal(
  406. '<codeBlock language="css"><softBreak></softBreak>[]<softBreak></softBreak>foo</codeBlock>' );
  407. } );
  408. it( 'should not leave the block when there is some text after the selection', () => {
  409. setModelData( model, '<codeBlock language="css">[]foo<softBreak></softBreak>foo</codeBlock>' );
  410. viewDoc.fire( 'enter', getEvent() );
  411. expect( getModelData( model ) ).to.equal(
  412. '<codeBlock language="css"><softBreak></softBreak>[]foo<softBreak></softBreak>foo</codeBlock>' );
  413. } );
  414. it( 'should not leave the block when there is some text before the selection', () => {
  415. setModelData( model, '<codeBlock language="css">[]<softBreak></softBreak>foo</codeBlock>' );
  416. // <codeBlock language="css"> []<softBreak></softBreak>foo</codeBlock>
  417. model.change( writer => {
  418. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  419. } );
  420. viewDoc.fire( 'enter', getEvent() );
  421. // Extra spaces before "[]" come from the indentation retention mechanism.
  422. expect( getModelData( model ) ).to.equal(
  423. '<codeBlock language="css"> <softBreak></softBreak> []<softBreak></softBreak>foo</codeBlock>' );
  424. } );
  425. } );
  426. } );
  427. function getEvent( data = {} ) {
  428. return new DomEventData( viewDoc, {
  429. preventDefault: sinon.spy()
  430. }, data );
  431. }
  432. } );
  433. describe( 'indent plugin integration', () => {
  434. it( 'should add indent code block command to indent command', () => {
  435. const element = document.createElement( 'div' );
  436. document.body.appendChild( element );
  437. return ClassicTestEditor
  438. .create( element, {
  439. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph, Undo, IndentEditing ]
  440. } )
  441. .then( newEditor => {
  442. const editor = newEditor;
  443. const indentCodeBlockCommand = editor.commands.get( 'indentCodeBlock' );
  444. const indentCommand = editor.commands.get( 'indent' );
  445. const spy = sinon.spy( indentCodeBlockCommand, 'execute' );
  446. indentCodeBlockCommand.isEnabled = true;
  447. indentCommand.execute();
  448. sinon.assert.calledOnce( spy );
  449. element.remove();
  450. return editor.destroy();
  451. } );
  452. } );
  453. it( 'should add outdent code block command to outdent command', () => {
  454. return ClassicTestEditor
  455. .create( element, {
  456. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph, Undo, IndentEditing ]
  457. } )
  458. .then( newEditor => {
  459. const editor = newEditor;
  460. const outdentCodeBlockCommand = editor.commands.get( 'outdentCodeBlock' );
  461. const outdentCommand = editor.commands.get( 'outdent' );
  462. const spy = sinon.spy( outdentCodeBlockCommand, 'execute' );
  463. outdentCodeBlockCommand.isEnabled = true;
  464. outdentCommand.execute();
  465. sinon.assert.calledOnce( spy );
  466. element.remove();
  467. return editor.destroy();
  468. } );
  469. } );
  470. } );
  471. describe( 'editing pipeline m -> v', () => {
  472. it( 'should convert empty codeBlock to empty pre tag', () => {
  473. setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
  474. expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">[]</code></pre>' );
  475. } );
  476. it( 'should convert non-empty codeBlock to pre tag', () => {
  477. setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
  478. expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">{}Foo</code></pre>' );
  479. } );
  480. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  481. setModelData( model,
  482. '<codeBlock language="plaintext">' +
  483. 'Foo<softBreak></softBreak>' +
  484. 'Bar<softBreak></softBreak>' +
  485. 'Biz' +
  486. '</codeBlock>'
  487. );
  488. expect( getViewData( view ) ).to.equal(
  489. '<pre data-language="Plain text">' +
  490. '<code class="plaintext">{}Foo<br></br>Bar<br></br>Biz</code>' +
  491. '</pre>' );
  492. } );
  493. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  494. setModelData( model,
  495. '<codeBlock language="plaintext">' +
  496. '<softBreak></softBreak>' +
  497. '<softBreak></softBreak>' +
  498. 'Foo' +
  499. '<softBreak></softBreak>' +
  500. '<softBreak></softBreak>' +
  501. '</codeBlock>'
  502. );
  503. expect( getViewData( view ) ).to.equal(
  504. '<pre data-language="Plain text">' +
  505. '<code class="plaintext">[]<br></br><br></br>Foo<br></br><br></br></code>' +
  506. '</pre>' );
  507. } );
  508. it( 'should use localized "Plain text" label', () => {
  509. const element = document.createElement( 'div' );
  510. document.body.appendChild( element );
  511. return ClassicTestEditor
  512. .create( element, {
  513. language: 'pl',
  514. plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph ]
  515. } )
  516. .then( newEditor => {
  517. const editor = newEditor;
  518. const model = editor.model;
  519. const view = editor.editing.view;
  520. setModelData( model,
  521. '<codeBlock language="plaintext">foo</codeBlock>'
  522. );
  523. expect( getViewData( view ) ).to.equal(
  524. '<pre data-language="Zwykły tekst">' +
  525. '<code class="plaintext">{}foo</code>' +
  526. '</pre>' );
  527. element.remove();
  528. return editor.destroy();
  529. } );
  530. } );
  531. } );
  532. describe( 'data pipeline m -> v conversion ', () => {
  533. it( 'should convert empty codeBlock to empty pre tag', () => {
  534. setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
  535. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<pre><code class="plaintext">&nbsp;</code></pre>' );
  536. } );
  537. it( 'should convert non-empty codeBlock to pre tag', () => {
  538. setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
  539. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo</code></pre>' );
  540. } );
  541. it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
  542. setModelData( model,
  543. '<codeBlock language="plaintext">' +
  544. 'Foo<softBreak></softBreak>' +
  545. 'Bar<softBreak></softBreak>' +
  546. 'Biz' +
  547. '</codeBlock>' +
  548. '<paragraph>A<softBreak></softBreak>B</paragraph>'
  549. );
  550. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo\nBar\nBiz</code></pre><p>A<br>B</p>' );
  551. } );
  552. it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
  553. setModelData( model,
  554. '<codeBlock language="plaintext">' +
  555. '<softBreak></softBreak>' +
  556. '<softBreak></softBreak>' +
  557. 'Foo' +
  558. '<softBreak></softBreak>' +
  559. '<softBreak></softBreak>' +
  560. '</codeBlock>'
  561. );
  562. expect( editor.getData() ).to.equal( '<pre><code class="plaintext">\n\nFoo\n\n</code></pre>' );
  563. } );
  564. it( 'should convert codeBlock with html content', () => {
  565. setModelData( model, '<codeBlock language="plaintext">[]</codeBlock>' );
  566. model.change( writer => writer.insertText( '<div><p>Foo</p></div>', model.document.selection.getFirstPosition() ) );
  567. expect( editor.getData() ).to.equal(
  568. '<pre>' +
  569. '<code class="plaintext">&lt;div&gt;&lt;p&gt;Foo&lt;/p&gt;&lt;/div&gt;</code>' +
  570. '</pre>' );
  571. } );
  572. it( 'should be overridable', () => {
  573. editor.data.downcastDispatcher.on( 'insert:codeBlock', ( evt, data, api ) => {
  574. const targetViewPosition = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
  575. const code = api.writer.createContainerElement( 'code' );
  576. api.consumable.consume( data.item, 'insert' );
  577. api.writer.insert( targetViewPosition, code );
  578. api.mapper.bindElements( data.item, code );
  579. }, { priority: 'high' } );
  580. editor.data.downcastDispatcher.on( 'insert:softBreak', ( evt, data, api ) => {
  581. const position = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
  582. api.consumable.consume( data.item, 'insert' );
  583. api.writer.insert( position, api.writer.createText( '\n' ) );
  584. }, { priority: 'highest' } );
  585. setModelData( model, '<codeBlock language="plaintext">Foo<softBreak></softBreak>Bar</codeBlock>' );
  586. expect( editor.getData() ).to.equal( '<code>Foo\nBar</code>' );
  587. } );
  588. } );
  589. describe( 'data pipeline v -> m conversion ', () => {
  590. it( 'should not convert empty pre tag to code block', () => {
  591. editor.setData( '<pre></pre>' );
  592. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  593. } );
  594. it( 'should not convert pre with no code child to code block', () => {
  595. editor.setData( '<pre><samp></samp></pre>' );
  596. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  597. } );
  598. it( 'should convert pre > code to code block', () => {
  599. editor.setData( '<pre><code></code></pre>' );
  600. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
  601. } );
  602. it( 'should convert pre > code with multi-line text to code block #1', () => {
  603. editor.setData( '<pre><code>foo\nbar</code></pre>' );
  604. expect( getModelData( model ) ).to.equal(
  605. '<codeBlock language="plaintext">[]' +
  606. 'foo' +
  607. '<softBreak></softBreak>' +
  608. 'bar' +
  609. '</codeBlock>'
  610. );
  611. } );
  612. it( 'should convert pre > code with multi-line text to code block #2', () => {
  613. editor.setData( '<pre><code>\n\nfoo\n\n</code></pre>' );
  614. expect( getModelData( model ) ).to.equal(
  615. '<codeBlock language="plaintext">[]' +
  616. '<softBreak></softBreak>' +
  617. '<softBreak></softBreak>' +
  618. 'foo' +
  619. '<softBreak></softBreak>' +
  620. '<softBreak></softBreak>' +
  621. '</codeBlock>'
  622. );
  623. } );
  624. it( 'should convert pre > code with HTML inside', () => {
  625. editor.setData( '<pre><code><p>Foo</p>\n<p>Bar</p></code></pre>' );
  626. expect( getModelData( model ) ).to.equal(
  627. '<codeBlock language="plaintext">[]' +
  628. '<p>Foo</p>' +
  629. '<softBreak></softBreak>' +
  630. '<p>Bar</p>' +
  631. '</codeBlock>'
  632. );
  633. } );
  634. it( 'should convert pre > code tag with HTML and nested pre > code tag', () => {
  635. editor.setData( '<pre><code><p>Foo</p><pre>Bar</pre><p>Biz</p></code></pre>' );
  636. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<p>Foo</p><pre>Bar</pre><p>Biz</p></codeBlock>' );
  637. } );
  638. it( 'should convert pre > code tag with escaped html content', () => {
  639. editor.setData( '<pre><code>&lt;div&gt;&lt;p&gt;Foo&lt;/p&gt;&lt;/div&gt;</code></pre>' );
  640. expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<div><p>Foo</p></div></codeBlock>' );
  641. } );
  642. it( 'should be overridable', () => {
  643. editor.data.upcastDispatcher.on( 'element:pre', ( evt, data, api ) => {
  644. const modelItem = api.writer.createElement( 'codeBlock' );
  645. api.writer.appendText( 'Hello World!', modelItem );
  646. api.writer.insert( modelItem, data.modelCursor );
  647. api.consumable.consume( data.viewItem, { name: true } );
  648. data.modelCursor = api.writer.createPositionAfter( modelItem );
  649. data.modelRange = api.writer.createRangeOn( modelItem );
  650. }, { priority: 'high' } );
  651. editor.setData( '<pre><code>Foo Bar</code></pre>' );
  652. expect( getModelData( model ) ).to.equal( '<codeBlock>[]Hello World!</codeBlock>' );
  653. } );
  654. it( 'should split parents to correctly upcast the code block', () => {
  655. editor.setData( '<p>foo<pre><code>x</code></pre>bar</p>' );
  656. // Note: The empty <paragraph> should not be here. It's a conversion/auto–paragraphing bug.
  657. expect( getModelData( model ) ).to.equal(
  658. '<paragraph>[]foo</paragraph>' +
  659. '<codeBlock language="plaintext">x</codeBlock>' +
  660. '<paragraph>bar</paragraph>' +
  661. '<paragraph></paragraph>' );
  662. } );
  663. it( 'should upcast two code blocks in a row (#1)', () => {
  664. editor.setData( '<pre><code>foo</code></pre><pre><code>bar</code></pre>' );
  665. expect( getModelData( model ) ).to.equal(
  666. '<codeBlock language="plaintext">[]foo</codeBlock><codeBlock language="plaintext">bar</codeBlock>' );
  667. } );
  668. it( 'should upcast two code blocks in a row (#2)', () => {
  669. editor.setData( `<pre><code>foo</code></pre>
  670. <pre><code>bar</code></pre>` );
  671. // Note: The empty <paragraph> in between should not be here. It's a conversion/auto–paragraphing bug.
  672. expect( getModelData( model ) ).to.equal(
  673. '<codeBlock language="plaintext">[]foo</codeBlock>' +
  674. '<paragraph> </paragraph>' +
  675. '<codeBlock language="plaintext">bar</codeBlock>' );
  676. } );
  677. it( 'should not convert when modelCursor and its ancestors disallow to insert codeBlock', () => {
  678. model.document.createRoot( '$title', 'title' );
  679. model.schema.register( '$title', {
  680. disallow: '$block',
  681. allow: 'inline'
  682. } );
  683. editor.data.set( { title: '<pre><code>foo</code></pre>' } );
  684. expect( getModelData( model, { rootName: 'title', withoutSelection: true } ) ).to.equal( '' );
  685. } );
  686. } );
  687. describe( 'clipboard integration', () => {
  688. it( 'should not intercept input when selection anchored outside any code block', () => {
  689. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  690. const dataTransferMock = {
  691. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar' )
  692. };
  693. viewDoc.fire( 'clipboardInput', {
  694. dataTransfer: dataTransferMock,
  695. stop: sinon.spy()
  696. } );
  697. expect( getModelData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  698. sinon.assert.notCalled( dataTransferMock.getData );
  699. } );
  700. it( 'should intercept input when selection anchored in the code block', () => {
  701. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  702. const dataTransferMock = {
  703. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar\nbaz\n' )
  704. };
  705. viewDoc.fire( 'clipboardInput', {
  706. dataTransfer: dataTransferMock,
  707. stop: sinon.spy()
  708. } );
  709. expect( getModelData( model ) ).to.equal(
  710. '<codeBlock language="css">' +
  711. 'fbar' +
  712. '<softBreak></softBreak>' +
  713. 'baz' +
  714. '<softBreak></softBreak>' +
  715. '[]o' +
  716. '</codeBlock>' );
  717. sinon.assert.calledOnce( dataTransferMock.getData );
  718. } );
  719. describe( 'getSelectedContent()', () => {
  720. it( 'should not engage when there is nothing selected', () => {
  721. setModelData( model, '<codeBlock language="css">fo[]o<softBreak></softBreak>bar</codeBlock>' );
  722. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal( '' );
  723. } );
  724. it( 'should wrap a partial multi-line selection into a code block (#1)', () => {
  725. setModelData( model, '<codeBlock language="css">fo[o<softBreak></softBreak>b]ar</codeBlock>' );
  726. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  727. '<codeBlock language="css">o<softBreak></softBreak>b</codeBlock>'
  728. );
  729. } );
  730. it( 'should wrap a partial multi-line selection into a code block (#2)', () => {
  731. setModelData( model, '<codeBlock language="css">fo[o<softBreak></softBreak>]bar</codeBlock>' );
  732. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  733. '<codeBlock language="css">o<softBreak></softBreak></codeBlock>'
  734. );
  735. } );
  736. it( 'should wrap a partial multi-line selection into a code block (#3)', () => {
  737. setModelData( model, '<codeBlock language="css">[foo<softBreak></softBreak>bar]</codeBlock>' );
  738. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  739. '<codeBlock language="css">foo<softBreak></softBreak>bar</codeBlock>'
  740. );
  741. } );
  742. it( 'should wrap a complete single-line selection into a code block', () => {
  743. setModelData( model, '<codeBlock language="css">[foo]</codeBlock>' );
  744. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  745. '<codeBlock language="css">foo</codeBlock>'
  746. );
  747. } );
  748. it( 'should wrap a partial single-line selection into an inline code (#1)', () => {
  749. model.schema.extend( '$text', {
  750. allowAttributes: 'code'
  751. } );
  752. setModelData( model, '<codeBlock language="css">[fo]o<softBreak></softBreak>bar</codeBlock>' );
  753. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  754. '<$text code="true">fo</$text>'
  755. );
  756. } );
  757. it( 'should wrap a partial single-line selection into an inline code (#2)', () => {
  758. model.schema.extend( '$text', {
  759. allowAttributes: 'code'
  760. } );
  761. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[a]r</codeBlock>' );
  762. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  763. '<$text code="true">a</$text>'
  764. );
  765. } );
  766. it( 'should now wrap a partial single-line selection into an inline code when the attribute is disallowed', () => {
  767. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[a]r</codeBlock>' );
  768. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal( 'a' );
  769. } );
  770. it( 'should preserve a code block in a cross-selection (#1)', () => {
  771. setModelData( model, '<paragraph>[x</paragraph><codeBlock language="css">fo]o<softBreak></softBreak>bar</codeBlock>' );
  772. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  773. '<paragraph>x</paragraph><codeBlock language="css">fo</codeBlock>'
  774. );
  775. } );
  776. it( 'should preserve a code block in a cross-selection (#2)', () => {
  777. setModelData( model, '<paragraph>[x</paragraph><codeBlock language="css">foo<softBreak></softBreak>b]ar</codeBlock>' );
  778. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  779. '<paragraph>x</paragraph><codeBlock language="css">foo<softBreak></softBreak>b</codeBlock>'
  780. );
  781. } );
  782. it( 'should preserve a code block in a cross-selection (#3)', () => {
  783. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[ar</codeBlock><paragraph>x]</paragraph>' );
  784. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  785. '<codeBlock language="css">ar</codeBlock><paragraph>x</paragraph>'
  786. );
  787. } );
  788. } );
  789. } );
  790. } );