tableediting.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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.true;
  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>foo[]</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( model, '<table headingRows="1"><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  123. expect( editor.getData() ).to.equal(
  124. '<figure class="table">' +
  125. '<table>' +
  126. '<thead>' +
  127. '<tr><th>foo</th></tr>' +
  128. '</thead>' +
  129. '</table>' +
  130. '</figure>'
  131. );
  132. } );
  133. } );
  134. describe( 'view to model', () => {
  135. it( 'should convert table', () => {
  136. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  137. expect( getModelData( model, { withoutSelection: true } ) )
  138. .to.equal( '<table><tableRow><tableCell>foo</tableCell></tableRow></table>' );
  139. } );
  140. } );
  141. } );
  142. describe( 'caret movement', () => {
  143. let domEvtDataStub;
  144. beforeEach( () => {
  145. domEvtDataStub = {
  146. keyCode: getCode( 'Tab' ),
  147. preventDefault: sinon.spy(),
  148. stopPropagation: sinon.spy()
  149. };
  150. } );
  151. it( 'should do nothing if not tab pressed', () => {
  152. setModelData( model, modelTable( [
  153. [ '11', '12[]' ]
  154. ] ) );
  155. domEvtDataStub.keyCode = getCode( 'a' );
  156. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  157. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  158. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  159. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  160. [ '11', '12[]' ]
  161. ] ) );
  162. } );
  163. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  164. setModelData( model, modelTable( [
  165. [ '11', '12[]' ]
  166. ] ) );
  167. domEvtDataStub.ctrlKey = true;
  168. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  169. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  170. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  171. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  172. [ '11', '12[]' ]
  173. ] ) );
  174. } );
  175. describe( 'on TAB', () => {
  176. it( 'should do nothing if selection is not in a table', () => {
  177. setModelData( model, '[]' + modelTable( [
  178. [ '11', '12' ]
  179. ] ) );
  180. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  181. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  182. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  183. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  184. [ '11', '12' ]
  185. ] ) );
  186. } );
  187. it( 'should move to next cell', () => {
  188. setModelData( model, modelTable( [
  189. [ '11[]', '12' ]
  190. ] ) );
  191. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  192. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  193. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  194. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  195. [ '11', '[12]' ]
  196. ] ) );
  197. } );
  198. it( 'should create another row and move to first cell in new row', () => {
  199. setModelData( model, modelTable( [
  200. [ '11', '[12]' ]
  201. ] ) );
  202. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  203. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  204. [ '11', '12' ],
  205. [ '[]', '' ]
  206. ] ) );
  207. } );
  208. it( 'should move to the first cell of next row if on end of a row', () => {
  209. setModelData( model, modelTable( [
  210. [ '11', '12[]' ],
  211. [ '21', '22' ]
  212. ] ) );
  213. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  214. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  215. [ '11', '12' ],
  216. [ '[21]', '22' ]
  217. ] ) );
  218. } );
  219. it( 'should move to the next table cell if part of block content is selected', () => {
  220. setModelData( model, modelTable( [
  221. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ],
  222. ] ) );
  223. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  224. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  225. [ '11', '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>', '[13]' ],
  226. ] ) );
  227. } );
  228. describe( 'on table widget selected', () => {
  229. beforeEach( () => {
  230. editor.model.schema.register( 'block', {
  231. allowWhere: '$block',
  232. allowContentOf: '$block',
  233. isObject: true
  234. } );
  235. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  236. } );
  237. it( 'should move caret to the first table cell on TAB', () => {
  238. const spy = sinon.spy();
  239. editor.editing.view.document.on( 'keydown', spy );
  240. setModelData( model, '[' + modelTable( [
  241. [ '11', '12' ]
  242. ] ) + ']' );
  243. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  244. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  245. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  246. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  247. [ '[11]', '12' ]
  248. ] ) );
  249. // Should cancel event - so no other tab handler is called.
  250. sinon.assert.notCalled( spy );
  251. } );
  252. it( 'shouldn\'t do anything on other blocks', () => {
  253. const spy = sinon.spy();
  254. editor.editing.view.document.on( 'keydown', spy );
  255. setModelData( model, '[<block>foo</block>]' );
  256. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  257. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  258. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  259. expect( formatTable( getModelData( model ) ) ).to.equal( '[<block>foo</block>]' );
  260. // Should not cancel event.
  261. sinon.assert.calledOnce( spy );
  262. } );
  263. } );
  264. } );
  265. describe( 'on SHIFT+TAB', () => {
  266. beforeEach( () => {
  267. domEvtDataStub.shiftKey = true;
  268. } );
  269. it( 'should do nothing if selection is not in a table', () => {
  270. setModelData( model, '[]' + modelTable( [
  271. [ '11', '12' ]
  272. ] ) );
  273. domEvtDataStub.keyCode = getCode( 'Tab' );
  274. domEvtDataStub.shiftKey = true;
  275. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  276. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  277. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  278. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  279. [ '11', '12' ]
  280. ] ) );
  281. } );
  282. it( 'should move to previous cell', () => {
  283. setModelData( model, modelTable( [
  284. [ '11', '12[]' ]
  285. ] ) );
  286. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  287. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  288. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  289. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  290. [ '[11]', '12' ]
  291. ] ) );
  292. } );
  293. it( 'should not move if caret is in first table cell', () => {
  294. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  295. [ '[]11', '12' ]
  296. ] ) );
  297. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  298. expect( formatTable( getModelData( model ) ) ).to.equal(
  299. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  300. );
  301. } );
  302. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  303. setModelData( model, modelTable( [
  304. [ '11', '12' ],
  305. [ '[]21', '22' ]
  306. ] ) );
  307. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  308. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  309. [ '11', '[12]' ],
  310. [ '21', '22' ]
  311. ] ) );
  312. } );
  313. it( 'should move to the previous table cell if part of block content is selected', () => {
  314. setModelData( model, modelTable( [
  315. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ],
  316. ] ) );
  317. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  318. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  319. [ '[11]', '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>', '13' ],
  320. ] ) );
  321. } );
  322. } );
  323. } );
  324. describe( 'enter key', () => {
  325. let evtDataStub, viewDocument;
  326. beforeEach( () => {
  327. evtDataStub = {
  328. preventDefault: sinon.spy(),
  329. stopPropagation: sinon.spy(),
  330. isSoft: false
  331. };
  332. return VirtualTestEditor
  333. .create( {
  334. plugins: [ TableEditing, Paragraph ]
  335. } )
  336. .then( newEditor => {
  337. editor = newEditor;
  338. sinon.stub( editor, 'execute' );
  339. viewDocument = editor.editing.view.document;
  340. model = editor.model;
  341. } );
  342. } );
  343. it( 'should do nothing if not in table cell', () => {
  344. setModelData( model, '<paragraph>[]foo</paragraph>' );
  345. viewDocument.fire( 'enter', evtDataStub );
  346. sinon.assert.notCalled( editor.execute );
  347. expect( formatTable( getModelData( model ) ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  348. } );
  349. it( 'should do nothing if table cell has already a block content', () => {
  350. setModelData( model, modelTable( [
  351. [ '<paragraph>[]11</paragraph>' ]
  352. ] ) );
  353. viewDocument.fire( 'enter', evtDataStub );
  354. sinon.assert.notCalled( editor.execute );
  355. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  356. [ '<paragraph>[]11</paragraph>' ]
  357. ] ) );
  358. } );
  359. it( 'should do nothing if table cell with a block content is selected as a whole', () => {
  360. setModelData( model, modelTable( [
  361. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  362. ] ) );
  363. viewDocument.fire( 'enter', evtDataStub );
  364. sinon.assert.notCalled( editor.execute );
  365. setModelData( model, modelTable( [
  366. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  367. ] ) );
  368. } );
  369. it( 'should allow default behavior of Shift+Enter pressed', () => {
  370. setModelData( model, modelTable( [
  371. [ '[]11' ]
  372. ] ) );
  373. evtDataStub.isSoft = true;
  374. viewDocument.fire( 'enter', evtDataStub );
  375. sinon.assert.notCalled( editor.execute );
  376. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  377. [ '[]11' ]
  378. ] ) );
  379. } );
  380. it( 'should wrap table cell in paragraph and set selection', () => {
  381. setModelData( model, modelTable( [
  382. [ '[]11' ]
  383. ] ) );
  384. viewDocument.fire( 'enter', evtDataStub );
  385. sinon.assert.calledOnce( editor.execute );
  386. sinon.assert.calledWithExactly( editor.execute, 'enter' );
  387. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  388. [ '<paragraph>[]11</paragraph>' ]
  389. ] ) );
  390. } );
  391. } );
  392. } );