tableediting.js 17 KB

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