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

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