8
0

tablecellproperties.js 18 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 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. it( 'should allow to be overriden (only border-top consumed)', () => {
  153. editor.conversion.for( 'upcast' ).add( dispatcher => dispatcher.on( 'element:td', ( evt, data, conversionApi ) => {
  154. conversionApi.consumable.consume( data.viewItem, {
  155. styles: [ 'border-top' ]
  156. } );
  157. }, { priority: 'high' } ) );
  158. editor.setData( '<table><tr><td style="border:1px solid blue;">foo</td></tr></table>' );
  159. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  160. expect( tableCell.getAttribute( 'borderColor' ) ).to.be.undefined;
  161. expect( tableCell.getAttribute( 'borderStyle' ) ).to.be.undefined;
  162. expect( tableCell.getAttribute( 'borderWidth' ) ).to.be.undefined;
  163. } );
  164. } );
  165. describe( 'downcast', () => {
  166. let tableCell;
  167. beforeEach( () => {
  168. setModelData(
  169. model,
  170. '<table headingRows="0" headingColumns="0">' +
  171. '<tableRow>' +
  172. '<tableCell>' +
  173. '<paragraph>foo</paragraph>' +
  174. '</tableCell>' +
  175. '</tableRow>' +
  176. '</table>'
  177. );
  178. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  179. } );
  180. it( 'should downcast borderColor attribute (same top, right, bottom, left)', () => {
  181. model.change( writer => writer.setAttribute( 'borderColor', {
  182. top: '#f00',
  183. right: '#f00',
  184. bottom: '#f00',
  185. left: '#f00'
  186. }, tableCell ) );
  187. assertTableCellStyle( editor, 'border-top:#f00;border-right:#f00;border-bottom:#f00;border-left:#f00;' );
  188. } );
  189. it( 'should downcast borderColor attribute (different top, right, bottom, left)', () => {
  190. model.change( writer => writer.setAttribute( 'borderColor', {
  191. top: '#f00',
  192. right: 'hsla(0, 100%, 50%, 0.5)',
  193. bottom: 'deeppink',
  194. left: 'rgb(255, 0, 0)'
  195. }, tableCell ) );
  196. assertTableCellStyle( editor,
  197. 'border-top:#f00;' +
  198. 'border-right:hsla(0, 100%, 50%, 0.5);' +
  199. 'border-bottom:deeppink;' +
  200. 'border-left:rgb(255, 0, 0);'
  201. );
  202. } );
  203. it( 'should downcast borderStyle attribute (same top, right, bottom, left)', () => {
  204. model.change( writer => writer.setAttribute( 'borderStyle', {
  205. top: 'solid',
  206. right: 'solid',
  207. bottom: 'solid',
  208. left: 'solid'
  209. }, tableCell ) );
  210. assertTableCellStyle( editor, 'border-top:solid;border-right:solid;border-bottom:solid;border-left:solid;' );
  211. } );
  212. it( 'should downcast borderStyle attribute (different top, right, bottom, left)', () => {
  213. model.change( writer => writer.setAttribute( 'borderStyle', {
  214. top: 'solid',
  215. right: 'ridge',
  216. bottom: 'dotted',
  217. left: 'dashed'
  218. }, tableCell ) );
  219. assertTableCellStyle( editor, 'border-top:solid;border-right:ridge;border-bottom:dotted;border-left:dashed;' );
  220. } );
  221. it( 'should downcast borderWidth attribute (same top, right, bottom, left)', () => {
  222. model.change( writer => writer.setAttribute( 'borderWidth', {
  223. top: '42px',
  224. right: '.1em',
  225. bottom: '1337rem',
  226. left: 'thick'
  227. }, tableCell ) );
  228. assertTableCellStyle( editor, 'border-top:42px;border-right:.1em;border-bottom:1337rem;border-left:thick;' );
  229. } );
  230. it( 'should downcast borderWidth attribute (different top, right, bottom, left)', () => {
  231. model.change( writer => writer.setAttribute( 'borderWidth', {
  232. top: '42px',
  233. right: '42px',
  234. bottom: '42px',
  235. left: '42px'
  236. }, tableCell ) );
  237. assertTableCellStyle( editor, 'border-top:42px;border-right:42px;border-bottom:42px;border-left:42px;' );
  238. } );
  239. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (same top, right, bottom, left)', () => {
  240. model.change( writer => {
  241. writer.setAttribute( 'borderColor', {
  242. top: '#f00',
  243. right: '#f00',
  244. bottom: '#f00',
  245. left: '#f00'
  246. }, tableCell );
  247. writer.setAttribute( 'borderStyle', {
  248. top: 'solid',
  249. right: 'solid',
  250. bottom: 'solid',
  251. left: 'solid'
  252. }, tableCell );
  253. writer.setAttribute( 'borderWidth', {
  254. top: '42px',
  255. right: '42px',
  256. bottom: '42px',
  257. left: '42px'
  258. }, tableCell );
  259. } );
  260. assertTableCellStyle( editor,
  261. 'border-top:42px solid #f00;' +
  262. 'border-right:42px solid #f00;' +
  263. 'border-bottom:42px solid #f00;' +
  264. 'border-left:42px solid #f00;'
  265. );
  266. } );
  267. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (different top, right, bottom, left)', () => {
  268. model.change( writer => {
  269. writer.setAttribute( 'borderColor', {
  270. top: '#f00',
  271. right: 'hsla(0, 100%, 50%, 0.5)',
  272. bottom: 'deeppink',
  273. left: 'rgb(255, 0, 0)'
  274. }, tableCell );
  275. writer.setAttribute( 'borderStyle', {
  276. top: 'solid',
  277. right: 'ridge',
  278. bottom: 'dotted',
  279. left: 'dashed'
  280. }, tableCell );
  281. writer.setAttribute( 'borderWidth', {
  282. top: '42px',
  283. right: '.1em',
  284. bottom: '1337rem',
  285. left: 'thick'
  286. }, tableCell );
  287. } );
  288. assertTableCellStyle( editor,
  289. 'border-top:42px solid #f00;' +
  290. 'border-right:.1em ridge hsla(0, 100%, 50%, 0.5);' +
  291. 'border-bottom:1337rem dotted deeppink;' +
  292. 'border-left:thick dashed rgb(255, 0, 0);'
  293. );
  294. } );
  295. it( 'should downcast backgroundColor', () => {
  296. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
  297. assertTableCellStyle( editor, 'background-color:#f00;' );
  298. } );
  299. it( 'should downcast padding (same top, right, bottom, left)', () => {
  300. model.change( writer => writer.setAttribute( 'padding', {
  301. top: '2px',
  302. right: '2px',
  303. bottom: '2px',
  304. left: '2px'
  305. }, tableCell ) );
  306. assertTableCellStyle( editor, 'padding:2px;' );
  307. } );
  308. it( 'should downcast padding (different top, right, bottom, left)', () => {
  309. model.change( writer => writer.setAttribute( 'padding', {
  310. top: '2px',
  311. right: '3px',
  312. bottom: '4px',
  313. left: '5px'
  314. }, tableCell ) );
  315. assertTableCellStyle( editor, 'padding:2px 3px 4px 5px;' );
  316. } );
  317. it( 'should downcast verticalAlignment', () => {
  318. model.change( writer => writer.setAttribute( 'verticalAlignment', 'middle', tableCell ) );
  319. assertTableCellStyle( editor, 'vertical-align:middle;' );
  320. } );
  321. describe( 'change attribute', () => {
  322. beforeEach( () => {
  323. model.change( writer => {
  324. writer.setAttribute( 'borderColor', {
  325. top: '#f00',
  326. right: '#f00',
  327. bottom: '#f00',
  328. left: '#f00'
  329. }, tableCell );
  330. writer.setAttribute( 'borderStyle', {
  331. top: 'solid',
  332. right: 'solid',
  333. bottom: 'solid',
  334. left: 'solid'
  335. }, tableCell );
  336. writer.setAttribute( 'borderWidth', {
  337. top: '42px',
  338. right: '42px',
  339. bottom: '42px',
  340. left: '42px'
  341. }, tableCell );
  342. } );
  343. } );
  344. it( 'should downcast borderColor attribute change', () => {
  345. model.change( writer => writer.setAttribute( 'borderColor', {
  346. top: 'deeppink',
  347. right: 'deeppink',
  348. bottom: 'deeppink',
  349. left: 'deeppink'
  350. }, tableCell ) );
  351. assertTableCellStyle( editor,
  352. 'border-top:42px solid deeppink;' +
  353. 'border-right:42px solid deeppink;' +
  354. 'border-bottom:42px solid deeppink;' +
  355. 'border-left:42px solid deeppink;'
  356. );
  357. } );
  358. it( 'should downcast borderStyle attribute change', () => {
  359. model.change( writer => writer.setAttribute( 'borderStyle', {
  360. top: 'ridge',
  361. right: 'ridge',
  362. bottom: 'ridge',
  363. left: 'ridge'
  364. }, tableCell ) );
  365. assertTableCellStyle( editor,
  366. 'border-top:42px ridge #f00;' +
  367. 'border-right:42px ridge #f00;' +
  368. 'border-bottom:42px ridge #f00;' +
  369. 'border-left:42px ridge #f00;'
  370. );
  371. } );
  372. it( 'should downcast borderWidth attribute change', () => {
  373. model.change( writer => writer.setAttribute( 'borderWidth', {
  374. top: 'thick',
  375. right: 'thick',
  376. bottom: 'thick',
  377. left: 'thick'
  378. }, tableCell ) );
  379. assertTableCellStyle( editor,
  380. 'border-top:thick solid #f00;' +
  381. 'border-right:thick solid #f00;' +
  382. 'border-bottom:thick solid #f00;' +
  383. 'border-left:thick solid #f00;'
  384. );
  385. } );
  386. it( 'should downcast borderColor attribute removal', () => {
  387. model.change( writer => writer.removeAttribute( 'borderColor', tableCell ) );
  388. assertTableCellStyle( editor,
  389. 'border-top:42px solid;' +
  390. 'border-right:42px solid;' +
  391. 'border-bottom:42px solid;' +
  392. 'border-left:42px solid;'
  393. );
  394. } );
  395. it( 'should downcast borderStyle attribute removal', () => {
  396. model.change( writer => writer.removeAttribute( 'borderStyle', tableCell ) );
  397. assertTableCellStyle( editor,
  398. 'border-top:42px #f00;' +
  399. 'border-right:42px #f00;' +
  400. 'border-bottom:42px #f00;' +
  401. 'border-left:42px #f00;'
  402. );
  403. } );
  404. it( 'should downcast borderWidth attribute removal', () => {
  405. model.change( writer => writer.removeAttribute( 'borderWidth', tableCell ) );
  406. assertTableCellStyle( editor,
  407. 'border-top:solid #f00;' +
  408. 'border-right:solid #f00;' +
  409. 'border-bottom:solid #f00;' +
  410. 'border-left:solid #f00;'
  411. );
  412. } );
  413. it( 'should downcast borderColor, borderStyle and borderWidth attributes removal', () => {
  414. model.change( writer => {
  415. writer.removeAttribute( 'borderColor', tableCell );
  416. writer.removeAttribute( 'borderStyle', tableCell );
  417. writer.removeAttribute( 'borderWidth', tableCell );
  418. } );
  419. assertEqualMarkup(
  420. editor.getData(),
  421. '<figure class="table"><table><tbody><tr><td>foo</td></tr></tbody></table></figure>'
  422. );
  423. } );
  424. } );
  425. } );
  426. } );
  427. } );