tablecellproperties.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 mixed shorthands', () => {
  91. editor.setData(
  92. '<table><tr><td style="border-top:1px solid #f00;border-bottom:2em ridge rgba(255,0,0,1)">foo</td></tr></table>'
  93. );
  94. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  95. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, 'rgba(255, 0, 0, 1)', null );
  96. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, 'ridge', null );
  97. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, '2em', null );
  98. } );
  99. it( 'should upcast border-top-* styles', () => {
  100. editor.setData(
  101. '<table><tr><td style="border-top-width:1px;border-top-style:solid;border-top-color:#f00">foo</td></tr></table>'
  102. );
  103. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  104. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, null, null );
  105. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, null, null );
  106. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, null, null );
  107. } );
  108. it( 'should upcast border-right-* styles', () => {
  109. editor.setData(
  110. '<table><tr><td style="border-right-width:1px;border-right-style:solid;border-right-color:#f00">foo</td></tr></table>'
  111. );
  112. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  113. assertTRBLAttribute( tableCell, 'borderColor', null, '#f00', null, null );
  114. assertTRBLAttribute( tableCell, 'borderStyle', null, 'solid', null, null );
  115. assertTRBLAttribute( tableCell, 'borderWidth', null, '1px', null, null );
  116. } );
  117. it( 'should upcast border-bottom-* styles', () => {
  118. editor.setData(
  119. '<table><tr>' +
  120. '<td style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:#f00">foo</td>' +
  121. '</tr></table>'
  122. );
  123. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  124. assertTRBLAttribute( tableCell, 'borderColor', null, null, '#f00', null );
  125. assertTRBLAttribute( tableCell, 'borderStyle', null, null, 'solid', null );
  126. assertTRBLAttribute( tableCell, 'borderWidth', null, null, '1px', null );
  127. } );
  128. it( 'should upcast border-left-* styles', () => {
  129. editor.setData(
  130. '<table><tr><td style="border-left-width:1px;border-left-style:solid;border-left-color:#f00">foo</td></tr></table>'
  131. );
  132. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  133. assertTRBLAttribute( tableCell, 'borderColor', null, null, null, '#f00' );
  134. assertTRBLAttribute( tableCell, 'borderStyle', null, null, null, 'solid' );
  135. assertTRBLAttribute( tableCell, 'borderWidth', null, null, null, '1px' );
  136. } );
  137. it( 'should upcast background-color', () => {
  138. editor.setData( '<table><tr><td style="background-color:#f00">foo</td></tr></table>' );
  139. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  140. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#f00' );
  141. } );
  142. it( 'should upcast padding shorthand', () => {
  143. editor.setData( '<table><tr><td style="padding:2px 4em">foo</td></tr></table>' );
  144. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  145. assertTRBLAttribute( tableCell, 'padding', '2px', '4em' );
  146. } );
  147. it( 'should upcast vertical-align', () => {
  148. editor.setData( '<table><tr><td style="vertical-align:top">foo</td></tr></table>' );
  149. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  150. expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.equal( 'top' );
  151. } );
  152. } );
  153. describe( 'downcast', () => {
  154. let tableCell;
  155. beforeEach( () => {
  156. setModelData(
  157. model,
  158. '<table headingRows="0" headingColumns="0">' +
  159. '<tableRow>' +
  160. '<tableCell>' +
  161. '<paragraph>foo</paragraph>' +
  162. '</tableCell>' +
  163. '</tableRow>' +
  164. '</table>'
  165. );
  166. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  167. } );
  168. it( 'should downcast borderColor attribute (same top, right, bottom, left)', () => {
  169. model.change( writer => writer.setAttribute( 'borderColor', {
  170. top: '#f00',
  171. right: '#f00',
  172. bottom: '#f00',
  173. left: '#f00'
  174. }, tableCell ) );
  175. assertTableCellStyle( editor, 'border-top:#f00;border-right:#f00;border-bottom:#f00;border-left:#f00;' );
  176. } );
  177. it( 'should downcast borderColor attribute (different top, right, bottom, left)', () => {
  178. model.change( writer => writer.setAttribute( 'borderColor', {
  179. top: '#f00',
  180. right: 'hsla(0, 100%, 50%, 0.5)',
  181. bottom: 'deeppink',
  182. left: 'rgb(255, 0, 0)'
  183. }, tableCell ) );
  184. assertTableCellStyle( editor,
  185. 'border-top:#f00;' +
  186. 'border-right:hsla(0, 100%, 50%, 0.5);' +
  187. 'border-bottom:deeppink;' +
  188. 'border-left:rgb(255, 0, 0);'
  189. );
  190. } );
  191. it( 'should downcast borderStyle attribute (same top, right, bottom, left)', () => {
  192. model.change( writer => writer.setAttribute( 'borderStyle', {
  193. top: 'solid',
  194. right: 'solid',
  195. bottom: 'solid',
  196. left: 'solid'
  197. }, tableCell ) );
  198. assertTableCellStyle( editor, 'border-top:solid;border-right:solid;border-bottom:solid;border-left:solid;' );
  199. } );
  200. it( 'should downcast borderStyle attribute (different top, right, bottom, left)', () => {
  201. model.change( writer => writer.setAttribute( 'borderStyle', {
  202. top: 'solid',
  203. right: 'ridge',
  204. bottom: 'dotted',
  205. left: 'dashed'
  206. }, tableCell ) );
  207. assertTableCellStyle( editor, 'border-top:solid;border-right:ridge;border-bottom:dotted;border-left:dashed;' );
  208. } );
  209. it( 'should downcast borderWidth attribute (same top, right, bottom, left)', () => {
  210. model.change( writer => writer.setAttribute( 'borderWidth', {
  211. top: '42px',
  212. right: '.1em',
  213. bottom: '1337rem',
  214. left: 'thick'
  215. }, tableCell ) );
  216. assertTableCellStyle( editor, 'border-top:42px;border-right:.1em;border-bottom:1337rem;border-left:thick;' );
  217. } );
  218. it( 'should downcast borderWidth attribute (different top, right, bottom, left)', () => {
  219. model.change( writer => writer.setAttribute( 'borderWidth', {
  220. top: '42px',
  221. right: '42px',
  222. bottom: '42px',
  223. left: '42px'
  224. }, tableCell ) );
  225. assertTableCellStyle( editor, 'border-top:42px;border-right:42px;border-bottom:42px;border-left:42px;' );
  226. } );
  227. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (same top, right, bottom, left)', () => {
  228. model.change( writer => {
  229. writer.setAttribute( 'borderColor', {
  230. top: '#f00',
  231. right: '#f00',
  232. bottom: '#f00',
  233. left: '#f00'
  234. }, tableCell );
  235. writer.setAttribute( 'borderStyle', {
  236. top: 'solid',
  237. right: 'solid',
  238. bottom: 'solid',
  239. left: 'solid'
  240. }, tableCell );
  241. writer.setAttribute( 'borderWidth', {
  242. top: '42px',
  243. right: '42px',
  244. bottom: '42px',
  245. left: '42px'
  246. }, tableCell );
  247. } );
  248. assertTableCellStyle( editor,
  249. 'border-top:42px solid #f00;' +
  250. 'border-right:42px solid #f00;' +
  251. 'border-bottom:42px solid #f00;' +
  252. 'border-left:42px solid #f00;'
  253. );
  254. } );
  255. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (different top, right, bottom, left)', () => {
  256. model.change( writer => {
  257. writer.setAttribute( 'borderColor', {
  258. top: '#f00',
  259. right: 'hsla(0, 100%, 50%, 0.5)',
  260. bottom: 'deeppink',
  261. left: 'rgb(255, 0, 0)'
  262. }, tableCell );
  263. writer.setAttribute( 'borderStyle', {
  264. top: 'solid',
  265. right: 'ridge',
  266. bottom: 'dotted',
  267. left: 'dashed'
  268. }, tableCell );
  269. writer.setAttribute( 'borderWidth', {
  270. top: '42px',
  271. right: '.1em',
  272. bottom: '1337rem',
  273. left: 'thick'
  274. }, tableCell );
  275. } );
  276. assertTableCellStyle( editor,
  277. 'border-top:42px solid #f00;' +
  278. 'border-right:.1em ridge hsla(0, 100%, 50%, 0.5);' +
  279. 'border-bottom:1337rem dotted deeppink;' +
  280. 'border-left:thick dashed rgb(255, 0, 0);'
  281. );
  282. } );
  283. it( 'should downcast backgroundColor', () => {
  284. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
  285. assertTableCellStyle( editor, 'background-color:#f00;' );
  286. } );
  287. it( 'should downcast padding (same top, right, bottom, left)', () => {
  288. model.change( writer => writer.setAttribute( 'padding', {
  289. top: '2px',
  290. right: '2px',
  291. bottom: '2px',
  292. left: '2px'
  293. }, tableCell ) );
  294. assertTableCellStyle( editor, 'padding:2px;' );
  295. } );
  296. it( 'should downcast padding (different top, right, bottom, left)', () => {
  297. model.change( writer => writer.setAttribute( 'padding', {
  298. top: '2px',
  299. right: '3px',
  300. bottom: '4px',
  301. left: '5px'
  302. }, tableCell ) );
  303. assertTableCellStyle( editor, 'padding:2px 3px 4px 5px;' );
  304. } );
  305. it( 'should downcast verticalAlignment', () => {
  306. model.change( writer => writer.setAttribute( 'verticalAlignment', 'middle', tableCell ) );
  307. assertTableCellStyle( editor, 'vertical-align:middle;' );
  308. } );
  309. describe( 'change attribute', () => {
  310. beforeEach( () => {
  311. model.change( writer => {
  312. writer.setAttribute( 'borderColor', {
  313. top: '#f00',
  314. right: '#f00',
  315. bottom: '#f00',
  316. left: '#f00'
  317. }, tableCell );
  318. writer.setAttribute( 'borderStyle', {
  319. top: 'solid',
  320. right: 'solid',
  321. bottom: 'solid',
  322. left: 'solid'
  323. }, tableCell );
  324. writer.setAttribute( 'borderWidth', {
  325. top: '42px',
  326. right: '42px',
  327. bottom: '42px',
  328. left: '42px'
  329. }, tableCell );
  330. } );
  331. } );
  332. it( 'should downcast borderColor attribute change', () => {
  333. model.change( writer => writer.setAttribute( 'borderColor', {
  334. top: 'deeppink',
  335. right: 'deeppink',
  336. bottom: 'deeppink',
  337. left: 'deeppink'
  338. }, tableCell ) );
  339. assertTableCellStyle( editor,
  340. 'border-top:42px solid deeppink;' +
  341. 'border-right:42px solid deeppink;' +
  342. 'border-bottom:42px solid deeppink;' +
  343. 'border-left:42px solid deeppink;'
  344. );
  345. } );
  346. it( 'should downcast borderStyle attribute change', () => {
  347. model.change( writer => writer.setAttribute( 'borderStyle', {
  348. top: 'ridge',
  349. right: 'ridge',
  350. bottom: 'ridge',
  351. left: 'ridge'
  352. }, tableCell ) );
  353. assertTableCellStyle( editor,
  354. 'border-top:42px ridge #f00;' +
  355. 'border-right:42px ridge #f00;' +
  356. 'border-bottom:42px ridge #f00;' +
  357. 'border-left:42px ridge #f00;'
  358. );
  359. } );
  360. it( 'should downcast borderWidth attribute change', () => {
  361. model.change( writer => writer.setAttribute( 'borderWidth', {
  362. top: 'thick',
  363. right: 'thick',
  364. bottom: 'thick',
  365. left: 'thick'
  366. }, tableCell ) );
  367. assertTableCellStyle( editor,
  368. 'border-top:thick solid #f00;' +
  369. 'border-right:thick solid #f00;' +
  370. 'border-bottom:thick solid #f00;' +
  371. 'border-left:thick solid #f00;'
  372. );
  373. } );
  374. it( 'should downcast borderColor attribute removal', () => {
  375. model.change( writer => writer.removeAttribute( 'borderColor', tableCell ) );
  376. assertTableCellStyle( editor,
  377. 'border-top:42px solid;' +
  378. 'border-right:42px solid;' +
  379. 'border-bottom:42px solid;' +
  380. 'border-left:42px solid;'
  381. );
  382. } );
  383. it( 'should downcast borderStyle attribute removal', () => {
  384. model.change( writer => writer.removeAttribute( 'borderStyle', tableCell ) );
  385. assertTableCellStyle( editor,
  386. 'border-top:42px #f00;' +
  387. 'border-right:42px #f00;' +
  388. 'border-bottom:42px #f00;' +
  389. 'border-left:42px #f00;'
  390. );
  391. } );
  392. it( 'should downcast borderWidth attribute removal', () => {
  393. model.change( writer => writer.removeAttribute( 'borderWidth', tableCell ) );
  394. assertTableCellStyle( editor,
  395. 'border-top:solid #f00;' +
  396. 'border-right:solid #f00;' +
  397. 'border-bottom:solid #f00;' +
  398. 'border-left:solid #f00;'
  399. );
  400. } );
  401. it( 'should downcast borderColor, borderStyle and borderWidth attributes removal', () => {
  402. model.change( writer => {
  403. writer.removeAttribute( 'borderColor', tableCell );
  404. writer.removeAttribute( 'borderStyle', tableCell );
  405. writer.removeAttribute( 'borderWidth', tableCell );
  406. } );
  407. assertEqualMarkup(
  408. editor.getData(),
  409. '<figure class="table"><table><tbody><tr><td>foo</td></tr></tbody></table></figure>'
  410. );
  411. } );
  412. } );
  413. } );
  414. } );
  415. } );