codeblockediting.js 41 KB

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