tableproperties.js 15 KB

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