tablestyles.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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 TableStyleEditing from '../../src/tablestyleediting';
  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( 'Table styles conversion', () => {
  13. let editor, model;
  14. beforeEach( () => {
  15. return VirtualTestEditor
  16. .create( {
  17. plugins: [ TableEditing, TableStyleEditing, 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. describe( 'table cell', () => {
  28. it( 'should upcast border shorthand', () => {
  29. editor.setData( '<table><tr><td style="border:1px solid #f00">foo</td></tr></table>' );
  30. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  31. assertTRBLAttribute( tableCell, 'borderColor', '#f00' );
  32. assertTRBLAttribute( tableCell, 'borderStyle', 'solid' );
  33. assertTRBLAttribute( tableCell, 'borderWidth', '1px' );
  34. } );
  35. it( 'should upcast border-color shorthand', () => {
  36. editor.setData( '<table><tr><td style="border-color:#f00">foo</td></tr></table>' );
  37. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  38. assertTRBLAttribute( tableCell, 'borderColor', '#f00' );
  39. } );
  40. it( 'should upcast border-style shorthand', () => {
  41. editor.setData( '<table><tr><td style="border-style:ridge">foo</td></tr></table>' );
  42. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  43. assertTRBLAttribute( tableCell, 'borderStyle', 'ridge' );
  44. } );
  45. it( 'should upcast border-width shorthand', () => {
  46. editor.setData( '<table><tr><td style="border-width:1px">foo</td></tr></table>' );
  47. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  48. assertTRBLAttribute( tableCell, 'borderWidth', '1px' );
  49. } );
  50. it( 'should upcast border-top shorthand', () => {
  51. editor.setData( '<table><tr><td style="border-top:1px solid #f00">foo</td></tr></table>' );
  52. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  53. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, null, null );
  54. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, null, null );
  55. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, null, null );
  56. } );
  57. it( 'should upcast border-right shorthand', () => {
  58. editor.setData( '<table><tr><td style="border-right:1px solid #f00">foo</td></tr></table>' );
  59. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  60. assertTRBLAttribute( tableCell, 'borderColor', null, '#f00', null, null );
  61. assertTRBLAttribute( tableCell, 'borderStyle', null, 'solid', null, null );
  62. assertTRBLAttribute( tableCell, 'borderWidth', null, '1px', null, null );
  63. } );
  64. it( 'should upcast border-bottom shorthand', () => {
  65. editor.setData( '<table><tr><td style="border-bottom:1px solid #f00">foo</td></tr></table>' );
  66. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  67. assertTRBLAttribute( tableCell, 'borderColor', null, null, '#f00', null );
  68. assertTRBLAttribute( tableCell, 'borderStyle', null, null, 'solid', null );
  69. assertTRBLAttribute( tableCell, 'borderWidth', null, null, '1px', null );
  70. } );
  71. it( 'should upcast border-left shorthand', () => {
  72. editor.setData( '<table><tr><td style="border-left:1px solid #f00">foo</td></tr></table>' );
  73. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  74. assertTRBLAttribute( tableCell, 'borderColor', null, null, null, '#f00' );
  75. assertTRBLAttribute( tableCell, 'borderStyle', null, null, null, 'solid' );
  76. assertTRBLAttribute( tableCell, 'borderWidth', null, null, null, '1px' );
  77. } );
  78. it( 'should upcast border-top-* styles', () => {
  79. editor.setData(
  80. '<table><tr><td style="border-top-width:1px;border-top-style:solid;border-top-color:#f00">foo</td></tr></table>'
  81. );
  82. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  83. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, null, null );
  84. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, null, null );
  85. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, null, null );
  86. } );
  87. it( 'should upcast border-right-* styles', () => {
  88. editor.setData(
  89. '<table><tr><td style="border-right-width:1px;border-right-style:solid;border-right-color:#f00">foo</td></tr></table>'
  90. );
  91. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  92. assertTRBLAttribute( tableCell, 'borderColor', null, '#f00', null, null );
  93. assertTRBLAttribute( tableCell, 'borderStyle', null, 'solid', null, null );
  94. assertTRBLAttribute( tableCell, 'borderWidth', null, '1px', null, null );
  95. } );
  96. it( 'should upcast border-bottom-* styles', () => {
  97. editor.setData(
  98. '<table><tr>' +
  99. '<td style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:#f00">foo</td>' +
  100. '</tr></table>'
  101. );
  102. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  103. assertTRBLAttribute( tableCell, 'borderColor', null, null, '#f00', null );
  104. assertTRBLAttribute( tableCell, 'borderStyle', null, null, 'solid', null );
  105. assertTRBLAttribute( tableCell, 'borderWidth', null, null, '1px', null );
  106. } );
  107. it( 'should upcast border-left-* styles', () => {
  108. editor.setData(
  109. '<table><tr><td style="border-left-width:1px;border-left-style:solid;border-left-color:#f00">foo</td></tr></table>'
  110. );
  111. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  112. assertTRBLAttribute( tableCell, 'borderColor', null, null, null, '#f00' );
  113. assertTRBLAttribute( tableCell, 'borderStyle', null, null, null, 'solid' );
  114. assertTRBLAttribute( tableCell, 'borderWidth', null, null, null, '1px' );
  115. } );
  116. it( 'should upcast background-color', () => {
  117. editor.setData( '<table><tr><td style="background-color:#f00">foo</td></tr></table>' );
  118. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  119. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#f00' );
  120. } );
  121. it( 'should upcast padding shorthand', () => {
  122. editor.setData( '<table><tr><td style="padding:2px 4em">foo</td></tr></table>' );
  123. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  124. assertTRBLAttribute( tableCell, 'padding', '2px', '4em' );
  125. } );
  126. it( 'should upcast vertical-align', () => {
  127. editor.setData( '<table><tr><td style="vertical-align:top">foo</td></tr></table>' );
  128. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  129. expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.equal( 'top' );
  130. } );
  131. } );
  132. describe( 'table row', () => {
  133. it( 'should upcast height attribute', () => {
  134. editor.setData( '<table><tr style="height:20px"><td>foo</td></tr></table>' );
  135. const tableRow = model.document.getRoot().getNodeByPath( [ 0, 0 ] );
  136. expect( tableRow.getAttribute( 'height' ) ).to.equal( '20px' );
  137. } );
  138. } );
  139. describe( 'table', () => {
  140. it( 'should upcast border shorthand', () => {
  141. editor.setData( '<table style="border:1px solid #f00"><tr><td>foo</td></tr></table>' );
  142. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  143. assertTRBLAttribute( table, 'borderColor', '#f00' );
  144. assertTRBLAttribute( table, 'borderStyle', 'solid' );
  145. assertTRBLAttribute( table, 'borderWidth', '1px' );
  146. } );
  147. it( 'should upcast border-color shorthand', () => {
  148. editor.setData( '<table style="border-color:#f00"><tr><td>foo</td></tr></table>' );
  149. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  150. assertTRBLAttribute( table, 'borderColor', '#f00' );
  151. } );
  152. it( 'should upcast border-style shorthand', () => {
  153. editor.setData( '<table style="border-style:ridge"><tr><td>foo</td></tr></table>' );
  154. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  155. assertTRBLAttribute( table, 'borderStyle', 'ridge' );
  156. } );
  157. it( 'should upcast border-width shorthand', () => {
  158. editor.setData( '<table style="border-width:1px"><tr><td>foo</td></tr></table>' );
  159. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  160. assertTRBLAttribute( table, 'borderWidth', '1px' );
  161. } );
  162. it( 'should upcast border-top shorthand', () => {
  163. editor.setData( '<table style="border-top:1px solid #f00"><tr><td>foo</td></tr></table>' );
  164. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  165. assertTRBLAttribute( table, 'borderColor', '#f00', null, null, null );
  166. assertTRBLAttribute( table, 'borderStyle', 'solid', null, null, null );
  167. assertTRBLAttribute( table, 'borderWidth', '1px', null, null, null );
  168. } );
  169. it( 'should upcast border-right shorthand', () => {
  170. editor.setData( '<table style="border-right:1px solid #f00"><tr><td>foo</td></tr></table>' );
  171. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  172. assertTRBLAttribute( table, 'borderColor', null, '#f00', null, null );
  173. assertTRBLAttribute( table, 'borderStyle', null, 'solid', null, null );
  174. assertTRBLAttribute( table, 'borderWidth', null, '1px', null, null );
  175. } );
  176. it( 'should upcast border-bottom shorthand', () => {
  177. editor.setData( '<table style="border-bottom:1px solid #f00"><tr><td>foo</td></tr></table>' );
  178. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  179. assertTRBLAttribute( table, 'borderColor', null, null, '#f00', null );
  180. assertTRBLAttribute( table, 'borderStyle', null, null, 'solid', null );
  181. assertTRBLAttribute( table, 'borderWidth', null, null, '1px', null );
  182. } );
  183. it( 'should upcast border-left shorthand', () => {
  184. editor.setData( '<table style="border-left:1px solid #f00"><tr><td>foo</td></tr></table>' );
  185. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  186. assertTRBLAttribute( table, 'borderColor', null, null, null, '#f00' );
  187. assertTRBLAttribute( table, 'borderStyle', null, null, null, 'solid' );
  188. assertTRBLAttribute( table, 'borderWidth', null, null, null, '1px' );
  189. } );
  190. it( 'should upcast border-top-* styles', () => {
  191. editor.setData(
  192. '<table style="border-top-width:1px;border-top-style:solid;border-top-color:#f00"><tr><td>foo</td></tr></table>'
  193. );
  194. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  195. assertTRBLAttribute( table, 'borderColor', '#f00', null, null, null );
  196. assertTRBLAttribute( table, 'borderStyle', 'solid', null, null, null );
  197. assertTRBLAttribute( table, 'borderWidth', '1px', null, null, null );
  198. } );
  199. it( 'should upcast border-right-* styles', () => {
  200. editor.setData(
  201. '<table style="border-right-width:1px;border-right-style:solid;border-right-color:#f00"><tr><td>foo</td></tr></table>'
  202. );
  203. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  204. assertTRBLAttribute( table, 'borderColor', null, '#f00', null, null );
  205. assertTRBLAttribute( table, 'borderStyle', null, 'solid', null, null );
  206. assertTRBLAttribute( table, 'borderWidth', null, '1px', null, null );
  207. } );
  208. it( 'should upcast border-bottom-* styles', () => {
  209. editor.setData(
  210. '<table style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:#f00">' +
  211. '<tr>' +
  212. '<td>foo</td>' +
  213. '</tr>' +
  214. '</table>'
  215. );
  216. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  217. assertTRBLAttribute( table, 'borderColor', null, null, '#f00', null );
  218. assertTRBLAttribute( table, 'borderStyle', null, null, 'solid', null );
  219. assertTRBLAttribute( table, 'borderWidth', null, null, '1px', null );
  220. } );
  221. it( 'should upcast border-left-* styles', () => {
  222. editor.setData(
  223. '<table style="border-left-width:1px;border-left-style:solid;border-left-color:#f00"><tr><td>foo</td></tr></table>'
  224. );
  225. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  226. assertTRBLAttribute( table, 'borderColor', null, null, null, '#f00' );
  227. assertTRBLAttribute( table, 'borderStyle', null, null, null, 'solid' );
  228. assertTRBLAttribute( table, 'borderWidth', null, null, null, '1px' );
  229. } );
  230. it( 'should upcast background-color', () => {
  231. editor.setData( '<table style="background-color:#f00"><tr><td>foo</td></tr></table>' );
  232. const table = model.document.getRoot().getNodeByPath( [ 0 ] );
  233. expect( table.getAttribute( 'backgroundColor' ) ).to.equal( '#f00' );
  234. } );
  235. } );
  236. } );
  237. describe( 'downcast', () => {
  238. describe( 'table cell', () => {
  239. let tableCell;
  240. beforeEach( () => {
  241. setModelData(
  242. model,
  243. '<table headingRows="0" headingColumns="0">' +
  244. '<tableRow>' +
  245. '<tableCell>' +
  246. '<paragraph>foo</paragraph>' +
  247. '</tableCell>' +
  248. '</tableRow>' +
  249. '</table>'
  250. );
  251. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  252. } );
  253. it( 'should downcast borderColor attribute (same top, right, bottom, left)', () => {
  254. model.change( writer => writer.setAttribute( 'borderColor', {
  255. top: '#f00',
  256. right: '#f00',
  257. bottom: '#f00',
  258. left: '#f00'
  259. }, tableCell ) );
  260. assertTableCellStyle( 'border-top:#f00;border-right:#f00;border-bottom:#f00;border-left:#f00;' );
  261. } );
  262. it( 'should downcast borderColor attribute (different top, right, bottom, left)', () => {
  263. model.change( writer => writer.setAttribute( 'borderColor', {
  264. top: '#f00',
  265. right: 'hsla(0, 100%, 50%, 0.5)',
  266. bottom: 'deeppink',
  267. left: 'rgb(255, 0, 0)'
  268. }, tableCell ) );
  269. assertTableCellStyle(
  270. 'border-top:#f00;' +
  271. 'border-right:hsla(0, 100%, 50%, 0.5);' +
  272. 'border-bottom:deeppink;' +
  273. 'border-left:rgb(255, 0, 0);'
  274. );
  275. } );
  276. it( 'should downcast borderStyle attribute (same top, right, bottom, left)', () => {
  277. model.change( writer => writer.setAttribute( 'borderStyle', {
  278. top: 'solid',
  279. right: 'solid',
  280. bottom: 'solid',
  281. left: 'solid'
  282. }, tableCell ) );
  283. assertTableCellStyle( 'border-top:solid;border-right:solid;border-bottom:solid;border-left:solid;' );
  284. } );
  285. it( 'should downcast borderStyle attribute (different top, right, bottom, left)', () => {
  286. model.change( writer => writer.setAttribute( 'borderStyle', {
  287. top: 'solid',
  288. right: 'ridge',
  289. bottom: 'dotted',
  290. left: 'dashed'
  291. }, tableCell ) );
  292. assertTableCellStyle( 'border-top:solid;border-right:ridge;border-bottom:dotted;border-left:dashed;' );
  293. } );
  294. it( 'should downcast borderWidth attribute (same top, right, bottom, left)', () => {
  295. model.change( writer => writer.setAttribute( 'borderWidth', {
  296. top: '42px',
  297. right: '.1em',
  298. bottom: '1337rem',
  299. left: 'thick'
  300. }, tableCell ) );
  301. assertTableCellStyle( 'border-top:42px;border-right:.1em;border-bottom:1337rem;border-left:thick;' );
  302. } );
  303. it( 'should downcast borderWidth attribute (different top, right, bottom, left)', () => {
  304. model.change( writer => writer.setAttribute( 'borderWidth', {
  305. top: '42px',
  306. right: '42px',
  307. bottom: '42px',
  308. left: '42px'
  309. }, tableCell ) );
  310. assertTableCellStyle( 'border-top:42px;border-right:42px;border-bottom:42px;border-left:42px;' );
  311. } );
  312. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (same top, right, bottom, left)', () => {
  313. model.change( writer => {
  314. writer.setAttribute( 'borderColor', {
  315. top: '#f00',
  316. right: '#f00',
  317. bottom: '#f00',
  318. left: '#f00'
  319. }, tableCell );
  320. writer.setAttribute( 'borderStyle', {
  321. top: 'solid',
  322. right: 'solid',
  323. bottom: 'solid',
  324. left: 'solid'
  325. }, tableCell );
  326. writer.setAttribute( 'borderWidth', {
  327. top: '42px',
  328. right: '42px',
  329. bottom: '42px',
  330. left: '42px'
  331. }, tableCell );
  332. } );
  333. assertTableCellStyle(
  334. 'border-top:42px solid #f00;' +
  335. 'border-right:42px solid #f00;' +
  336. 'border-bottom:42px solid #f00;' +
  337. 'border-left:42px solid #f00;'
  338. );
  339. } );
  340. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (different top, right, bottom, left)', () => {
  341. model.change( writer => {
  342. writer.setAttribute( 'borderColor', {
  343. top: '#f00',
  344. right: 'hsla(0, 100%, 50%, 0.5)',
  345. bottom: 'deeppink',
  346. left: 'rgb(255, 0, 0)'
  347. }, tableCell );
  348. writer.setAttribute( 'borderStyle', {
  349. top: 'solid',
  350. right: 'ridge',
  351. bottom: 'dotted',
  352. left: 'dashed'
  353. }, tableCell );
  354. writer.setAttribute( 'borderWidth', {
  355. top: '42px',
  356. right: '.1em',
  357. bottom: '1337rem',
  358. left: 'thick'
  359. }, tableCell );
  360. } );
  361. assertTableCellStyle(
  362. 'border-top:42px solid #f00;' +
  363. 'border-right:.1em ridge hsla(0, 100%, 50%, 0.5);' +
  364. 'border-bottom:1337rem dotted deeppink;' +
  365. 'border-left:thick dashed rgb(255, 0, 0);'
  366. );
  367. } );
  368. it( 'should downcast backgroundColor', () => {
  369. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
  370. assertTableCellStyle( 'background-color:#f00;' );
  371. } );
  372. it( 'should downcast padding (same top, right, bottom, left)', () => {
  373. model.change( writer => writer.setAttribute( 'padding', {
  374. top: '2px',
  375. right: '2px',
  376. bottom: '2px',
  377. left: '2px'
  378. }, tableCell ) );
  379. assertTableCellStyle( 'padding:2px;' );
  380. } );
  381. it( 'should downcast padding (different top, right, bottom, left)', () => {
  382. model.change( writer => writer.setAttribute( 'padding', {
  383. top: '2px',
  384. right: '3px',
  385. bottom: '4px',
  386. left: '5px'
  387. }, tableCell ) );
  388. assertTableCellStyle( 'padding:2px 3px 4px 5px;' );
  389. } );
  390. it( 'should downcast verticalAlignment', () => {
  391. model.change( writer => writer.setAttribute( 'verticalAlignment', 'middle', tableCell ) );
  392. assertTableCellStyle( 'vertical-align:middle;' );
  393. } );
  394. describe( 'change attribute', () => {
  395. beforeEach( () => {
  396. model.change( writer => {
  397. writer.setAttribute( 'borderColor', {
  398. top: '#f00',
  399. right: '#f00',
  400. bottom: '#f00',
  401. left: '#f00'
  402. }, tableCell );
  403. writer.setAttribute( 'borderStyle', {
  404. top: 'solid',
  405. right: 'solid',
  406. bottom: 'solid',
  407. left: 'solid'
  408. }, tableCell );
  409. writer.setAttribute( 'borderWidth', {
  410. top: '42px',
  411. right: '42px',
  412. bottom: '42px',
  413. left: '42px'
  414. }, tableCell );
  415. } );
  416. } );
  417. it( 'should downcast borderColor attribute change', () => {
  418. model.change( writer => writer.setAttribute( 'borderColor', {
  419. top: 'deeppink',
  420. right: 'deeppink',
  421. bottom: 'deeppink',
  422. left: 'deeppink'
  423. }, tableCell ) );
  424. assertTableCellStyle(
  425. 'border-top:42px solid deeppink;' +
  426. 'border-right:42px solid deeppink;' +
  427. 'border-bottom:42px solid deeppink;' +
  428. 'border-left:42px solid deeppink;'
  429. );
  430. } );
  431. it( 'should downcast borderStyle attribute change', () => {
  432. model.change( writer => writer.setAttribute( 'borderStyle', {
  433. top: 'ridge',
  434. right: 'ridge',
  435. bottom: 'ridge',
  436. left: 'ridge'
  437. }, tableCell ) );
  438. assertTableCellStyle(
  439. 'border-top:42px ridge #f00;' +
  440. 'border-right:42px ridge #f00;' +
  441. 'border-bottom:42px ridge #f00;' +
  442. 'border-left:42px ridge #f00;'
  443. );
  444. } );
  445. it( 'should downcast borderWidth attribute change', () => {
  446. model.change( writer => writer.setAttribute( 'borderWidth', {
  447. top: 'thick',
  448. right: 'thick',
  449. bottom: 'thick',
  450. left: 'thick'
  451. }, tableCell ) );
  452. assertTableCellStyle(
  453. 'border-top:thick solid #f00;' +
  454. 'border-right:thick solid #f00;' +
  455. 'border-bottom:thick solid #f00;' +
  456. 'border-left:thick solid #f00;'
  457. );
  458. } );
  459. it( 'should downcast borderColor attribute removal', () => {
  460. model.change( writer => writer.removeAttribute( 'borderColor', tableCell ) );
  461. assertTableCellStyle(
  462. 'border-top:42px solid;' +
  463. 'border-right:42px solid;' +
  464. 'border-bottom:42px solid;' +
  465. 'border-left:42px solid;'
  466. );
  467. } );
  468. it( 'should downcast borderStyle attribute removal', () => {
  469. model.change( writer => writer.removeAttribute( 'borderStyle', tableCell ) );
  470. assertTableCellStyle(
  471. 'border-top:42px #f00;' +
  472. 'border-right:42px #f00;' +
  473. 'border-bottom:42px #f00;' +
  474. 'border-left:42px #f00;'
  475. );
  476. } );
  477. it( 'should downcast borderWidth attribute removal', () => {
  478. model.change( writer => writer.removeAttribute( 'borderWidth', tableCell ) );
  479. assertTableCellStyle(
  480. 'border-top:solid #f00;' +
  481. 'border-right:solid #f00;' +
  482. 'border-bottom:solid #f00;' +
  483. 'border-left:solid #f00;'
  484. );
  485. } );
  486. it( 'should downcast borderColor, borderStyle and borderWidth attributes removal', () => {
  487. model.change( writer => {
  488. writer.removeAttribute( 'borderColor', tableCell );
  489. writer.removeAttribute( 'borderStyle', tableCell );
  490. writer.removeAttribute( 'borderWidth', tableCell );
  491. } );
  492. assertEqualMarkup(
  493. editor.getData(),
  494. '<figure class="table"><table><tbody><tr><td>foo</td></tr></tbody></table></figure>'
  495. );
  496. } );
  497. } );
  498. } );
  499. describe( 'table row', () => {
  500. let tableRow;
  501. beforeEach( () => {
  502. setModelData(
  503. model,
  504. '<table headingRows="0" headingColumns="0">' +
  505. '<tableRow>' +
  506. '<tableCell>' +
  507. '<paragraph>foo</paragraph>' +
  508. '</tableCell>' +
  509. '</tableRow>' +
  510. '</table>'
  511. );
  512. tableRow = model.document.getRoot().getNodeByPath( [ 0, 0 ] );
  513. } );
  514. it( 'should downcast height attribute', () => {
  515. model.change( writer => writer.setAttribute( 'height', '20px', tableRow ) );
  516. assertEqualMarkup(
  517. editor.getData(),
  518. '<figure class="table"><table><tbody><tr style="height:20px;"><td>foo</td></tr></tbody></table></figure>'
  519. );
  520. } );
  521. } );
  522. describe( 'table', () => {
  523. let table;
  524. beforeEach( () => {
  525. setModelData(
  526. model,
  527. '<table headingRows="0" headingColumns="0">' +
  528. '<tableRow>' +
  529. '<tableCell>' +
  530. '<paragraph>foo</paragraph>' +
  531. '</tableCell>' +
  532. '</tableRow>' +
  533. '</table>'
  534. );
  535. table = model.document.getRoot().getNodeByPath( [ 0 ] );
  536. } );
  537. it( 'should downcast borderColor attribute (same top, right, bottom, left)', () => {
  538. model.change( writer => writer.setAttribute( 'borderColor', {
  539. top: '#f00',
  540. right: '#f00',
  541. bottom: '#f00',
  542. left: '#f00'
  543. }, table ) );
  544. assertTableStyle( 'border-top:#f00;border-right:#f00;border-bottom:#f00;border-left:#f00;' );
  545. } );
  546. it( 'should downcast borderColor attribute (different top, right, bottom, left)', () => {
  547. model.change( writer => writer.setAttribute( 'borderColor', {
  548. top: '#f00',
  549. right: 'hsla(0, 100%, 50%, 0.5)',
  550. bottom: 'deeppink',
  551. left: 'rgb(255, 0, 0)'
  552. }, table ) );
  553. assertTableStyle(
  554. 'border-top:#f00;' +
  555. 'border-right:hsla(0, 100%, 50%, 0.5);' +
  556. 'border-bottom:deeppink;' +
  557. 'border-left:rgb(255, 0, 0);'
  558. );
  559. } );
  560. it( 'should downcast borderStyle attribute (same top, right, bottom, left)', () => {
  561. model.change( writer => writer.setAttribute( 'borderStyle', {
  562. top: 'solid',
  563. right: 'solid',
  564. bottom: 'solid',
  565. left: 'solid'
  566. }, table ) );
  567. assertTableStyle( 'border-top:solid;border-right:solid;border-bottom:solid;border-left:solid;' );
  568. } );
  569. it( 'should downcast borderStyle attribute (different top, right, bottom, left)', () => {
  570. model.change( writer => writer.setAttribute( 'borderStyle', {
  571. top: 'solid',
  572. right: 'ridge',
  573. bottom: 'dotted',
  574. left: 'dashed'
  575. }, table ) );
  576. assertTableStyle( 'border-top:solid;border-right:ridge;border-bottom:dotted;border-left:dashed;' );
  577. } );
  578. it( 'should downcast borderWidth attribute (same top, right, bottom, left)', () => {
  579. model.change( writer => writer.setAttribute( 'borderWidth', {
  580. top: '42px',
  581. right: '.1em',
  582. bottom: '1337rem',
  583. left: 'thick'
  584. }, table ) );
  585. assertTableStyle( 'border-top:42px;border-right:.1em;border-bottom:1337rem;border-left:thick;' );
  586. } );
  587. it( 'should downcast borderWidth attribute (different top, right, bottom, left)', () => {
  588. model.change( writer => writer.setAttribute( 'borderWidth', {
  589. top: '42px',
  590. right: '42px',
  591. bottom: '42px',
  592. left: '42px'
  593. }, table ) );
  594. assertTableStyle( 'border-top:42px;border-right:42px;border-bottom:42px;border-left:42px;' );
  595. } );
  596. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (same top, right, bottom, left)', () => {
  597. model.change( writer => {
  598. writer.setAttribute( 'borderColor', {
  599. top: '#f00',
  600. right: '#f00',
  601. bottom: '#f00',
  602. left: '#f00'
  603. }, table );
  604. writer.setAttribute( 'borderStyle', {
  605. top: 'solid',
  606. right: 'solid',
  607. bottom: 'solid',
  608. left: 'solid'
  609. }, table );
  610. writer.setAttribute( 'borderWidth', {
  611. top: '42px',
  612. right: '42px',
  613. bottom: '42px',
  614. left: '42px'
  615. }, table );
  616. } );
  617. assertTableStyle(
  618. 'border-top:42px solid #f00;' +
  619. 'border-right:42px solid #f00;' +
  620. 'border-bottom:42px solid #f00;' +
  621. 'border-left:42px solid #f00;'
  622. );
  623. } );
  624. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (different top, right, bottom, left)', () => {
  625. model.change( writer => {
  626. writer.setAttribute( 'borderColor', {
  627. top: '#f00',
  628. right: 'hsla(0, 100%, 50%, 0.5)',
  629. bottom: 'deeppink',
  630. left: 'rgb(255, 0, 0)'
  631. }, table );
  632. writer.setAttribute( 'borderStyle', {
  633. top: 'solid',
  634. right: 'ridge',
  635. bottom: 'dotted',
  636. left: 'dashed'
  637. }, table );
  638. writer.setAttribute( 'borderWidth', {
  639. top: '42px',
  640. right: '.1em',
  641. bottom: '1337rem',
  642. left: 'thick'
  643. }, table );
  644. } );
  645. assertTableStyle(
  646. 'border-top:42px solid #f00;' +
  647. 'border-right:.1em ridge hsla(0, 100%, 50%, 0.5);' +
  648. 'border-bottom:1337rem dotted deeppink;' +
  649. 'border-left:thick dashed rgb(255, 0, 0);'
  650. );
  651. } );
  652. it( 'should downcast backgroundColor', () => {
  653. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', table ) );
  654. assertTableStyle( 'background-color:#f00;' );
  655. } );
  656. describe( 'change attribute', () => {
  657. beforeEach( () => {
  658. model.change( writer => {
  659. writer.setAttribute( 'borderColor', {
  660. top: '#f00',
  661. right: '#f00',
  662. bottom: '#f00',
  663. left: '#f00'
  664. }, table );
  665. writer.setAttribute( 'borderStyle', {
  666. top: 'solid',
  667. right: 'solid',
  668. bottom: 'solid',
  669. left: 'solid'
  670. }, table );
  671. writer.setAttribute( 'borderWidth', {
  672. top: '42px',
  673. right: '42px',
  674. bottom: '42px',
  675. left: '42px'
  676. }, table );
  677. } );
  678. } );
  679. it( 'should downcast borderColor attribute change', () => {
  680. model.change( writer => writer.setAttribute( 'borderColor', {
  681. top: 'deeppink',
  682. right: 'deeppink',
  683. bottom: 'deeppink',
  684. left: 'deeppink'
  685. }, table ) );
  686. assertTableStyle(
  687. 'border-top:42px solid deeppink;' +
  688. 'border-right:42px solid deeppink;' +
  689. 'border-bottom:42px solid deeppink;' +
  690. 'border-left:42px solid deeppink;'
  691. );
  692. } );
  693. it( 'should downcast borderStyle attribute change', () => {
  694. model.change( writer => writer.setAttribute( 'borderStyle', {
  695. top: 'ridge',
  696. right: 'ridge',
  697. bottom: 'ridge',
  698. left: 'ridge'
  699. }, table ) );
  700. assertTableStyle(
  701. 'border-top:42px ridge #f00;' +
  702. 'border-right:42px ridge #f00;' +
  703. 'border-bottom:42px ridge #f00;' +
  704. 'border-left:42px ridge #f00;'
  705. );
  706. } );
  707. it( 'should downcast borderWidth attribute change', () => {
  708. model.change( writer => writer.setAttribute( 'borderWidth', {
  709. top: 'thick',
  710. right: 'thick',
  711. bottom: 'thick',
  712. left: 'thick'
  713. }, table ) );
  714. assertTableStyle(
  715. 'border-top:thick solid #f00;' +
  716. 'border-right:thick solid #f00;' +
  717. 'border-bottom:thick solid #f00;' +
  718. 'border-left:thick solid #f00;'
  719. );
  720. } );
  721. it( 'should downcast borderColor attribute removal', () => {
  722. model.change( writer => writer.removeAttribute( 'borderColor', table ) );
  723. assertTableStyle(
  724. 'border-top:42px solid;' +
  725. 'border-right:42px solid;' +
  726. 'border-bottom:42px solid;' +
  727. 'border-left:42px solid;'
  728. );
  729. } );
  730. it( 'should downcast borderStyle attribute removal', () => {
  731. model.change( writer => writer.removeAttribute( 'borderStyle', table ) );
  732. assertTableStyle(
  733. 'border-top:42px #f00;' +
  734. 'border-right:42px #f00;' +
  735. 'border-bottom:42px #f00;' +
  736. 'border-left:42px #f00;'
  737. );
  738. } );
  739. it( 'should downcast borderWidth attribute removal', () => {
  740. model.change( writer => writer.removeAttribute( 'borderWidth', table ) );
  741. assertTableStyle(
  742. 'border-top:solid #f00;' +
  743. 'border-right:solid #f00;' +
  744. 'border-bottom:solid #f00;' +
  745. 'border-left:solid #f00;'
  746. );
  747. } );
  748. it( 'should downcast borderColor, borderStyle and borderWidth attributes removal', () => {
  749. model.change( writer => {
  750. writer.removeAttribute( 'borderColor', table );
  751. writer.removeAttribute( 'borderStyle', table );
  752. writer.removeAttribute( 'borderWidth', table );
  753. } );
  754. assertEqualMarkup(
  755. editor.getData(),
  756. '<figure class="table"><table><tbody><tr><td>foo</td></tr></tbody></table></figure>'
  757. );
  758. } );
  759. } );
  760. } );
  761. } );
  762. /**
  763. * Assertion helper for top-right-bottom-left attribute object.
  764. *
  765. * @param {module:engine/model/node~Node} tableCell
  766. * @param {String} key Attribute key
  767. * @param {String} top Top value. Pass null to omit value in attributes object.
  768. * @param {String} [right=top] Right value - defaults to top if not provided.
  769. * Pass null to omit value in attributes object.
  770. * @param {String} [bottom=top] Bottom value - defaults to top (right value must be defined).
  771. * Pass null to omit value in attributes object.
  772. * @param {String} [left=right] Left value - defaults to right (bottom and right values must be defined).
  773. * Pass null to omit value in attributes object.
  774. */
  775. function assertTRBLAttribute( tableCell, key, top, right = top, bottom = top, left = right ) {
  776. const styleObject = {};
  777. if ( top ) {
  778. styleObject.top = top;
  779. }
  780. if ( right ) {
  781. styleObject.right = right;
  782. }
  783. if ( bottom ) {
  784. styleObject.bottom = bottom;
  785. }
  786. if ( left ) {
  787. styleObject.left = left;
  788. }
  789. expect( tableCell.getAttribute( key ) ).to.deep.equal( styleObject );
  790. }
  791. /**
  792. * Assertion helper for testing <td> style attribute.
  793. *
  794. * @param {String} tableCellStyle A style to assert on td.
  795. */
  796. function assertTableCellStyle( tableCellStyle ) {
  797. assertEqualMarkup( editor.getData(),
  798. `<figure class="table"><table><tbody><tr><td style="${ tableCellStyle }">foo</td></tr></tbody></table></figure>`
  799. );
  800. }
  801. /**
  802. * Assertion helper for testing <table> style attribute.
  803. *
  804. * @param {String} tableStyle A style to assert on table.
  805. */
  806. function assertTableStyle( tableStyle ) {
  807. assertEqualMarkup( editor.getData(),
  808. `<figure class="table"><table style="${ tableStyle }"><tbody><tr><td>foo</td></tr></tbody></table></figure>`
  809. );
  810. }
  811. } );