codeblockediting.js 42 KB

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