8
0

codeblockediting.js 36 KB

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