8
0

tableediting.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. it( 'should listen with lower priority then its children', () => {
  236. // Cancel TAB event.
  237. editor.keystrokes.set( 'Tab', ( data, cancel ) => cancel() );
  238. setModelData( model, modelTable( [
  239. [ '11[]', '12' ]
  240. ] ) );
  241. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  242. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  243. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  244. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  245. [ '11[]', '12' ]
  246. ] ) );
  247. } );
  248. describe( 'on table widget selected', () => {
  249. beforeEach( () => {
  250. editor.model.schema.register( 'block', {
  251. allowWhere: '$block',
  252. allowContentOf: '$block',
  253. isObject: true
  254. } );
  255. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  256. } );
  257. it( 'should move caret to the first table cell on TAB', () => {
  258. const spy = sinon.spy();
  259. editor.keystrokes.set( 'Tab', spy, { priority: 'lowest' } );
  260. setModelData( model, '[' + modelTable( [
  261. [ '11', '12' ]
  262. ] ) + ']' );
  263. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  264. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  265. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  266. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  267. [ '[11]', '12' ]
  268. ] ) );
  269. // Should cancel event - so no other tab handler is called.
  270. sinon.assert.notCalled( spy );
  271. } );
  272. it( 'shouldn\'t do anything on other blocks', () => {
  273. const spy = sinon.spy();
  274. editor.editing.view.document.on( 'keydown', spy );
  275. setModelData( model, '[<block>foo</block>]' );
  276. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  277. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  278. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  279. expect( formatTable( getModelData( model ) ) ).to.equal( '[<block>foo</block>]' );
  280. // Should not cancel event.
  281. sinon.assert.calledOnce( spy );
  282. } );
  283. } );
  284. } );
  285. describe( 'on SHIFT+TAB', () => {
  286. beforeEach( () => {
  287. domEvtDataStub.shiftKey = true;
  288. } );
  289. it( 'should do nothing if selection is not in a table', () => {
  290. setModelData( model, '[]' + modelTable( [
  291. [ '11', '12' ]
  292. ] ) );
  293. domEvtDataStub.keyCode = getCode( 'Tab' );
  294. domEvtDataStub.shiftKey = true;
  295. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  296. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  297. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  298. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  299. [ '11', '12' ]
  300. ] ) );
  301. } );
  302. it( 'should move to previous cell', () => {
  303. setModelData( model, modelTable( [
  304. [ '11', '12[]' ]
  305. ] ) );
  306. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  307. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  308. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  309. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  310. [ '[11]', '12' ]
  311. ] ) );
  312. } );
  313. it( 'should not move if caret is in first table cell', () => {
  314. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  315. [ '[]11', '12' ]
  316. ] ) );
  317. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  318. expect( formatTable( getModelData( model ) ) ).to.equal(
  319. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  320. );
  321. } );
  322. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  323. setModelData( model, modelTable( [
  324. [ '11', '12' ],
  325. [ '[]21', '22' ]
  326. ] ) );
  327. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  328. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  329. [ '11', '[12]' ],
  330. [ '21', '22' ]
  331. ] ) );
  332. } );
  333. it( 'should move to the previous table cell if part of block content is selected', () => {
  334. setModelData( model, modelTable( [
  335. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ],
  336. ] ) );
  337. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  338. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  339. [
  340. '[11]',
  341. '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>',
  342. '13'
  343. ],
  344. ] ) );
  345. } );
  346. } );
  347. } );
  348. describe( 'enter key', () => {
  349. let evtDataStub, viewDocument;
  350. beforeEach( () => {
  351. evtDataStub = {
  352. preventDefault: sinon.spy(),
  353. stopPropagation: sinon.spy(),
  354. isSoft: false
  355. };
  356. return VirtualTestEditor
  357. .create( {
  358. plugins: [ TableEditing, Paragraph ]
  359. } )
  360. .then( newEditor => {
  361. editor = newEditor;
  362. sinon.stub( editor, 'execute' );
  363. viewDocument = editor.editing.view.document;
  364. model = editor.model;
  365. } );
  366. } );
  367. it( 'should do nothing if not in table cell', () => {
  368. setModelData( model, '<paragraph>[]foo</paragraph>' );
  369. viewDocument.fire( 'enter', evtDataStub );
  370. sinon.assert.notCalled( editor.execute );
  371. expect( formatTable( getModelData( model ) ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  372. } );
  373. it( 'should do nothing if table cell has already a block content', () => {
  374. setModelData( model, modelTable( [
  375. [ '<paragraph>[]11</paragraph>' ]
  376. ] ) );
  377. viewDocument.fire( 'enter', evtDataStub );
  378. sinon.assert.notCalled( editor.execute );
  379. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  380. [ '<paragraph>[]11</paragraph>' ]
  381. ] ) );
  382. } );
  383. it( 'should do nothing if table cell with a block content is selected as a whole', () => {
  384. setModelData( model, modelTable( [
  385. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  386. ] ) );
  387. viewDocument.fire( 'enter', evtDataStub );
  388. sinon.assert.notCalled( editor.execute );
  389. setModelData( model, modelTable( [
  390. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  391. ] ) );
  392. } );
  393. it( 'should allow default behavior of Shift+Enter pressed', () => {
  394. setModelData( model, modelTable( [
  395. [ '[]11' ]
  396. ] ) );
  397. evtDataStub.isSoft = true;
  398. viewDocument.fire( 'enter', evtDataStub );
  399. sinon.assert.notCalled( editor.execute );
  400. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  401. [ '[]11' ]
  402. ] ) );
  403. } );
  404. } );
  405. } );