codeblockediting.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import CodeBlockEditing from '../src/codeblockediting';
  7. import CodeBlockCommand from '../src/codeblockcommand';
  8. import IndentCodeBlockCommand from '../src/indentcodeblockcommand';
  9. import 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. it( 'should upcast using only the first class from config as a defining language class', () => {
  801. return ClassicTestEditor.create( '<pre><code class="baz">foo</code></pre>', {
  802. plugins: [ CodeBlockEditing ],
  803. codeBlock: {
  804. languages: [
  805. { language: 'foo', label: 'Foo', class: 'foo' },
  806. { language: 'baz', label: 'Baz', class: 'baz bar' },
  807. { language: 'bar', label: 'Bar', class: 'bar' },
  808. ]
  809. }
  810. } ).then( editor => {
  811. model = editor.model;
  812. expect( getModelData( model ) ).to.equal( '<codeBlock language="baz">[]foo</codeBlock>' );
  813. return editor.destroy();
  814. } );
  815. } );
  816. } );
  817. } );
  818. describe( 'clipboard integration', () => {
  819. it( 'should not intercept input when selection anchored outside any code block', () => {
  820. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  821. const dataTransferMock = {
  822. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar' )
  823. };
  824. viewDoc.fire( 'clipboardInput', {
  825. dataTransfer: dataTransferMock,
  826. stop: sinon.spy()
  827. } );
  828. expect( getModelData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
  829. sinon.assert.notCalled( dataTransferMock.getData );
  830. } );
  831. it( 'should intercept input when selection anchored in the code block', () => {
  832. setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
  833. const dataTransferMock = {
  834. getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar\nbaz\n' )
  835. };
  836. viewDoc.fire( 'clipboardInput', {
  837. dataTransfer: dataTransferMock,
  838. stop: sinon.spy()
  839. } );
  840. expect( getModelData( model ) ).to.equal(
  841. '<codeBlock language="css">' +
  842. 'fbar' +
  843. '<softBreak></softBreak>' +
  844. 'baz' +
  845. '<softBreak></softBreak>' +
  846. '[]o' +
  847. '</codeBlock>' );
  848. sinon.assert.calledOnce( dataTransferMock.getData );
  849. } );
  850. describe( 'getSelectedContent()', () => {
  851. it( 'should not engage when there is nothing selected', () => {
  852. setModelData( model, '<codeBlock language="css">fo[]o<softBreak></softBreak>bar</codeBlock>' );
  853. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal( '' );
  854. } );
  855. it( 'should wrap a partial multi-line selection into a code block (#1)', () => {
  856. setModelData( model, '<codeBlock language="css">fo[o<softBreak></softBreak>b]ar</codeBlock>' );
  857. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  858. '<codeBlock language="css">o<softBreak></softBreak>b</codeBlock>'
  859. );
  860. } );
  861. it( 'should wrap a partial multi-line selection into a code block (#2)', () => {
  862. setModelData( model, '<codeBlock language="css">fo[o<softBreak></softBreak>]bar</codeBlock>' );
  863. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  864. '<codeBlock language="css">o<softBreak></softBreak></codeBlock>'
  865. );
  866. } );
  867. it( 'should wrap a partial multi-line selection into a code block (#3)', () => {
  868. setModelData( model, '<codeBlock language="css">[foo<softBreak></softBreak>bar]</codeBlock>' );
  869. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  870. '<codeBlock language="css">foo<softBreak></softBreak>bar</codeBlock>'
  871. );
  872. } );
  873. it( 'should wrap a complete single-line selection into a code block', () => {
  874. setModelData( model, '<codeBlock language="css">[foo]</codeBlock>' );
  875. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  876. '<codeBlock language="css">foo</codeBlock>'
  877. );
  878. } );
  879. it( 'should wrap a partial single-line selection into an inline code (#1)', () => {
  880. model.schema.extend( '$text', {
  881. allowAttributes: 'code'
  882. } );
  883. setModelData( model, '<codeBlock language="css">[fo]o<softBreak></softBreak>bar</codeBlock>' );
  884. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  885. '<$text code="true">fo</$text>'
  886. );
  887. } );
  888. it( 'should wrap a partial single-line selection into an inline code (#2)', () => {
  889. model.schema.extend( '$text', {
  890. allowAttributes: 'code'
  891. } );
  892. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[a]r</codeBlock>' );
  893. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  894. '<$text code="true">a</$text>'
  895. );
  896. } );
  897. it( 'should now wrap a partial single-line selection into an inline code when the attribute is disallowed', () => {
  898. setModelData( model, '<codeBlock language="css">foo<softBreak></softBreak>b[a]r</codeBlock>' );
  899. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal( 'a' );
  900. } );
  901. it( 'should preserve a code block in a cross-selection (#1)', () => {
  902. setModelData( model,
  903. '<paragraph>[x</paragraph><codeBlock language="css">fo]o<softBreak></softBreak>bar</codeBlock>' );
  904. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  905. '<paragraph>x</paragraph><codeBlock language="css">fo</codeBlock>'
  906. );
  907. } );
  908. it( 'should preserve a code block in a cross-selection (#2)', () => {
  909. setModelData( model,
  910. '<paragraph>[x</paragraph><codeBlock language="css">foo<softBreak></softBreak>b]ar</codeBlock>' );
  911. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  912. '<paragraph>x</paragraph><codeBlock language="css">foo<softBreak></softBreak>b</codeBlock>'
  913. );
  914. } );
  915. it( 'should preserve a code block in a cross-selection (#3)', () => {
  916. setModelData( model,
  917. '<codeBlock language="css">foo<softBreak></softBreak>b[ar</codeBlock><paragraph>x]</paragraph>' );
  918. expect( stringify( model.getSelectedContent( model.document.selection ) ) ).to.equal(
  919. '<codeBlock language="css">ar</codeBlock><paragraph>x</paragraph>'
  920. );
  921. } );
  922. } );
  923. } );
  924. } );