8
0

downcast.js 37 KB

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