tableediting.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
  10. import TableEditing from '../src/tableediting';
  11. import { formatTable, formattedModelTable, modelTable } from './_utils/utils';
  12. import InsertRowCommand from '../src/commands/insertrowcommand';
  13. import InsertTableCommand from '../src/commands/inserttablecommand';
  14. import InsertColumnCommand from '../src/commands/insertcolumncommand';
  15. import RemoveRowCommand from '../src/commands/removerowcommand';
  16. import RemoveColumnCommand from '../src/commands/removecolumncommand';
  17. import SplitCellCommand from '../src/commands/splitcellcommand';
  18. import MergeCellCommand from '../src/commands/mergecellcommand';
  19. import SetHeaderRowCommand from '../src/commands/setheaderrowcommand';
  20. import SetHeaderColumnCommand from '../src/commands/setheadercolumncommand';
  21. import MediaEmbedEditing from '@ckeditor/ckeditor5-media-embed/src/mediaembedediting';
  22. describe( 'TableEditing', () => {
  23. let editor, model;
  24. beforeEach( () => {
  25. return VirtualTestEditor
  26. .create( {
  27. plugins: [ TableEditing, Paragraph, ImageEditing, MediaEmbedEditing ]
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. model = editor.model;
  32. } );
  33. } );
  34. afterEach( () => {
  35. editor.destroy();
  36. } );
  37. it( 'should have pluginName', () => {
  38. expect( TableEditing.pluginName ).to.equal( 'TableEditing' );
  39. } );
  40. it( 'should set proper schema rules', () => {
  41. // Table:
  42. expect( model.schema.isRegistered( 'table' ) ).to.be.true;
  43. expect( model.schema.isObject( 'table' ) ).to.be.true;
  44. expect( model.schema.isBlock( 'table' ) ).to.be.true;
  45. expect( model.schema.isLimit( 'table' ) ).to.be.true;
  46. expect( model.schema.checkChild( [ '$root' ], 'table' ) ).to.be.true;
  47. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingRows' ) ).to.be.true;
  48. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingColumns' ) ).to.be.true;
  49. // Table row:
  50. expect( model.schema.isRegistered( 'tableRow' ) ).to.be.true;
  51. expect( model.schema.isLimit( 'tableRow' ) ).to.be.true;
  52. expect( model.schema.checkChild( [ '$root' ], 'tableRow' ) ).to.be.false;
  53. expect( model.schema.checkChild( [ 'table' ], 'tableRow' ) ).to.be.true;
  54. // Table cell:
  55. expect( model.schema.isRegistered( 'tableCell' ) ).to.be.true;
  56. expect( model.schema.isLimit( 'tableCell' ) ).to.be.true;
  57. expect( model.schema.checkChild( [ '$root' ], 'tableCell' ) ).to.be.false;
  58. expect( model.schema.checkChild( [ 'table' ], 'tableCell' ) ).to.be.false;
  59. expect( model.schema.checkChild( [ 'tableRow' ], 'tableCell' ) ).to.be.true;
  60. expect( model.schema.checkChild( [ 'tableCell' ], 'tableCell' ) ).to.be.false;
  61. expect( model.schema.checkAttribute( [ 'tableCell' ], 'colspan' ) ).to.be.true;
  62. expect( model.schema.checkAttribute( [ 'tableCell' ], 'rowspan' ) ).to.be.true;
  63. // Table cell contents:
  64. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$text' ) ).to.be.false;
  65. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$block' ) ).to.be.true;
  66. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], 'table' ) ).to.be.false;
  67. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], 'image' ) ).to.be.true;
  68. } );
  69. it( 'adds insertTable command', () => {
  70. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  71. } );
  72. it( 'adds insertRowAbove command', () => {
  73. expect( editor.commands.get( 'insertTableRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  74. } );
  75. it( 'adds insertRowBelow command', () => {
  76. expect( editor.commands.get( 'insertTableRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  77. } );
  78. it( 'adds insertColumnLeft command', () => {
  79. expect( editor.commands.get( 'insertTableColumnLeft' ) ).to.be.instanceOf( InsertColumnCommand );
  80. } );
  81. it( 'adds insertColumnRight command', () => {
  82. expect( editor.commands.get( 'insertTableColumnRight' ) ).to.be.instanceOf( InsertColumnCommand );
  83. } );
  84. it( 'adds removeRow command', () => {
  85. expect( editor.commands.get( 'removeTableRow' ) ).to.be.instanceOf( RemoveRowCommand );
  86. } );
  87. it( 'adds removeColumn command', () => {
  88. expect( editor.commands.get( 'removeTableColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  89. } );
  90. it( 'adds splitCellVertically command', () => {
  91. expect( editor.commands.get( 'splitTableCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  92. } );
  93. it( 'adds splitCellHorizontally command', () => {
  94. expect( editor.commands.get( 'splitTableCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  95. } );
  96. it( 'adds mergeCellRight command', () => {
  97. expect( editor.commands.get( 'mergeTableCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  98. } );
  99. it( 'adds mergeCellLeft command', () => {
  100. expect( editor.commands.get( 'mergeTableCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  101. } );
  102. it( 'adds mergeCellDown command', () => {
  103. expect( editor.commands.get( 'mergeTableCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  104. } );
  105. it( 'adds mergeCellUp command', () => {
  106. expect( editor.commands.get( 'mergeTableCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  107. } );
  108. it( 'adds setColumnHeader command', () => {
  109. expect( editor.commands.get( 'setTableColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
  110. } );
  111. it( 'adds setRowHeader command', () => {
  112. expect( editor.commands.get( 'setTableRowHeader' ) ).to.be.instanceOf( SetHeaderRowCommand );
  113. } );
  114. describe( 'conversion in data pipeline', () => {
  115. describe( 'model to view', () => {
  116. it( 'should create tbody section', () => {
  117. setModelData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  118. expect( editor.getData() ).to.equal(
  119. '<figure class="table">' +
  120. '<table>' +
  121. '<tbody>' +
  122. '<tr><td>foo</td></tr>' +
  123. '</tbody>' +
  124. '</table>' +
  125. '</figure>'
  126. );
  127. } );
  128. it( 'should create thead section', () => {
  129. setModelData(
  130. model,
  131. '<table headingRows="1"><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>'
  132. );
  133. expect( editor.getData() ).to.equal(
  134. '<figure class="table">' +
  135. '<table>' +
  136. '<thead>' +
  137. '<tr><th>foo</th></tr>' +
  138. '</thead>' +
  139. '</table>' +
  140. '</figure>'
  141. );
  142. } );
  143. } );
  144. describe( 'view to model', () => {
  145. it( 'should convert table', () => {
  146. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  147. expect( getModelData( model, { withoutSelection: true } ) )
  148. .to.equal( '<table><tableRow><tableCell><paragraph>foo</paragraph></tableCell></tableRow></table>' );
  149. } );
  150. it( 'should convert table with image', () => {
  151. editor.setData( '<table><tbody><tr><td><img src="sample.png"></td></tr></tbody></table>' );
  152. expect( getModelData( model, { withoutSelection: true } ) )
  153. .to.equal( '<table><tableRow><tableCell><image src="sample.png"></image></tableCell></tableRow></table>' );
  154. } );
  155. it( 'should insert a paragraph when the cell content is unsupported', () => {
  156. editor.setData(
  157. '<table><tbody><tr><td><foo></foo></td></tr></tbody></table>'
  158. );
  159. expect( getModelData( model, { withoutSelection: true } ) )
  160. .to.equal( '<table><tableRow><tableCell><paragraph></paragraph></tableCell></tableRow></table>' );
  161. } );
  162. it( 'should convert a table with media', () => {
  163. editor.setData(
  164. '<table><tbody><tr><td><oembed url="https://www.youtube.com/watch?v=H08tGjXNHO4"></oembed></td></tr></tbody></table>'
  165. );
  166. expect( getModelData( model, { withoutSelection: true } ) )
  167. .to.equal( '<table><tableRow><tableCell>' +
  168. '<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>' +
  169. '</tableCell></tableRow></table>' );
  170. } );
  171. } );
  172. } );
  173. describe( 'caret movement', () => {
  174. let domEvtDataStub;
  175. beforeEach( () => {
  176. domEvtDataStub = {
  177. keyCode: getCode( 'Tab' ),
  178. preventDefault: sinon.spy(),
  179. stopPropagation: sinon.spy()
  180. };
  181. } );
  182. it( 'should do nothing if not tab pressed', () => {
  183. setModelData( model, modelTable( [
  184. [ '11', '12[]' ]
  185. ] ) );
  186. domEvtDataStub.keyCode = getCode( 'a' );
  187. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  188. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  189. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  190. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  191. [ '11', '12[]' ]
  192. ] ) );
  193. } );
  194. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  195. setModelData( model, modelTable( [
  196. [ '11', '12[]' ]
  197. ] ) );
  198. domEvtDataStub.ctrlKey = true;
  199. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  200. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  201. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  202. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  203. [ '11', '12[]' ]
  204. ] ) );
  205. } );
  206. describe( 'on TAB', () => {
  207. it( 'should do nothing if selection is not in a table', () => {
  208. setModelData( model, '[]' + modelTable( [
  209. [ '11', '12' ]
  210. ] ) );
  211. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  212. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  213. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  214. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  215. [ '11', '12' ]
  216. ] ) );
  217. } );
  218. it( 'should move to next cell', () => {
  219. setModelData( model, modelTable( [
  220. [ '11[]', '12' ]
  221. ] ) );
  222. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  223. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  224. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  225. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  226. [ '11', '[12]' ]
  227. ] ) );
  228. } );
  229. it( 'should create another row and move to first cell in new row', () => {
  230. setModelData( model, modelTable( [
  231. [ '11', '[12]' ]
  232. ] ) );
  233. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  234. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  235. [ '11', '12' ],
  236. [ '[]', '' ]
  237. ] ) );
  238. } );
  239. it( 'should not create another row and not move the caret if insertTableRowBelow command is disabled', () => {
  240. setModelData( model, modelTable( [
  241. [ '11', '12[]' ]
  242. ] ) );
  243. const insertTableRowBelowCommand = editor.commands.get( 'insertTableRowBelow' );
  244. insertTableRowBelowCommand.forceDisabled( 'test' );
  245. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  246. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  247. [ '11', '12[]' ]
  248. ] ) );
  249. } );
  250. it( 'should move to the first cell of next row if on end of a row', () => {
  251. setModelData( model, modelTable( [
  252. [ '11', '12[]' ],
  253. [ '21', '22' ]
  254. ] ) );
  255. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  256. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  257. [ '11', '12' ],
  258. [ '[21]', '22' ]
  259. ] ) );
  260. } );
  261. it( 'should move to the next table cell if part of block content is selected', () => {
  262. setModelData( model, modelTable( [
  263. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ],
  264. ] ) );
  265. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  266. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  267. [
  268. '11',
  269. '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>',
  270. '[13]'
  271. ],
  272. ] ) );
  273. } );
  274. it( 'should move to next cell with an image', () => {
  275. setModelData( model, modelTable( [
  276. [ '11[]', '<paragraph>foo</paragraph><image></image>' ]
  277. ] ) );
  278. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  279. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  280. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  281. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  282. [ '11', '<paragraph>[foo</paragraph><image></image>]' ]
  283. ] ) );
  284. } );
  285. it( 'should move to next cell with an blockQuote', () => {
  286. model.schema.register( 'blockQuote', {
  287. allowWhere: '$block',
  288. allowContentOf: '$root'
  289. } );
  290. editor.conversion.elementToElement( { model: 'blockQuote', view: 'blockquote' } );
  291. setModelData( model, modelTable( [
  292. [ '11[]', '<blockQuote><paragraph>foo</paragraph></blockQuote>' ]
  293. ] ) );
  294. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  295. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  296. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  297. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  298. [ '11', '<blockQuote><paragraph>[foo]</paragraph></blockQuote>' ]
  299. ] ) );
  300. } );
  301. it( 'should listen with lower priority then its children', () => {
  302. // Cancel TAB event.
  303. editor.keystrokes.set( 'Tab', ( data, cancel ) => cancel() );
  304. setModelData( model, modelTable( [
  305. [ '11[]', '12' ]
  306. ] ) );
  307. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  308. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  309. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  310. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  311. [ '11[]', '12' ]
  312. ] ) );
  313. } );
  314. describe( 'on table widget selected', () => {
  315. beforeEach( () => {
  316. editor.model.schema.register( 'block', {
  317. allowWhere: '$block',
  318. allowContentOf: '$block',
  319. isObject: true
  320. } );
  321. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  322. } );
  323. it( 'should move caret to the first table cell on TAB', () => {
  324. const spy = sinon.spy();
  325. editor.keystrokes.set( 'Tab', spy, { priority: 'lowest' } );
  326. setModelData( model, '[' + modelTable( [
  327. [ '11', '12' ]
  328. ] ) + ']' );
  329. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  330. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  331. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  332. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  333. [ '[11]', '12' ]
  334. ] ) );
  335. // Should cancel event - so no other tab handler is called.
  336. sinon.assert.notCalled( spy );
  337. } );
  338. it( 'shouldn\'t do anything on other blocks', () => {
  339. const spy = sinon.spy();
  340. editor.editing.view.document.on( 'keydown', spy );
  341. setModelData( model, '[<block>foo</block>]' );
  342. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  343. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  344. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  345. expect( formatTable( getModelData( model ) ) ).to.equal( '[<block>foo</block>]' );
  346. // Should not cancel event.
  347. sinon.assert.calledOnce( spy );
  348. } );
  349. } );
  350. } );
  351. describe( 'on SHIFT+TAB', () => {
  352. beforeEach( () => {
  353. domEvtDataStub.shiftKey = true;
  354. } );
  355. it( 'should do nothing if selection is not in a table', () => {
  356. setModelData( model, '[]' + modelTable( [
  357. [ '11', '12' ]
  358. ] ) );
  359. domEvtDataStub.keyCode = getCode( 'Tab' );
  360. domEvtDataStub.shiftKey = true;
  361. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  362. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  363. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  364. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  365. [ '11', '12' ]
  366. ] ) );
  367. } );
  368. it( 'should move to previous cell', () => {
  369. setModelData( model, modelTable( [
  370. [ '11', '12[]' ]
  371. ] ) );
  372. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  373. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  374. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  375. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  376. [ '[11]', '12' ]
  377. ] ) );
  378. } );
  379. it( 'should not move if caret is in first table cell', () => {
  380. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  381. [ '[]11', '12' ]
  382. ] ) );
  383. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  384. expect( formatTable( getModelData( model ) ) ).to.equal(
  385. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  386. );
  387. } );
  388. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  389. setModelData( model, modelTable( [
  390. [ '11', '12' ],
  391. [ '[]21', '22' ]
  392. ] ) );
  393. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  394. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  395. [ '11', '[12]' ],
  396. [ '21', '22' ]
  397. ] ) );
  398. } );
  399. it( 'should move to the previous table cell if part of block content is selected', () => {
  400. setModelData( model, modelTable( [
  401. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ],
  402. ] ) );
  403. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  404. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  405. [
  406. '[11]',
  407. '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>',
  408. '13'
  409. ],
  410. ] ) );
  411. } );
  412. it( 'should move to previous cell with an image', () => {
  413. setModelData( model, modelTable( [
  414. [ '<paragraph>foo</paragraph><image></image>', 'bar[]' ]
  415. ] ) );
  416. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  417. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  418. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  419. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  420. [ '<paragraph>[foo</paragraph><image></image>]', 'bar' ]
  421. ] ) );
  422. } );
  423. } );
  424. } );
  425. describe( 'enter key', () => {
  426. let evtDataStub, viewDocument;
  427. beforeEach( () => {
  428. evtDataStub = {
  429. preventDefault: sinon.spy(),
  430. stopPropagation: sinon.spy(),
  431. isSoft: false
  432. };
  433. return VirtualTestEditor
  434. .create( {
  435. plugins: [ TableEditing, Paragraph ]
  436. } )
  437. .then( newEditor => {
  438. editor = newEditor;
  439. sinon.stub( editor, 'execute' );
  440. viewDocument = editor.editing.view.document;
  441. model = editor.model;
  442. } );
  443. } );
  444. it( 'should do nothing if not in table cell', () => {
  445. setModelData( model, '<paragraph>[]foo</paragraph>' );
  446. viewDocument.fire( 'enter', evtDataStub );
  447. sinon.assert.notCalled( editor.execute );
  448. expect( formatTable( getModelData( model ) ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  449. } );
  450. it( 'should do nothing if table cell has already a block content', () => {
  451. setModelData( model, modelTable( [
  452. [ '<paragraph>[]11</paragraph>' ]
  453. ] ) );
  454. viewDocument.fire( 'enter', evtDataStub );
  455. sinon.assert.notCalled( editor.execute );
  456. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  457. [ '<paragraph>[]11</paragraph>' ]
  458. ] ) );
  459. } );
  460. it( 'should do nothing if table cell with a block content is selected as a whole', () => {
  461. setModelData( model, modelTable( [
  462. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  463. ] ) );
  464. viewDocument.fire( 'enter', evtDataStub );
  465. sinon.assert.notCalled( editor.execute );
  466. setModelData( model, modelTable( [
  467. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  468. ] ) );
  469. } );
  470. it( 'should allow default behavior of Shift+Enter pressed', () => {
  471. setModelData( model, modelTable( [
  472. [ '[]11' ]
  473. ] ) );
  474. evtDataStub.isSoft = true;
  475. viewDocument.fire( 'enter', evtDataStub );
  476. sinon.assert.notCalled( editor.execute );
  477. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  478. [ '[]11' ]
  479. ] ) );
  480. } );
  481. } );
  482. } );