table-ascii-art.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 { createTableAsciiArt, modelTable, prepareModelTableInput, prettyFormatModelTableInput } from '../_utils/utils';
  6. import TableEditing from '../../src/tableediting';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. describe( 'table ascii-art and model helpers', () => {
  11. let editor, model, modelRoot;
  12. beforeEach( () => {
  13. return VirtualTestEditor
  14. .create( {
  15. plugins: [ TableEditing, Paragraph ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. modelRoot = model.document.getRoot();
  21. } );
  22. } );
  23. afterEach( () => {
  24. editor.destroy();
  25. } );
  26. describe( 'for the table with only one cell', () => {
  27. let table, tableData;
  28. beforeEach( () => {
  29. tableData = [
  30. [ '00' ]
  31. ];
  32. setModelData( model, modelTable( tableData ) );
  33. table = modelRoot.getChild( 0 );
  34. } );
  35. it( 'should create proper ascii-art', () => {
  36. const asciiArt = createTableAsciiArt( model, table );
  37. expect( asciiArt ).to.equal( [
  38. '+----+',
  39. '| 00 |',
  40. '+----+'
  41. ].join( '\n' ) );
  42. } );
  43. it( 'should create proper tableData', () => {
  44. const modelData = prepareModelTableInput( model, table );
  45. const modelDataString = prettyFormatModelTableInput( modelData );
  46. expect( modelData ).to.deep.equal( tableData );
  47. assertSameCodeString( modelDataString,
  48. `[
  49. [ '00' ]
  50. ]`
  51. );
  52. } );
  53. } );
  54. describe( 'for the table containing only one row', () => {
  55. let table, tableData;
  56. beforeEach( () => {
  57. tableData = [
  58. [ '00', '01' ]
  59. ];
  60. setModelData( model, modelTable( tableData ) );
  61. table = modelRoot.getChild( 0 );
  62. } );
  63. it( 'should create proper ascii-art', () => {
  64. const asciiArt = createTableAsciiArt( model, table );
  65. expect( asciiArt ).to.equal( [
  66. '+----+----+',
  67. '| 00 | 01 |',
  68. '+----+----+'
  69. ].join( '\n' ) );
  70. } );
  71. it( 'should create proper tableData', () => {
  72. const modelData = prepareModelTableInput( model, table );
  73. const modelDataString = prettyFormatModelTableInput( modelData );
  74. expect( modelData ).to.deep.equal( tableData );
  75. assertSameCodeString( modelDataString,
  76. `[
  77. [ '00', '01' ]
  78. ]`
  79. );
  80. } );
  81. } );
  82. describe( 'for the table containing only one column', () => {
  83. let table, tableData;
  84. beforeEach( () => {
  85. tableData = [
  86. [ '00' ],
  87. [ '10' ]
  88. ];
  89. setModelData( model, modelTable( tableData ) );
  90. table = modelRoot.getChild( 0 );
  91. } );
  92. it( 'should create proper ascii-art', () => {
  93. const asciiArt = createTableAsciiArt( model, table );
  94. expect( asciiArt ).to.equal( [
  95. '+----+',
  96. '| 00 |',
  97. '+----+',
  98. '| 10 |',
  99. '+----+'
  100. ].join( '\n' ) );
  101. } );
  102. it( 'should create proper tableData', () => {
  103. const modelData = prepareModelTableInput( model, table );
  104. const modelDataString = prettyFormatModelTableInput( modelData );
  105. expect( modelData ).to.deep.equal( tableData );
  106. assertSameCodeString( modelDataString,
  107. `[
  108. [ '00' ],
  109. [ '10' ]
  110. ]`
  111. );
  112. } );
  113. } );
  114. describe( 'for the table containing two rows and two columns', () => {
  115. let table, tableData;
  116. beforeEach( () => {
  117. tableData = [
  118. [ '00', '01' ],
  119. [ '10', '11' ]
  120. ];
  121. setModelData( model, modelTable( tableData ) );
  122. table = modelRoot.getChild( 0 );
  123. } );
  124. it( 'should create proper ascii-art', () => {
  125. const asciiArt = createTableAsciiArt( model, table );
  126. expect( asciiArt ).to.equal( [
  127. '+----+----+',
  128. '| 00 | 01 |',
  129. '+----+----+',
  130. '| 10 | 11 |',
  131. '+----+----+'
  132. ].join( '\n' ) );
  133. } );
  134. it( 'should create proper tableData', () => {
  135. const modelData = prepareModelTableInput( model, table );
  136. const modelDataString = prettyFormatModelTableInput( modelData );
  137. expect( modelData ).to.deep.equal( tableData );
  138. assertSameCodeString( modelDataString,
  139. `[
  140. [ '00', '01' ],
  141. [ '10', '11' ]
  142. ]`
  143. );
  144. } );
  145. } );
  146. describe( 'for the table containing column and row-spanned cells', () => {
  147. let table, tableData;
  148. beforeEach( () => {
  149. tableData = [
  150. [ { contents: '00', colspan: 2, rowspan: 2 }, { contents: '02', rowspan: 2 }, '03' ],
  151. [ '13' ],
  152. [ { contents: '20', colspan: 2 }, { contents: '22', colspan: 2, rowspan: 2 } ],
  153. [ '30', '31' ]
  154. ];
  155. setModelData( model, modelTable( structuredClone( tableData ) ) );
  156. table = modelRoot.getChild( 0 );
  157. } );
  158. it( 'should create proper ascii-art', () => {
  159. const asciiArt = createTableAsciiArt( model, table );
  160. expect( asciiArt ).to.equal( [
  161. '+----+----+----+----+',
  162. '| 00 | 02 | 03 |',
  163. '+ + +----+',
  164. '| | | 13 |',
  165. '+----+----+----+----+',
  166. '| 20 | 22 |',
  167. '+----+----+ +',
  168. '| 30 | 31 | |',
  169. '+----+----+----+----+'
  170. ].join( '\n' ) );
  171. } );
  172. it( 'should create proper tableData', () => {
  173. const modelData = prepareModelTableInput( model, table );
  174. const modelDataString = prettyFormatModelTableInput( modelData );
  175. expect( modelData ).to.deep.equal( tableData );
  176. assertSameCodeString( modelDataString,
  177. `[
  178. [ { contents: '00', colspan: 2, rowspan: 2 }, { contents: '02', rowspan: 2 }, '03' ],
  179. [ '13' ],
  180. [ { contents: '20', colspan: 2 }, { contents: '22', colspan: 2, rowspan: 2 } ],
  181. [ '30', '31' ]
  182. ]`
  183. );
  184. } );
  185. } );
  186. describe( 'for the table containing larger column and row-spanned cells', () => {
  187. let table, tableData;
  188. beforeEach( () => {
  189. tableData = [
  190. [ '00', { contents: '01', rowspan: 2 }, { contents: '02', rowspan: 3 }, { contents: '03', rowspan: 4 } ],
  191. [ '10' ],
  192. [ { contents: '20', colspan: 2 } ],
  193. [ { contents: '30', colspan: 3 } ],
  194. [ { contents: '40', colspan: 4 } ]
  195. ];
  196. setModelData( model, modelTable( structuredClone( tableData ) ) );
  197. table = modelRoot.getChild( 0 );
  198. } );
  199. it( 'should create proper ascii-art', () => {
  200. const asciiArt = createTableAsciiArt( model, table );
  201. expect( asciiArt ).to.equal( [
  202. '+----+----+----+----+',
  203. '| 00 | 01 | 02 | 03 |',
  204. '+----+ + + +',
  205. '| 10 | | | |',
  206. '+----+----+ + +',
  207. '| 20 | | |',
  208. '+----+----+----+ +',
  209. '| 30 | |',
  210. '+----+----+----+----+',
  211. '| 40 |',
  212. '+----+----+----+----+'
  213. ].join( '\n' ) );
  214. } );
  215. it( 'should create proper tableData', () => {
  216. const modelData = prepareModelTableInput( model, table );
  217. const modelDataString = prettyFormatModelTableInput( modelData );
  218. expect( modelData ).to.deep.equal( tableData );
  219. assertSameCodeString( modelDataString,
  220. `[
  221. [ '00', { contents: '01', rowspan: 2 }, { contents: '02', rowspan: 3 }, { contents: '03', rowspan: 4 } ],
  222. [ '10' ],
  223. [ { contents: '20', colspan: 2 } ],
  224. [ { contents: '30', colspan: 3 } ],
  225. [ { contents: '40', colspan: 4 } ]
  226. ]`
  227. );
  228. } );
  229. } );
  230. describe( 'with cells\' content not matching cell\'s coordinates', () => {
  231. let table, tableData;
  232. beforeEach( () => {
  233. tableData = [
  234. [ '', 'x' ],
  235. [ '10', 'foobar' ]
  236. ];
  237. setModelData( model, modelTable( tableData ) );
  238. table = modelRoot.getChild( 0 );
  239. } );
  240. it( 'should create proper ascii-art', () => {
  241. const asciiArt = createTableAsciiArt( model, table );
  242. expect( asciiArt ).to.equal( [
  243. '+----+----+',
  244. '| | x |',
  245. '+----+----+',
  246. '| 10 | fo |',
  247. '+----+----+'
  248. ].join( '\n' ) );
  249. } );
  250. it( 'should create proper tableData', () => {
  251. const modelData = prepareModelTableInput( model, table );
  252. const modelDataString = prettyFormatModelTableInput( modelData );
  253. tableData = [
  254. [ '', 'x' ],
  255. [ '10', 'foobar' ]
  256. ];
  257. expect( modelData ).to.deep.equal( tableData );
  258. assertSameCodeString( modelDataString,
  259. `[
  260. [ '', 'x' ],
  261. [ '10', 'foobar' ]
  262. ]`
  263. );
  264. } );
  265. } );
  266. function structuredClone( data ) {
  267. return JSON.parse( JSON.stringify( data ) );
  268. }
  269. function assertSameCodeString( actual, expected ) {
  270. expect( trimLines( actual ) ).to.equal( trimLines( expected ) );
  271. }
  272. function trimLines( string ) {
  273. return string.replace( /^\s+|\s+$/gm, '' );
  274. }
  275. } );