tablecellproperties.js 17 KB

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