tablecellproperties.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import Widget from '@ckeditor/ckeditor5-widget/src/widget';
  8. import TableEditing from '../../src/tableediting';
  9. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. import TableCellProperties from '../../src/tablecellproperites';
  12. describe( 'Table cell properties conversion', () => {
  13. let editor, model;
  14. beforeEach( () => {
  15. return VirtualTestEditor
  16. .create( {
  17. plugins: [ TableEditing, TableCellProperties, Paragraph, Widget ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. // Since this part of test tests only data conversion so editing pipeline is not necessary.
  23. editor.editing.destroy();
  24. } );
  25. } );
  26. describe( 'upcast', () => {
  27. it( 'should upcast border shorthand', () => {
  28. editor.setData( '<table><tr><td style="border:1px solid #f00">foo</td></tr></table>' );
  29. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  30. assertTRBLAttribute( tableCell, 'borderColor', '#f00' );
  31. assertTRBLAttribute( tableCell, 'borderStyle', 'solid' );
  32. assertTRBLAttribute( tableCell, 'borderWidth', '1px' );
  33. } );
  34. it( 'should upcast border-color shorthand', () => {
  35. editor.setData( '<table><tr><td style="border-color:#f00">foo</td></tr></table>' );
  36. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  37. assertTRBLAttribute( tableCell, 'borderColor', '#f00' );
  38. } );
  39. it( 'should upcast border-style shorthand', () => {
  40. editor.setData( '<table><tr><td style="border-style:ridge">foo</td></tr></table>' );
  41. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  42. assertTRBLAttribute( tableCell, 'borderStyle', 'ridge' );
  43. } );
  44. it( 'should upcast border-width shorthand', () => {
  45. editor.setData( '<table><tr><td style="border-width:1px">foo</td></tr></table>' );
  46. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  47. assertTRBLAttribute( tableCell, 'borderWidth', '1px' );
  48. } );
  49. it( 'should upcast border-top shorthand', () => {
  50. editor.setData( '<table><tr><td style="border-top:1px solid #f00">foo</td></tr></table>' );
  51. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  52. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, null, null );
  53. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, null, null );
  54. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, null, null );
  55. } );
  56. it( 'should upcast border-right shorthand', () => {
  57. editor.setData( '<table><tr><td style="border-right:1px solid #f00">foo</td></tr></table>' );
  58. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  59. assertTRBLAttribute( tableCell, 'borderColor', null, '#f00', null, null );
  60. assertTRBLAttribute( tableCell, 'borderStyle', null, 'solid', null, null );
  61. assertTRBLAttribute( tableCell, 'borderWidth', null, '1px', null, null );
  62. } );
  63. it( 'should upcast border-bottom shorthand', () => {
  64. editor.setData( '<table><tr><td style="border-bottom:1px solid #f00">foo</td></tr></table>' );
  65. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  66. assertTRBLAttribute( tableCell, 'borderColor', null, null, '#f00', null );
  67. assertTRBLAttribute( tableCell, 'borderStyle', null, null, 'solid', null );
  68. assertTRBLAttribute( tableCell, 'borderWidth', null, null, '1px', null );
  69. } );
  70. it( 'should upcast border-left shorthand', () => {
  71. editor.setData( '<table><tr><td style="border-left:1px solid #f00">foo</td></tr></table>' );
  72. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  73. assertTRBLAttribute( tableCell, 'borderColor', null, null, null, '#f00' );
  74. assertTRBLAttribute( tableCell, 'borderStyle', null, null, null, 'solid' );
  75. assertTRBLAttribute( tableCell, 'borderWidth', null, null, null, '1px' );
  76. } );
  77. it( 'should upcast border-top-* styles', () => {
  78. editor.setData(
  79. '<table><tr><td style="border-top-width:1px;border-top-style:solid;border-top-color:#f00">foo</td></tr></table>'
  80. );
  81. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  82. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, null, null );
  83. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, null, null );
  84. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, null, null );
  85. } );
  86. it( 'should upcast border-right-* styles', () => {
  87. editor.setData(
  88. '<table><tr><td style="border-right-width:1px;border-right-style:solid;border-right-color:#f00">foo</td></tr></table>'
  89. );
  90. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  91. assertTRBLAttribute( tableCell, 'borderColor', null, '#f00', null, null );
  92. assertTRBLAttribute( tableCell, 'borderStyle', null, 'solid', null, null );
  93. assertTRBLAttribute( tableCell, 'borderWidth', null, '1px', null, null );
  94. } );
  95. it( 'should upcast border-bottom-* styles', () => {
  96. editor.setData(
  97. '<table><tr>' +
  98. '<td style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:#f00">foo</td>' +
  99. '</tr></table>'
  100. );
  101. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  102. assertTRBLAttribute( tableCell, 'borderColor', null, null, '#f00', null );
  103. assertTRBLAttribute( tableCell, 'borderStyle', null, null, 'solid', null );
  104. assertTRBLAttribute( tableCell, 'borderWidth', null, null, '1px', null );
  105. } );
  106. it( 'should upcast border-left-* styles', () => {
  107. editor.setData(
  108. '<table><tr><td style="border-left-width:1px;border-left-style:solid;border-left-color:#f00">foo</td></tr></table>'
  109. );
  110. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  111. assertTRBLAttribute( tableCell, 'borderColor', null, null, null, '#f00' );
  112. assertTRBLAttribute( tableCell, 'borderStyle', null, null, null, 'solid' );
  113. assertTRBLAttribute( tableCell, 'borderWidth', null, null, null, '1px' );
  114. } );
  115. it( 'should upcast background-color', () => {
  116. editor.setData( '<table><tr><td style="background-color:#f00">foo</td></tr></table>' );
  117. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  118. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#f00' );
  119. } );
  120. it( 'should upcast padding shorthand', () => {
  121. editor.setData( '<table><tr><td style="padding:2px 4em">foo</td></tr></table>' );
  122. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  123. assertTRBLAttribute( tableCell, 'padding', '2px', '4em' );
  124. } );
  125. it( 'should upcast vertical-align', () => {
  126. editor.setData( '<table><tr><td style="vertical-align:top">foo</td></tr></table>' );
  127. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  128. expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.equal( 'top' );
  129. } );
  130. } );
  131. describe( 'downcast', () => {
  132. let tableCell;
  133. beforeEach( () => {
  134. setModelData(
  135. model,
  136. '<table headingRows="0" headingColumns="0">' +
  137. '<tableRow>' +
  138. '<tableCell>' +
  139. '<paragraph>foo</paragraph>' +
  140. '</tableCell>' +
  141. '</tableRow>' +
  142. '</table>'
  143. );
  144. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  145. } );
  146. it( 'should downcast borderColor attribute (same top, right, bottom, left)', () => {
  147. model.change( writer => writer.setAttribute( 'borderColor', {
  148. top: '#f00',
  149. right: '#f00',
  150. bottom: '#f00',
  151. left: '#f00'
  152. }, tableCell ) );
  153. assertTableCellStyle( 'border-top:#f00;border-right:#f00;border-bottom:#f00;border-left:#f00;' );
  154. } );
  155. it( 'should downcast borderColor attribute (different top, right, bottom, left)', () => {
  156. model.change( writer => writer.setAttribute( 'borderColor', {
  157. top: '#f00',
  158. right: 'hsla(0, 100%, 50%, 0.5)',
  159. bottom: 'deeppink',
  160. left: 'rgb(255, 0, 0)'
  161. }, tableCell ) );
  162. assertTableCellStyle(
  163. 'border-top:#f00;' +
  164. 'border-right:hsla(0, 100%, 50%, 0.5);' +
  165. 'border-bottom:deeppink;' +
  166. 'border-left:rgb(255, 0, 0);'
  167. );
  168. } );
  169. it( 'should downcast borderStyle attribute (same top, right, bottom, left)', () => {
  170. model.change( writer => writer.setAttribute( 'borderStyle', {
  171. top: 'solid',
  172. right: 'solid',
  173. bottom: 'solid',
  174. left: 'solid'
  175. }, tableCell ) );
  176. assertTableCellStyle( 'border-top:solid;border-right:solid;border-bottom:solid;border-left:solid;' );
  177. } );
  178. it( 'should downcast borderStyle attribute (different top, right, bottom, left)', () => {
  179. model.change( writer => writer.setAttribute( 'borderStyle', {
  180. top: 'solid',
  181. right: 'ridge',
  182. bottom: 'dotted',
  183. left: 'dashed'
  184. }, tableCell ) );
  185. assertTableCellStyle( 'border-top:solid;border-right:ridge;border-bottom:dotted;border-left:dashed;' );
  186. } );
  187. it( 'should downcast borderWidth attribute (same top, right, bottom, left)', () => {
  188. model.change( writer => writer.setAttribute( 'borderWidth', {
  189. top: '42px',
  190. right: '.1em',
  191. bottom: '1337rem',
  192. left: 'thick'
  193. }, tableCell ) );
  194. assertTableCellStyle( 'border-top:42px;border-right:.1em;border-bottom:1337rem;border-left:thick;' );
  195. } );
  196. it( 'should downcast borderWidth attribute (different top, right, bottom, left)', () => {
  197. model.change( writer => writer.setAttribute( 'borderWidth', {
  198. top: '42px',
  199. right: '42px',
  200. bottom: '42px',
  201. left: '42px'
  202. }, tableCell ) );
  203. assertTableCellStyle( 'border-top:42px;border-right:42px;border-bottom:42px;border-left:42px;' );
  204. } );
  205. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (same top, right, bottom, left)', () => {
  206. model.change( writer => {
  207. writer.setAttribute( 'borderColor', {
  208. top: '#f00',
  209. right: '#f00',
  210. bottom: '#f00',
  211. left: '#f00'
  212. }, tableCell );
  213. writer.setAttribute( 'borderStyle', {
  214. top: 'solid',
  215. right: 'solid',
  216. bottom: 'solid',
  217. left: 'solid'
  218. }, tableCell );
  219. writer.setAttribute( 'borderWidth', {
  220. top: '42px',
  221. right: '42px',
  222. bottom: '42px',
  223. left: '42px'
  224. }, tableCell );
  225. } );
  226. assertTableCellStyle(
  227. 'border-top:42px solid #f00;' +
  228. 'border-right:42px solid #f00;' +
  229. 'border-bottom:42px solid #f00;' +
  230. 'border-left:42px solid #f00;'
  231. );
  232. } );
  233. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (different top, right, bottom, left)', () => {
  234. model.change( writer => {
  235. writer.setAttribute( 'borderColor', {
  236. top: '#f00',
  237. right: 'hsla(0, 100%, 50%, 0.5)',
  238. bottom: 'deeppink',
  239. left: 'rgb(255, 0, 0)'
  240. }, tableCell );
  241. writer.setAttribute( 'borderStyle', {
  242. top: 'solid',
  243. right: 'ridge',
  244. bottom: 'dotted',
  245. left: 'dashed'
  246. }, tableCell );
  247. writer.setAttribute( 'borderWidth', {
  248. top: '42px',
  249. right: '.1em',
  250. bottom: '1337rem',
  251. left: 'thick'
  252. }, tableCell );
  253. } );
  254. assertTableCellStyle(
  255. 'border-top:42px solid #f00;' +
  256. 'border-right:.1em ridge hsla(0, 100%, 50%, 0.5);' +
  257. 'border-bottom:1337rem dotted deeppink;' +
  258. 'border-left:thick dashed rgb(255, 0, 0);'
  259. );
  260. } );
  261. it( 'should downcast backgroundColor', () => {
  262. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
  263. assertTableCellStyle( 'background-color:#f00;' );
  264. } );
  265. it( 'should downcast padding (same top, right, bottom, left)', () => {
  266. model.change( writer => writer.setAttribute( 'padding', {
  267. top: '2px',
  268. right: '2px',
  269. bottom: '2px',
  270. left: '2px'
  271. }, tableCell ) );
  272. assertTableCellStyle( 'padding:2px;' );
  273. } );
  274. it( 'should downcast padding (different top, right, bottom, left)', () => {
  275. model.change( writer => writer.setAttribute( 'padding', {
  276. top: '2px',
  277. right: '3px',
  278. bottom: '4px',
  279. left: '5px'
  280. }, tableCell ) );
  281. assertTableCellStyle( 'padding:2px 3px 4px 5px;' );
  282. } );
  283. it( 'should downcast verticalAlignment', () => {
  284. model.change( writer => writer.setAttribute( 'verticalAlignment', 'middle', tableCell ) );
  285. assertTableCellStyle( 'vertical-align:middle;' );
  286. } );
  287. describe( 'change attribute', () => {
  288. beforeEach( () => {
  289. model.change( writer => {
  290. writer.setAttribute( 'borderColor', {
  291. top: '#f00',
  292. right: '#f00',
  293. bottom: '#f00',
  294. left: '#f00'
  295. }, tableCell );
  296. writer.setAttribute( 'borderStyle', {
  297. top: 'solid',
  298. right: 'solid',
  299. bottom: 'solid',
  300. left: 'solid'
  301. }, tableCell );
  302. writer.setAttribute( 'borderWidth', {
  303. top: '42px',
  304. right: '42px',
  305. bottom: '42px',
  306. left: '42px'
  307. }, tableCell );
  308. } );
  309. } );
  310. it( 'should downcast borderColor attribute change', () => {
  311. model.change( writer => writer.setAttribute( 'borderColor', {
  312. top: 'deeppink',
  313. right: 'deeppink',
  314. bottom: 'deeppink',
  315. left: 'deeppink'
  316. }, tableCell ) );
  317. assertTableCellStyle(
  318. 'border-top:42px solid deeppink;' +
  319. 'border-right:42px solid deeppink;' +
  320. 'border-bottom:42px solid deeppink;' +
  321. 'border-left:42px solid deeppink;'
  322. );
  323. } );
  324. it( 'should downcast borderStyle attribute change', () => {
  325. model.change( writer => writer.setAttribute( 'borderStyle', {
  326. top: 'ridge',
  327. right: 'ridge',
  328. bottom: 'ridge',
  329. left: 'ridge'
  330. }, tableCell ) );
  331. assertTableCellStyle(
  332. 'border-top:42px ridge #f00;' +
  333. 'border-right:42px ridge #f00;' +
  334. 'border-bottom:42px ridge #f00;' +
  335. 'border-left:42px ridge #f00;'
  336. );
  337. } );
  338. it( 'should downcast borderWidth attribute change', () => {
  339. model.change( writer => writer.setAttribute( 'borderWidth', {
  340. top: 'thick',
  341. right: 'thick',
  342. bottom: 'thick',
  343. left: 'thick'
  344. }, tableCell ) );
  345. assertTableCellStyle(
  346. 'border-top:thick solid #f00;' +
  347. 'border-right:thick solid #f00;' +
  348. 'border-bottom:thick solid #f00;' +
  349. 'border-left:thick solid #f00;'
  350. );
  351. } );
  352. it( 'should downcast borderColor attribute removal', () => {
  353. model.change( writer => writer.removeAttribute( 'borderColor', tableCell ) );
  354. assertTableCellStyle(
  355. 'border-top:42px solid;' +
  356. 'border-right:42px solid;' +
  357. 'border-bottom:42px solid;' +
  358. 'border-left:42px solid;'
  359. );
  360. } );
  361. it( 'should downcast borderStyle attribute removal', () => {
  362. model.change( writer => writer.removeAttribute( 'borderStyle', tableCell ) );
  363. assertTableCellStyle(
  364. 'border-top:42px #f00;' +
  365. 'border-right:42px #f00;' +
  366. 'border-bottom:42px #f00;' +
  367. 'border-left:42px #f00;'
  368. );
  369. } );
  370. it( 'should downcast borderWidth attribute removal', () => {
  371. model.change( writer => writer.removeAttribute( 'borderWidth', tableCell ) );
  372. assertTableCellStyle(
  373. 'border-top:solid #f00;' +
  374. 'border-right:solid #f00;' +
  375. 'border-bottom:solid #f00;' +
  376. 'border-left:solid #f00;'
  377. );
  378. } );
  379. it( 'should downcast borderColor, borderStyle and borderWidth attributes removal', () => {
  380. model.change( writer => {
  381. writer.removeAttribute( 'borderColor', tableCell );
  382. writer.removeAttribute( 'borderStyle', tableCell );
  383. writer.removeAttribute( 'borderWidth', tableCell );
  384. } );
  385. assertEqualMarkup(
  386. editor.getData(),
  387. '<figure class="table"><table><tbody><tr><td>foo</td></tr></tbody></table></figure>'
  388. );
  389. } );
  390. } );
  391. } );
  392. /**
  393. * Assertion helper for top-right-bottom-left attribute object.
  394. *
  395. * @param {module:engine/model/node~Node} tableCell
  396. * @param {String} key Attribute key
  397. * @param {String} top Top value. Pass null to omit value in attributes object.
  398. * @param {String} [right=top] Right value - defaults to top if not provided.
  399. * Pass null to omit value in attributes object.
  400. * @param {String} [bottom=top] Bottom value - defaults to top (right value must be defined).
  401. * Pass null to omit value in attributes object.
  402. * @param {String} [left=right] Left value - defaults to right (bottom and right values must be defined).
  403. * Pass null to omit value in attributes object.
  404. */
  405. function assertTRBLAttribute( tableCell, key, top, right = top, bottom = top, left = right ) {
  406. const styleObject = {};
  407. if ( top ) {
  408. styleObject.top = top;
  409. }
  410. if ( right ) {
  411. styleObject.right = right;
  412. }
  413. if ( bottom ) {
  414. styleObject.bottom = bottom;
  415. }
  416. if ( left ) {
  417. styleObject.left = left;
  418. }
  419. expect( tableCell.getAttribute( key ) ).to.deep.equal( styleObject );
  420. }
  421. /**
  422. * Assertion helper for testing <td> style attribute.
  423. *
  424. * @param {String} tableCellStyle A style to assert on td.
  425. */
  426. function assertTableCellStyle( tableCellStyle ) {
  427. assertEqualMarkup( editor.getData(),
  428. `<figure class="table"><table><tbody><tr><td style="${ tableCellStyle }">foo</td></tr></tbody></table></figure>`
  429. );
  430. }
  431. } );