8
0

tableediting.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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 { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  9. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  10. import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
  11. import TableEditing from '../src/tableediting';
  12. import { modelTable } from './_utils/utils';
  13. import InsertRowCommand from '../src/commands/insertrowcommand';
  14. import InsertTableCommand from '../src/commands/inserttablecommand';
  15. import InsertColumnCommand from '../src/commands/insertcolumncommand';
  16. import RemoveRowCommand from '../src/commands/removerowcommand';
  17. import RemoveColumnCommand from '../src/commands/removecolumncommand';
  18. import SplitCellCommand from '../src/commands/splitcellcommand';
  19. import MergeCellsCommand from '../src/commands/mergecellscommand';
  20. import SetHeaderRowCommand from '../src/commands/setheaderrowcommand';
  21. import SetHeaderColumnCommand from '../src/commands/setheadercolumncommand';
  22. import MediaEmbedEditing from '@ckeditor/ckeditor5-media-embed/src/mediaembedediting';
  23. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  24. describe( 'TableEditing', () => {
  25. let editor, model;
  26. beforeEach( () => {
  27. return VirtualTestEditor
  28. .create( {
  29. plugins: [ TableEditing, Paragraph, ImageEditing, MediaEmbedEditing ]
  30. } )
  31. .then( newEditor => {
  32. editor = newEditor;
  33. model = editor.model;
  34. } );
  35. } );
  36. afterEach( () => {
  37. editor.destroy();
  38. } );
  39. it( 'should have pluginName', () => {
  40. expect( TableEditing.pluginName ).to.equal( 'TableEditing' );
  41. } );
  42. it( 'should set proper schema rules', () => {
  43. // Table:
  44. expect( model.schema.isRegistered( 'table' ) ).to.be.true;
  45. expect( model.schema.isObject( 'table' ) ).to.be.true;
  46. expect( model.schema.isBlock( 'table' ) ).to.be.true;
  47. expect( model.schema.isLimit( 'table' ) ).to.be.true;
  48. expect( model.schema.checkChild( [ '$root' ], 'table' ) ).to.be.true;
  49. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingRows' ) ).to.be.true;
  50. expect( model.schema.checkAttribute( [ '$root', 'table' ], 'headingColumns' ) ).to.be.true;
  51. // Table row:
  52. expect( model.schema.isRegistered( 'tableRow' ) ).to.be.true;
  53. expect( model.schema.isLimit( 'tableRow' ) ).to.be.true;
  54. expect( model.schema.checkChild( [ '$root' ], 'tableRow' ) ).to.be.false;
  55. expect( model.schema.checkChild( [ 'table' ], 'tableRow' ) ).to.be.true;
  56. // Table cell:
  57. expect( model.schema.isRegistered( 'tableCell' ) ).to.be.true;
  58. expect( model.schema.isLimit( 'tableCell' ) ).to.be.true;
  59. expect( model.schema.checkChild( [ '$root' ], 'tableCell' ) ).to.be.false;
  60. expect( model.schema.checkChild( [ 'table' ], 'tableCell' ) ).to.be.false;
  61. expect( model.schema.checkChild( [ 'tableRow' ], 'tableCell' ) ).to.be.true;
  62. expect( model.schema.checkChild( [ 'tableCell' ], 'tableCell' ) ).to.be.false;
  63. expect( model.schema.checkAttribute( [ 'tableCell' ], 'colspan' ) ).to.be.true;
  64. expect( model.schema.checkAttribute( [ 'tableCell' ], 'rowspan' ) ).to.be.true;
  65. // Table cell contents:
  66. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$text' ) ).to.be.false;
  67. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], '$block' ) ).to.be.true;
  68. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], 'table' ) ).to.be.false;
  69. expect( model.schema.checkChild( [ '$root', 'table', 'tableRow', 'tableCell' ], 'image' ) ).to.be.true;
  70. } );
  71. it( 'adds insertTable command', () => {
  72. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  73. } );
  74. it( 'adds insertRowAbove command', () => {
  75. expect( editor.commands.get( 'insertTableRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  76. } );
  77. it( 'adds insertRowBelow command', () => {
  78. expect( editor.commands.get( 'insertTableRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  79. } );
  80. it( 'adds insertColumnLeft command', () => {
  81. expect( editor.commands.get( 'insertTableColumnLeft' ) ).to.be.instanceOf( InsertColumnCommand );
  82. } );
  83. it( 'adds insertColumnRight command', () => {
  84. expect( editor.commands.get( 'insertTableColumnRight' ) ).to.be.instanceOf( InsertColumnCommand );
  85. } );
  86. it( 'adds removeRow command', () => {
  87. expect( editor.commands.get( 'removeTableRow' ) ).to.be.instanceOf( RemoveRowCommand );
  88. } );
  89. it( 'adds removeColumn command', () => {
  90. expect( editor.commands.get( 'removeTableColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  91. } );
  92. it( 'adds splitCellVertically command', () => {
  93. expect( editor.commands.get( 'splitTableCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  94. } );
  95. it( 'adds splitCellHorizontally command', () => {
  96. expect( editor.commands.get( 'splitTableCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  97. } );
  98. it( 'adds mergeTableCells command', () => {
  99. expect( editor.commands.get( 'mergeTableCells' ) ).to.be.instanceOf( MergeCellsCommand );
  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. it( 'should convert table with image', () => {
  144. editor.setData( '<table><tbody><tr><td><img src="sample.png"></td></tr></tbody></table>' );
  145. expect( getModelData( model, { withoutSelection: true } ) )
  146. .to.equal( '<table><tableRow><tableCell><image src="sample.png"></image></tableCell></tableRow></table>' );
  147. } );
  148. it( 'should insert a paragraph when the cell content is unsupported', () => {
  149. editor.setData(
  150. '<table><tbody><tr><td><foo></foo></td></tr></tbody></table>'
  151. );
  152. expect( getModelData( model, { withoutSelection: true } ) )
  153. .to.equal( '<table><tableRow><tableCell><paragraph></paragraph></tableCell></tableRow></table>' );
  154. } );
  155. it( 'should convert a table with media', () => {
  156. editor.setData(
  157. '<table><tbody><tr><td><oembed url="https://www.youtube.com/watch?v=H08tGjXNHO4"></oembed></td></tr></tbody></table>'
  158. );
  159. expect( getModelData( model, { withoutSelection: true } ) )
  160. .to.equal( '<table><tableRow><tableCell>' +
  161. '<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>' +
  162. '</tableCell></tableRow></table>' );
  163. } );
  164. } );
  165. } );
  166. describe( 'caret movement', () => {
  167. let domEvtDataStub;
  168. beforeEach( () => {
  169. domEvtDataStub = {
  170. keyCode: getCode( 'Tab' ),
  171. preventDefault: sinon.spy(),
  172. stopPropagation: sinon.spy()
  173. };
  174. } );
  175. it( 'should do nothing if not tab pressed', () => {
  176. setModelData( model, modelTable( [
  177. [ '11', '12[]' ]
  178. ] ) );
  179. domEvtDataStub.keyCode = getCode( 'a' );
  180. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  181. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  182. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  183. assertEqualMarkup( getModelData( model ), modelTable( [
  184. [ '11', '12[]' ]
  185. ] ) );
  186. } );
  187. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  188. setModelData( model, modelTable( [
  189. [ '11', '12[]' ]
  190. ] ) );
  191. domEvtDataStub.ctrlKey = true;
  192. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  193. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  194. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  195. assertEqualMarkup( getModelData( model ), modelTable( [
  196. [ '11', '12[]' ]
  197. ] ) );
  198. } );
  199. describe( 'on TAB', () => {
  200. it( 'should do nothing if selection is not in a table', () => {
  201. setModelData( model, '<paragraph>[]</paragraph>' + modelTable( [ [ '11', '12' ] ] ) );
  202. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  203. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  204. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  205. assertEqualMarkup( getModelData( model ), '[]' + modelTable( [
  206. [ '11', '12' ]
  207. ] ) );
  208. } );
  209. it( 'should move to next cell', () => {
  210. setModelData( model, modelTable( [
  211. [ '11[]', '12' ]
  212. ] ) );
  213. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  214. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  215. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  216. assertEqualMarkup( getModelData( model ), modelTable( [
  217. [ '11', '[12]' ]
  218. ] ) );
  219. } );
  220. it( 'should create another row and move to first cell in new row', () => {
  221. setModelData( model, modelTable( [
  222. [ '11', '[12]' ]
  223. ] ) );
  224. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  225. assertEqualMarkup( getModelData( model ), modelTable( [
  226. [ '11', '12' ],
  227. [ '[]', '' ]
  228. ] ) );
  229. } );
  230. it( 'should not create another row and not move the caret if insertTableRowBelow command is disabled', () => {
  231. setModelData( model, modelTable( [
  232. [ '11', '12[]' ]
  233. ] ) );
  234. const insertTableRowBelowCommand = editor.commands.get( 'insertTableRowBelow' );
  235. insertTableRowBelowCommand.forceDisabled( 'test' );
  236. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  237. assertEqualMarkup( getModelData( model ), modelTable( [
  238. [ '11', '12[]' ]
  239. ] ) );
  240. } );
  241. it( 'should move to the first cell of next row if on end of a row', () => {
  242. setModelData( model, modelTable( [
  243. [ '11', '12[]' ],
  244. [ '21', '22' ]
  245. ] ) );
  246. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  247. assertEqualMarkup( getModelData( model ), modelTable( [
  248. [ '11', '12' ],
  249. [ '[21]', '22' ]
  250. ] ) );
  251. } );
  252. it( 'should move to the next table cell if part of block content is selected', () => {
  253. setModelData( model, modelTable( [
  254. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ]
  255. ] ) );
  256. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  257. assertEqualMarkup( getModelData( model ), modelTable( [
  258. [
  259. '11',
  260. '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>',
  261. '[13]'
  262. ]
  263. ] ) );
  264. } );
  265. it( 'should move to next cell with an image', () => {
  266. setModelData( model, modelTable( [
  267. [ '11[]', '<paragraph>foo</paragraph><image></image>' ]
  268. ] ) );
  269. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  270. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  271. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  272. assertEqualMarkup( getModelData( model ), modelTable( [
  273. [ '11', '<paragraph>[foo</paragraph><image></image>]' ]
  274. ] ) );
  275. } );
  276. it( 'should move to next cell with an blockQuote', () => {
  277. model.schema.register( 'blockQuote', {
  278. allowWhere: '$block',
  279. allowContentOf: '$root'
  280. } );
  281. editor.conversion.elementToElement( { model: 'blockQuote', view: 'blockquote' } );
  282. setModelData( model, modelTable( [
  283. [ '11[]', '<blockQuote><paragraph>foo</paragraph></blockQuote>' ]
  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', '<blockQuote><paragraph>[foo]</paragraph></blockQuote>' ]
  290. ] ) );
  291. } );
  292. it( 'should listen with lower priority then its children', () => {
  293. // Cancel TAB event.
  294. editor.keystrokes.set( 'Tab', ( data, cancel ) => cancel() );
  295. setModelData( model, modelTable( [
  296. [ '11[]', '12' ]
  297. ] ) );
  298. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  299. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  300. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  301. assertEqualMarkup( getModelData( model ), modelTable( [
  302. [ '11[]', '12' ]
  303. ] ) );
  304. } );
  305. describe( 'on table widget selected', () => {
  306. beforeEach( () => {
  307. editor.model.schema.register( 'block', {
  308. allowWhere: '$block',
  309. allowContentOf: '$block',
  310. isObject: true
  311. } );
  312. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  313. } );
  314. it( 'should move caret to the first table cell on TAB', () => {
  315. const spy = sinon.spy();
  316. editor.keystrokes.set( 'Tab', spy, { priority: 'lowest' } );
  317. setModelData( model, '[' + modelTable( [
  318. [ '11', '12' ]
  319. ] ) + ']' );
  320. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  321. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  322. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  323. assertEqualMarkup( getModelData( model ), modelTable( [
  324. [ '[11]', '12' ]
  325. ] ) );
  326. // Should cancel event - so no other tab handler is called.
  327. sinon.assert.notCalled( spy );
  328. } );
  329. it( 'shouldn\'t do anything on other blocks', () => {
  330. const spy = sinon.spy();
  331. editor.editing.view.document.on( 'keydown', spy );
  332. setModelData( model, '[<block>foo</block>]' );
  333. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  334. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  335. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  336. assertEqualMarkup( getModelData( model ), '[<block>foo</block>]' );
  337. // Should not cancel event.
  338. sinon.assert.calledOnce( spy );
  339. } );
  340. } );
  341. } );
  342. describe( 'on SHIFT+TAB', () => {
  343. beforeEach( () => {
  344. domEvtDataStub.shiftKey = true;
  345. } );
  346. it( 'should do nothing if selection is not in a table', () => {
  347. setModelData( model, '<paragraph>[]</paragraph>' + modelTable( [
  348. [ '11', '12' ]
  349. ] ) );
  350. domEvtDataStub.keyCode = getCode( 'Tab' );
  351. domEvtDataStub.shiftKey = true;
  352. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  353. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  354. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  355. assertEqualMarkup( getModelData( model ), '[]' + modelTable( [
  356. [ '11', '12' ]
  357. ] ) );
  358. } );
  359. it( 'should move to previous cell', () => {
  360. setModelData( model, modelTable( [
  361. [ '11', '12[]' ]
  362. ] ) );
  363. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  364. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  365. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  366. assertEqualMarkup( getModelData( model ), modelTable( [
  367. [ '[11]', '12' ]
  368. ] ) );
  369. } );
  370. it( 'should not move if caret is in first table cell', () => {
  371. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  372. [ '[]11', '12' ]
  373. ] ) );
  374. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  375. assertEqualMarkup( getModelData( model ),
  376. '<paragraph>foo</paragraph>' + modelTable( [ [ '[]11', '12' ] ] )
  377. );
  378. } );
  379. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  380. setModelData( model, modelTable( [
  381. [ '11', '12' ],
  382. [ '[]21', '22' ]
  383. ] ) );
  384. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  385. assertEqualMarkup( getModelData( model ), modelTable( [
  386. [ '11', '[12]' ],
  387. [ '21', '22' ]
  388. ] ) );
  389. } );
  390. it( 'should move to the previous table cell if part of block content is selected', () => {
  391. setModelData( model, modelTable( [
  392. [ '11', '<paragraph>12</paragraph><paragraph>[foo]</paragraph><paragraph>bar</paragraph>', '13' ]
  393. ] ) );
  394. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  395. assertEqualMarkup( getModelData( model ), modelTable( [
  396. [
  397. '[11]',
  398. '<paragraph>12</paragraph><paragraph>foo</paragraph><paragraph>bar</paragraph>',
  399. '13'
  400. ]
  401. ] ) );
  402. } );
  403. it( 'should move to previous cell with an image', () => {
  404. setModelData( model, modelTable( [
  405. [ '<paragraph>foo</paragraph><image></image>', 'bar[]' ]
  406. ] ) );
  407. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  408. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  409. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  410. assertEqualMarkup( getModelData( model ), modelTable( [
  411. [ '<paragraph>[foo</paragraph><image></image>]', 'bar' ]
  412. ] ) );
  413. } );
  414. } );
  415. } );
  416. describe( 'enter key', () => {
  417. let evtDataStub, viewDocument;
  418. beforeEach( () => {
  419. evtDataStub = {
  420. preventDefault: sinon.spy(),
  421. stopPropagation: sinon.spy(),
  422. isSoft: false
  423. };
  424. return VirtualTestEditor
  425. .create( {
  426. plugins: [ TableEditing, Paragraph ]
  427. } )
  428. .then( newEditor => {
  429. editor = newEditor;
  430. sinon.stub( editor, 'execute' );
  431. viewDocument = editor.editing.view.document;
  432. model = editor.model;
  433. } );
  434. } );
  435. it( 'should do nothing if not in table cell', () => {
  436. setModelData( model, '<paragraph>[]foo</paragraph>' );
  437. viewDocument.fire( 'enter', evtDataStub );
  438. sinon.assert.notCalled( editor.execute );
  439. assertEqualMarkup( getModelData( model ), '<paragraph>[]foo</paragraph>' );
  440. } );
  441. it( 'should do nothing if table cell has already a block content', () => {
  442. setModelData( model, modelTable( [
  443. [ '<paragraph>[]11</paragraph>' ]
  444. ] ) );
  445. viewDocument.fire( 'enter', evtDataStub );
  446. sinon.assert.notCalled( editor.execute );
  447. assertEqualMarkup( getModelData( model ), modelTable( [
  448. [ '<paragraph>[]11</paragraph>' ]
  449. ] ) );
  450. } );
  451. it( 'should do nothing if table cell with a block content is selected as a whole', () => {
  452. setModelData( model, modelTable( [
  453. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  454. ] ) );
  455. viewDocument.fire( 'enter', evtDataStub );
  456. sinon.assert.notCalled( editor.execute );
  457. setModelData( model, modelTable( [
  458. [ '<paragraph>[1</paragraph><paragraph>1]</paragraph>' ]
  459. ] ) );
  460. } );
  461. it( 'should allow default behavior of Shift+Enter pressed', () => {
  462. setModelData( model, modelTable( [
  463. [ '[]11' ]
  464. ] ) );
  465. evtDataStub.isSoft = true;
  466. viewDocument.fire( 'enter', evtDataStub );
  467. sinon.assert.notCalled( editor.execute );
  468. assertEqualMarkup( getModelData( model ), modelTable( [
  469. [ '[]11' ]
  470. ] ) );
  471. } );
  472. } );
  473. describe( 'table selection', () => {
  474. let view, domEvtDataStub;
  475. beforeEach( () => {
  476. view = editor.editing.view;
  477. domEvtDataStub = {
  478. domEvent: {
  479. buttons: 1
  480. },
  481. target: undefined,
  482. preventDefault: sinon.spy(),
  483. stopPropagation: sinon.spy()
  484. };
  485. } );
  486. it( 'should not start table selection when mouse move is inside one table cell', () => {
  487. setModelData( model, modelTable( [
  488. [ '[]00', '01' ],
  489. [ '10', '11' ]
  490. ] ) );
  491. selectTableCell( domEvtDataStub, view, 0, 0, 0, 0 );
  492. view.document.fire( 'mousedown', domEvtDataStub );
  493. assertEqualMarkup( getModelData( model ), modelTable( [
  494. [ '[]00', '01' ],
  495. [ '10', '11' ]
  496. ] ) );
  497. view.document.fire( 'mousemove', domEvtDataStub );
  498. assertEqualMarkup( getModelData( model ), modelTable( [
  499. [ '[]00', '01' ],
  500. [ '10', '11' ]
  501. ] ) );
  502. } );
  503. it( 'should start table selection when mouse move expands over two cells', () => {
  504. setModelData( model, modelTable( [
  505. [ '[]00', '01' ],
  506. [ '10', '11' ]
  507. ] ) );
  508. selectTableCell( domEvtDataStub, view, 0, 0, 0, 0 );
  509. view.document.fire( 'mousedown', domEvtDataStub );
  510. assertEqualMarkup( getModelData( model ), modelTable( [
  511. [ '[]00', '01' ],
  512. [ '10', '11' ]
  513. ] ) );
  514. selectTableCell( domEvtDataStub, view, 0, 0, 0, 1 );
  515. view.document.fire( 'mousemove', domEvtDataStub );
  516. assertEqualMarkup( getModelData( model ), modelTable( [
  517. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true } ],
  518. [ '10', '11' ]
  519. ] ) );
  520. expect( getViewData( view ) ).to.equal( modelTable( [
  521. [ { contents: '00', class: 'selected', isSelected: true }, { contents: '01', class: 'selected', isSelected: true } ],
  522. [ '10', '11' ]
  523. ], { asWidget: true } ) );
  524. } );
  525. it( 'should select rectangular table cells when mouse moved to diagonal cell (up -> down)', () => {
  526. setModelData( model, modelTable( [
  527. [ '[]00', '01' ],
  528. [ '10', '11' ]
  529. ] ) );
  530. selectTableCell( domEvtDataStub, view, 0, 0, 0, 0 );
  531. view.document.fire( 'mousedown', domEvtDataStub );
  532. assertEqualMarkup( getModelData( model ), modelTable( [
  533. [ '[]00', '01' ],
  534. [ '10', '11' ]
  535. ] ) );
  536. selectTableCell( domEvtDataStub, view, 0, 0, 1, 1 );
  537. view.document.fire( 'mousemove', domEvtDataStub );
  538. assertEqualMarkup( getModelData( model ), modelTable( [
  539. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true } ],
  540. [ { contents: '10', isSelected: true }, { contents: '11', isSelected: true } ]
  541. ] ) );
  542. expect( getViewData( view ) ).to.equal( modelTable( [
  543. [ { contents: '00', class: 'selected', isSelected: true }, { contents: '01', class: 'selected', isSelected: true } ],
  544. [ { contents: '10', class: 'selected', isSelected: true }, { contents: '11', class: 'selected', isSelected: true } ]
  545. ], { asWidget: true } ) );
  546. } );
  547. it( 'should select rectangular table cells when mouse moved to diagonal cell (down -> up)', () => {
  548. setModelData( model, modelTable( [
  549. [ '00', '01' ],
  550. [ '10', '[]11' ]
  551. ] ) );
  552. selectTableCell( domEvtDataStub, view, 0, 0, 1, 1 );
  553. view.document.fire( 'mousedown', domEvtDataStub );
  554. assertEqualMarkup( getModelData( model ), modelTable( [
  555. [ '00', '01' ],
  556. [ '10', '[]11' ]
  557. ] ) );
  558. selectTableCell( domEvtDataStub, view, 0, 0, 0, 0 );
  559. view.document.fire( 'mousemove', domEvtDataStub );
  560. assertEqualMarkup( getModelData( model ), modelTable( [
  561. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true } ],
  562. [ { contents: '10', isSelected: true }, { contents: '11', isSelected: true } ]
  563. ] ) );
  564. expect( getViewData( view ) ).to.equal( modelTable( [
  565. [ { contents: '00', class: 'selected', isSelected: true }, { contents: '01', class: 'selected', isSelected: true } ],
  566. [ { contents: '10', class: 'selected', isSelected: true }, { contents: '11', class: 'selected', isSelected: true } ]
  567. ], { asWidget: true } ) );
  568. } );
  569. it( 'should update view selection after changing selection rect', () => {
  570. setModelData( model, modelTable( [
  571. [ '[]00', '01', '02' ],
  572. [ '10', '11', '12' ],
  573. [ '20', '21', '22' ]
  574. ] ) );
  575. selectTableCell( domEvtDataStub, view, 0, 0, 0, 0 );
  576. view.document.fire( 'mousedown', domEvtDataStub );
  577. selectTableCell( domEvtDataStub, view, 0, 0, 2, 2 );
  578. view.document.fire( 'mousemove', domEvtDataStub );
  579. assertEqualMarkup( getModelData( model ), modelTable( [
  580. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true }, { contents: '02', isSelected: true } ],
  581. [ { contents: '10', isSelected: true }, { contents: '11', isSelected: true }, { contents: '12', isSelected: true } ],
  582. [ { contents: '20', isSelected: true }, { contents: '21', isSelected: true }, { contents: '22', isSelected: true } ]
  583. ] ) );
  584. expect( getViewData( view ) ).to.equal( modelTable( [
  585. [
  586. { contents: '00', class: 'selected', isSelected: true },
  587. { contents: '01', class: 'selected', isSelected: true },
  588. { contents: '02', class: 'selected', isSelected: true }
  589. ],
  590. [
  591. { contents: '10', class: 'selected', isSelected: true },
  592. { contents: '11', class: 'selected', isSelected: true },
  593. { contents: '12', class: 'selected', isSelected: true }
  594. ],
  595. [
  596. { contents: '20', class: 'selected', isSelected: true },
  597. { contents: '21', class: 'selected', isSelected: true },
  598. { contents: '22', class: 'selected', isSelected: true }
  599. ]
  600. ], { asWidget: true } ) );
  601. selectTableCell( domEvtDataStub, view, 0, 0, 1, 1 );
  602. view.document.fire( 'mousemove', domEvtDataStub );
  603. assertEqualMarkup( getModelData( model ), modelTable( [
  604. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true }, '02' ],
  605. [ { contents: '10', isSelected: true }, { contents: '11', isSelected: true }, '12' ],
  606. [ '20', '21', '22' ]
  607. ] ) );
  608. expect( getViewData( view ) ).to.equal( modelTable( [
  609. [
  610. { contents: '00', class: 'selected', isSelected: true },
  611. { contents: '01', class: 'selected', isSelected: true },
  612. '02'
  613. ],
  614. [
  615. { contents: '10', class: 'selected', isSelected: true },
  616. { contents: '11', class: 'selected', isSelected: true },
  617. '12'
  618. ],
  619. [
  620. '20',
  621. '21',
  622. '22'
  623. ]
  624. ], { asWidget: true } ) );
  625. } );
  626. it( 'should stop selecting after "mouseup" event', () => {
  627. setModelData( model, modelTable( [
  628. [ '[]00', '01' ],
  629. [ '10', '11' ]
  630. ] ) );
  631. selectTableCell( domEvtDataStub, view, 0, 0, 0, 0 );
  632. view.document.fire( 'mousedown', domEvtDataStub );
  633. assertEqualMarkup( getModelData( model ), modelTable( [
  634. [ '[]00', '01' ],
  635. [ '10', '11' ]
  636. ] ) );
  637. selectTableCell( domEvtDataStub, view, 0, 0, 0, 1 );
  638. view.document.fire( 'mousemove', domEvtDataStub );
  639. view.document.fire( 'mouseup', domEvtDataStub );
  640. assertEqualMarkup( getModelData( model ), modelTable( [
  641. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true } ],
  642. [ '10', '11' ]
  643. ] ) );
  644. expect( getViewData( view ) ).to.equal( modelTable( [
  645. [ { contents: '00', class: 'selected', isSelected: true }, { contents: '01', class: 'selected', isSelected: true } ],
  646. [ '10', '11' ]
  647. ], { asWidget: true } ) );
  648. selectTableCell( domEvtDataStub, view, 0, 0, 1, 1 );
  649. view.document.fire( 'mousemove', domEvtDataStub );
  650. assertEqualMarkup( getModelData( model ), modelTable( [
  651. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true } ],
  652. [ '10', '11' ]
  653. ] ) );
  654. } );
  655. it( 'should stop selection mode on "mouseleve" event', () => {
  656. setModelData( model, modelTable( [
  657. [ '[]00', '01' ],
  658. [ '10', '11' ]
  659. ] ) );
  660. selectTableCell( domEvtDataStub, view, 0, 0, 0, 0 );
  661. view.document.fire( 'mousedown', domEvtDataStub );
  662. assertEqualMarkup( getModelData( model ), modelTable( [
  663. [ '[]00', '01' ],
  664. [ '10', '11' ]
  665. ] ) );
  666. selectTableCell( domEvtDataStub, view, 0, 0, 0, 1 );
  667. view.document.fire( 'mousemove', domEvtDataStub );
  668. view.document.fire( 'mouseleave', domEvtDataStub );
  669. assertEqualMarkup( getModelData( model ), modelTable( [
  670. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true } ],
  671. [ '10', '11' ]
  672. ] ) );
  673. expect( getViewData( view ) ).to.equal( modelTable( [
  674. [ { contents: '00', class: 'selected', isSelected: true }, { contents: '01', class: 'selected', isSelected: true } ],
  675. [ '10', '11' ]
  676. ], { asWidget: true } ) );
  677. selectTableCell( domEvtDataStub, view, 0, 0, 1, 1 );
  678. view.document.fire( 'mousemove', domEvtDataStub );
  679. assertEqualMarkup( getModelData( model ), modelTable( [
  680. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true } ],
  681. [ '10', '11' ]
  682. ] ) );
  683. } );
  684. it( 'should clear view table selection after mouse click outside table', () => {
  685. setModelData( model, modelTable( [
  686. [ '[]00', '01' ],
  687. [ '10', '11' ]
  688. ] ) + '<paragraph>foo</paragraph>' );
  689. selectTableCell( domEvtDataStub, view, 0, 0, 0, 0 );
  690. view.document.fire( 'mousedown', domEvtDataStub );
  691. assertEqualMarkup( getModelData( model ), modelTable( [
  692. [ '[]00', '01' ],
  693. [ '10', '11' ]
  694. ] ) + '<paragraph>foo</paragraph>' );
  695. selectTableCell( domEvtDataStub, view, 0, 0, 0, 1 );
  696. view.document.fire( 'mousemove', domEvtDataStub );
  697. domEvtDataStub.target = view.document.getRoot().getChild( 1 );
  698. view.document.fire( 'mousemove', domEvtDataStub );
  699. view.document.fire( 'mousedown', domEvtDataStub );
  700. view.document.fire( 'mouseup', domEvtDataStub );
  701. // The click in the DOM would trigger selection change and it will set the selection:
  702. model.change( writer => {
  703. writer.setSelection( writer.createRange( writer.createPositionAt( model.document.getRoot().getChild( 1 ), 0 ) ) );
  704. } );
  705. expect( getViewData( view ) ).to.equal( modelTable( [
  706. [ '00', '01' ],
  707. [ '10', '11' ]
  708. ], { asWidget: true } ) + '<p>{}foo</p>' );
  709. } );
  710. } );
  711. } );
  712. function selectTableCell( domEvtDataStub, view, tableIndex, sectionIndex, rowInSectionIndex, tableCellIndex ) {
  713. domEvtDataStub.target = view.document.getRoot()
  714. .getChild( tableIndex )
  715. .getChild( 1 ) // Table is second in widget
  716. .getChild( sectionIndex )
  717. .getChild( rowInSectionIndex )
  718. .getChild( tableCellIndex );
  719. }