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

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