tableediting.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 TableEditing from '../src/tableediting';
  10. import { formatTable, formattedModelTable, modelTable } from './_utils/utils';
  11. import InsertRowCommand from '../src/commands/insertrowcommand';
  12. import InsertTableCommand from '../src/commands/inserttablecommand';
  13. import InsertColumnCommand from '../src/commands/insertcolumncommand';
  14. import RemoveRowCommand from '../src/commands/removerowcommand';
  15. import RemoveColumnCommand from '../src/commands/removecolumncommand';
  16. import SplitCellCommand from '../src/commands/splitcellcommand';
  17. import MergeCellCommand from '../src/commands/mergecellcommand';
  18. import SetHeaderRowCommand from '../src/commands/setheaderrowcommand';
  19. import SetHeaderColumnCommand from '../src/commands/setheadercolumncommand';
  20. describe( 'TableEditing', () => {
  21. let editor, model;
  22. beforeEach( () => {
  23. return VirtualTestEditor
  24. .create( {
  25. plugins: [ TableEditing, Paragraph ]
  26. } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. model = editor.model;
  30. } );
  31. } );
  32. afterEach( () => {
  33. editor.destroy();
  34. } );
  35. it( 'should set proper schema rules', () => {
  36. // Table:
  37. expect( model.schema.isRegistered( 'table' ) ).to.be.true;
  38. expect( model.schema.isObject( 'table' ) ).to.be.true;
  39. expect( model.schema.isLimit( 'table' ) ).to.be.true;
  40. expect( model.schema.checkChild( [ '$root' ], 'table' ) ).to.be.true;
  41. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingRows' ) ).to.be.true;
  42. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingColumns' ) ).to.be.true;
  43. // Table row:
  44. expect( model.schema.isRegistered( 'tableRow' ) ).to.be.true;
  45. expect( model.schema.isLimit( 'tableRow' ) ).to.be.true;
  46. expect( model.schema.checkChild( [ '$root' ], 'tableRow' ) ).to.be.false;
  47. expect( model.schema.checkChild( [ 'table' ], 'tableRow' ) ).to.be.true;
  48. // Table cell:
  49. expect( model.schema.isRegistered( 'tableCell' ) ).to.be.true;
  50. expect( model.schema.isLimit( 'tableCell' ) ).to.be.true;
  51. expect( model.schema.checkChild( [ '$root' ], 'tableCell' ) ).to.be.false;
  52. expect( model.schema.checkChild( [ 'table' ], 'tableCell' ) ).to.be.false;
  53. expect( model.schema.checkChild( [ 'tableRow' ], 'tableCell' ) ).to.be.true;
  54. expect( model.schema.checkChild( [ 'tableCell' ], 'tableCell' ) ).to.be.false;
  55. expect( model.schema.checkAttribute( [ 'tableCell' ], 'colspan' ) ).to.be.true;
  56. expect( model.schema.checkAttribute( [ 'tableCell' ], 'rowspan' ) ).to.be.true;
  57. // Table cell contents:
  58. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$text' ) ).to.be.false;
  59. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$block' ) ).to.be.true;
  60. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], 'table' ) ).to.be.false;
  61. } );
  62. it( 'adds insertTable command', () => {
  63. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  64. } );
  65. it( 'adds insertRowAbove command', () => {
  66. expect( editor.commands.get( 'insertTableRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  67. } );
  68. it( 'adds insertRowBelow command', () => {
  69. expect( editor.commands.get( 'insertTableRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  70. } );
  71. it( 'adds insertColumnBefore command', () => {
  72. expect( editor.commands.get( 'insertTableColumnBefore' ) ).to.be.instanceOf( InsertColumnCommand );
  73. } );
  74. it( 'adds insertColumnAfter command', () => {
  75. expect( editor.commands.get( 'insertTableColumnAfter' ) ).to.be.instanceOf( InsertColumnCommand );
  76. } );
  77. it( 'adds removeRow command', () => {
  78. expect( editor.commands.get( 'removeTableRow' ) ).to.be.instanceOf( RemoveRowCommand );
  79. } );
  80. it( 'adds removeColumn command', () => {
  81. expect( editor.commands.get( 'removeTableColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  82. } );
  83. it( 'adds splitCellVertically command', () => {
  84. expect( editor.commands.get( 'splitTableCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  85. } );
  86. it( 'adds splitCellHorizontally command', () => {
  87. expect( editor.commands.get( 'splitTableCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  88. } );
  89. it( 'adds mergeCellRight command', () => {
  90. expect( editor.commands.get( 'mergeTableCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  91. } );
  92. it( 'adds mergeCellLeft command', () => {
  93. expect( editor.commands.get( 'mergeTableCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  94. } );
  95. it( 'adds mergeCellDown command', () => {
  96. expect( editor.commands.get( 'mergeTableCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  97. } );
  98. it( 'adds mergeCellUp command', () => {
  99. expect( editor.commands.get( 'mergeTableCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  100. } );
  101. it( 'adds setColumnHeader command', () => {
  102. expect( editor.commands.get( 'setTableColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
  103. } );
  104. it( 'adds setRowHeader command', () => {
  105. expect( editor.commands.get( 'setTableRowHeader' ) ).to.be.instanceOf( SetHeaderRowCommand );
  106. } );
  107. describe( 'conversion in data pipeline', () => {
  108. describe( 'model to view', () => {
  109. it( 'should create tbody section', () => {
  110. setModelData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  111. expect( editor.getData() ).to.equal(
  112. '<figure class="table">' +
  113. '<table>' +
  114. '<tbody>' +
  115. '<tr><td>foo</td></tr>' +
  116. '</tbody>' +
  117. '</table>' +
  118. '</figure>'
  119. );
  120. } );
  121. it( 'should create thead section', () => {
  122. setModelData(
  123. model,
  124. '<table headingRows="1"><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>'
  125. );
  126. expect( editor.getData() ).to.equal(
  127. '<figure class="table">' +
  128. '<table>' +
  129. '<thead>' +
  130. '<tr><th>foo</th></tr>' +
  131. '</thead>' +
  132. '</table>' +
  133. '</figure>'
  134. );
  135. } );
  136. } );
  137. describe( 'view to model', () => {
  138. it( 'should convert table', () => {
  139. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  140. expect( getModelData( model, { withoutSelection: true } ) )
  141. .to.equal( '<table><tableRow><tableCell><paragraph>foo</paragraph></tableCell></tableRow></table>' );
  142. } );
  143. } );
  144. } );
  145. describe( 'caret movement', () => {
  146. let domEvtDataStub;
  147. beforeEach( () => {
  148. domEvtDataStub = {
  149. keyCode: getCode( 'Tab' ),
  150. preventDefault: sinon.spy(),
  151. stopPropagation: sinon.spy()
  152. };
  153. } );
  154. it( 'should do nothing if not tab pressed', () => {
  155. setModelData( model, modelTable( [
  156. [ '11', '12[]' ]
  157. ] ) );
  158. domEvtDataStub.keyCode = getCode( 'a' );
  159. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  160. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  161. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  162. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  163. [ '11', '12[]' ]
  164. ] ) );
  165. } );
  166. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  167. setModelData( model, modelTable( [
  168. [ '11', '12[]' ]
  169. ] ) );
  170. domEvtDataStub.ctrlKey = true;
  171. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  172. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  173. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  174. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  175. [ '11', '12[]' ]
  176. ] ) );
  177. } );
  178. describe( 'on TAB', () => {
  179. it( 'should do nothing if selection is not in a table', () => {
  180. setModelData( model, '[]' + modelTable( [
  181. [ '11', '12' ]
  182. ] ) );
  183. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  184. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  185. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  186. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  187. [ '11', '12' ]
  188. ] ) );
  189. } );
  190. it( 'should move to next cell', () => {
  191. setModelData( model, modelTable( [
  192. [ '11[]', '12' ]
  193. ] ) );
  194. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  195. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  196. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  197. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  198. [ '11', '[12]' ]
  199. ] ) );
  200. } );
  201. it( 'should create another row and move to first cell in new row', () => {
  202. setModelData( model, modelTable( [
  203. [ '11', '[12]' ]
  204. ] ) );
  205. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  206. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  207. [ '11', '12' ],
  208. [ '[]', '' ]
  209. ] ) );
  210. } );
  211. it( 'should move to the first cell of next row if on end of a row', () => {
  212. setModelData( model, modelTable( [
  213. [ '11', '12[]' ],
  214. [ '21', '22' ]
  215. ] ) );
  216. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  217. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  218. [ '11', '12' ],
  219. [ '[21]', '22' ]
  220. ] ) );
  221. } );
  222. it( 'should move to the next table cell if part of block content is selected', () => {
  223. setModelData( model, modelTable( [
  224. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ],
  225. ] ) );
  226. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  227. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  228. [
  229. '11',
  230. '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>',
  231. '[13]'
  232. ],
  233. ] ) );
  234. } );
  235. describe( 'on table widget selected', () => {
  236. beforeEach( () => {
  237. editor.model.schema.register( 'block', {
  238. allowWhere: '$block',
  239. allowContentOf: '$block',
  240. isObject: true
  241. } );
  242. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  243. } );
  244. it( 'should move caret to the first table cell on TAB', () => {
  245. const spy = sinon.spy();
  246. editor.editing.view.document.on( 'keydown', spy );
  247. setModelData( model, '[' + modelTable( [
  248. [ '11', '12' ]
  249. ] ) + ']' );
  250. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  251. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  252. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  253. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  254. [ '[11]', '12' ]
  255. ] ) );
  256. // Should cancel event - so no other tab handler is called.
  257. sinon.assert.notCalled( spy );
  258. } );
  259. it( 'shouldn\'t do anything on other blocks', () => {
  260. const spy = sinon.spy();
  261. editor.editing.view.document.on( 'keydown', spy );
  262. setModelData( model, '[<block>foo</block>]' );
  263. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  264. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  265. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  266. expect( formatTable( getModelData( model ) ) ).to.equal( '[<block>foo</block>]' );
  267. // Should not cancel event.
  268. sinon.assert.calledOnce( spy );
  269. } );
  270. } );
  271. } );
  272. describe( 'on SHIFT+TAB', () => {
  273. beforeEach( () => {
  274. domEvtDataStub.shiftKey = true;
  275. } );
  276. it( 'should do nothing if selection is not in a table', () => {
  277. setModelData( model, '[]' + modelTable( [
  278. [ '11', '12' ]
  279. ] ) );
  280. domEvtDataStub.keyCode = getCode( 'Tab' );
  281. domEvtDataStub.shiftKey = true;
  282. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  283. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  284. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  285. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  286. [ '11', '12' ]
  287. ] ) );
  288. } );
  289. it( 'should move to previous cell', () => {
  290. setModelData( model, modelTable( [
  291. [ '11', '12[]' ]
  292. ] ) );
  293. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  294. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  295. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  296. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  297. [ '[11]', '12' ]
  298. ] ) );
  299. } );
  300. it( 'should not move if caret is in first table cell', () => {
  301. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  302. [ '[]11', '12' ]
  303. ] ) );
  304. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  305. expect( formatTable( getModelData( model ) ) ).to.equal(
  306. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  307. );
  308. } );
  309. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  310. setModelData( model, modelTable( [
  311. [ '11', '12' ],
  312. [ '[]21', '22' ]
  313. ] ) );
  314. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  315. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  316. [ '11', '[12]' ],
  317. [ '21', '22' ]
  318. ] ) );
  319. } );
  320. it( 'should move to the previous table cell if part of block content is selected', () => {
  321. setModelData( model, modelTable( [
  322. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ],
  323. ] ) );
  324. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  325. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  326. [
  327. '[11]',
  328. '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>',
  329. '13'
  330. ],
  331. ] ) );
  332. } );
  333. } );
  334. } );
  335. describe( 'enter key', () => {
  336. let evtDataStub, viewDocument;
  337. beforeEach( () => {
  338. evtDataStub = {
  339. preventDefault: sinon.spy(),
  340. stopPropagation: sinon.spy(),
  341. isSoft: false
  342. };
  343. return VirtualTestEditor
  344. .create( {
  345. plugins: [ TableEditing, Paragraph ]
  346. } )
  347. .then( newEditor => {
  348. editor = newEditor;
  349. sinon.stub( editor, 'execute' );
  350. viewDocument = editor.editing.view.document;
  351. model = editor.model;
  352. } );
  353. } );
  354. it( 'should do nothing if not in table cell', () => {
  355. setModelData( model, '<paragraph>[]foo</paragraph>' );
  356. viewDocument.fire( 'enter', evtDataStub );
  357. sinon.assert.notCalled( editor.execute );
  358. expect( formatTable( getModelData( model ) ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  359. } );
  360. it( 'should do nothing if table cell has already a block content', () => {
  361. setModelData( model, modelTable( [
  362. [ '<paragraph>[]11</paragraph>' ]
  363. ] ) );
  364. viewDocument.fire( 'enter', evtDataStub );
  365. sinon.assert.notCalled( editor.execute );
  366. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  367. [ '<paragraph>[]11</paragraph>' ]
  368. ] ) );
  369. } );
  370. it( 'should do nothing if table cell with a block content is selected as a whole', () => {
  371. setModelData( model, modelTable( [
  372. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  373. ] ) );
  374. viewDocument.fire( 'enter', evtDataStub );
  375. sinon.assert.notCalled( editor.execute );
  376. setModelData( model, modelTable( [
  377. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  378. ] ) );
  379. } );
  380. it( 'should allow default behavior of Shift+Enter pressed', () => {
  381. setModelData( model, modelTable( [
  382. [ '[]11' ]
  383. ] ) );
  384. evtDataStub.isSoft = true;
  385. viewDocument.fire( 'enter', evtDataStub );
  386. sinon.assert.notCalled( editor.execute );
  387. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  388. [ '[]11' ]
  389. ] ) );
  390. } );
  391. } );
  392. } );