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

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