8
0

table-cell-refresh-post-fixer.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. /* globals document */
  6. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  7. import { defaultConversion, defaultSchema, formatTable, formattedViewTable, viewTable } from '../_utils/utils';
  8. import injectTableCellRefreshPostFixer from '../../src/converters/table-cell-refresh-post-fixer';
  9. import env from '@ckeditor/ckeditor5-utils/src/env';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  12. import Delete from '@ckeditor/ckeditor5-typing/src/delete';
  13. describe( 'Table cell refresh post-fixer', () => {
  14. let editor, model, doc, root, view;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. const element = document.createElement( 'div' );
  18. document.body.appendChild( element );
  19. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  20. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  21. return ClassicTestEditor.create( element, { extraPlugins: [ Delete ] } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. model = editor.model;
  25. doc = model.document;
  26. root = doc.getRoot( 'main' );
  27. view = editor.editing.view;
  28. defaultSchema( model.schema );
  29. defaultConversion( editor.conversion, true );
  30. editor.model.schema.register( 'block', {
  31. inheritAllFrom: '$block'
  32. } );
  33. editor.conversion.elementToElement( { model: 'block', view: 'div' } );
  34. model.schema.extend( '$block', { allowAttributes: 'foo' } );
  35. editor.conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  36. injectTableCellRefreshPostFixer( model );
  37. } );
  38. } );
  39. it( 'should rename <span> to <p> when adding more <paragraph> elements to the same table cell', () => {
  40. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  41. const table = root.getChild( 0 );
  42. model.change( writer => {
  43. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  44. const paragraph = writer.createElement( 'paragraph' );
  45. writer.insert( paragraph, nodeByPath, 'after' );
  46. writer.setSelection( nodeByPath.nextSibling, 0 );
  47. } );
  48. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  49. [ '<p>00</p><p></p>' ]
  50. ], { asWidget: true } ) );
  51. } );
  52. it( 'should rename <span> to <p> on adding other block element to the same table cell', () => {
  53. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  54. const table = root.getChild( 0 );
  55. model.change( writer => {
  56. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  57. const paragraph = writer.createElement( 'block' );
  58. writer.insert( paragraph, nodeByPath, 'after' );
  59. writer.setSelection( nodeByPath.nextSibling, 0 );
  60. } );
  61. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  62. [ '<p>00</p><div></div>' ]
  63. ], { asWidget: true } ) );
  64. } );
  65. it( 'should properly rename the same element on consecutive changes', () => {
  66. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  67. const table = root.getChild( 0 );
  68. model.change( writer => {
  69. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  70. writer.insertElement( 'paragraph', nodeByPath, 'after' );
  71. writer.setSelection( nodeByPath.nextSibling, 0 );
  72. } );
  73. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  74. [ '<p>00</p><p></p>' ]
  75. ], { asWidget: true } ) );
  76. model.change( writer => {
  77. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  78. } );
  79. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  80. [ '00' ]
  81. ], { asWidget: true } ) );
  82. } );
  83. it( 'should rename <span> to <p> when setting attribute on <paragraph>', () => {
  84. editor.setData( '<table><tr><td><p>00</p></td></tr></table>' );
  85. const table = root.getChild( 0 );
  86. model.change( writer => {
  87. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  88. } );
  89. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  90. [ '<p foo="bar">00</p>' ]
  91. ], { asWidget: true } ) );
  92. } );
  93. it( 'should rename <p> to <span> when removing all but one paragraph inside table cell', () => {
  94. editor.setData( viewTable( [ [ '<p>00</p><p>foo</p>' ] ] ) );
  95. const table = root.getChild( 0 );
  96. model.change( writer => {
  97. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  98. } );
  99. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  100. [ '00' ]
  101. ], { asWidget: true } ) );
  102. } );
  103. it( 'should rename <p> to <span> when removing attribute from <paragraph>', () => {
  104. editor.setData( '<table><tr><td><p foo="bar">00</p></td></tr></table>' );
  105. const table = root.getChild( 0 );
  106. model.change( writer => {
  107. writer.removeAttribute( 'foo', table.getNodeByPath( [ 0, 0, 0 ] ) );
  108. } );
  109. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  110. [ '<span>00</span>' ]
  111. ], { asWidget: true } ) );
  112. } );
  113. it( 'should keep <p> in the view when <paragraph> attribute value is changed', () => {
  114. editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
  115. const table = root.getChild( 0 );
  116. model.change( writer => {
  117. writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
  118. } );
  119. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  120. [ '<p foo="baz">00</p>' ]
  121. ], { asWidget: true } ) );
  122. } );
  123. it( 'should keep <p> in the view when <paragraph> attribute value is changed (table cell with multiple blocks)', () => {
  124. editor.setData( viewTable( [ [ '<p foo="bar">00</p><p>00</p>' ] ] ) );
  125. const table = root.getChild( 0 );
  126. model.change( writer => {
  127. writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
  128. } );
  129. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  130. [ '<p foo="baz">00</p><p>00</p>' ]
  131. ], { asWidget: true } ) );
  132. } );
  133. it( 'should do nothing on rename <paragraph> to other block', () => {
  134. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  135. const table = root.getChild( 0 );
  136. model.change( writer => {
  137. writer.rename( table.getNodeByPath( [ 0, 0, 0 ] ), 'block' );
  138. } );
  139. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  140. [ '<div>00</div>' ]
  141. ], { asWidget: true } ) );
  142. } );
  143. it( 'should do nothing when setting attribute on block item other then <paragraph>', () => {
  144. editor.setData( viewTable( [ [ '<div>foo</div>' ] ] ) );
  145. const table = root.getChild( 0 );
  146. model.change( writer => {
  147. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  148. } );
  149. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  150. [ '<div foo="bar">foo</div>' ]
  151. ], { asWidget: true } ) );
  152. } );
  153. it( 'should keep <p> in the view when <paragraph> attribute value is changed (table cell with multiple blocks)', () => {
  154. editor.setData( viewTable( [ [ '<p>00</p><p>00</p>' ] ] ) );
  155. const table = root.getChild( 0 );
  156. model.change( writer => {
  157. writer.remove( writer.createRangeOn( table.getNodeByPath( [ 0, 0, 1 ] ) ) );
  158. } );
  159. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  160. [ '<span>00</span>' ]
  161. ], { asWidget: true } ) );
  162. } );
  163. it( 'should update view selection after deleting content', () => {
  164. editor.setData( viewTable( [ [ '<p>foo</p><p>bar</p>' ] ] ) );
  165. const tableCell = root.getNodeByPath( [ 0, 0, 0 ] );
  166. // Replace table cell contents with paragraph - as model.deleteContent() does.
  167. model.change( writer => {
  168. writer.remove( writer.createRangeIn( tableCell ) );
  169. const paragraph = writer.createElement( 'paragraph' );
  170. writer.insert( paragraph, writer.createPositionAt( tableCell, 0 ) );
  171. // Set selection to newly created paragraph.
  172. writer.setSelection( paragraph, 0 );
  173. } );
  174. const viewRange = view.document.selection.getFirstRange();
  175. // Trying to map view selection to DOM range shouldn't throw after post-fixer will fix inserted <p> to <span>.
  176. expect( () => view.domConverter.viewRangeToDom( viewRange ) ).to.not.throw();
  177. } );
  178. it( 'should not update view selection after other feature set selection', () => {
  179. editor.model.schema.register( 'widget', {
  180. isObject: true,
  181. isBlock: true,
  182. allowWhere: '$block'
  183. } );
  184. editor.conversion.elementToElement( {
  185. model: 'widget',
  186. view: 'widget'
  187. } );
  188. editor.setData( viewTable( [ [ '<p>foo[]</p>' ] ] ) );
  189. const spy = sinon.spy();
  190. view.document.selection.on( 'change', spy );
  191. // Insert a widget in table cell and select it.
  192. model.change( writer => {
  193. const widgetElement = writer.createElement( 'widget' );
  194. const tableCell = root.getNodeByPath( [ 0, 0, 0, 0 ] );
  195. writer.insert( widgetElement, writer.createPositionAfter( tableCell ) );
  196. // Update the selection so it will be set on the widget and not in renamed paragraph.
  197. writer.setSelection( widgetElement, 'on' );
  198. } );
  199. // View selection should be updated only twice - will be set to null and then to widget.
  200. // If called thrice the selection post fixer for table cell was also called.
  201. sinon.assert.calledTwice( spy );
  202. } );
  203. // https://github.com/ckeditor/ckeditor5-table/issues/191.
  204. it( 'should not fire (and crash) for removed view elements', () => {
  205. editor.setData( viewTable( [ [ '<p>foo</p>' ] ] ) );
  206. const p = root.getNodeByPath( [ 0, 0, 0, 0 ] );
  207. // Replace table cell contents with paragraph - as model.deleteContent() does.
  208. model.change( writer => {
  209. writer.setSelection( writer.createRangeIn( root ) );
  210. editor.execute( 'delete' ); // For some reason it didn't crash with `writer.remove()`.
  211. writer.setAttribute( 'foo', 'bar', p );
  212. } );
  213. // Trying to map view selection to DOM range shouldn't throw after post-fixer will fix inserted <p> to <span>.
  214. expect( editor.getData() ).to.equal( '' );
  215. } );
  216. } );