tablecellpropertiesediting.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 TableEditing from '../../src/tableediting';
  8. import TableCellPropertiesEditing from '../../src/tablecellproperties/tablecellpropertiesediting';
  9. import TableCellBorderColorCommand from '../../src/tablecellproperties/commands/tablecellbordercolorcommand';
  10. import TableCellBorderStyleCommand from '../../src/tablecellproperties/commands/tablecellborderstylecommand';
  11. import TableCellBorderWidthCommand from '../../src/tablecellproperties/commands/tablecellborderwidthcommand';
  12. import TableCellHorizontalAlignmentCommand from '../../src/tablecellproperties/commands/tablecellhorizontalalignmentcommand';
  13. import TableCellWidthCommand from '../../src/tablecellproperties/commands/tablecellwidthcommand';
  14. import TableCellHeightCommand from '../../src/tablecellproperties/commands/tablecellheightcommand';
  15. import TableCellVerticalAlignmentCommand from '../../src/tablecellproperties/commands/tablecellverticalalignmentcommand';
  16. import TableCellPaddingCommand from '../../src/tablecellproperties/commands/tablecellpaddingcommand';
  17. import TableCellBackgroundColorCommand from '../../src/tablecellproperties/commands/tablecellbackgroundcolorcommand';
  18. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  19. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  20. import { assertTableCellStyle, assertTRBLAttribute } from '../_utils/utils';
  21. describe( 'table cell properties', () => {
  22. describe( 'TableCellPropertiesEditing', () => {
  23. let editor, model;
  24. beforeEach( async () => {
  25. editor = await VirtualTestEditor.create( {
  26. plugins: [ TableCellPropertiesEditing, Paragraph, TableEditing ]
  27. } );
  28. model = editor.model;
  29. } );
  30. afterEach( async () => {
  31. await editor.destroy();
  32. } );
  33. it( 'should have pluginName', () => {
  34. expect( TableCellPropertiesEditing.pluginName ).to.equal( 'TableCellPropertiesEditing' );
  35. } );
  36. it( 'adds tableCellBorderColor command', () => {
  37. expect( editor.commands.get( 'tableCellBorderColor' ) ).to.be.instanceOf( TableCellBorderColorCommand );
  38. } );
  39. it( 'adds tableCellBorderStyle command', () => {
  40. expect( editor.commands.get( 'tableCellBorderStyle' ) ).to.be.instanceOf( TableCellBorderStyleCommand );
  41. } );
  42. it( 'adds tableCellBorderWidth command', () => {
  43. expect( editor.commands.get( 'tableCellBorderWidth' ) ).to.be.instanceOf( TableCellBorderWidthCommand );
  44. } );
  45. it( 'adds tableCellAlignment command', () => {
  46. expect( editor.commands.get( 'tableCellHorizontalAlignment' ) ).to.be.instanceOf( TableCellHorizontalAlignmentCommand );
  47. } );
  48. it( 'adds tableCellAlignment command', () => {
  49. expect( editor.commands.get( 'tableCellVerticalAlignment' ) ).to.be.instanceOf( TableCellVerticalAlignmentCommand );
  50. } );
  51. it( 'adds tableCellPadding command', () => {
  52. expect( editor.commands.get( 'tableCellPadding' ) ).to.be.instanceOf( TableCellPaddingCommand );
  53. } );
  54. it( 'adds tableCellBackgroundColor command', () => {
  55. expect( editor.commands.get( 'tableCellBackgroundColor' ) ).to.be.instanceOf( TableCellBackgroundColorCommand );
  56. } );
  57. it( 'adds tableCellWidth command', () => {
  58. expect( editor.commands.get( 'tableCellWidth' ) ).to.be.instanceOf( TableCellWidthCommand );
  59. } );
  60. it( 'adds tableCellHeight command', () => {
  61. expect( editor.commands.get( 'tableCellHeight' ) ).to.be.instanceOf( TableCellHeightCommand );
  62. } );
  63. describe( 'border', () => {
  64. it( 'should set proper schema rules', () => {
  65. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'borderColor' ) ).to.be.true;
  66. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'borderStyle' ) ).to.be.true;
  67. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'borderWidth' ) ).to.be.true;
  68. } );
  69. describe( 'upcast conversion', () => {
  70. it( 'should upcast border shorthand', () => {
  71. editor.setData( '<table><tr><td style="border:1px solid #f00">foo</td></tr></table>' );
  72. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  73. assertTRBLAttribute( tableCell, 'borderColor', '#f00' );
  74. assertTRBLAttribute( tableCell, 'borderStyle', 'solid' );
  75. assertTRBLAttribute( tableCell, 'borderWidth', '1px' );
  76. } );
  77. it( 'should upcast border-color shorthand', () => {
  78. editor.setData( '<table><tr><td style="border-color:#f00">foo</td></tr></table>' );
  79. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  80. assertTRBLAttribute( tableCell, 'borderColor', '#f00' );
  81. } );
  82. it( 'should upcast border-style shorthand', () => {
  83. editor.setData( '<table><tr><td style="border-style:ridge">foo</td></tr></table>' );
  84. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  85. assertTRBLAttribute( tableCell, 'borderStyle', 'ridge' );
  86. } );
  87. it( 'should upcast border-width shorthand', () => {
  88. editor.setData( '<table><tr><td style="border-width:1px">foo</td></tr></table>' );
  89. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  90. assertTRBLAttribute( tableCell, 'borderWidth', '1px' );
  91. } );
  92. it( 'should upcast border-top shorthand', () => {
  93. editor.setData( '<table><tr><td style="border-top:1px solid #f00">foo</td></tr></table>' );
  94. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  95. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, null, null );
  96. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, null, null );
  97. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, null, null );
  98. } );
  99. it( 'should upcast border-right shorthand', () => {
  100. editor.setData( '<table><tr><td style="border-right:1px solid #f00">foo</td></tr></table>' );
  101. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  102. assertTRBLAttribute( tableCell, 'borderColor', null, '#f00', null, null );
  103. assertTRBLAttribute( tableCell, 'borderStyle', null, 'solid', null, null );
  104. assertTRBLAttribute( tableCell, 'borderWidth', null, '1px', null, null );
  105. } );
  106. it( 'should upcast border-bottom shorthand', () => {
  107. editor.setData( '<table><tr><td style="border-bottom:1px solid #f00">foo</td></tr></table>' );
  108. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  109. assertTRBLAttribute( tableCell, 'borderColor', null, null, '#f00', null );
  110. assertTRBLAttribute( tableCell, 'borderStyle', null, null, 'solid', null );
  111. assertTRBLAttribute( tableCell, 'borderWidth', null, null, '1px', null );
  112. } );
  113. it( 'should upcast border-left shorthand', () => {
  114. editor.setData( '<table><tr><td style="border-left:1px solid #f00">foo</td></tr></table>' );
  115. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  116. assertTRBLAttribute( tableCell, 'borderColor', null, null, null, '#f00' );
  117. assertTRBLAttribute( tableCell, 'borderStyle', null, null, null, 'solid' );
  118. assertTRBLAttribute( tableCell, 'borderWidth', null, null, null, '1px' );
  119. } );
  120. it( 'should upcast mixed shorthands', () => {
  121. editor.setData(
  122. '<table><tr><td style="border-top:1px solid #f00;border-bottom:2em ridge rgba(255,0,0,1)">foo</td></tr></table>'
  123. );
  124. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  125. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, 'rgba(255, 0, 0, 1)', null );
  126. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, 'ridge', null );
  127. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, '2em', null );
  128. } );
  129. it( 'should upcast border-top-* styles', () => {
  130. editor.setData(
  131. '<table><tr><td style="border-top-width:1px;border-top-style:solid;border-top-color:#f00">foo</td></tr></table>'
  132. );
  133. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  134. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, null, null );
  135. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, null, null );
  136. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, null, null );
  137. } );
  138. it( 'should upcast border-right-* styles', () => {
  139. editor.setData(
  140. '<table>' +
  141. '<tr>' +
  142. '<td style="border-right-width:1px;border-right-style:solid;border-right-color:#f00">foo</td>' +
  143. '</tr>' +
  144. '</table>'
  145. );
  146. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  147. assertTRBLAttribute( tableCell, 'borderColor', null, '#f00', null, null );
  148. assertTRBLAttribute( tableCell, 'borderStyle', null, 'solid', null, null );
  149. assertTRBLAttribute( tableCell, 'borderWidth', null, '1px', null, null );
  150. } );
  151. it( 'should upcast border-bottom-* styles', () => {
  152. editor.setData(
  153. '<table>' +
  154. '<tr>' +
  155. '<td style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:#f00">foo</td>' +
  156. '</tr>' +
  157. '</table>'
  158. );
  159. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  160. assertTRBLAttribute( tableCell, 'borderColor', null, null, '#f00', null );
  161. assertTRBLAttribute( tableCell, 'borderStyle', null, null, 'solid', null );
  162. assertTRBLAttribute( tableCell, 'borderWidth', null, null, '1px', null );
  163. } );
  164. it( 'should upcast border-left-* styles', () => {
  165. editor.setData(
  166. '<table>' +
  167. '<tr>' +
  168. '<td style="border-left-width:1px;border-left-style:solid;border-left-color:#f00">foo</td>' +
  169. '</tr>' +
  170. '</table>'
  171. );
  172. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  173. assertTRBLAttribute( tableCell, 'borderColor', null, null, null, '#f00' );
  174. assertTRBLAttribute( tableCell, 'borderStyle', null, null, null, 'solid' );
  175. assertTRBLAttribute( tableCell, 'borderWidth', null, null, null, '1px' );
  176. } );
  177. it( 'should allow to be overriden (only border-top consumed)', () => {
  178. editor.conversion.for( 'upcast' ).add( dispatcher => dispatcher.on( 'element:td', ( evt, data, conversionApi ) => {
  179. conversionApi.consumable.consume( data.viewItem, {
  180. styles: [ 'border-top' ]
  181. } );
  182. }, { priority: 'high' } ) );
  183. editor.setData( '<table><tr><td style="border:1px solid blue;">foo</td></tr></table>' );
  184. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  185. expect( tableCell.getAttribute( 'borderColor' ) ).to.be.undefined;
  186. expect( tableCell.getAttribute( 'borderStyle' ) ).to.be.undefined;
  187. expect( tableCell.getAttribute( 'borderWidth' ) ).to.be.undefined;
  188. } );
  189. } );
  190. describe( 'downcast conversion', () => {
  191. let tableCell;
  192. beforeEach( () => {
  193. setModelData(
  194. model,
  195. '<table headingRows="0" headingColumns="0">' +
  196. '<tableRow>' +
  197. '<tableCell>' +
  198. '<paragraph>foo</paragraph>' +
  199. '</tableCell>' +
  200. '</tableRow>' +
  201. '</table>'
  202. );
  203. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  204. } );
  205. it( 'should downcast borderColor attribute (same top, right, bottom, left)', () => {
  206. model.change( writer => writer.setAttribute( 'borderColor', {
  207. top: '#f00',
  208. right: '#f00',
  209. bottom: '#f00',
  210. left: '#f00'
  211. }, tableCell ) );
  212. assertTableCellStyle( editor, 'border-bottom:#f00;border-left:#f00;border-right:#f00;border-top:#f00;' );
  213. } );
  214. it( 'should downcast borderColor attribute (different top, right, bottom, left)', () => {
  215. model.change( writer => writer.setAttribute( 'borderColor', {
  216. top: '#f00',
  217. right: 'hsla(0, 100%, 50%, 0.5)',
  218. bottom: 'deeppink',
  219. left: 'rgb(255, 0, 0)'
  220. }, tableCell ) );
  221. assertTableCellStyle( editor,
  222. 'border-bottom:deeppink;' +
  223. 'border-left:rgb(255, 0, 0);' +
  224. 'border-right:hsla(0, 100%, 50%, 0.5);' +
  225. 'border-top:#f00;'
  226. );
  227. } );
  228. it( 'should downcast borderStyle attribute (same top, right, bottom, left)', () => {
  229. model.change( writer => writer.setAttribute( 'borderStyle', {
  230. top: 'solid',
  231. right: 'solid',
  232. bottom: 'solid',
  233. left: 'solid'
  234. }, tableCell ) );
  235. assertTableCellStyle( editor, 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' );
  236. } );
  237. it( 'should downcast borderStyle attribute (different top, right, bottom, left)', () => {
  238. model.change( writer => writer.setAttribute( 'borderStyle', {
  239. top: 'solid',
  240. right: 'ridge',
  241. bottom: 'dotted',
  242. left: 'dashed'
  243. }, tableCell ) );
  244. assertTableCellStyle( editor, 'border-bottom:dotted;border-left:dashed;border-right:ridge;border-top:solid;' );
  245. } );
  246. it( 'should downcast borderWidth attribute (same top, right, bottom, left)', () => {
  247. model.change( writer => writer.setAttribute( 'borderWidth', {
  248. top: '42px',
  249. right: '.1em',
  250. bottom: '1337rem',
  251. left: 'thick'
  252. }, tableCell ) );
  253. assertTableCellStyle( editor, 'border-bottom:1337rem;border-left:thick;border-right:.1em;border-top:42px;' );
  254. } );
  255. it( 'should downcast borderWidth attribute (different top, right, bottom, left)', () => {
  256. model.change( writer => writer.setAttribute( 'borderWidth', {
  257. top: '42px',
  258. right: '42px',
  259. bottom: '42px',
  260. left: '42px'
  261. }, tableCell ) );
  262. assertTableCellStyle( editor, 'border-bottom:42px;border-left:42px;border-right:42px;border-top:42px;' );
  263. } );
  264. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (same top, right, bottom, left)', () => {
  265. model.change( writer => {
  266. writer.setAttribute( 'borderColor', {
  267. top: '#f00',
  268. right: '#f00',
  269. bottom: '#f00',
  270. left: '#f00'
  271. }, tableCell );
  272. writer.setAttribute( 'borderStyle', {
  273. top: 'solid',
  274. right: 'solid',
  275. bottom: 'solid',
  276. left: 'solid'
  277. }, tableCell );
  278. writer.setAttribute( 'borderWidth', {
  279. top: '42px',
  280. right: '42px',
  281. bottom: '42px',
  282. left: '42px'
  283. }, tableCell );
  284. } );
  285. assertTableCellStyle( editor,
  286. 'border-bottom:42px solid #f00;' +
  287. 'border-left:42px solid #f00;' +
  288. 'border-right:42px solid #f00;' +
  289. 'border-top:42px solid #f00;'
  290. );
  291. } );
  292. it(
  293. 'should downcast borderColor, borderStyle and borderWidth attributes together (different top, right, bottom, left)',
  294. () => {
  295. model.change( writer => {
  296. writer.setAttribute( 'borderColor', {
  297. top: '#f00',
  298. right: 'hsla(0, 100%, 50%, 0.5)',
  299. bottom: 'deeppink',
  300. left: 'rgb(255, 0, 0)'
  301. }, tableCell );
  302. writer.setAttribute( 'borderStyle', {
  303. top: 'solid',
  304. right: 'ridge',
  305. bottom: 'dotted',
  306. left: 'dashed'
  307. }, tableCell );
  308. writer.setAttribute( 'borderWidth', {
  309. top: '42px',
  310. right: '.1em',
  311. bottom: '1337rem',
  312. left: 'thick'
  313. }, tableCell );
  314. } );
  315. assertTableCellStyle( editor,
  316. 'border-bottom:1337rem dotted deeppink;' +
  317. 'border-left:thick dashed rgb(255, 0, 0);' +
  318. 'border-right:.1em ridge hsla(0, 100%, 50%, 0.5);' +
  319. 'border-top:42px solid #f00;'
  320. );
  321. }
  322. );
  323. describe( 'change attribute', () => {
  324. beforeEach( () => {
  325. model.change( writer => {
  326. writer.setAttribute( 'borderColor', {
  327. top: '#f00',
  328. right: '#f00',
  329. bottom: '#f00',
  330. left: '#f00'
  331. }, tableCell );
  332. writer.setAttribute( 'borderStyle', {
  333. top: 'solid',
  334. right: 'solid',
  335. bottom: 'solid',
  336. left: 'solid'
  337. }, tableCell );
  338. writer.setAttribute( 'borderWidth', {
  339. top: '42px',
  340. right: '42px',
  341. bottom: '42px',
  342. left: '42px'
  343. }, tableCell );
  344. } );
  345. } );
  346. it( 'should downcast borderColor attribute change', () => {
  347. model.change( writer => writer.setAttribute( 'borderColor', {
  348. top: 'deeppink',
  349. right: 'deeppink',
  350. bottom: 'deeppink',
  351. left: 'deeppink'
  352. }, tableCell ) );
  353. assertTableCellStyle( editor,
  354. 'border-bottom:42px solid deeppink;' +
  355. 'border-left:42px solid deeppink;' +
  356. 'border-right:42px solid deeppink;' +
  357. 'border-top:42px solid deeppink;'
  358. );
  359. } );
  360. it( 'should downcast borderStyle attribute change', () => {
  361. model.change( writer => writer.setAttribute( 'borderStyle', {
  362. top: 'ridge',
  363. right: 'ridge',
  364. bottom: 'ridge',
  365. left: 'ridge'
  366. }, tableCell ) );
  367. assertTableCellStyle( editor,
  368. 'border-bottom:42px ridge #f00;' +
  369. 'border-left:42px ridge #f00;' +
  370. 'border-right:42px ridge #f00;' +
  371. 'border-top:42px ridge #f00;'
  372. );
  373. } );
  374. it( 'should downcast borderWidth attribute change', () => {
  375. model.change( writer => writer.setAttribute( 'borderWidth', {
  376. top: 'thick',
  377. right: 'thick',
  378. bottom: 'thick',
  379. left: 'thick'
  380. }, tableCell ) );
  381. assertTableCellStyle( editor,
  382. 'border-bottom:thick solid #f00;' +
  383. 'border-left:thick solid #f00;' +
  384. 'border-right:thick solid #f00;' +
  385. 'border-top:thick solid #f00;'
  386. );
  387. } );
  388. it( 'should downcast borderColor attribute removal', () => {
  389. model.change( writer => writer.removeAttribute( 'borderColor', tableCell ) );
  390. assertTableCellStyle( editor,
  391. 'border-bottom:42px solid;' +
  392. 'border-left:42px solid;' +
  393. 'border-right:42px solid;' +
  394. 'border-top:42px solid;'
  395. );
  396. } );
  397. it( 'should downcast borderStyle attribute removal', () => {
  398. model.change( writer => writer.removeAttribute( 'borderStyle', tableCell ) );
  399. assertTableCellStyle( editor,
  400. 'border-bottom:42px #f00;' +
  401. 'border-left:42px #f00;' +
  402. 'border-right:42px #f00;' +
  403. 'border-top:42px #f00;'
  404. );
  405. } );
  406. it( 'should downcast borderWidth attribute removal', () => {
  407. model.change( writer => writer.removeAttribute( 'borderWidth', tableCell ) );
  408. assertTableCellStyle( editor,
  409. 'border-bottom:solid #f00;' +
  410. 'border-left:solid #f00;' +
  411. 'border-right:solid #f00;' +
  412. 'border-top:solid #f00;'
  413. );
  414. } );
  415. it( 'should downcast borderColor, borderStyle and borderWidth attributes removal', () => {
  416. model.change( writer => {
  417. writer.removeAttribute( 'borderColor', tableCell );
  418. writer.removeAttribute( 'borderStyle', tableCell );
  419. writer.removeAttribute( 'borderWidth', tableCell );
  420. } );
  421. assertEqualMarkup(
  422. editor.getData(),
  423. '<figure class="table"><table><tbody><tr><td>foo</td></tr></tbody></table></figure>'
  424. );
  425. } );
  426. } );
  427. } );
  428. } );
  429. describe( 'background color', () => {
  430. it( 'should set proper schema rules', () => {
  431. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'backgroundColor' ) ).to.be.true;
  432. } );
  433. describe( 'upcast conversion', () => {
  434. it( 'should upcast background-color', () => {
  435. editor.setData( '<table><tr><td style="background-color:#f00">foo</td></tr></table>' );
  436. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  437. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#f00' );
  438. } );
  439. } );
  440. describe( 'downcast conversion', () => {
  441. let tableCell;
  442. beforeEach( () => {
  443. setModelData(
  444. model,
  445. '<table headingRows="0" headingColumns="0">' +
  446. '<tableRow>' +
  447. '<tableCell>' +
  448. '<paragraph>foo</paragraph>' +
  449. '</tableCell>' +
  450. '</tableRow>' +
  451. '</table>'
  452. );
  453. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  454. } );
  455. it( 'should downcast backgroundColor', () => {
  456. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
  457. assertTableCellStyle( editor, 'background-color:#f00;' );
  458. } );
  459. } );
  460. } );
  461. describe( 'horizontal alignment', () => {
  462. it( 'should set proper schema rules', () => {
  463. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'horizontalAlignment' ) ).to.be.true;
  464. } );
  465. describe( 'upcast conversion', () => {
  466. it( 'should upcast text-align:left style', () => {
  467. editor.setData( '<table><tr><td style="text-align:left">foo</td></tr></table>' );
  468. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  469. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.equal( 'left' );
  470. } );
  471. it( 'should upcast text-align:right style', () => {
  472. editor.setData( '<table><tr><td style="text-align:right">foo</td></tr></table>' );
  473. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  474. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.equal( 'right' );
  475. } );
  476. it( 'should upcast text-align:center style', () => {
  477. editor.setData( '<table><tr><td style="text-align:center">foo</td></tr></table>' );
  478. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  479. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.equal( 'center' );
  480. } );
  481. it( 'should upcast text-align:justify style', () => {
  482. editor.setData( '<table><tr><td style="text-align:justify">foo</td></tr></table>' );
  483. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  484. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.equal( 'justify' );
  485. } );
  486. } );
  487. describe( 'downcast conversion', () => {
  488. let tableCell;
  489. beforeEach( () => {
  490. setModelData(
  491. model,
  492. '<table headingRows="0" headingColumns="0">' +
  493. '<tableRow>' +
  494. '<tableCell>' +
  495. '<paragraph>foo</paragraph>' +
  496. '</tableCell>' +
  497. '</tableRow>' +
  498. '</table>'
  499. );
  500. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  501. } );
  502. it( 'should downcast horizontalAlignment=left', () => {
  503. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'left', tableCell ) );
  504. assertTableCellStyle( editor, 'text-align:left;' );
  505. } );
  506. it( 'should downcast horizontalAlignment=right', () => {
  507. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'right', tableCell ) );
  508. assertTableCellStyle( editor, 'text-align:right;' );
  509. } );
  510. it( 'should downcast horizontalAlignment=center', () => {
  511. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'center', tableCell ) );
  512. assertTableCellStyle( editor, 'text-align:center;' );
  513. } );
  514. it( 'should downcast horizontalAlignment=justify', () => {
  515. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'justify', tableCell ) );
  516. assertTableCellStyle( editor, 'text-align:justify;' );
  517. } );
  518. } );
  519. } );
  520. describe( 'vertical alignment', () => {
  521. it( 'should set proper schema rules', () => {
  522. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'verticalAlignment' ) ).to.be.true;
  523. } );
  524. describe( 'upcast conversion', () => {
  525. it( 'should upcast vertical-align', () => {
  526. editor.setData( '<table><tr><td style="vertical-align:top">foo</td></tr></table>' );
  527. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  528. expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.equal( 'top' );
  529. } );
  530. } );
  531. describe( 'downcast conversion', () => {
  532. let tableCell;
  533. beforeEach( () => {
  534. setModelData(
  535. model,
  536. '<table headingRows="0" headingColumns="0">' +
  537. '<tableRow>' +
  538. '<tableCell>' +
  539. '<paragraph>foo</paragraph>' +
  540. '</tableCell>' +
  541. '</tableRow>' +
  542. '</table>'
  543. );
  544. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  545. } );
  546. it( 'should downcast verticalAlignment', () => {
  547. model.change( writer => writer.setAttribute( 'verticalAlignment', 'middle', tableCell ) );
  548. assertTableCellStyle( editor, 'vertical-align:middle;' );
  549. } );
  550. } );
  551. } );
  552. describe( 'padding', () => {
  553. it( 'should set proper schema rules', () => {
  554. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'padding' ) ).to.be.true;
  555. } );
  556. describe( 'upcast conversion', () => {
  557. it( 'should upcast padding shorthand', () => {
  558. editor.setData( '<table><tr><td style="padding:2px 4em">foo</td></tr></table>' );
  559. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  560. assertTRBLAttribute( tableCell, 'padding', '2px', '4em' );
  561. } );
  562. } );
  563. describe( 'downcast conversion', () => {
  564. let tableCell;
  565. beforeEach( () => {
  566. setModelData(
  567. model,
  568. '<table headingRows="0" headingColumns="0">' +
  569. '<tableRow>' +
  570. '<tableCell>' +
  571. '<paragraph>foo</paragraph>' +
  572. '</tableCell>' +
  573. '</tableRow>' +
  574. '</table>'
  575. );
  576. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  577. } );
  578. it( 'should downcast padding (same top, right, bottom, left)', () => {
  579. model.change( writer => writer.setAttribute( 'padding', {
  580. top: '2px',
  581. right: '2px',
  582. bottom: '2px',
  583. left: '2px'
  584. }, tableCell ) );
  585. assertTableCellStyle( editor, 'padding:2px;' );
  586. } );
  587. it( 'should downcast padding (different top, right, bottom, left)', () => {
  588. model.change( writer => writer.setAttribute( 'padding', {
  589. top: '2px',
  590. right: '3px',
  591. bottom: '4px',
  592. left: '5px'
  593. }, tableCell ) );
  594. assertTableCellStyle( editor, 'padding:2px 3px 4px 5px;' );
  595. } );
  596. } );
  597. } );
  598. describe( 'cell width', () => {
  599. it( 'should set proper schema rules', () => {
  600. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'width' ) ).to.be.true;
  601. } );
  602. describe( 'upcast conversion', () => {
  603. it( 'should upcast width attribute on table cell', () => {
  604. editor.setData( '<table><tr><td style="width:20px">foo</td></tr></table>' );
  605. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  606. expect( tableCell.getAttribute( 'width' ) ).to.equal( '20px' );
  607. } );
  608. } );
  609. describe( 'downcast conversion', () => {
  610. let tableCell;
  611. beforeEach( () => {
  612. setModelData(
  613. model,
  614. '<table headingRows="0" headingColumns="0">' +
  615. '<tableRow>' +
  616. '<tableCell>' +
  617. '<paragraph>foo</paragraph>' +
  618. '</tableCell>' +
  619. '</tableRow>' +
  620. '</table>'
  621. );
  622. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  623. } );
  624. it( 'should downcast width attribute', () => {
  625. model.change( writer => writer.setAttribute( 'width', '20px', tableCell ) );
  626. assertEqualMarkup(
  627. editor.getData(),
  628. '<figure class="table"><table><tbody><tr><td style="width:20px;">foo</td></tr></tbody></table></figure>'
  629. );
  630. } );
  631. } );
  632. } );
  633. describe( 'cell height', () => {
  634. it( 'should set proper schema rules', () => {
  635. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'height' ) ).to.be.true;
  636. } );
  637. describe( 'upcast conversion', () => {
  638. it( 'should upcast height attribute on table cell', () => {
  639. editor.setData( '<table><tr><td style="height:20px">foo</td></tr></table>' );
  640. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  641. expect( tableCell.getAttribute( 'height' ) ).to.equal( '20px' );
  642. } );
  643. } );
  644. describe( 'downcast conversion', () => {
  645. let tableCell;
  646. beforeEach( () => {
  647. setModelData(
  648. model,
  649. '<table headingRows="0" headingColumns="0">' +
  650. '<tableRow>' +
  651. '<tableCell>' +
  652. '<paragraph>foo</paragraph>' +
  653. '</tableCell>' +
  654. '</tableRow>' +
  655. '</table>'
  656. );
  657. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  658. } );
  659. it( 'should downcast height attribute', () => {
  660. model.change( writer => writer.setAttribute( 'height', '20px', tableCell ) );
  661. assertEqualMarkup(
  662. editor.getData(),
  663. '<figure class="table"><table><tbody><tr><td style="height:20px;">foo</td></tr></tbody></table></figure>'
  664. );
  665. } );
  666. } );
  667. } );
  668. } );
  669. } );