tableproperties.js 15 KB

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