tablecellpropertiesediting.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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 tableCellVerticalAlignment 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 consume converted item borderColor attribute', () => {
  206. editor.conversion.for( 'downcast' )
  207. .add( dispatcher => dispatcher.on( 'attribute:borderColor:tableCell', ( evt, data, conversionApi ) => {
  208. expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
  209. } ) );
  210. model.change( writer => writer.setAttribute( 'borderColor', '#f00', tableCell ) );
  211. } );
  212. it( 'should be overridable', () => {
  213. editor.conversion.for( 'downcast' )
  214. .add( dispatcher => dispatcher.on( 'attribute:borderColor:tableCell', ( evt, data, conversionApi ) => {
  215. conversionApi.consumable.consume( data.item, evt.name );
  216. }, { priority: 'high' } ) );
  217. model.change( writer => writer.setAttribute( 'borderColor', '#f00', tableCell ) );
  218. assertTableCellStyle( editor, '' );
  219. } );
  220. it( 'should downcast borderColor attribute (same top, right, bottom, left)', () => {
  221. model.change( writer => writer.setAttribute( 'borderColor', {
  222. top: '#f00',
  223. right: '#f00',
  224. bottom: '#f00',
  225. left: '#f00'
  226. }, tableCell ) );
  227. assertTableCellStyle( editor, 'border-bottom:#f00;border-left:#f00;border-right:#f00;border-top:#f00;' );
  228. } );
  229. it( 'should downcast borderColor attribute (different top, right, bottom, left)', () => {
  230. model.change( writer => writer.setAttribute( 'borderColor', {
  231. top: '#f00',
  232. right: 'hsla(0, 100%, 50%, 0.5)',
  233. bottom: 'deeppink',
  234. left: 'rgb(255, 0, 0)'
  235. }, tableCell ) );
  236. assertTableCellStyle( editor,
  237. 'border-bottom:deeppink;' +
  238. 'border-left:rgb(255, 0, 0);' +
  239. 'border-right:hsla(0, 100%, 50%, 0.5);' +
  240. 'border-top:#f00;'
  241. );
  242. } );
  243. it( 'should consume converted item borderStyle attribute', () => {
  244. editor.conversion.for( 'downcast' )
  245. .add( dispatcher => dispatcher.on( 'attribute:borderStyle:tableCell', ( evt, data, conversionApi ) => {
  246. expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
  247. } ) );
  248. model.change( writer => writer.setAttribute( 'borderStyle', 'ridge', tableCell ) );
  249. } );
  250. it( 'should be overridable for borderStyle', () => {
  251. editor.conversion.for( 'downcast' )
  252. .add( dispatcher => dispatcher.on( 'attribute:borderStyle:tableCell', ( evt, data, conversionApi ) => {
  253. conversionApi.consumable.consume( data.item, evt.name );
  254. }, { priority: 'high' } ) );
  255. model.change( writer => writer.setAttribute( 'borderStyle', 'ridge', tableCell ) );
  256. assertTableCellStyle( editor, '' );
  257. } );
  258. it( 'should downcast borderStyle attribute (same top, right, bottom, left)', () => {
  259. model.change( writer => writer.setAttribute( 'borderStyle', {
  260. top: 'solid',
  261. right: 'solid',
  262. bottom: 'solid',
  263. left: 'solid'
  264. }, tableCell ) );
  265. assertTableCellStyle( editor, 'border-bottom:solid;border-left:solid;border-right:solid;border-top:solid;' );
  266. } );
  267. it( 'should downcast borderStyle attribute (different top, right, bottom, left)', () => {
  268. model.change( writer => writer.setAttribute( 'borderStyle', {
  269. top: 'solid',
  270. right: 'ridge',
  271. bottom: 'dotted',
  272. left: 'dashed'
  273. }, tableCell ) );
  274. assertTableCellStyle( editor, 'border-bottom:dotted;border-left:dashed;border-right:ridge;border-top:solid;' );
  275. } );
  276. it( 'should consume converted item borderWidth attribute', () => {
  277. editor.conversion.for( 'downcast' )
  278. .add( dispatcher => dispatcher.on( 'attribute:borderWidth:tableCell', ( evt, data, conversionApi ) => {
  279. expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
  280. } ) );
  281. model.change( writer => writer.setAttribute( 'borderWidth', '2px', tableCell ) );
  282. } );
  283. it( 'should be overridable for borderWidth', () => {
  284. editor.conversion.for( 'downcast' )
  285. .add( dispatcher => dispatcher.on( 'attribute:borderWidth:tableCell', ( evt, data, conversionApi ) => {
  286. conversionApi.consumable.consume( data.item, evt.name );
  287. }, { priority: 'high' } ) );
  288. model.change( writer => writer.setAttribute( 'borderWidth', '2px', tableCell ) );
  289. assertTableCellStyle( editor, '' );
  290. } );
  291. it( 'should downcast borderWidth attribute (same top, right, bottom, left)', () => {
  292. model.change( writer => writer.setAttribute( 'borderWidth', {
  293. top: '42px',
  294. right: '.1em',
  295. bottom: '1337rem',
  296. left: 'thick'
  297. }, tableCell ) );
  298. assertTableCellStyle( editor, 'border-bottom:1337rem;border-left:thick;border-right:.1em;border-top:42px;' );
  299. } );
  300. it( 'should downcast borderWidth attribute (different top, right, bottom, left)', () => {
  301. model.change( writer => writer.setAttribute( 'borderWidth', {
  302. top: '42px',
  303. right: '42px',
  304. bottom: '42px',
  305. left: '42px'
  306. }, tableCell ) );
  307. assertTableCellStyle( editor, 'border-bottom:42px;border-left:42px;border-right:42px;border-top:42px;' );
  308. } );
  309. it( 'should downcast borderColor, borderStyle and borderWidth attributes together (same top, right, bottom, left)', () => {
  310. model.change( writer => {
  311. writer.setAttribute( 'borderColor', {
  312. top: '#f00',
  313. right: '#f00',
  314. bottom: '#f00',
  315. left: '#f00'
  316. }, tableCell );
  317. writer.setAttribute( 'borderStyle', {
  318. top: 'solid',
  319. right: 'solid',
  320. bottom: 'solid',
  321. left: 'solid'
  322. }, tableCell );
  323. writer.setAttribute( 'borderWidth', {
  324. top: '42px',
  325. right: '42px',
  326. bottom: '42px',
  327. left: '42px'
  328. }, tableCell );
  329. } );
  330. assertTableCellStyle( editor,
  331. 'border-bottom:42px solid #f00;' +
  332. 'border-left:42px solid #f00;' +
  333. 'border-right:42px solid #f00;' +
  334. 'border-top:42px solid #f00;'
  335. );
  336. } );
  337. it(
  338. 'should downcast borderColor, borderStyle and borderWidth attributes together (different top, right, bottom, left)',
  339. () => {
  340. model.change( writer => {
  341. writer.setAttribute( 'borderColor', {
  342. top: '#f00',
  343. right: 'hsla(0, 100%, 50%, 0.5)',
  344. bottom: 'deeppink',
  345. left: 'rgb(255, 0, 0)'
  346. }, tableCell );
  347. writer.setAttribute( 'borderStyle', {
  348. top: 'solid',
  349. right: 'ridge',
  350. bottom: 'dotted',
  351. left: 'dashed'
  352. }, tableCell );
  353. writer.setAttribute( 'borderWidth', {
  354. top: '42px',
  355. right: '.1em',
  356. bottom: '1337rem',
  357. left: 'thick'
  358. }, tableCell );
  359. } );
  360. assertTableCellStyle( editor,
  361. 'border-bottom:1337rem dotted deeppink;' +
  362. 'border-left:thick dashed rgb(255, 0, 0);' +
  363. 'border-right:.1em ridge hsla(0, 100%, 50%, 0.5);' +
  364. 'border-top:42px solid #f00;'
  365. );
  366. }
  367. );
  368. describe( 'change attribute', () => {
  369. beforeEach( () => {
  370. model.change( writer => {
  371. writer.setAttribute( 'borderColor', {
  372. top: '#f00',
  373. right: '#f00',
  374. bottom: '#f00',
  375. left: '#f00'
  376. }, tableCell );
  377. writer.setAttribute( 'borderStyle', {
  378. top: 'solid',
  379. right: 'solid',
  380. bottom: 'solid',
  381. left: 'solid'
  382. }, tableCell );
  383. writer.setAttribute( 'borderWidth', {
  384. top: '42px',
  385. right: '42px',
  386. bottom: '42px',
  387. left: '42px'
  388. }, tableCell );
  389. } );
  390. } );
  391. it( 'should downcast borderColor attribute change', () => {
  392. model.change( writer => writer.setAttribute( 'borderColor', {
  393. top: 'deeppink',
  394. right: 'deeppink',
  395. bottom: 'deeppink',
  396. left: 'deeppink'
  397. }, tableCell ) );
  398. assertTableCellStyle( editor,
  399. 'border-bottom:42px solid deeppink;' +
  400. 'border-left:42px solid deeppink;' +
  401. 'border-right:42px solid deeppink;' +
  402. 'border-top:42px solid deeppink;'
  403. );
  404. } );
  405. it( 'should downcast borderStyle attribute change', () => {
  406. model.change( writer => writer.setAttribute( 'borderStyle', {
  407. top: 'ridge',
  408. right: 'ridge',
  409. bottom: 'ridge',
  410. left: 'ridge'
  411. }, tableCell ) );
  412. assertTableCellStyle( editor,
  413. 'border-bottom:42px ridge #f00;' +
  414. 'border-left:42px ridge #f00;' +
  415. 'border-right:42px ridge #f00;' +
  416. 'border-top:42px ridge #f00;'
  417. );
  418. } );
  419. it( 'should downcast borderWidth attribute change', () => {
  420. model.change( writer => writer.setAttribute( 'borderWidth', {
  421. top: 'thick',
  422. right: 'thick',
  423. bottom: 'thick',
  424. left: 'thick'
  425. }, tableCell ) );
  426. assertTableCellStyle( editor,
  427. 'border-bottom:thick solid #f00;' +
  428. 'border-left:thick solid #f00;' +
  429. 'border-right:thick solid #f00;' +
  430. 'border-top:thick solid #f00;'
  431. );
  432. } );
  433. it( 'should downcast borderColor attribute removal', () => {
  434. model.change( writer => writer.removeAttribute( 'borderColor', tableCell ) );
  435. assertTableCellStyle( editor,
  436. 'border-bottom:42px solid;' +
  437. 'border-left:42px solid;' +
  438. 'border-right:42px solid;' +
  439. 'border-top:42px solid;'
  440. );
  441. } );
  442. it( 'should downcast borderStyle attribute removal', () => {
  443. model.change( writer => writer.removeAttribute( 'borderStyle', tableCell ) );
  444. assertTableCellStyle( editor,
  445. 'border-bottom:42px #f00;' +
  446. 'border-left:42px #f00;' +
  447. 'border-right:42px #f00;' +
  448. 'border-top:42px #f00;'
  449. );
  450. } );
  451. it( 'should downcast borderWidth attribute removal', () => {
  452. model.change( writer => writer.removeAttribute( 'borderWidth', tableCell ) );
  453. assertTableCellStyle( editor,
  454. 'border-bottom:solid #f00;' +
  455. 'border-left:solid #f00;' +
  456. 'border-right:solid #f00;' +
  457. 'border-top:solid #f00;'
  458. );
  459. } );
  460. it( 'should downcast borderColor, borderStyle and borderWidth attributes removal', () => {
  461. model.change( writer => {
  462. writer.removeAttribute( 'borderColor', tableCell );
  463. writer.removeAttribute( 'borderStyle', tableCell );
  464. writer.removeAttribute( 'borderWidth', tableCell );
  465. } );
  466. assertEqualMarkup(
  467. editor.getData(),
  468. '<figure class="table"><table><tbody><tr><td>foo</td></tr></tbody></table></figure>'
  469. );
  470. } );
  471. } );
  472. } );
  473. } );
  474. describe( 'background color', () => {
  475. it( 'should set proper schema rules', () => {
  476. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'backgroundColor' ) ).to.be.true;
  477. } );
  478. describe( 'upcast conversion', () => {
  479. it( 'should upcast background-color', () => {
  480. editor.setData( '<table><tr><td style="background-color:#f00">foo</td></tr></table>' );
  481. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  482. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#f00' );
  483. } );
  484. it( 'should upcast from background shorthand', () => {
  485. editor.setData( '<table><tr><td style="background:#f00 center center">foo</td></tr></table>' );
  486. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  487. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#f00' );
  488. } );
  489. it( 'should upcast from background shorthand (rbg color value with spaces)', () => {
  490. editor.setData( '<table><tr><td style="background:rgb(253, 253, 119) center center">foo</td></tr></table>' );
  491. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  492. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( 'rgb(253, 253, 119)' );
  493. } );
  494. } );
  495. describe( 'downcast conversion', () => {
  496. let tableCell;
  497. beforeEach( () => {
  498. setModelData(
  499. model,
  500. '<table headingRows="0" headingColumns="0">' +
  501. '<tableRow>' +
  502. '<tableCell>' +
  503. '<paragraph>foo</paragraph>' +
  504. '</tableCell>' +
  505. '</tableRow>' +
  506. '</table>'
  507. );
  508. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  509. } );
  510. it( 'should consume converted item backgroundColor attribute', () => {
  511. editor.conversion.for( 'downcast' )
  512. .add( dispatcher => dispatcher.on( 'attribute:backgroundColor:tableCell', ( evt, data, conversionApi ) => {
  513. expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
  514. } ) );
  515. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
  516. } );
  517. it( 'should be overridable', () => {
  518. editor.conversion.for( 'downcast' )
  519. .add( dispatcher => dispatcher.on( 'attribute:backgroundColor:tableCell', ( evt, data, conversionApi ) => {
  520. conversionApi.consumable.consume( data.item, evt.name );
  521. }, { priority: 'high' } ) );
  522. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
  523. assertTableCellStyle( editor, '' );
  524. } );
  525. it( 'should downcast backgroundColor', () => {
  526. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
  527. assertTableCellStyle( editor, 'background-color:#f00;' );
  528. } );
  529. it( 'should downcast backgroundColor removal', () => {
  530. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
  531. model.change( writer => writer.removeAttribute( 'backgroundColor', tableCell ) );
  532. assertTableCellStyle( editor );
  533. } );
  534. it( 'should downcast backgroundColor change', () => {
  535. model.change( writer => writer.setAttribute( 'backgroundColor', '#f00', tableCell ) );
  536. assertTableCellStyle( editor, 'background-color:#f00;' );
  537. model.change( writer => writer.setAttribute( 'backgroundColor', '#ba7', tableCell ) );
  538. assertTableCellStyle( editor, 'background-color:#ba7;' );
  539. } );
  540. } );
  541. } );
  542. describe( 'horizontal alignment', () => {
  543. it( 'should set proper schema rules', () => {
  544. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'horizontalAlignment' ) ).to.be.true;
  545. } );
  546. describe( 'upcast conversion', () => {
  547. it( 'should not upcast text-align:left style', () => {
  548. editor.setData( '<table><tr><td style="text-align:left">foo</td></tr></table>' );
  549. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  550. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.be.undefined;
  551. } );
  552. it( 'should upcast text-align:right style', () => {
  553. editor.setData( '<table><tr><td style="text-align:right">foo</td></tr></table>' );
  554. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  555. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.equal( 'right' );
  556. } );
  557. it( 'should upcast text-align:center style', () => {
  558. editor.setData( '<table><tr><td style="text-align:center">foo</td></tr></table>' );
  559. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  560. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.equal( 'center' );
  561. } );
  562. it( 'should upcast text-align:justify style', () => {
  563. editor.setData( '<table><tr><td style="text-align:justify">foo</td></tr></table>' );
  564. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  565. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.equal( 'justify' );
  566. } );
  567. describe( 'for RTL content language', () => {
  568. let editor, model;
  569. beforeEach( async () => {
  570. editor = await VirtualTestEditor.create( {
  571. plugins: [ TableCellPropertiesEditing, Paragraph, TableEditing ],
  572. language: 'ar'
  573. } );
  574. model = editor.model;
  575. } );
  576. afterEach( async () => {
  577. await editor.destroy();
  578. } );
  579. it( 'should not upcast text-align:right style', () => {
  580. editor.setData( '<table><tr><td style="text-align:right">foo</td></tr></table>' );
  581. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  582. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.be.undefined;
  583. } );
  584. it( 'should upcast text-align:left style', () => {
  585. editor.setData( '<table><tr><td style="text-align:left">foo</td></tr></table>' );
  586. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  587. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.equal( 'left' );
  588. } );
  589. it( 'should upcast text-align:center style', () => {
  590. editor.setData( '<table><tr><td style="text-align:center">foo</td></tr></table>' );
  591. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  592. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.equal( 'center' );
  593. } );
  594. it( 'should upcast text-align:justify style', () => {
  595. editor.setData( '<table><tr><td style="text-align:justify">foo</td></tr></table>' );
  596. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  597. expect( tableCell.getAttribute( 'horizontalAlignment' ) ).to.equal( 'justify' );
  598. } );
  599. } );
  600. } );
  601. describe( 'downcast conversion', () => {
  602. let tableCell;
  603. beforeEach( () => {
  604. setModelData(
  605. model,
  606. '<table headingRows="0" headingColumns="0">' +
  607. '<tableRow>' +
  608. '<tableCell>' +
  609. '<paragraph>foo</paragraph>' +
  610. '</tableCell>' +
  611. '</tableRow>' +
  612. '</table>'
  613. );
  614. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  615. } );
  616. it( 'should consume converted item horizontalAlignment attribute', () => {
  617. editor.conversion.for( 'downcast' )
  618. .add( dispatcher => dispatcher.on( 'attribute:horizontalAlignment:tableCell', ( evt, data, conversionApi ) => {
  619. expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
  620. } ) );
  621. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'right', tableCell ) );
  622. } );
  623. it( 'should be overridable', () => {
  624. editor.conversion.for( 'downcast' )
  625. .add( dispatcher => dispatcher.on( 'attribute:horizontalAlignment:tableCell', ( evt, data, conversionApi ) => {
  626. conversionApi.consumable.consume( data.item, evt.name );
  627. }, { priority: 'high' } ) );
  628. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'right', tableCell ) );
  629. assertTableCellStyle( editor, '' );
  630. } );
  631. it( 'should not downcast horizontalAlignment=left', () => {
  632. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'left', tableCell ) );
  633. assertTableCellStyle( editor );
  634. } );
  635. it( 'should downcast horizontalAlignment=right', () => {
  636. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'right', tableCell ) );
  637. assertTableCellStyle( editor, 'text-align:right;' );
  638. } );
  639. it( 'should downcast horizontalAlignment=center', () => {
  640. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'center', tableCell ) );
  641. assertTableCellStyle( editor, 'text-align:center;' );
  642. } );
  643. it( 'should downcast horizontalAlignment=justify', () => {
  644. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'justify', tableCell ) );
  645. assertTableCellStyle( editor, 'text-align:justify;' );
  646. } );
  647. describe( 'for RTL content language', () => {
  648. let editor, model;
  649. beforeEach( async () => {
  650. editor = await VirtualTestEditor.create( {
  651. plugins: [ TableCellPropertiesEditing, Paragraph, TableEditing ],
  652. language: 'ar'
  653. } );
  654. model = editor.model;
  655. setModelData(
  656. model,
  657. '<table headingRows="0" headingColumns="0">' +
  658. '<tableRow>' +
  659. '<tableCell>' +
  660. '<paragraph>foo</paragraph>' +
  661. '</tableCell>' +
  662. '</tableRow>' +
  663. '</table>'
  664. );
  665. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  666. } );
  667. afterEach( async () => {
  668. await editor.destroy();
  669. } );
  670. it( 'should consume converted item\'s horizontalAlignment attribute', () => {
  671. editor.conversion.for( 'downcast' )
  672. .add( dispatcher => dispatcher.on( 'attribute:horizontalAlignment:tableCell', ( evt, data, conversionApi ) => {
  673. expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
  674. } ) );
  675. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'center', tableCell ) );
  676. } );
  677. it( 'should be overridable', () => {
  678. editor.conversion.for( 'downcast' )
  679. .add( dispatcher => dispatcher.on( 'attribute:horizontalAlignment:tableCell', ( evt, data, conversionApi ) => {
  680. conversionApi.consumable.consume( data.item, evt.name );
  681. }, { priority: 'high' } ) );
  682. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'center', tableCell ) );
  683. assertTableCellStyle( editor, '' );
  684. } );
  685. it( 'should not downcast horizontalAlignment=right', () => {
  686. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'right', tableCell ) );
  687. assertTableCellStyle( editor );
  688. } );
  689. it( 'should downcast horizontalAlignment=left', () => {
  690. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'left', tableCell ) );
  691. assertTableCellStyle( editor, 'text-align:left;' );
  692. } );
  693. it( 'should downcast horizontalAlignment=center', () => {
  694. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'center', tableCell ) );
  695. assertTableCellStyle( editor, 'text-align:center;' );
  696. } );
  697. it( 'should downcast horizontalAlignment=justify', () => {
  698. model.change( writer => writer.setAttribute( 'horizontalAlignment', 'justify', tableCell ) );
  699. assertTableCellStyle( editor, 'text-align:justify;' );
  700. } );
  701. } );
  702. } );
  703. } );
  704. describe( 'vertical alignment', () => {
  705. it( 'should set proper schema rules', () => {
  706. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'verticalAlignment' ) ).to.be.true;
  707. } );
  708. describe( 'upcast conversion', () => {
  709. it( 'should upcast "top" vertical-align', () => {
  710. editor.setData( '<table><tr><td style="vertical-align:top">foo</td></tr></table>' );
  711. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  712. expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.equal( 'top' );
  713. } );
  714. it( 'should upcast "bottom" vertical-align', () => {
  715. editor.setData( '<table><tr><td style="vertical-align:bottom">foo</td></tr></table>' );
  716. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  717. expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.equal( 'bottom' );
  718. } );
  719. it( 'should not upcast "middle" vertical-align', () => {
  720. editor.setData( '<table><tr><td style="vertical-align:middle">foo</td></tr></table>' );
  721. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  722. expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.be.undefined;
  723. } );
  724. it( 'should upcast "top" valign attribute', () => {
  725. editor.setData( '<table><tr><td valign="top">foo</td></tr></table>' );
  726. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  727. expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.equal( 'top' );
  728. } );
  729. it( 'should upcast "bottom" valign attribute', () => {
  730. editor.setData( '<table><tr><td valign="bottom">foo</td></tr></table>' );
  731. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  732. expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.equal( 'bottom' );
  733. } );
  734. it( 'should not upcast "middle" valign attribute', () => {
  735. editor.setData( '<table><tr><td valign="middle">foo</td></tr></table>' );
  736. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  737. expect( tableCell.getAttribute( 'verticalAlignment' ) ).to.be.undefined;
  738. } );
  739. } );
  740. describe( 'downcast conversion', () => {
  741. let tableCell;
  742. beforeEach( () => {
  743. setModelData(
  744. model,
  745. '<table headingRows="0" headingColumns="0">' +
  746. '<tableRow>' +
  747. '<tableCell>' +
  748. '<paragraph>foo</paragraph>' +
  749. '</tableCell>' +
  750. '</tableRow>' +
  751. '</table>'
  752. );
  753. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  754. } );
  755. it( 'should consume converted item verticalAlignment attribute', () => {
  756. editor.conversion.for( 'downcast' )
  757. .add( dispatcher => dispatcher.on( 'attribute:verticalAlignment:tableCell', ( evt, data, conversionApi ) => {
  758. expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
  759. } ) );
  760. model.change( writer => writer.setAttribute( 'verticalAlignment', 'top', tableCell ) );
  761. } );
  762. it( 'should be overridable', () => {
  763. editor.conversion.for( 'downcast' )
  764. .add( dispatcher => dispatcher.on( 'attribute:verticalAlignment:tableCell', ( evt, data, conversionApi ) => {
  765. conversionApi.consumable.consume( data.item, evt.name );
  766. }, { priority: 'high' } ) );
  767. model.change( writer => writer.setAttribute( 'verticalAlignment', 'top', tableCell ) );
  768. assertTableCellStyle( editor, '' );
  769. } );
  770. it( 'should downcast verticalAlignment', () => {
  771. model.change( writer => writer.setAttribute( 'verticalAlignment', 'top', tableCell ) );
  772. assertTableCellStyle( editor, 'vertical-align:top;' );
  773. } );
  774. } );
  775. } );
  776. describe( 'padding', () => {
  777. it( 'should set proper schema rules', () => {
  778. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'padding' ) ).to.be.true;
  779. } );
  780. describe( 'upcast conversion', () => {
  781. it( 'should upcast padding shorthand', () => {
  782. editor.setData( '<table><tr><td style="padding:2px 4em">foo</td></tr></table>' );
  783. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  784. assertTRBLAttribute( tableCell, 'padding', '2px', '4em' );
  785. } );
  786. } );
  787. describe( 'downcast conversion', () => {
  788. let tableCell;
  789. beforeEach( () => {
  790. setModelData(
  791. model,
  792. '<table headingRows="0" headingColumns="0">' +
  793. '<tableRow>' +
  794. '<tableCell>' +
  795. '<paragraph>foo</paragraph>' +
  796. '</tableCell>' +
  797. '</tableRow>' +
  798. '</table>'
  799. );
  800. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  801. } );
  802. it( 'should consume converted item borderColor attribute', () => {
  803. editor.conversion.for( 'downcast' )
  804. .add( dispatcher => dispatcher.on( 'attribute:padding:tableCell', ( evt, data, conversionApi ) => {
  805. expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
  806. } ) );
  807. model.change( writer => writer.setAttribute( 'padding', '1px', tableCell ) );
  808. } );
  809. it( 'should be overridable', () => {
  810. editor.conversion.for( 'downcast' )
  811. .add( dispatcher => dispatcher.on( 'attribute:padding:tableCell', ( evt, data, conversionApi ) => {
  812. conversionApi.consumable.consume( data.item, evt.name );
  813. }, { priority: 'high' } ) );
  814. model.change( writer => writer.setAttribute( 'padding', '1px', tableCell ) );
  815. assertTableCellStyle( editor, '' );
  816. } );
  817. it( 'should downcast padding (same top, right, bottom, left)', () => {
  818. model.change( writer => writer.setAttribute( 'padding', {
  819. top: '2px',
  820. right: '2px',
  821. bottom: '2px',
  822. left: '2px'
  823. }, tableCell ) );
  824. assertTableCellStyle( editor, 'padding:2px;' );
  825. } );
  826. it( 'should downcast padding (different top, right, bottom, left)', () => {
  827. model.change( writer => writer.setAttribute( 'padding', {
  828. top: '2px',
  829. right: '3px',
  830. bottom: '4px',
  831. left: '5px'
  832. }, tableCell ) );
  833. assertTableCellStyle( editor, 'padding:2px 3px 4px 5px;' );
  834. } );
  835. it( 'should downcast padding removal', () => {
  836. model.change( writer => writer.setAttribute( 'padding', '1337px', tableCell ) );
  837. model.change( writer => writer.removeAttribute( 'padding', tableCell ) );
  838. assertTableCellStyle( editor );
  839. } );
  840. it( 'should downcast padding change', () => {
  841. model.change( writer => writer.setAttribute( 'padding', '1337px', tableCell ) );
  842. assertTableCellStyle( editor, 'padding:1337px;' );
  843. model.change( writer => writer.setAttribute( 'padding', '1410em', tableCell ) );
  844. assertTableCellStyle( editor, 'padding:1410em;' );
  845. } );
  846. } );
  847. } );
  848. describe( 'cell width', () => {
  849. it( 'should set proper schema rules', () => {
  850. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'width' ) ).to.be.true;
  851. } );
  852. describe( 'upcast conversion', () => {
  853. it( 'should upcast width attribute on table cell', () => {
  854. editor.setData( '<table><tr><td style="width:20px">foo</td></tr></table>' );
  855. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  856. expect( tableCell.getAttribute( 'width' ) ).to.equal( '20px' );
  857. } );
  858. } );
  859. describe( 'downcast conversion', () => {
  860. let tableCell;
  861. beforeEach( () => {
  862. setModelData(
  863. model,
  864. '<table headingRows="0" headingColumns="0">' +
  865. '<tableRow>' +
  866. '<tableCell>' +
  867. '<paragraph>foo</paragraph>' +
  868. '</tableCell>' +
  869. '</tableRow>' +
  870. '</table>'
  871. );
  872. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  873. } );
  874. it( 'should consume converted item width attribute', () => {
  875. editor.conversion.for( 'downcast' )
  876. .add( dispatcher => dispatcher.on( 'attribute:width:tableCell', ( evt, data, conversionApi ) => {
  877. expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
  878. } ) );
  879. model.change( writer => writer.setAttribute( 'width', '40px', tableCell ) );
  880. } );
  881. it( 'should be overridable', () => {
  882. editor.conversion.for( 'downcast' )
  883. .add( dispatcher => dispatcher.on( 'attribute:width:tableCell', ( evt, data, conversionApi ) => {
  884. conversionApi.consumable.consume( data.item, evt.name );
  885. }, { priority: 'high' } ) );
  886. model.change( writer => writer.setAttribute( 'width', '40px', tableCell ) );
  887. assertTableCellStyle( editor, '' );
  888. } );
  889. it( 'should downcast width attribute', () => {
  890. model.change( writer => writer.setAttribute( 'width', '20px', tableCell ) );
  891. assertEqualMarkup(
  892. editor.getData(),
  893. '<figure class="table"><table><tbody><tr><td style="width:20px;">foo</td></tr></tbody></table></figure>'
  894. );
  895. } );
  896. it( 'should downcast width removal', () => {
  897. model.change( writer => writer.setAttribute( 'width', '1337px', tableCell ) );
  898. model.change( writer => writer.removeAttribute( 'width', tableCell ) );
  899. assertTableCellStyle( editor );
  900. } );
  901. it( 'should downcast width change', () => {
  902. model.change( writer => writer.setAttribute( 'width', '1337px', tableCell ) );
  903. assertTableCellStyle( editor, 'width:1337px;' );
  904. model.change( writer => writer.setAttribute( 'width', '1410em', tableCell ) );
  905. assertTableCellStyle( editor, 'width:1410em;' );
  906. } );
  907. } );
  908. } );
  909. describe( 'cell height', () => {
  910. it( 'should set proper schema rules', () => {
  911. expect( model.schema.checkAttribute( [ '$root', 'tableCell' ], 'height' ) ).to.be.true;
  912. } );
  913. describe( 'upcast conversion', () => {
  914. it( 'should upcast height attribute on table cell', () => {
  915. editor.setData( '<table><tr><td style="height:20px">foo</td></tr></table>' );
  916. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  917. expect( tableCell.getAttribute( 'height' ) ).to.equal( '20px' );
  918. } );
  919. } );
  920. describe( 'downcast conversion', () => {
  921. let tableCell;
  922. beforeEach( () => {
  923. setModelData(
  924. model,
  925. '<table headingRows="0" headingColumns="0">' +
  926. '<tableRow>' +
  927. '<tableCell>' +
  928. '<paragraph>foo</paragraph>' +
  929. '</tableCell>' +
  930. '</tableRow>' +
  931. '</table>'
  932. );
  933. tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  934. } );
  935. it( 'should consume converted item height attribute', () => {
  936. editor.conversion.for( 'downcast' )
  937. .add( dispatcher => dispatcher.on( 'attribute:height:tableCell', ( evt, data, conversionApi ) => {
  938. expect( conversionApi.consumable.consume( data.item, evt.name ) ).to.be.false;
  939. } ) );
  940. model.change( writer => writer.setAttribute( 'height', '40px', tableCell ) );
  941. } );
  942. it( 'should be overridable', () => {
  943. editor.conversion.for( 'downcast' )
  944. .add( dispatcher => dispatcher.on( 'attribute:height:tableCell', ( evt, data, conversionApi ) => {
  945. conversionApi.consumable.consume( data.item, evt.name );
  946. }, { priority: 'high' } ) );
  947. model.change( writer => writer.setAttribute( 'height', '40px', tableCell ) );
  948. assertTableCellStyle( editor, '' );
  949. } );
  950. it( 'should downcast height attribute', () => {
  951. model.change( writer => writer.setAttribute( 'height', '20px', tableCell ) );
  952. assertEqualMarkup(
  953. editor.getData(),
  954. '<figure class="table"><table><tbody><tr><td style="height:20px;">foo</td></tr></tbody></table></figure>'
  955. );
  956. } );
  957. it( 'should downcast height removal', () => {
  958. model.change( writer => writer.setAttribute( 'height', '1337px', tableCell ) );
  959. model.change( writer => writer.removeAttribute( 'height', tableCell ) );
  960. assertTableCellStyle( editor );
  961. } );
  962. it( 'should downcast height change', () => {
  963. model.change( writer => writer.setAttribute( 'height', '1337px', tableCell ) );
  964. assertTableCellStyle( editor, 'height:1337px;' );
  965. model.change( writer => writer.setAttribute( 'height', '1410em', tableCell ) );
  966. assertTableCellStyle( editor, 'height:1410em;' );
  967. } );
  968. } );
  969. } );
  970. } );
  971. } );