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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 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 { formatTable } from './../_utils/utils';
  10. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  11. describe( 'Table cell paragraph post-fixer', () => {
  12. let editor, model, root, postFixerSpy;
  13. beforeEach( () => {
  14. return VirtualTestEditor
  15. .create( {
  16. plugins: [ function( editor ) {
  17. editor.model.schema.register( 'block', { inheritAllFrom: '$block' } );
  18. // Add a post-fixer spy before any other plugin so it will be always run if other post-fixer returns true.
  19. // Use spy.resetHistory() before checking a case when post fixer should be run.
  20. // It should be called once if no other post-fixer fixed the model or more then one if another post-fixer changed model.
  21. postFixerSpy = sinon.spy();
  22. editor.model.document.registerPostFixer( postFixerSpy );
  23. }, TableEditing, Paragraph, UndoEditing ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. model = editor.model;
  28. root = model.document.getRoot();
  29. } );
  30. } );
  31. afterEach( () => {
  32. editor.destroy();
  33. } );
  34. it( 'should add a paragraph to an empty table cell (on table insert)', () => {
  35. setModelData( model,
  36. '<table>' +
  37. '<tableRow>' +
  38. '<tableCell></tableCell>' +
  39. '</tableRow>' +
  40. '</table>'
  41. );
  42. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  43. '<table>' +
  44. '<tableRow>' +
  45. '<tableCell><paragraph></paragraph></tableCell>' +
  46. '</tableRow>' +
  47. '</table>'
  48. ) );
  49. } );
  50. it( 'should add a paragraph to an empty table cell (on row insert)', () => {
  51. setModelData( model,
  52. '<table>' +
  53. '<tableRow>' +
  54. '<tableCell><paragraph></paragraph></tableCell>' +
  55. '</tableRow>' +
  56. '</table>'
  57. );
  58. // Insert table row with one table cell
  59. model.change( writer => {
  60. writer.insertElement( 'tableRow', writer.createPositionAfter( root.getNodeByPath( [ 0, 0 ] ) ) );
  61. writer.insertElement( 'tableCell', writer.createPositionAt( root.getNodeByPath( [ 0, 1 ] ), 0 ) );
  62. } );
  63. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  64. '<table>' +
  65. '<tableRow>' +
  66. '<tableCell><paragraph></paragraph></tableCell>' +
  67. '</tableRow>' +
  68. '<tableRow>' +
  69. '<tableCell><paragraph></paragraph></tableCell>' +
  70. '</tableRow>' +
  71. '</table>'
  72. ) );
  73. } );
  74. it( 'should add a paragraph to an empty table cell (on table cell insert)', () => {
  75. setModelData( model,
  76. '<table>' +
  77. '<tableRow>' +
  78. '<tableCell><paragraph></paragraph></tableCell>' +
  79. '</tableRow>' +
  80. '</table>'
  81. );
  82. // Insert table row with one table cell
  83. model.change( writer => {
  84. writer.insertElement( 'tableCell', writer.createPositionAt( root.getNodeByPath( [ 0, 0 ] ), 'end' ) );
  85. } );
  86. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  87. '<table>' +
  88. '<tableRow>' +
  89. '<tableCell><paragraph></paragraph></tableCell>' +
  90. '<tableCell><paragraph></paragraph></tableCell>' +
  91. '</tableRow>' +
  92. '</table>'
  93. ) );
  94. } );
  95. it( 'should add a paragraph to an empty table cell (after remove)', () => {
  96. setModelData( model,
  97. '<table>' +
  98. '<tableRow>' +
  99. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  100. '</tableRow>' +
  101. '</table>'
  102. );
  103. // Remove paragraph from table cell.
  104. model.change( writer => {
  105. writer.remove( writer.createRangeIn( root.getNodeByPath( [ 0, 0, 0 ] ) ) );
  106. } );
  107. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  108. '<table>' +
  109. '<tableRow>' +
  110. '<tableCell><paragraph></paragraph></tableCell>' +
  111. '</tableRow>' +
  112. '</table>'
  113. ) );
  114. } );
  115. it( 'should wrap in paragraph $text nodes placed directly in tableCell (on table cell modification) ', () => {
  116. setModelData( model,
  117. '<table>' +
  118. '<tableRow>' +
  119. '<tableCell><paragraph></paragraph></tableCell>' +
  120. '</tableRow>' +
  121. '</table>'
  122. );
  123. // Remove paragraph from table cell & insert: $text<paragraph>$text</paragraph>$text.
  124. model.change( writer => {
  125. writer.remove( writer.createRangeIn( root.getNodeByPath( [ 0, 0, 0 ] ) ) );
  126. const paragraph = writer.createElement( 'paragraph' );
  127. writer.insertText( 'foo', root.getNodeByPath( [ 0, 0, 0 ] ) );
  128. writer.insert( paragraph, root.getNodeByPath( [ 0, 0, 0 ] ), 'end' );
  129. writer.insertText( 'bar', paragraph );
  130. writer.insertText( 'baz', root.getNodeByPath( [ 0, 0, 0 ] ), 'end' );
  131. } );
  132. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  133. '<table>' +
  134. '<tableRow>' +
  135. '<tableCell>' +
  136. '<paragraph>foo</paragraph>' +
  137. '<paragraph>bar</paragraph>' +
  138. '<paragraph>baz</paragraph>' +
  139. '</tableCell>' +
  140. '</tableRow>' +
  141. '</table>'
  142. ) );
  143. } );
  144. it( 'should wrap in paragraph $text nodes placed directly in tableCell (on inserting table cell)', () => {
  145. setModelData( model,
  146. '<table>' +
  147. '<tableRow>' +
  148. '<tableCell><paragraph></paragraph></tableCell>' +
  149. '</tableRow>' +
  150. '</table>'
  151. );
  152. // Insert new tableCell with $text.
  153. model.change( writer => {
  154. const tableCell = writer.createElement( 'tableCell' );
  155. writer.insertText( 'foo', tableCell );
  156. writer.insert( tableCell, writer.createPositionAt( root.getNodeByPath( [ 0, 0 ] ), 'end' ) );
  157. } );
  158. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  159. '<table>' +
  160. '<tableRow>' +
  161. '<tableCell>' +
  162. '<paragraph></paragraph>' +
  163. '</tableCell>' +
  164. '<tableCell>' +
  165. '<paragraph>foo</paragraph>' +
  166. '</tableCell>' +
  167. '</tableRow>' +
  168. '</table>'
  169. ) );
  170. } );
  171. it( 'should wrap in paragraph $text nodes placed directly in tableCell (on inserting table rows)', () => {
  172. setModelData( model,
  173. '<table>' +
  174. '<tableRow>' +
  175. '<tableCell><paragraph></paragraph></tableCell>' +
  176. '</tableRow>' +
  177. '</table>'
  178. );
  179. // Insert new tableRow with tableCell with $text.
  180. model.change( writer => {
  181. const tableRow = writer.createElement( 'tableRow' );
  182. const tableCell = writer.createElement( 'tableCell' );
  183. writer.insertText( 'foo', tableCell );
  184. writer.insert( tableCell, tableRow );
  185. writer.insert( tableRow, writer.createPositionAt( root.getNodeByPath( [ 0 ] ), 'end' ) );
  186. } );
  187. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  188. '<table>' +
  189. '<tableRow>' +
  190. '<tableCell>' +
  191. '<paragraph></paragraph>' +
  192. '</tableCell>' +
  193. '</tableRow>' +
  194. '<tableRow>' +
  195. '<tableCell>' +
  196. '<paragraph>foo</paragraph>' +
  197. '</tableCell>' +
  198. '</tableRow>' +
  199. '</table>'
  200. ) );
  201. } );
  202. it( 'should not be run on changing attribute of other element in a table cell', () => {
  203. setModelData( model,
  204. '<table>' +
  205. '<tableRow>' +
  206. '<tableCell><block></block></tableCell>' +
  207. '</tableRow>' +
  208. '</table>'
  209. );
  210. postFixerSpy.resetHistory();
  211. // Insert table row with one table cell
  212. model.change( writer => {
  213. writer.setAttribute( 'foo', 'bar', root.getNodeByPath( [ 0, 0, 0, 0 ] ) );
  214. } );
  215. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  216. '<table>' +
  217. '<tableRow>' +
  218. '<tableCell><block foo="bar"></block></tableCell>' +
  219. '</tableRow>' +
  220. '</table>'
  221. ) );
  222. sinon.assert.calledOnce( postFixerSpy );
  223. } );
  224. it( 'should be run on changing attribute of an paragraph in a table cell', () => {
  225. setModelData( model,
  226. '<table>' +
  227. '<tableRow>' +
  228. '<tableCell><paragraph></paragraph></tableCell>' +
  229. '</tableRow>' +
  230. '</table>'
  231. );
  232. postFixerSpy.resetHistory();
  233. // Insert table row with one table cell
  234. model.change( writer => {
  235. writer.setAttribute( 'foo', 'bar', root.getNodeByPath( [ 0, 0, 0, 0 ] ) );
  236. } );
  237. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  238. '<table>' +
  239. '<tableRow>' +
  240. '<tableCell><paragraph foo="bar"></paragraph></tableCell>' +
  241. '</tableRow>' +
  242. '</table>'
  243. ) );
  244. sinon.assert.calledTwice( postFixerSpy );
  245. } );
  246. } );