table-cell-paragraph-post-fixer.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import TableEditing from '../../src/tableediting';
  9. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  10. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'Table cell paragraph post-fixer', () => {
  12. let editor, model, root;
  13. beforeEach( () => {
  14. return VirtualTestEditor
  15. .create( {
  16. plugins: [ TableEditing, Paragraph, UndoEditing ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. model = editor.model;
  21. root = model.document.getRoot();
  22. } );
  23. } );
  24. afterEach( () => {
  25. editor.destroy();
  26. } );
  27. it( 'should add a paragraph to an empty table cell (on table insert)', () => {
  28. setModelData( model,
  29. '<table>' +
  30. '<tableRow>' +
  31. '<tableCell></tableCell>' +
  32. '</tableRow>' +
  33. '</table>'
  34. );
  35. assertEqualMarkup( getModelData( model, { withoutSelection: true } ),
  36. '<table>' +
  37. '<tableRow>' +
  38. '<tableCell><paragraph></paragraph></tableCell>' +
  39. '</tableRow>' +
  40. '</table>'
  41. );
  42. } );
  43. it( 'should add a paragraph to an empty table cell (on row insert)', () => {
  44. setModelData( model,
  45. '<table>' +
  46. '<tableRow>' +
  47. '<tableCell><paragraph></paragraph></tableCell>' +
  48. '</tableRow>' +
  49. '</table>'
  50. );
  51. // Insert table row with one table cell
  52. model.change( writer => {
  53. writer.insertElement( 'tableRow', writer.createPositionAfter( root.getNodeByPath( [ 0, 0 ] ) ) );
  54. writer.insertElement( 'tableCell', writer.createPositionAt( root.getNodeByPath( [ 0, 1 ] ), 0 ) );
  55. } );
  56. assertEqualMarkup( getModelData( model, { withoutSelection: true } ),
  57. '<table>' +
  58. '<tableRow>' +
  59. '<tableCell><paragraph></paragraph></tableCell>' +
  60. '</tableRow>' +
  61. '<tableRow>' +
  62. '<tableCell><paragraph></paragraph></tableCell>' +
  63. '</tableRow>' +
  64. '</table>'
  65. );
  66. } );
  67. it( 'should add a paragraph to an empty table cell (on table cell insert)', () => {
  68. setModelData( model,
  69. '<table>' +
  70. '<tableRow>' +
  71. '<tableCell><paragraph></paragraph></tableCell>' +
  72. '</tableRow>' +
  73. '</table>'
  74. );
  75. // Insert table row with one table cell
  76. model.change( writer => {
  77. writer.insertElement( 'tableCell', writer.createPositionAt( root.getNodeByPath( [ 0, 0 ] ), 'end' ) );
  78. } );
  79. assertEqualMarkup( getModelData( model, { withoutSelection: true } ),
  80. '<table>' +
  81. '<tableRow>' +
  82. '<tableCell><paragraph></paragraph></tableCell>' +
  83. '<tableCell><paragraph></paragraph></tableCell>' +
  84. '</tableRow>' +
  85. '</table>'
  86. );
  87. } );
  88. it( 'should add a paragraph to an empty table cell (after remove)', () => {
  89. setModelData( model,
  90. '<table>' +
  91. '<tableRow>' +
  92. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  93. '</tableRow>' +
  94. '</table>'
  95. );
  96. // Remove paragraph from table cell.
  97. model.change( writer => {
  98. writer.remove( writer.createRangeIn( root.getNodeByPath( [ 0, 0, 0 ] ) ) );
  99. } );
  100. assertEqualMarkup( getModelData( model, { withoutSelection: true } ),
  101. '<table>' +
  102. '<tableRow>' +
  103. '<tableCell><paragraph></paragraph></tableCell>' +
  104. '</tableRow>' +
  105. '</table>'
  106. );
  107. } );
  108. it( 'should wrap in paragraph $text nodes placed directly in tableCell (on table cell modification) ', () => {
  109. setModelData( model,
  110. '<table>' +
  111. '<tableRow>' +
  112. '<tableCell><paragraph></paragraph></tableCell>' +
  113. '</tableRow>' +
  114. '</table>'
  115. );
  116. // Remove paragraph from table cell & insert: $text<paragraph>$text</paragraph>$text.
  117. model.change( writer => {
  118. writer.remove( writer.createRangeIn( root.getNodeByPath( [ 0, 0, 0 ] ) ) );
  119. const paragraph = writer.createElement( 'paragraph' );
  120. writer.insertText( 'foo', root.getNodeByPath( [ 0, 0, 0 ] ) );
  121. writer.insert( paragraph, root.getNodeByPath( [ 0, 0, 0 ] ), 'end' );
  122. writer.insertText( 'bar', paragraph );
  123. writer.insertText( 'baz', root.getNodeByPath( [ 0, 0, 0 ] ), 'end' );
  124. } );
  125. assertEqualMarkup( getModelData( model, { withoutSelection: true } ),
  126. '<table>' +
  127. '<tableRow>' +
  128. '<tableCell>' +
  129. '<paragraph>foo</paragraph>' +
  130. '<paragraph>bar</paragraph>' +
  131. '<paragraph>baz</paragraph>' +
  132. '</tableCell>' +
  133. '</tableRow>' +
  134. '</table>'
  135. );
  136. } );
  137. it( 'should wrap in paragraph $text nodes placed directly in tableCell (on inserting table cell)', () => {
  138. setModelData( model,
  139. '<table>' +
  140. '<tableRow>' +
  141. '<tableCell><paragraph></paragraph></tableCell>' +
  142. '</tableRow>' +
  143. '</table>'
  144. );
  145. // Insert new tableCell with $text.
  146. model.change( writer => {
  147. const tableCell = writer.createElement( 'tableCell' );
  148. writer.insertText( 'foo', tableCell );
  149. writer.insert( tableCell, writer.createPositionAt( root.getNodeByPath( [ 0, 0 ] ), 'end' ) );
  150. } );
  151. assertEqualMarkup( getModelData( model, { withoutSelection: true } ),
  152. '<table>' +
  153. '<tableRow>' +
  154. '<tableCell>' +
  155. '<paragraph></paragraph>' +
  156. '</tableCell>' +
  157. '<tableCell>' +
  158. '<paragraph>foo</paragraph>' +
  159. '</tableCell>' +
  160. '</tableRow>' +
  161. '</table>'
  162. );
  163. } );
  164. it( 'should wrap in paragraph $text nodes placed directly in tableCell (on inserting table rows)', () => {
  165. setModelData( model,
  166. '<table>' +
  167. '<tableRow>' +
  168. '<tableCell><paragraph></paragraph></tableCell>' +
  169. '</tableRow>' +
  170. '</table>'
  171. );
  172. // Insert new tableRow with tableCell with $text.
  173. model.change( writer => {
  174. const tableRow = writer.createElement( 'tableRow' );
  175. const tableCell = writer.createElement( 'tableCell' );
  176. writer.insertText( 'foo', tableCell );
  177. writer.insert( tableCell, tableRow );
  178. writer.insert( tableRow, writer.createPositionAt( root.getNodeByPath( [ 0 ] ), 'end' ) );
  179. } );
  180. assertEqualMarkup( getModelData( model, { withoutSelection: true } ),
  181. '<table>' +
  182. '<tableRow>' +
  183. '<tableCell>' +
  184. '<paragraph></paragraph>' +
  185. '</tableCell>' +
  186. '</tableRow>' +
  187. '<tableRow>' +
  188. '<tableCell>' +
  189. '<paragraph>foo</paragraph>' +
  190. '</tableCell>' +
  191. '</tableRow>' +
  192. '</table>'
  193. );
  194. } );
  195. } );