8
0

downcast.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  7. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import {
  9. downcastInsertCell,
  10. downcastInsertRow,
  11. downcastInsertTable,
  12. downcastRemoveRow,
  13. downcastTableHeadingColumnsChange,
  14. downcastTableHeadingRowsChange
  15. } from '../../src/converters/downcast';
  16. import { formatTable, formattedViewTable, modelTable, viewTable } from '../_utils/utils';
  17. describe( 'downcast converters', () => {
  18. let editor, model, doc, root, viewDocument;
  19. beforeEach( () => {
  20. return VirtualTestEditor.create()
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. doc = model.document;
  25. root = doc.getRoot( 'main' );
  26. viewDocument = editor.editing.view;
  27. const conversion = editor.conversion;
  28. const schema = model.schema;
  29. schema.register( 'table', {
  30. allowWhere: '$block',
  31. allowAttributes: [ 'headingRows', 'headingColumns' ],
  32. isBlock: true,
  33. isObject: true
  34. } );
  35. schema.register( 'tableRow', {
  36. allowIn: 'table',
  37. allowAttributes: [],
  38. isBlock: true,
  39. isLimit: true
  40. } );
  41. schema.register( 'tableCell', {
  42. allowIn: 'tableRow',
  43. allowContentOf: '$block',
  44. allowAttributes: [ 'colspan', 'rowspan' ],
  45. isBlock: true,
  46. isLimit: true
  47. } );
  48. conversion.for( 'downcast' ).add( downcastInsertTable() );
  49. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  50. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  51. // Insert conversion
  52. conversion.for( 'downcast' ).add( downcastInsertRow() );
  53. conversion.for( 'downcast' ).add( downcastInsertCell() );
  54. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  55. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange() );
  56. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange() );
  57. } );
  58. } );
  59. describe( 'downcastInsertTable()', () => {
  60. it( 'should create table with tbody', () => {
  61. setModelData( model,
  62. '<table>' +
  63. '<tableRow><tableCell></tableCell></tableRow>' +
  64. '</table>'
  65. );
  66. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  67. '<table>' +
  68. '<tbody>' +
  69. '<tr><td></td></tr>' +
  70. '</tbody>' +
  71. '</table>'
  72. );
  73. } );
  74. it( 'should create table with tbody and thead', () => {
  75. setModelData( model,
  76. '<table headingRows="1">' +
  77. '<tableRow><tableCell>1</tableCell></tableRow>' +
  78. '<tableRow><tableCell>2</tableCell></tableRow>' +
  79. '</table>'
  80. );
  81. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  82. '<table>' +
  83. '<thead>' +
  84. '<tr><th>1</th></tr>' +
  85. '</thead>' +
  86. '<tbody>' +
  87. '<tr><td>2</td></tr>' +
  88. '</tbody>' +
  89. '</table>'
  90. );
  91. } );
  92. it( 'should create table with thead', () => {
  93. setModelData( model,
  94. '<table headingRows="2">' +
  95. '<tableRow><tableCell>1</tableCell></tableRow>' +
  96. '<tableRow><tableCell>2</tableCell></tableRow>' +
  97. '</table>'
  98. );
  99. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  100. '<table>' +
  101. '<thead>' +
  102. '<tr><th>1</th></tr>' +
  103. '<tr><th>2</th></tr>' +
  104. '</thead>' +
  105. '</table>'
  106. );
  107. } );
  108. it( 'should create table with heading columns and rows', () => {
  109. setModelData( model,
  110. '<table headingColumns="3" headingRows="1">' +
  111. '<tableRow>' +
  112. '<tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell><tableCell>14</tableCell>' +
  113. '</tableRow>' +
  114. '<tableRow>' +
  115. '<tableCell>21</tableCell><tableCell>22</tableCell><tableCell>23</tableCell><tableCell>24</tableCell>' +
  116. '</tableRow>' +
  117. '</table>'
  118. );
  119. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  120. '<table>' +
  121. '<thead>' +
  122. '<tr><th>11</th><th>12</th><th>13</th><th>14</th></tr>' +
  123. '</thead>' +
  124. '<tbody>' +
  125. '<tr><th>21</th><th>22</th><th>23</th><td>24</td></tr>' +
  126. '</tbody>' +
  127. '</table>'
  128. );
  129. } );
  130. it( 'should be possible to overwrite', () => {
  131. editor.conversion.elementToElement( { model: 'tableRow', view: 'tr', converterPriority: 'high' } );
  132. editor.conversion.elementToElement( { model: 'tableCell', view: 'td', converterPriority: 'high' } );
  133. editor.conversion.for( 'downcast' ).add( dispatcher => {
  134. dispatcher.on( 'insert:table', ( evt, data, conversionApi ) => {
  135. conversionApi.consumable.consume( data.item, 'insert' );
  136. const tableElement = conversionApi.writer.createContainerElement( 'table', { foo: 'bar' } );
  137. const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
  138. conversionApi.mapper.bindElements( data.item, tableElement );
  139. conversionApi.writer.insert( viewPosition, tableElement );
  140. }, { priority: 'high' } );
  141. } );
  142. setModelData( model,
  143. '<table>' +
  144. '<tableRow><tableCell></tableCell></tableRow>' +
  145. '</table>'
  146. );
  147. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  148. '<table foo="bar">' +
  149. '<tr><td></td></tr>' +
  150. '</table>'
  151. );
  152. } );
  153. describe( 'headingColumns attribute', () => {
  154. it( 'should mark heading columns table cells', () => {
  155. setModelData( model,
  156. '<table headingColumns="2">' +
  157. '<tableRow><tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell></tableRow>' +
  158. '<tableRow><tableCell>21</tableCell><tableCell>22</tableCell><tableCell>23</tableCell></tableRow>' +
  159. '</table>'
  160. );
  161. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  162. '<table>' +
  163. '<tbody>' +
  164. '<tr><th>11</th><th>12</th><td>13</td></tr>' +
  165. '<tr><th>21</th><th>22</th><td>23</td></tr>' +
  166. '</tbody>' +
  167. '</table>'
  168. );
  169. } );
  170. it( 'should mark heading columns table cells when one has colspan attribute', () => {
  171. setModelData( model,
  172. '<table headingColumns="3">' +
  173. '<tableRow>' +
  174. '<tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell><tableCell>14</tableCell>' +
  175. '</tableRow>' +
  176. '<tableRow><tableCell colspan="2">21</tableCell><tableCell>23</tableCell><tableCell>24</tableCell></tableRow>' +
  177. '</table>'
  178. );
  179. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  180. '<table>' +
  181. '<tbody>' +
  182. '<tr><th>11</th><th>12</th><th>13</th><td>14</td></tr>' +
  183. '<tr><th colspan="2">21</th><th>23</th><td>24</td></tr>' +
  184. '</tbody>' +
  185. '</table>'
  186. );
  187. } );
  188. it( 'should work with colspan and rowspan attributes on table cells', () => {
  189. // The table in this test looks like a table below:
  190. //
  191. // Row headings | Normal cells
  192. // |
  193. // +----+----+----+----+
  194. // | 11 | 12 | 13 | 14 |
  195. // | +----+ +----+
  196. // | | 22 | | 24 |
  197. // |----+----+ +----+
  198. // | 31 | | 34 |
  199. // | +----+----+
  200. // | | 43 | 44 |
  201. // +----+----+----+----+
  202. setModelData( model,
  203. '<table headingColumns="3">' +
  204. '<tableRow>' +
  205. '<tableCell rowspan="2">11</tableCell>' +
  206. '<tableCell>12</tableCell>' +
  207. '<tableCell rowspan="3">13</tableCell>' +
  208. '<tableCell>14</tableCell>' +
  209. '</tableRow>' +
  210. '<tableRow><tableCell>22</tableCell><tableCell>24</tableCell></tableRow>' +
  211. '<tableRow><tableCell colspan="2" rowspan="2">31</tableCell><tableCell>34</tableCell></tableRow>' +
  212. '<tableRow><tableCell>43</tableCell><tableCell>44</tableCell></tableRow>' +
  213. '</table>'
  214. );
  215. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  216. '<table>' +
  217. '<tbody>' +
  218. '<tr><th rowspan="2">11</th><th>12</th><th rowspan="3">13</th><td>14</td></tr>' +
  219. '<tr><th>22</th><td>24</td></tr>' +
  220. '<tr><th colspan="2" rowspan="2">31</th><td>34</td></tr>' +
  221. '<tr><th>43</th><td>44</td></tr>' +
  222. '</tbody>' +
  223. '</table>'
  224. );
  225. } );
  226. } );
  227. describe( 'asWidget', () => {
  228. beforeEach( () => {
  229. return VirtualTestEditor.create()
  230. .then( newEditor => {
  231. editor = newEditor;
  232. model = editor.model;
  233. doc = model.document;
  234. root = doc.getRoot( 'main' );
  235. viewDocument = editor.editing.view;
  236. const conversion = editor.conversion;
  237. const schema = model.schema;
  238. schema.register( 'table', {
  239. allowWhere: '$block',
  240. allowAttributes: [ 'headingRows', 'headingColumns' ],
  241. isBlock: true,
  242. isObject: true
  243. } );
  244. schema.register( 'tableRow', {
  245. allowIn: 'table',
  246. allowAttributes: [],
  247. isBlock: true,
  248. isLimit: true
  249. } );
  250. schema.register( 'tableCell', {
  251. allowIn: 'tableRow',
  252. allowContentOf: '$block',
  253. allowAttributes: [ 'colspan', 'rowspan' ],
  254. isBlock: true,
  255. isLimit: true
  256. } );
  257. conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget: true } ) );
  258. conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget: true } ) );
  259. conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget: true } ) );
  260. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  261. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  262. } );
  263. } );
  264. it( 'should create table as a widget', () => {
  265. setModelData( model,
  266. '<table>' +
  267. '<tableRow><tableCell></tableCell></tableRow>' +
  268. '</table>'
  269. );
  270. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  271. '<table class="ck-widget" contenteditable="false">' +
  272. '<tbody>' +
  273. '<tr><td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td></tr>' +
  274. '</tbody>' +
  275. '</table>'
  276. );
  277. } );
  278. } );
  279. } );
  280. describe( 'downcastInsertRow()', () => {
  281. it( 'should react to changed rows', () => {
  282. setModelData( model, modelTable( [
  283. [ '11', '12' ]
  284. ] ) );
  285. const table = root.getChild( 0 );
  286. model.change( writer => {
  287. const row = writer.createElement( 'tableRow' );
  288. writer.insert( row, table, 1 );
  289. writer.insertElement( 'tableCell', row, 'end' );
  290. writer.insertElement( 'tableCell', row, 'end' );
  291. } );
  292. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  293. [ '11', '12' ],
  294. [ '', '' ]
  295. ] ) );
  296. } );
  297. it( 'should properly consume already added rows', () => {
  298. setModelData( model, modelTable( [
  299. [ '11', '12' ]
  300. ] ) );
  301. const table = root.getChild( 0 );
  302. model.change( writer => {
  303. const row = writer.createElement( 'tableRow' );
  304. writer.insert( row, table, 1 );
  305. writer.insertElement( 'tableCell', row, 'end' );
  306. writer.insertElement( 'tableCell', row, 'end' );
  307. } );
  308. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  309. [ '11', '12' ],
  310. [ '', '' ]
  311. ] ) );
  312. model.change( writer => {
  313. const row = writer.createElement( 'tableRow' );
  314. writer.insert( row, table, 2 );
  315. writer.insertElement( 'tableCell', row, 'end' );
  316. writer.insertElement( 'tableCell', row, 'end' );
  317. } );
  318. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  319. [ '11', '12' ],
  320. [ '', '' ],
  321. [ '', '' ]
  322. ] ) );
  323. } );
  324. it( 'should insert row on proper index', () => {
  325. setModelData( model, modelTable( [
  326. [ '11', '12' ],
  327. [ '21', '22' ],
  328. [ '31', '32' ]
  329. ] ) );
  330. const table = root.getChild( 0 );
  331. model.change( writer => {
  332. const row = writer.createElement( 'tableRow' );
  333. writer.insert( row, table, 1 );
  334. writer.insertElement( 'tableCell', row, 'end' );
  335. writer.insertElement( 'tableCell', row, 'end' );
  336. } );
  337. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  338. [ '11', '12' ],
  339. [ '', '' ],
  340. [ '21', '22' ],
  341. [ '31', '32' ]
  342. ] ) );
  343. } );
  344. it( 'should insert row on proper index when table has heading rows defined - insert in body', () => {
  345. setModelData( model, modelTable( [
  346. [ '11', '12' ],
  347. [ '21', '22' ],
  348. [ '31', '32' ]
  349. ], { headingRows: 1 } ) );
  350. const table = root.getChild( 0 );
  351. model.change( writer => {
  352. const row = writer.createElement( 'tableRow' );
  353. writer.insert( row, table, 1 );
  354. writer.insertElement( 'tableCell', row, 'end' );
  355. writer.insertElement( 'tableCell', row, 'end' );
  356. } );
  357. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  358. [ '11', '12' ],
  359. [ '', '' ],
  360. [ '21', '22' ],
  361. [ '31', '32' ]
  362. ], { headingRows: 1 } ) );
  363. } );
  364. it( 'should insert row on proper index when table has heading rows defined - insert in heading', () => {
  365. setModelData( model, modelTable( [
  366. [ '11', '12' ],
  367. [ '21', '22' ],
  368. [ '31', '32' ]
  369. ], { headingRows: 2 } ) );
  370. const table = root.getChild( 0 );
  371. model.change( writer => {
  372. const row = writer.createElement( 'tableRow' );
  373. writer.insert( row, table, 1 );
  374. writer.insertElement( 'tableCell', row, 'end' );
  375. writer.insertElement( 'tableCell', row, 'end' );
  376. } );
  377. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  378. [ '11', '12' ],
  379. [ '', '' ],
  380. [ '21', '22' ],
  381. [ '31', '32' ]
  382. ], { headingRows: 3 } ) );
  383. } );
  384. it( 'should react to changed rows when previous rows\' cells has rowspans', () => {
  385. setModelData( model, modelTable( [
  386. [ { rowspan: 3, contents: '11' }, '12' ],
  387. [ '22' ]
  388. ] ) );
  389. const table = root.getChild( 0 );
  390. model.change( writer => {
  391. const row = writer.createElement( 'tableRow' );
  392. writer.insert( row, table, 2 );
  393. writer.insertElement( 'tableCell', row, 'end' );
  394. } );
  395. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  396. [ { rowspan: 3, contents: '11' }, '12' ],
  397. [ '22' ],
  398. [ '' ]
  399. ] ) );
  400. } );
  401. it( 'should properly create row headings', () => {
  402. setModelData( model, modelTable( [
  403. [ { rowspan: 3, contents: '11' }, '12' ],
  404. [ '22' ]
  405. ], { headingColumns: 1 } ) );
  406. const table = root.getChild( 0 );
  407. model.change( writer => {
  408. const firstRow = writer.createElement( 'tableRow' );
  409. writer.insert( firstRow, table, 2 );
  410. writer.insert( writer.createElement( 'tableCell' ), firstRow, 'end' );
  411. const secondRow = writer.createElement( 'tableRow' );
  412. writer.insert( secondRow, table, 3 );
  413. writer.insert( writer.createElement( 'tableCell' ), secondRow, 'end' );
  414. writer.insert( writer.createElement( 'tableCell' ), secondRow, 'end' );
  415. } );
  416. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  417. [ { rowspan: 3, contents: '11', isHeading: true }, '12' ],
  418. [ '22' ],
  419. [ '' ],
  420. [ { contents: '', isHeading: true }, '' ]
  421. ] ) );
  422. } );
  423. describe( 'asWidget', () => {
  424. beforeEach( () => {
  425. return VirtualTestEditor.create()
  426. .then( newEditor => {
  427. editor = newEditor;
  428. model = editor.model;
  429. doc = model.document;
  430. root = doc.getRoot( 'main' );
  431. viewDocument = editor.editing.view;
  432. const conversion = editor.conversion;
  433. const schema = model.schema;
  434. schema.register( 'table', {
  435. allowWhere: '$block',
  436. allowAttributes: [ 'headingRows', 'headingColumns' ],
  437. isBlock: true,
  438. isObject: true
  439. } );
  440. schema.register( 'tableRow', {
  441. allowIn: 'table',
  442. allowAttributes: [],
  443. isBlock: true,
  444. isLimit: true
  445. } );
  446. schema.register( 'tableCell', {
  447. allowIn: 'tableRow',
  448. allowContentOf: '$block',
  449. allowAttributes: [ 'colspan', 'rowspan' ],
  450. isBlock: true,
  451. isLimit: true
  452. } );
  453. conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget: true } ) );
  454. conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget: true } ) );
  455. conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget: true } ) );
  456. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  457. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  458. } );
  459. } );
  460. it( 'should create table cell inside inserted row as a widget', () => {
  461. setModelData( model,
  462. '<table>' +
  463. '<tableRow><tableCell>foo</tableCell></tableRow>' +
  464. '</table>'
  465. );
  466. const table = root.getChild( 0 );
  467. model.change( writer => {
  468. const firstRow = writer.createElement( 'tableRow' );
  469. writer.insert( firstRow, table, 1 );
  470. writer.insert( writer.createElement( 'tableCell' ), firstRow, 'end' );
  471. } );
  472. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  473. '<table class="ck-widget" contenteditable="false">' +
  474. '<tbody>' +
  475. '<tr><td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">foo</td></tr>' +
  476. '<tr><td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td></tr>' +
  477. '</tbody>' +
  478. '</table>'
  479. );
  480. } );
  481. } );
  482. } );
  483. describe( 'downcastInsertCell()', () => {
  484. it( 'should add tableCell on proper index in tr', () => {
  485. setModelData( model, modelTable( [
  486. [ '11', '12' ]
  487. ] ) );
  488. const table = root.getChild( 0 );
  489. model.change( writer => {
  490. const row = table.getChild( 0 );
  491. writer.insertElement( 'tableCell', row, 1 );
  492. } );
  493. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  494. [ '11', '', '12' ]
  495. ] ) );
  496. } );
  497. it( 'should add tableCell on proper index in tr when previous have colspans', () => {
  498. setModelData( model, modelTable( [
  499. [ { colspan: 2, contents: '11' }, '13' ]
  500. ] ) );
  501. const table = root.getChild( 0 );
  502. model.change( writer => {
  503. const row = table.getChild( 0 );
  504. writer.insertElement( 'tableCell', row, 1 );
  505. } );
  506. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  507. [ { colspan: 2, contents: '11' }, '', '13' ]
  508. ] ) );
  509. } );
  510. it( 'should add tableCell on proper index in tr when previous row have rowspans', () => {
  511. setModelData( model, modelTable( [
  512. [ { rowspan: 2, contents: '11' }, '13' ],
  513. [ '22', '23' ]
  514. ] ) );
  515. const table = root.getChild( 0 );
  516. model.change( writer => {
  517. writer.insertElement( 'tableCell', table.getChild( 0 ), 1 );
  518. writer.insertElement( 'tableCell', table.getChild( 1 ), 0 );
  519. } );
  520. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  521. [ { rowspan: 2, contents: '11' }, '', '13' ],
  522. [ '', '22', '23' ]
  523. ] ) );
  524. } );
  525. it( 'split cell simulation - simple', () => {
  526. setModelData( model, modelTable( [
  527. [ '11', '12' ],
  528. [ '21', '22' ]
  529. ] ) );
  530. const table = root.getChild( 0 );
  531. model.change( writer => {
  532. const firstRow = table.getChild( 0 );
  533. const secondRow = table.getChild( 1 );
  534. writer.insertElement( 'tableCell', firstRow, 1 );
  535. writer.setAttribute( 'colspan', 2, secondRow.getChild( 0 ) );
  536. } );
  537. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  538. [ '11', '', '12' ],
  539. [ { colspan: 2, contents: '21' }, '22' ]
  540. ] ) );
  541. } );
  542. it( 'merge simulation - simple', () => {
  543. setModelData( model, modelTable( [
  544. [ '11', '12' ],
  545. [ '21', '22' ]
  546. ] ) );
  547. const table = root.getChild( 0 );
  548. model.change( writer => {
  549. const firstRow = table.getChild( 0 );
  550. writer.setAttribute( 'colspan', 2, firstRow.getChild( 0 ) );
  551. writer.remove( firstRow.getChild( 1 ) );
  552. } );
  553. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  554. [ { colspan: 2, contents: '11' } ],
  555. [ '21', '22' ]
  556. ] ) );
  557. } );
  558. describe( 'asWidget', () => {
  559. beforeEach( () => {
  560. return VirtualTestEditor.create()
  561. .then( newEditor => {
  562. editor = newEditor;
  563. model = editor.model;
  564. doc = model.document;
  565. root = doc.getRoot( 'main' );
  566. viewDocument = editor.editing.view;
  567. const conversion = editor.conversion;
  568. const schema = model.schema;
  569. schema.register( 'table', {
  570. allowWhere: '$block',
  571. allowAttributes: [ 'headingRows', 'headingColumns' ],
  572. isBlock: true,
  573. isObject: true
  574. } );
  575. schema.register( 'tableRow', {
  576. allowIn: 'table',
  577. allowAttributes: [],
  578. isBlock: true,
  579. isLimit: true
  580. } );
  581. schema.register( 'tableCell', {
  582. allowIn: 'tableRow',
  583. allowContentOf: '$block',
  584. allowAttributes: [ 'colspan', 'rowspan' ],
  585. isBlock: true,
  586. isLimit: true
  587. } );
  588. conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget: true } ) );
  589. conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget: true } ) );
  590. conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget: true } ) );
  591. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  592. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  593. } );
  594. } );
  595. it( 'should create inserted table cell as a widget', () => {
  596. setModelData( model,
  597. '<table>' +
  598. '<tableRow><tableCell>foo</tableCell></tableRow>' +
  599. '</table>'
  600. );
  601. const table = root.getChild( 0 );
  602. model.change( writer => {
  603. const row = table.getChild( 0 );
  604. writer.insert( writer.createElement( 'tableCell' ), row, 'end' );
  605. } );
  606. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  607. '<table class="ck-widget" contenteditable="false">' +
  608. '<tbody>' +
  609. '<tr>' +
  610. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">foo</td>' +
  611. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td>' +
  612. '</tr>' +
  613. '</tbody>' +
  614. '</table>'
  615. );
  616. } );
  617. } );
  618. } );
  619. describe( 'downcastTableHeadingColumnsChange()', () => {
  620. it( 'should work for adding heading columns', () => {
  621. setModelData( model, modelTable( [
  622. [ '11', '12' ],
  623. [ '21', '22' ]
  624. ] ) );
  625. const table = root.getChild( 0 );
  626. model.change( writer => {
  627. writer.setAttribute( 'headingColumns', 1, table );
  628. } );
  629. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  630. [ { isHeading: true, contents: '11' }, '12' ],
  631. [ { isHeading: true, contents: '21' }, '22' ]
  632. ] ) );
  633. } );
  634. it( 'should work for changing heading columns to a bigger number', () => {
  635. setModelData( model, modelTable( [
  636. [ '11', '12', '13', '14' ],
  637. [ '21', '22', '23', '24' ]
  638. ], { headingColumns: 1 } ) );
  639. const table = root.getChild( 0 );
  640. model.change( writer => {
  641. writer.setAttribute( 'headingColumns', 3, table );
  642. } );
  643. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  644. [ { isHeading: true, contents: '11' }, { isHeading: true, contents: '12' }, { isHeading: true, contents: '13' }, '14' ],
  645. [ { isHeading: true, contents: '21' }, { isHeading: true, contents: '22' }, { isHeading: true, contents: '23' }, '24' ]
  646. ] ) );
  647. } );
  648. it( 'should work for changing heading columns to a smaller number', () => {
  649. setModelData( model, modelTable( [
  650. [ { isHeading: true, contents: '11' }, { isHeading: true, contents: '12' }, { isHeading: true, contents: '13' }, '14' ],
  651. [ { isHeading: true, contents: '21' }, { isHeading: true, contents: '22' }, { isHeading: true, contents: '23' }, '24' ]
  652. ], { headingColumns: 3 } ) );
  653. const table = root.getChild( 0 );
  654. model.change( writer => {
  655. writer.setAttribute( 'headingColumns', 1, table );
  656. } );
  657. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  658. [ { isHeading: true, contents: '11' }, '12', '13', '14' ],
  659. [ { isHeading: true, contents: '21' }, '22', '23', '24' ]
  660. ], { headingColumns: 3 } ) );
  661. } );
  662. it( 'should work for removing heading columns', () => {
  663. setModelData( model, modelTable( [
  664. [ '11', '12' ],
  665. [ '21', '22' ]
  666. ], { headingColumns: 1 } ) );
  667. const table = root.getChild( 0 );
  668. model.change( writer => {
  669. writer.removeAttribute( 'headingColumns', table );
  670. } );
  671. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  672. [ '11', '12' ],
  673. [ '21', '22' ]
  674. ] ) );
  675. } );
  676. it( 'should be possible to overwrite', () => {
  677. editor.conversion.attributeToAttribute( { model: 'headingColumns', view: 'headingColumns', converterPriority: 'high' } );
  678. setModelData( model, modelTable( [ [ '11' ] ] ) );
  679. const table = root.getChild( 0 );
  680. model.change( writer => {
  681. writer.setAttribute( 'headingColumns', 1, table );
  682. } );
  683. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  684. '<table headingColumns="1"><tbody><tr><td>11</td></tr></tbody></table>'
  685. );
  686. } );
  687. it( 'should work with adding table cells', () => {
  688. setModelData( model, modelTable( [
  689. [ { rowspan: 2, contents: '11' }, '12', '13', '14' ],
  690. [ '22', '23', '24' ],
  691. [ { colspan: 2, contents: '31' }, '33', '34' ]
  692. ], { headingColumns: 2 } ) );
  693. const table = root.getChild( 0 );
  694. model.change( writer => {
  695. // Inserting column in heading columns so update table's attribute also
  696. writer.setAttribute( 'headingColumns', 3, table );
  697. writer.insertElement( 'tableCell', table.getChild( 0 ), 2 );
  698. writer.insertElement( 'tableCell', table.getChild( 1 ), 1 );
  699. writer.insertElement( 'tableCell', table.getChild( 2 ), 1 );
  700. } );
  701. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  702. [
  703. { isHeading: true, rowspan: 2, contents: '11' },
  704. { isHeading: true, contents: '12' },
  705. { isHeading: true, contents: '' },
  706. '13',
  707. '14'
  708. ],
  709. [
  710. { isHeading: true, contents: '22' },
  711. { isHeading: true, contents: '' },
  712. '23',
  713. '24'
  714. ],
  715. [
  716. { isHeading: true, colspan: 2, contents: '31' },
  717. { isHeading: true, contents: '' },
  718. '33',
  719. '34'
  720. ]
  721. ] ) );
  722. } );
  723. describe( 'asWidget', () => {
  724. beforeEach( () => {
  725. return VirtualTestEditor.create()
  726. .then( newEditor => {
  727. editor = newEditor;
  728. model = editor.model;
  729. doc = model.document;
  730. root = doc.getRoot( 'main' );
  731. viewDocument = editor.editing.view;
  732. const conversion = editor.conversion;
  733. const schema = model.schema;
  734. schema.register( 'table', {
  735. allowWhere: '$block',
  736. allowAttributes: [ 'headingRows', 'headingColumns' ],
  737. isBlock: true,
  738. isObject: true
  739. } );
  740. schema.register( 'tableRow', {
  741. allowIn: 'table',
  742. allowAttributes: [],
  743. isBlock: true,
  744. isLimit: true
  745. } );
  746. schema.register( 'tableCell', {
  747. allowIn: 'tableRow',
  748. allowContentOf: '$block',
  749. allowAttributes: [ 'colspan', 'rowspan' ],
  750. isBlock: true,
  751. isLimit: true
  752. } );
  753. conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget: true } ) );
  754. conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget: true } ) );
  755. conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget: true } ) );
  756. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange( { asWidget: true } ) );
  757. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange( { asWidget: true } ) );
  758. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  759. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  760. } );
  761. } );
  762. it( 'should create renamed cell as a widget', () => {
  763. setModelData( model,
  764. '<table>' +
  765. '<tableRow><tableCell>foo</tableCell></tableRow>' +
  766. '</table>'
  767. );
  768. const table = root.getChild( 0 );
  769. model.change( writer => {
  770. writer.setAttribute( 'headingRows', 1, table );
  771. } );
  772. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  773. '<table class="ck-widget" contenteditable="false">' +
  774. '<thead>' +
  775. '<tr><th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">foo</th></tr>' +
  776. '</thead>' +
  777. '</table>'
  778. );
  779. } );
  780. } );
  781. } );
  782. describe( 'downcastTableHeadingRowsChange()', () => {
  783. it( 'should work for adding heading rows', () => {
  784. setModelData( model, modelTable( [
  785. [ '11', '12' ],
  786. [ '21', '22' ],
  787. [ '31', '32' ]
  788. ] ) );
  789. const table = root.getChild( 0 );
  790. model.change( writer => {
  791. writer.setAttribute( 'headingRows', 2, table );
  792. } );
  793. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  794. [ '11', '12' ],
  795. [ '21', '22' ],
  796. [ '31', '32' ]
  797. ], { headingRows: 2 } ) );
  798. } );
  799. it( 'should work for changing number of heading rows to a bigger number', () => {
  800. setModelData( model, modelTable( [
  801. [ '11', '12' ],
  802. [ '21', '22' ],
  803. [ '31', '32' ]
  804. ], { headingRows: 1 } ) );
  805. const table = root.getChild( 0 );
  806. model.change( writer => {
  807. writer.setAttribute( 'headingRows', 2, table );
  808. } );
  809. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  810. [ '11', '12' ],
  811. [ '21', '22' ],
  812. [ '31', '32' ]
  813. ], { headingRows: 2 } ) );
  814. } );
  815. it( 'should work for changing number of heading rows to a smaller number', () => {
  816. setModelData( model, modelTable( [
  817. [ '11', '12' ],
  818. [ '21', '22' ],
  819. [ '31', '32' ],
  820. [ '41', '42' ]
  821. ], { headingRows: 3 } ) );
  822. const table = root.getChild( 0 );
  823. model.change( writer => {
  824. writer.setAttribute( 'headingRows', 2, table );
  825. } );
  826. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  827. [ '11', '12' ],
  828. [ '21', '22' ],
  829. [ '31', '32' ],
  830. [ '41', '42' ]
  831. ], { headingRows: 2 } ) );
  832. } );
  833. it( 'should work for removing heading rows', () => {
  834. setModelData( model, modelTable( [
  835. [ '11', '12' ],
  836. [ '21', '22' ]
  837. ], { headingRows: 2 } ) );
  838. const table = root.getChild( 0 );
  839. model.change( writer => {
  840. writer.removeAttribute( 'headingRows', table );
  841. } );
  842. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  843. [ '11', '12' ],
  844. [ '21', '22' ]
  845. ] ) );
  846. } );
  847. it( 'should work for making heading rows without tbody', () => {
  848. setModelData( model, modelTable( [
  849. [ '11', '12' ],
  850. [ '21', '22' ]
  851. ] ) );
  852. const table = root.getChild( 0 );
  853. model.change( writer => {
  854. writer.setAttribute( 'headingRows', 2, table );
  855. } );
  856. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
  857. [ '11', '12' ],
  858. [ '21', '22' ]
  859. ], { headingRows: 2 } ) );
  860. } );
  861. it( 'should be possible to overwrite', () => {
  862. editor.conversion.attributeToAttribute( { model: 'headingRows', view: 'headingRows', converterPriority: 'high' } );
  863. setModelData( model, modelTable( [ [ '11' ] ] ) );
  864. const table = root.getChild( 0 );
  865. model.change( writer => {
  866. writer.setAttribute( 'headingRows', 1, table );
  867. } );
  868. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  869. '<table headingRows="1"><tbody><tr><td>11</td></tr></tbody></table>'
  870. );
  871. } );
  872. it( 'should work with adding table rows at the beginning of a table', () => {
  873. setModelData( model, modelTable( [
  874. [ '00', '01' ],
  875. [ '10', '11' ]
  876. ], { headingRows: 1 } ) );
  877. const table = root.getChild( 0 );
  878. model.change( writer => {
  879. writer.setAttribute( 'headingRows', 2, table );
  880. const tableRow = writer.createElement( 'tableRow' );
  881. writer.insert( tableRow, table, 0 );
  882. writer.insertElement( 'tableCell', tableRow, 'end' );
  883. writer.insertElement( 'tableCell', tableRow, 'end' );
  884. } );
  885. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  886. [ '', '' ],
  887. [ '00', '01' ],
  888. [ '10', '11' ]
  889. ], { headingRows: 2 } ) );
  890. } );
  891. describe( 'asWidget', () => {
  892. beforeEach( () => {
  893. return VirtualTestEditor.create()
  894. .then( newEditor => {
  895. editor = newEditor;
  896. model = editor.model;
  897. doc = model.document;
  898. root = doc.getRoot( 'main' );
  899. viewDocument = editor.editing.view;
  900. const conversion = editor.conversion;
  901. const schema = model.schema;
  902. schema.register( 'table', {
  903. allowWhere: '$block',
  904. allowAttributes: [ 'headingRows', 'headingColumns' ],
  905. isBlock: true,
  906. isObject: true
  907. } );
  908. schema.register( 'tableRow', {
  909. allowIn: 'table',
  910. allowAttributes: [],
  911. isBlock: true,
  912. isLimit: true
  913. } );
  914. schema.register( 'tableCell', {
  915. allowIn: 'tableRow',
  916. allowContentOf: '$block',
  917. allowAttributes: [ 'colspan', 'rowspan' ],
  918. isBlock: true,
  919. isLimit: true
  920. } );
  921. conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget: true } ) );
  922. conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget: true } ) );
  923. conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget: true } ) );
  924. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange( { asWidget: true } ) );
  925. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange( { asWidget: true } ) );
  926. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  927. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  928. } );
  929. } );
  930. it( 'should create renamed cell as a widget', () => {
  931. setModelData( model,
  932. '<table>' +
  933. '<tableRow><tableCell>foo</tableCell></tableRow>' +
  934. '</table>'
  935. );
  936. const table = root.getChild( 0 );
  937. model.change( writer => {
  938. writer.setAttribute( 'headingColumns', 1, table );
  939. } );
  940. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  941. '<table class="ck-widget" contenteditable="false">' +
  942. '<tbody>' +
  943. '<tr><th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">foo</th></tr>' +
  944. '</tbody>' +
  945. '</table>'
  946. );
  947. } );
  948. } );
  949. } );
  950. describe( 'downcastRemoveRow()', () => {
  951. it( 'should react to removed row from the beginning of a tbody', () => {
  952. setModelData( model, modelTable( [
  953. [ '00[]', '01' ],
  954. [ '10', '11' ]
  955. ] ) );
  956. const table = root.getChild( 0 );
  957. model.change( writer => {
  958. writer.remove( table.getChild( 1 ) );
  959. } );
  960. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  961. [ '00', '01' ]
  962. ] ) );
  963. } );
  964. it( 'should react to removed row form the end of a tbody', () => {
  965. setModelData( model, modelTable( [
  966. [ '00[]', '01' ],
  967. [ '10', '11' ]
  968. ] ) );
  969. const table = root.getChild( 0 );
  970. model.change( writer => {
  971. writer.remove( table.getChild( 0 ) );
  972. } );
  973. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  974. [ '10', '11' ]
  975. ] ) );
  976. } );
  977. it( 'should react to removed row from the beginning of a thead', () => {
  978. setModelData( model, modelTable( [
  979. [ '00[]', '01' ],
  980. [ '10', '11' ]
  981. ], { headingRows: 2 } ) );
  982. const table = root.getChild( 0 );
  983. model.change( writer => {
  984. writer.remove( table.getChild( 1 ) );
  985. } );
  986. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  987. [ '00', '01' ]
  988. ], { headingRows: 2 } ) );
  989. } );
  990. it( 'should react to removed row form the end of a thead', () => {
  991. setModelData( model, modelTable( [
  992. [ '00[]', '01' ],
  993. [ '10', '11' ]
  994. ], { headingRows: 2 } ) );
  995. const table = root.getChild( 0 );
  996. model.change( writer => {
  997. writer.remove( table.getChild( 0 ) );
  998. } );
  999. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  1000. [ '10', '11' ]
  1001. ], { headingRows: 2 } ) );
  1002. } );
  1003. it( 'should remove empty thead section if a last row was removed from thead', () => {
  1004. setModelData( model, modelTable( [
  1005. [ '00[]', '01' ],
  1006. [ '10', '11' ]
  1007. ], { headingRows: 1 } ) );
  1008. const table = root.getChild( 0 );
  1009. model.change( writer => {
  1010. writer.setAttribute( 'headingRows', 0, table );
  1011. writer.remove( table.getChild( 0 ) );
  1012. } );
  1013. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  1014. [ '10', '11' ]
  1015. ] ) );
  1016. } );
  1017. it( 'should remove empty tbody section if a last row was removed from tbody', () => {
  1018. setModelData( model, modelTable( [
  1019. [ '00[]', '01' ],
  1020. [ '10', '11' ]
  1021. ], { headingRows: 1 } ) );
  1022. const table = root.getChild( 0 );
  1023. model.change( writer => {
  1024. writer.remove( table.getChild( 1 ) );
  1025. } );
  1026. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  1027. [ '00', '01' ]
  1028. ], { headingRows: 1 } ) );
  1029. } );
  1030. } );
  1031. } );