8
0

tablestyles.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. describe.only( 'Table styles conversion', () => {
  11. let editor, model;
  12. beforeEach( () => {
  13. return VirtualTestEditor
  14. .create( {
  15. plugins: [ TableEditing, TableStyleEditing, Paragraph, Widget ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. // Since this part of test tests only view->model conversion editing pipeline is not necessary
  21. // so defining model->view converters won't be necessary.
  22. editor.editing.destroy();
  23. } );
  24. } );
  25. describe( 'upcast', () => {
  26. describe( 'table cell', () => {
  27. it( 'should upcast border shorthand', () => {
  28. editor.setData( '<table><tr><td style="border:1px solid #f00">foo</td></tr></table>' );
  29. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  30. assertTRBLAttribute( tableCell, 'borderColor', '#f00' );
  31. assertTRBLAttribute( tableCell, 'borderStyle', 'solid' );
  32. assertTRBLAttribute( tableCell, 'borderWidth', '1px' );
  33. } );
  34. it( 'should upcast border-color shorthand', () => {
  35. editor.setData( '<table><tr><td style="border-color:#f00">foo</td></tr></table>' );
  36. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  37. assertTRBLAttribute( tableCell, 'borderColor', '#f00' );
  38. } );
  39. it( 'should upcast border-style shorthand', () => {
  40. editor.setData( '<table><tr><td style="border-style:ridge">foo</td></tr></table>' );
  41. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  42. assertTRBLAttribute( tableCell, 'borderStyle', 'ridge' );
  43. } );
  44. it( 'should upcast border-width shorthand', () => {
  45. editor.setData( '<table><tr><td style="border-width:1px">foo</td></tr></table>' );
  46. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  47. assertTRBLAttribute( tableCell, 'borderWidth', '1px' );
  48. } );
  49. it( 'should upcast border-top shorthand', () => {
  50. editor.setData( '<table><tr><td style="border-top:1px solid #f00">foo</td></tr></table>' );
  51. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  52. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, null, null );
  53. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, null, null );
  54. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, null, null );
  55. } );
  56. it( 'should upcast border-right shorthand', () => {
  57. editor.setData( '<table><tr><td style="border-right:1px solid #f00">foo</td></tr></table>' );
  58. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  59. assertTRBLAttribute( tableCell, 'borderColor', null, '#f00', null, null );
  60. assertTRBLAttribute( tableCell, 'borderStyle', null, 'solid', null, null );
  61. assertTRBLAttribute( tableCell, 'borderWidth', null, '1px', null, null );
  62. } );
  63. it( 'should upcast border-bottom shorthand', () => {
  64. editor.setData( '<table><tr><td style="border-bottom:1px solid #f00">foo</td></tr></table>' );
  65. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  66. assertTRBLAttribute( tableCell, 'borderColor', null, null, '#f00', null );
  67. assertTRBLAttribute( tableCell, 'borderStyle', null, null, 'solid', null );
  68. assertTRBLAttribute( tableCell, 'borderWidth', null, null, '1px', null );
  69. } );
  70. it( 'should upcast border-left shorthand', () => {
  71. editor.setData( '<table><tr><td style="border-left:1px solid #f00">foo</td></tr></table>' );
  72. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  73. assertTRBLAttribute( tableCell, 'borderColor', null, null, null, '#f00' );
  74. assertTRBLAttribute( tableCell, 'borderStyle', null, null, null, 'solid' );
  75. assertTRBLAttribute( tableCell, 'borderWidth', null, null, null, '1px' );
  76. } );
  77. it( 'should upcast border-top-* styles', () => {
  78. editor.setData(
  79. '<table><tr><td style="border-top-width:1px;border-top-style:solid;border-top-color:#f00">foo</td></tr></table>'
  80. );
  81. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  82. assertTRBLAttribute( tableCell, 'borderColor', '#f00', null, null, null );
  83. assertTRBLAttribute( tableCell, 'borderStyle', 'solid', null, null, null );
  84. assertTRBLAttribute( tableCell, 'borderWidth', '1px', null, null, null );
  85. } );
  86. it( 'should upcast border-right-* styles', () => {
  87. editor.setData(
  88. '<table><tr><td style="border-right-width:1px;border-right-style:solid;border-right-color:#f00">foo</td></tr></table>'
  89. );
  90. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  91. assertTRBLAttribute( tableCell, 'borderColor', null, '#f00', null, null );
  92. assertTRBLAttribute( tableCell, 'borderStyle', null, 'solid', null, null );
  93. assertTRBLAttribute( tableCell, 'borderWidth', null, '1px', null, null );
  94. } );
  95. it( 'should upcast border-bottom-* styles', () => {
  96. editor.setData(
  97. '<table><tr>' +
  98. '<td style="border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:#f00">foo</td>' +
  99. '</tr></table>'
  100. );
  101. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  102. assertTRBLAttribute( tableCell, 'borderColor', null, null, '#f00', null );
  103. assertTRBLAttribute( tableCell, 'borderStyle', null, null, 'solid', null );
  104. assertTRBLAttribute( tableCell, 'borderWidth', null, null, '1px', null );
  105. } );
  106. it( 'should upcast border-left-* styles', () => {
  107. editor.setData(
  108. '<table><tr><td style="border-left-width:1px;border-left-style:solid;border-left-color:#f00">foo</td></tr></table>'
  109. );
  110. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  111. assertTRBLAttribute( tableCell, 'borderColor', null, null, null, '#f00' );
  112. assertTRBLAttribute( tableCell, 'borderStyle', null, null, null, 'solid' );
  113. assertTRBLAttribute( tableCell, 'borderWidth', null, null, null, '1px' );
  114. } );
  115. } );
  116. } );
  117. /**
  118. * Assertion helper for top-right-bottom-left attribute object.
  119. *
  120. * @param {module:engine/model/node~Node} tableCell
  121. * @param {String} key Attribute key
  122. * @param {String} top Top value. Pass null to omit value in attributes object.
  123. * @param {String} [right=top] Right value - defaults to top if not provided.
  124. * Pass null to omit value in attributes object.
  125. * @param {String} [bottom=top] Bottom value - defaults to top (right value must be defined).
  126. * Pass null to omit value in attributes object.
  127. * @param {String} [left=right] Left value - defaults to right (bottom and right values must be defined).
  128. * Pass null to omit value in attributes object.
  129. */
  130. function assertTRBLAttribute( tableCell, key, top, right = top, bottom = top, left = right ) {
  131. const styleObject = {};
  132. if ( top ) {
  133. styleObject.top = top;
  134. }
  135. if ( right ) {
  136. styleObject.right = right;
  137. }
  138. if ( bottom ) {
  139. styleObject.bottom = bottom;
  140. }
  141. if ( left ) {
  142. styleObject.left = left;
  143. }
  144. expect( tableCell.getAttribute( key ) ).to.deep.equal( styleObject );
  145. }
  146. } );