tableediting.js 20 KB

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